issue/activity: append related image to subsequent message
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 2 May 2021 18:21:29 +0000 (20:21 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sun, 2 May 2021 18:25:10 +0000 (20:25 +0200)
Prefer attribution of significant message instead of preceding images.

widget/issue/activity.php

index 371f11a665216d744b6526918b1363fadd2d05a1..a153f872c0763d6a7670c28ec1cdfb87bd3d0039 100644 (file)
@@ -8,17 +8,27 @@ $sql = "SELECT $cols FROM messages m JOIN issues i ON i.id = issue";
 $sql .= " WHERE message IS NOT NULL";
 $sql .= " ORDER BY m.created DESC LIMIT $limit";
 $query = $Db->query($sql);
+$rows = $query->fetchall();
 
 $msgformat = [
        "{(?=</p>\n).+}s" => ' <small class="footer">(Meer op de site)</small>',
        '{(.*)(<p><img [^>]+></p>)\s*}s' => "$2\n$1",
-       '{(?<=<img src=")(?=/)}' => '/thumb/300x',
+       '{(?<=<img src=")(?:/thumb/[^/]+)?(?=/)}' => '/thumb/300x',
 ];
 
 print '<dl class="replies">';
 
 $prev = NULL;
-while ($row = $query->fetch()) {
+foreach ($rows as $i => $row) {
+       $next = $rows[$i + 1] ?? NULL;
+
+       if ($next and $next->issue === $row->issue
+       and preg_match('/\A(?:<p><img[^>]*><\/p>)+\z/', $row->message)) {
+               # postpone related image to the following message
+               $next->message = $row->message . $next->message;
+               continue;
+       }
+
        if (!$prev or $prev->issue !== $row->issue or $prev->author !== $row->author
        or !preg_match('/\A<p><img/', $prev->message)) {
                print '<dt>';
@@ -37,9 +47,10 @@ while ($row = $query->fetch()) {
                );
                print "</span>";
                print '</dt>';
-               print '<dd>';
        }
+       print '<dd>';
        print preg_replace(array_keys($msgformat), array_values($msgformat), $row->message);
+       print "\n";
        $prev = $row;
 }