commit 9af9f29

default  ·  2023-11-29 08:36:03 +0000 UTC
parent b846c94
html_notifications() is now fully xs_html.
1 files changed,  +87, -87
M html.c
M html.c
+87, -87
  1@@ -2331,85 +2331,60 @@ xs_str *html_people(snac *user)
  2 }
  3 
  4 
  5-xs_str *html_notifications(snac *snac)
  6+xs_str *html_notifications(snac *user)
  7 {
  8-    xs_str *s  = xs_str_new(NULL);
  9-    xs *n_list = notify_list(snac, 0);
 10-    xs *n_time = notify_check_time(snac, 0);
 11-    xs_list *p = n_list;
 12-    xs_str *v;
 13-    enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
 14+    xs *n_list = notify_list(user, 0);
 15+    xs *n_time = notify_check_time(user, 0);
 16 
 17-    s = html_user_header(snac, s, 0);
 18+    xs_html *body = html_user_body(user, 0);
 19 
 20-    xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", snac->actor);
 21-    xs_html *clear_all_form = xs_html_tag("form",
 22-        xs_html_attr("autocomplete", "off"),
 23-        xs_html_attr("method",       "post"),
 24-        xs_html_attr("action",       clear_all_action),
 25-        xs_html_attr("id",           "clear"),
 26-        xs_html_sctag("input",
 27-            xs_html_attr("type",     "submit"),
 28-            xs_html_attr("class",    "snac-btn-like"),
 29-            xs_html_attr("value",    L("Clear all"))));
 30+    xs_html *html = xs_html_tag("html",
 31+        html_user_head(user),
 32+        body);
 33 
 34-    {
 35-        xs *s1 = xs_html_render(clear_all_form);
 36-        s = xs_str_cat(s, s1);
 37-    }
 38+    xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
 39 
 40+    xs_html_add(body,
 41+        xs_html_tag("form",
 42+            xs_html_attr("autocomplete", "off"),
 43+            xs_html_attr("method",       "post"),
 44+            xs_html_attr("action",       clear_all_action),
 45+            xs_html_attr("id",           "clear"),
 46+            xs_html_sctag("input",
 47+                xs_html_attr("type",     "submit"),
 48+                xs_html_attr("class",    "snac-btn-like"),
 49+                xs_html_attr("value",    L("Clear all")))));
 50+
 51+    xs_html *noti_new = NULL;
 52+    xs_html *noti_seen = NULL;
 53+
 54+    xs_list *p = n_list;
 55+    xs_str *v;
 56     while (xs_list_iter(&p, &v)) {
 57-        xs *noti = notify_get(snac, v);
 58+        xs *noti = notify_get(user, v);
 59 
 60         if (noti == NULL)
 61             continue;
 62 
 63         xs *obj = NULL;
 64-        const char *type  = xs_dict_get(noti, "type");
 65-        const char *utype = xs_dict_get(noti, "utype");
 66-        const char *id    = xs_dict_get(noti, "objid");
 67+        char *type  = xs_dict_get(noti, "type");
 68+        char *utype = xs_dict_get(noti, "utype");
 69+        char *id    = xs_dict_get(noti, "objid");
 70 
 71         if (xs_is_null(id) || !valid_status(object_get(id, &obj)))
 72             continue;
 73 
 74-        if (is_hidden(snac, id))
 75+        if (is_hidden(user, id))
 76             continue;
 77 
 78-        const char *actor_id = xs_dict_get(noti, "actor");
 79+        char *actor_id = xs_dict_get(noti, "actor");
 80         xs *actor = NULL;
 81 
 82         if (!valid_status(actor_get(actor_id, &actor)))
 83             continue;
 84 
 85         xs *a_name = actor_name(actor);
 86-
 87-        if (strcmp(v, n_time) > 0) {
 88-            /* unseen notification */
 89-            if (stage == NHDR_NONE) {
 90-                xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("New"));
 91-                s = xs_str_cat(s, s1);
 92-
 93-                s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
 94-
 95-                stage = NHDR_NEW;
 96-            }
 97-        }
 98-        else {
 99-            /* already seen notification */
100-            if (stage != NHDR_OLD) {
101-                if (stage == NHDR_NEW)
102-                    s = xs_str_cat(s, "</div>\n");
103-
104-                xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("Already seen"));
105-                s = xs_str_cat(s, s1);
106-
107-                s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
108-
109-                stage = NHDR_OLD;
110-            }
111-        }
112-
113-        const char *label = type;
114+        char *label = type;
115 
116         if (strcmp(type, "Create") == 0)
117             label = L("Mention");
118@@ -2420,56 +2395,81 @@ xs_str *html_notifications(snac *snac)
119         if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
120             label = L("Unfollow");
121 
122-        xs *es1 = encode_html(label);
123-        xs *s1 = xs_fmt("<div class=\"snac-post-with-desc\">\n"
124-                        "<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n",
125-            es1, actor_id, a_name);
126-        s = xs_str_cat(s, s1);
127+        xs_html *entry = xs_html_tag("div",
128+            xs_html_attr("class", "snac-post-with-desc"),
129+            xs_html_tag("p",
130+                xs_html_tag("b",
131+                    xs_html_text(label),
132+                    xs_html_text(" by "),
133+                    xs_html_tag("a",
134+                        xs_html_attr("href", actor_id),
135+                        xs_html_raw(a_name))))); /* a_name is already sanitized */
136 
137         if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
138-            xs_html *div = xs_html_tag("div",
139-                xs_html_attr("class", "snac-post"),
140-                html_actor_icon(actor, NULL, NULL, NULL, 0));
141-
142-            xs *s1 = xs_html_render(div);
143-            s = xs_str_cat(s, s1);
144+            xs_html_add(entry,
145+                xs_html_tag("div",
146+                    xs_html_attr("class", "snac-post"),
147+                    html_actor_icon(actor, NULL, NULL, NULL, 0)));
148         }
149         else {
150             xs *md5 = xs_md5_hex(id, strlen(id));
151 
152-            xs_html *entry = html_entry(snac, obj, 0, 0, md5, 1);
153+            xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
154 
155-            if (entry != NULL) {
156-                xs *s1 = xs_html_render(entry);
157-                s = xs_str_cat(s, s1);
158+            if (h != NULL) {
159+                xs_html_add(entry,
160+                    h);
161             }
162         }
163 
164-        s = xs_str_cat(s, "</div>\n");
165-    }
166+        if (strcmp(v, n_time) > 0) {
167+            /* unseen notification */
168+            if (noti_new == NULL) {
169+                noti_new = xs_html_tag("div",
170+                    xs_html_tag("h2",
171+                        xs_html_attr("class", "snac-header"),
172+                        xs_html_text(L("New"))));
173+
174+                xs_html_add(body,
175+                    noti_new);
176+            }
177 
178-    if (stage == NHDR_NONE) {
179-        xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("None"));
180-        s = xs_str_cat(s, s1);
181-    }
182-    else
183-        s = xs_str_cat(s, "</div>\n");
184+            xs_html_add(noti_new,
185+                entry);
186+        }
187+        else {
188+            /* already seen notification */
189+            if (noti_seen == NULL) {
190+                noti_seen = xs_html_tag("div",
191+                    xs_html_tag("h2",
192+                        xs_html_attr("class", "snac-header"),
193+                        xs_html_text(L("Already seen"))));
194+
195+                xs_html_add(body,
196+                    noti_seen);
197+            }
198 
199-    {
200-        xs_html *h = html_footer();
201-        xs *s1 = xs_html_render(h);
202-        s = xs_str_cat(s, s1);
203+            xs_html_add(noti_seen,
204+                entry);
205+        }
206     }
207 
208-    s = xs_str_cat(s, "</body>\n</html>\n");
209+    if (noti_new == NULL && noti_seen == NULL)
210+        xs_html_add(body,
211+            xs_html_tag("hd",
212+                xs_html_attr("class", "snac-header"),
213+                xs_html_text(L("None"))));
214+
215+    xs_html_add(body,
216+        html_footer());
217 
218     /* set the check time to now */
219-    xs *dummy = notify_check_time(snac, 1);
220+    xs *dummy = notify_check_time(user, 1);
221     dummy = xs_free(dummy);
222 
223-    timeline_touch(snac);
224+    timeline_touch(user);
225 
226-    return s;
227+    return xs_html_render_s(html, "<!DOCTYPE html>\n");
228 }
229 
230