html_notifications() is now fully xs_html.

This commit is contained in:
default 2023-11-29 09:36:03 +01:00
parent b846c943b3
commit 9af9f29d33

180
html.c
View File

@ -2331,85 +2331,60 @@ xs_str *html_people(snac *user)
} }
xs_str *html_notifications(snac *snac) xs_str *html_notifications(snac *user)
{ {
xs_str *s = xs_str_new(NULL); xs *n_list = notify_list(user, 0);
xs *n_list = notify_list(snac, 0); xs *n_time = notify_check_time(user, 0);
xs *n_time = notify_check_time(snac, 0);
xs_html *body = html_user_body(user, 0);
xs_html *html = xs_html_tag("html",
html_user_head(user),
body);
xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
xs_html_add(body,
xs_html_tag("form",
xs_html_attr("autocomplete", "off"),
xs_html_attr("method", "post"),
xs_html_attr("action", clear_all_action),
xs_html_attr("id", "clear"),
xs_html_sctag("input",
xs_html_attr("type", "submit"),
xs_html_attr("class", "snac-btn-like"),
xs_html_attr("value", L("Clear all")))));
xs_html *noti_new = NULL;
xs_html *noti_seen = NULL;
xs_list *p = n_list; xs_list *p = n_list;
xs_str *v; xs_str *v;
enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
s = html_user_header(snac, s, 0);
xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", snac->actor);
xs_html *clear_all_form = xs_html_tag("form",
xs_html_attr("autocomplete", "off"),
xs_html_attr("method", "post"),
xs_html_attr("action", clear_all_action),
xs_html_attr("id", "clear"),
xs_html_sctag("input",
xs_html_attr("type", "submit"),
xs_html_attr("class", "snac-btn-like"),
xs_html_attr("value", L("Clear all"))));
{
xs *s1 = xs_html_render(clear_all_form);
s = xs_str_cat(s, s1);
}
while (xs_list_iter(&p, &v)) { while (xs_list_iter(&p, &v)) {
xs *noti = notify_get(snac, v); xs *noti = notify_get(user, v);
if (noti == NULL) if (noti == NULL)
continue; continue;
xs *obj = NULL; xs *obj = NULL;
const char *type = xs_dict_get(noti, "type"); char *type = xs_dict_get(noti, "type");
const char *utype = xs_dict_get(noti, "utype"); char *utype = xs_dict_get(noti, "utype");
const char *id = xs_dict_get(noti, "objid"); char *id = xs_dict_get(noti, "objid");
if (xs_is_null(id) || !valid_status(object_get(id, &obj))) if (xs_is_null(id) || !valid_status(object_get(id, &obj)))
continue; continue;
if (is_hidden(snac, id)) if (is_hidden(user, id))
continue; continue;
const char *actor_id = xs_dict_get(noti, "actor"); char *actor_id = xs_dict_get(noti, "actor");
xs *actor = NULL; xs *actor = NULL;
if (!valid_status(actor_get(actor_id, &actor))) if (!valid_status(actor_get(actor_id, &actor)))
continue; continue;
xs *a_name = actor_name(actor); xs *a_name = actor_name(actor);
char *label = type;
if (strcmp(v, n_time) > 0) {
/* unseen notification */
if (stage == NHDR_NONE) {
xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("New"));
s = xs_str_cat(s, s1);
s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
stage = NHDR_NEW;
}
}
else {
/* already seen notification */
if (stage != NHDR_OLD) {
if (stage == NHDR_NEW)
s = xs_str_cat(s, "</div>\n");
xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("Already seen"));
s = xs_str_cat(s, s1);
s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
stage = NHDR_OLD;
}
}
const char *label = type;
if (strcmp(type, "Create") == 0) if (strcmp(type, "Create") == 0)
label = L("Mention"); label = L("Mention");
@ -2420,56 +2395,81 @@ xs_str *html_notifications(snac *snac)
if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0) if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
label = L("Unfollow"); label = L("Unfollow");
xs *es1 = encode_html(label); xs_html *entry = xs_html_tag("div",
xs *s1 = xs_fmt("<div class=\"snac-post-with-desc\">\n" xs_html_attr("class", "snac-post-with-desc"),
"<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n", xs_html_tag("p",
es1, actor_id, a_name); xs_html_tag("b",
s = xs_str_cat(s, s1); xs_html_text(label),
xs_html_text(" by "),
xs_html_tag("a",
xs_html_attr("href", actor_id),
xs_html_raw(a_name))))); /* a_name is already sanitized */
if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) { if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
xs_html *div = xs_html_tag("div", xs_html_add(entry,
xs_html_attr("class", "snac-post"), xs_html_tag("div",
html_actor_icon(actor, NULL, NULL, NULL, 0)); xs_html_attr("class", "snac-post"),
html_actor_icon(actor, NULL, NULL, NULL, 0)));
xs *s1 = xs_html_render(div);
s = xs_str_cat(s, s1);
} }
else { else {
xs *md5 = xs_md5_hex(id, strlen(id)); xs *md5 = xs_md5_hex(id, strlen(id));
xs_html *entry = html_entry(snac, obj, 0, 0, md5, 1); xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
if (entry != NULL) { if (h != NULL) {
xs *s1 = xs_html_render(entry); xs_html_add(entry,
s = xs_str_cat(s, s1); h);
} }
} }
s = xs_str_cat(s, "</div>\n"); if (strcmp(v, n_time) > 0) {
/* unseen notification */
if (noti_new == NULL) {
noti_new = xs_html_tag("div",
xs_html_tag("h2",
xs_html_attr("class", "snac-header"),
xs_html_text(L("New"))));
xs_html_add(body,
noti_new);
}
xs_html_add(noti_new,
entry);
}
else {
/* already seen notification */
if (noti_seen == NULL) {
noti_seen = xs_html_tag("div",
xs_html_tag("h2",
xs_html_attr("class", "snac-header"),
xs_html_text(L("Already seen"))));
xs_html_add(body,
noti_seen);
}
xs_html_add(noti_seen,
entry);
}
} }
if (stage == NHDR_NONE) { if (noti_new == NULL && noti_seen == NULL)
xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("None")); xs_html_add(body,
s = xs_str_cat(s, s1); xs_html_tag("hd",
} xs_html_attr("class", "snac-header"),
else xs_html_text(L("None"))));
s = xs_str_cat(s, "</div>\n");
{ xs_html_add(body,
xs_html *h = html_footer(); html_footer());
xs *s1 = xs_html_render(h);
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "</body>\n</html>\n");
/* set the check time to now */ /* set the check time to now */
xs *dummy = notify_check_time(snac, 1); xs *dummy = notify_check_time(user, 1);
dummy = xs_free(dummy); dummy = xs_free(dummy);
timeline_touch(snac); timeline_touch(user);
return s; return xs_html_render_s(html, "<!DOCTYPE html>\n");
} }