commit 428d756

default  ·  2023-01-24 10:14:45 +0000 UTC
parent 2921dbe
Added edit support for posts.
1 files changed,  +69, -3
M html.c
M html.c
+69, -3
 1@@ -479,6 +479,39 @@ d_char *html_entry_controls(snac *snac, d_char *os, char *msg, const char *md5)
 2 
 3     s = xs_str_cat(s, "</form>\n");
 4 
 5+    char *prev_src = xs_dict_get(msg, "sourceContent");
 6+
 7+    if (!xs_is_null(prev_src) && strcmp(actor, snac->actor) == 0) {
 8+        /* post can be edited */
 9+        xs *s1 = xs_fmt(
10+            "<p><details><summary>%s</summary>\n"
11+            "<p><div class=\"snac-note\" id=\"%s_edit\">\n"
12+            "<form method=\"post\" action=\"%s/admin/note\" "
13+            "enctype=\"multipart/form-data\" id=\"%s_edit_form\">\n"
14+            "<textarea class=\"snac-textarea\" name=\"content\" "
15+            "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
16+            "<input type=\"hidden\" name=\"edit_id\" value=\"%s\">\n"
17+            "<p><input type=\"checkbox\" name=\"sensitive\"> %s\n"
18+            "<p><input type=\"file\" name=\"attach\">\n"
19+            "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
20+            "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
21+            "</form><p></div>\n"
22+            "</details><p>"
23+            "\n",
24+
25+            L("Edit..."),
26+            md5,
27+            snac->actor, md5,
28+            prev_src,
29+            id,
30+            L("Sensitive content"),
31+            md5,
32+            L("Post")
33+        );
34+
35+        s = xs_str_cat(s, s1);
36+    }
37+
38     {
39         /* the post textarea */
40         xs *ct = build_mentions(snac, msg);
41@@ -1309,6 +1342,7 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
42         char *attach_file = xs_dict_get(p_vars, "attach");
43         char *to          = xs_dict_get(p_vars, "to");
44         char *sensitive   = xs_dict_get(p_vars, "sensitive");
45+        char *edit_id     = xs_dict_get(p_vars, "edit_id");
46         xs *attach_list   = xs_list_new();
47 
48         /* is attach_url set? */
49@@ -1348,11 +1382,43 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
50                 msg = xs_dict_set(msg, "summary",   "...");
51             }
52 
53-            c_msg = msg_create(&snac, msg);
54+            if (xs_is_null(edit_id)) {
55+                /* new message */
56+                c_msg = msg_create(&snac, msg);
57+                timeline_add(&snac, xs_dict_get(msg, "id"), msg);
58+            }
59+            else {
60+                /* an edition of a previous message */
61+                xs *p_msg = NULL;
62+
63+                if (valid_status(object_get(edit_id, &p_msg, NULL))) {
64+                    /* copy relevant fields from previous version */
65+                    char *fields[] = { "id", "context", "url", "published",
66+                                       "to", "inReplyTo", NULL };
67+                    int n;
68+
69+                    for (n = 0; fields[n]; n++) {
70+                        char *v = xs_dict_get(p_msg, fields[n]);
71+                        msg = xs_dict_set(msg, fields[n], v);
72+                    }
73+
74+                    /* set the updated field */
75+                    xs *updated = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
76+                    msg = xs_dict_set(msg, "updated", updated);
77+
78+                    /* overwrite object, not updating the indexes */
79+                    object_add_ow(edit_id, msg);
80+
81+                    /* update message */
82+                    c_msg = msg_update(&snac, msg);
83+                }
84+                else
85+                    snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
86+            }
87 
88-            enqueue_message(&snac, c_msg);
89+            if (c_msg != NULL)
90+                enqueue_message(&snac, c_msg);
91 
92-            timeline_add(&snac, xs_dict_get(msg, "id"), msg);
93         }
94 
95         status = 303;