issue: store main description as comment row
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 21 Sep 2021 17:13:07 +0000 (19:13 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 28 Sep 2021 16:29:16 +0000 (18:29 +0200)
Convert issue body to mandatory reply (recognised by oldest date):

INSERT INTO comments
SELECT concat(page, '/', id), body, created, author FROM issues;
CREATE OR REPLACE VIEW messages ...;

database.inc.php
issue/index.php
widget/comments.sql
widget/reply.php

index aaf20234fd42a27da4ee5dd7e9059a13d22f438e..12a82eb59badad8092c2ed8af8e84307ff90d249 100644 (file)
@@ -42,7 +42,7 @@ class DB
                        foreach ($row as $col => $val) {
                                $cols[] = $this->_value($val, $params);
                        }
-                       $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
+                       $sql = sprintf('INSERT INTO %s (%s) VALUES (%s) RETURNING id',
                                '"'.$table.'"',
                                implode(', ', array_keys($row)),
                                implode(', ', $cols)
index 0c18142f06e71c709e997e76514212ebeb1c3c8e..a4b131483522ea0f395a896fada6693b4f96e424 100644 (file)
@@ -14,7 +14,6 @@ if ($id and ctype_digit($id)) {
        $replies = $Page->widget('reply');  # handle updates
 
        $Page->title .= ': '.htmlspecialchars($Issue->subject);
-       $Page->teaser = $Issue->body;
        $Page->body = $replies;  # find image
        if ($Page->api) return;
 
@@ -36,7 +35,6 @@ if ($id and ctype_digit($id)) {
        print "</dl></aside>\n\n";
 
        print '<div>';
-       print $Issue->body;
        print $replies;
        print "</div>\n";
        return;
@@ -51,17 +49,30 @@ if ($_POST and isset($_POST['subject'])) {
                $query = $Db->set('issues', [
                        'page'    => $Page->handler,
                        'subject' => $_POST['subject'],
-                       'body'    => messagehtml($_POST['body']),
+                       'link'    => preg_replace('/\b(?:de|het|een)\s+|\W+/', '-', strtolower($_POST['subject'])),
                        'author'  => $User->login,
                ]);
                if (!$query->rowCount()) {
                        throw new Exception('Issue niet opgeslagen.');
                }
+               $row = $query->fetch();
+               if (!$row->id) {
+                       throw new Exception('Issue niet goed opgeslagen.');
+               }
+               $query = $Db->set('comments', [
+                       'page'    => "{$Page->handler}/{$row->id}",
+                       'raw'     => $_POST['body'],
+                       'message' => messagehtml($_POST['body']),
+                       'author'  => $User->login,
+               ]);
+               if (!$query->rowCount()) {
+                       throw new Exception('Issueinhoud niet opgeslagen.');
+               }
                $_POST = [];
 }
 
 $subsql = "SELECT count(*) FROM comments WHERE page=i.page||'/'||i.id";
-$cols = "*, ($subsql AND message IS NOT NULL) AS replycount";
+$cols = "*, ($subsql AND message IS NOT NULL) - 1 AS replycount";
 $cols .= ", ($subsql AND message ~ '<img') AS imagecount";
 $sql = "SELECT $cols FROM issues i WHERE page = ?";
 if (isset($_GET['open'])) {
index a1520793c06a2ff121f3b7bb8e9304e64e466c00..b805a2a33a6fdd89082ddebf6889560b4cffbd34 100644 (file)
@@ -2,7 +2,6 @@ CREATE TABLE issues (
        page       text        NOT NULL DEFAULT 'issue',
        link       text,
        subject    text,
-       body       text,
        created    timestamptz          DEFAULT now(),
        closed     timestamptz,
        updated    timestamptz NOT NULL DEFAULT now(),
@@ -31,6 +30,4 @@ CREATE TABLE journal (
 
 CREATE OR REPLACE VIEW messages AS (
        SELECT *, regexp_replace(page, '.*/', '')::int issue FROM comments
-               UNION ALL
-       SELECT concat(page,'/',id), body, created, author, NULL, id FROM issues
 );
index 685a611eca98778c7bf77ba8ed8c78f769c3ba45..66e958746b6f71bb330b4165ff138446c6178021 100644 (file)
@@ -88,6 +88,11 @@ if ($_POST) {
 $cols = '*, (SELECT json_agg(journal.*) FROM journal WHERE comment_id = comments.id) AS journal';
 $query = $Db->query("SELECT $cols FROM comments WHERE page = ? ORDER BY created", [$Page->link]);
 
+if ($row = $query->fetch()) {
+       print $row->message;
+       $Page->teaser = $row->raw;
+}
+
 print '<ul class="replies">';
 
 $imagecount = 0;