2.60
html.c
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
3
4#include "xs.h"
5#include "xs_io.h"
6#include "xs_json.h"
7#include "xs_regex.h"
8#include "xs_set.h"
9#include "xs_openssl.h"
10#include "xs_time.h"
11#include "xs_mime.h"
12#include "xs_match.h"
13#include "xs_html.h"
14
15#include "snac.h"
16
17int login(snac *snac, const xs_dict *headers)
18/* tries a login */
19{
20 int logged_in = 0;
21 const char *auth = xs_dict_get(headers, "authorization");
22
23 if (auth && xs_startswith(auth, "Basic ")) {
24 int sz;
25 xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
26 xs *s2 = xs_base64_dec(s1, &sz);
27
28 xs *l1 = xs_split_n(s2, ":", 1);
29
30 if (xs_list_len(l1) == 2) {
31 logged_in = check_password(
32 xs_list_get(l1, 0), xs_list_get(l1, 1),
33 xs_dict_get(snac->config, "passwd"));
34 }
35 }
36
37 if (logged_in)
38 lastlog_write(snac, "web");
39
40 return logged_in;
41}
42
43
44xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems)
45/* replaces all the :shortnames: with the emojis in tag */
46{
47 if (!xs_is_null(tag)) {
48 xs *tag_list = NULL;
49 if (xs_type(tag) == XSTYPE_DICT) {
50 /* not a list */
51 tag_list = xs_list_new();
52 tag_list = xs_list_append(tag_list, tag);
53 } else {
54 /* is a list */
55 tag_list = xs_dup(tag);
56 }
57
58 xs *style = xs_fmt("height: %dem; width: %dem; vertical-align: middle;", ems, ems);
59
60 const char *v;
61 int c = 0;
62
63 while (xs_list_next(tag_list, &v, &c)) {
64 const char *t = xs_dict_get(v, "type");
65
66 if (t && strcmp(t, "Emoji") == 0) {
67 const char *n = xs_dict_get(v, "name");
68 const char *i = xs_dict_get(v, "icon");
69
70 if (n && i) {
71 const char *u = xs_dict_get(i, "url");
72 xs_html *img = xs_html_sctag("img",
73 xs_html_attr("loading", "lazy"),
74 xs_html_attr("src", u),
75 xs_html_attr("style", style));
76
77 xs *s1 = xs_html_render(img);
78 s = xs_replace_i(s, n, s1);
79 }
80 }
81 }
82 }
83
84 return s;
85}
86
87
88xs_str *actor_name(xs_dict *actor)
89/* gets the actor name */
90{
91 const char *v;
92
93 if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
94 if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
95 v = "anonymous";
96 }
97 }
98
99 return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag"), 1);
100}
101
102
103xs_html *html_actor_icon(snac *user, xs_dict *actor, const char *date,
104 const char *udate, const char *url, int priv, int in_people)
105{
106 xs_html *actor_icon = xs_html_tag("p", NULL);
107
108 xs *avatar = NULL;
109 const char *v;
110 int fwing = 0;
111 int fwer = 0;
112
113 xs *name = actor_name(actor);
114
115 /* get the avatar */
116 if ((v = xs_dict_get(actor, "icon")) != NULL) {
117 /* if it's a list (Peertube), get the first one */
118 if (xs_type(v) == XSTYPE_LIST)
119 v = xs_list_get(v, 0);
120
121 if ((v = xs_dict_get(v, "url")) != NULL)
122 avatar = xs_dup(v);
123 }
124
125 if (avatar == NULL)
126 avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
127
128 const char *actor_id = xs_dict_get(actor, "id");
129 xs *href = NULL;
130
131 if (user) {
132 fwer = follower_check(user, actor_id);
133 fwing = following_check(user, actor_id);
134 }
135
136 if (user && !in_people) {
137 /* if this actor is a follower or being followed, create an
138 anchored link to the people page instead of the actor url */
139 if (fwer || fwing) {
140 xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
141 href = xs_fmt("%s/people#%s", user->actor, md5);
142 }
143 }
144
145 if (href == NULL)
146 href = xs_dup(actor_id);
147
148 xs_html_add(actor_icon,
149 xs_html_sctag("img",
150 xs_html_attr("loading", "lazy"),
151 xs_html_attr("class", "snac-avatar"),
152 xs_html_attr("src", avatar),
153 xs_html_attr("alt", "")),
154 xs_html_tag("a",
155 xs_html_attr("href", href),
156 xs_html_attr("class", "p-author h-card snac-author"),
157 xs_html_raw(name))); /* name is already html-escaped */
158
159 if (!xs_is_null(url)) {
160 xs *md5 = xs_md5_hex(url, strlen(url));
161
162 xs_html_add(actor_icon,
163 xs_html_text(" "),
164 xs_html_tag("a",
165 xs_html_attr("href", (char *)url),
166 xs_html_attr("title", md5),
167 xs_html_text("ยป")));
168 }
169
170 if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
171 xs_html_add(actor_icon,
172 xs_html_text(" "),
173 xs_html_tag("span",
174 xs_html_attr("title", "bot"),
175 xs_html_raw("🤖")));
176 }
177
178 if (fwing && fwer) {
179 xs_html_add(actor_icon,
180 xs_html_text(" "),
181 xs_html_tag("span",
182 xs_html_attr("title", "mutual relation"),
183 xs_html_raw("🤝")));
184 }
185
186 if (priv) {
187 xs_html_add(actor_icon,
188 xs_html_text(" "),
189 xs_html_tag("span",
190 xs_html_attr("title", "private"),
191 xs_html_raw("🔒")));
192 }
193
194 if (xs_is_null(date)) {
195 xs_html_add(actor_icon,
196 xs_html_raw(" "));
197 }
198 else {
199 xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
200 xs *date_title = xs_dup(date);
201
202 if (!xs_is_null(udate)) {
203 xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
204
205 date_label = xs_str_cat(date_label, " / ", sd);
206
207 date_title = xs_str_cat(date_title, " / ", udate);
208 }
209
210 xs_html_add(actor_icon,
211 xs_html_text(" "),
212 xs_html_tag("time",
213 xs_html_attr("class", "dt-published snac-pubdate"),
214 xs_html_attr("title", date_title),
215 xs_html_text(date_label)));
216 }
217
218 {
219 const char *username, *id;
220
221 if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
222 /* This should never be reached */
223 username = "anonymous";
224 }
225
226 if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
227 /* This should never be reached */
228 id = "https://social.example.org/anonymous";
229 }
230
231 /* "LIKE AN ANIMAL" */
232 xs *domain = xs_split(id, "/");
233 xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
234
235 xs_html_add(actor_icon,
236 xs_html_sctag("br", NULL),
237 xs_html_tag("a",
238 xs_html_attr("href", xs_dict_get(actor, "id")),
239 xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
240 xs_html_text(user)));
241 }
242
243 return actor_icon;
244}
245
246
247xs_html *html_msg_icon(snac *user, const char *actor_id, const xs_dict *msg)
248{
249 xs *actor = NULL;
250 xs_html *actor_icon = NULL;
251
252 if (actor_id && valid_status(actor_get_refresh(user, actor_id, &actor))) {
253 const char *date = NULL;
254 const char *udate = NULL;
255 const char *url = NULL;
256 int priv = 0;
257 const char *type = xs_dict_get(msg, "type");
258
259 if (xs_match(type, POSTLIKE_OBJECT_TYPE))
260 url = xs_dict_get(msg, "id");
261
262 priv = !is_msg_public(msg);
263
264 date = xs_dict_get(msg, "published");
265 udate = xs_dict_get(msg, "updated");
266
267 actor_icon = html_actor_icon(user, actor, date, udate, url, priv, 0);
268 }
269
270 return actor_icon;
271}
272
273
274xs_html *html_note(snac *user, const char *summary,
275 const char *div_id, const char *form_id,
276 const char *ta_plh, const char *ta_content,
277 const char *edit_id, const char *actor_id,
278 const xs_val *cw_yn, const char *cw_text,
279 const xs_val *mnt_only, const char *redir,
280 const char *in_reply_to, int poll,
281 const char *att_file, const char *att_alt_text,
282 int is_draft)
283/* Yes, this is a FUCKTON of arguments and I'm a bit embarrased */
284{
285 xs *action = xs_fmt("%s/admin/note", user->actor);
286
287 xs_html *form;
288
289 xs_html *note = xs_html_tag("div",
290 xs_html_tag("details",
291 xs_html_tag("summary",
292 xs_html_text(summary)),
293 xs_html_tag("p", NULL),
294 xs_html_tag("div",
295 xs_html_attr("class", "snac-note"),
296 xs_html_attr("id", div_id),
297 form = xs_html_tag("form",
298 xs_html_attr("autocomplete", "off"),
299 xs_html_attr("method", "post"),
300 xs_html_attr("action", action),
301 xs_html_attr("enctype", "multipart/form-data"),
302 xs_html_attr("id", form_id),
303 xs_html_tag("textarea",
304 xs_html_attr("class", "snac-textarea"),
305 xs_html_attr("name", "content"),
306 xs_html_attr("rows", "4"),
307 xs_html_attr("wrap", "virtual"),
308 xs_html_attr("required", "required"),
309 xs_html_attr("placeholder", ta_plh),
310 xs_html_text(ta_content)),
311 xs_html_tag("p", NULL),
312 xs_html_text(L("Sensitive content: ")),
313 xs_html_sctag("input",
314 xs_html_attr("type", "checkbox"),
315 xs_html_attr("name", "sensitive"),
316 xs_html_attr(xs_type(cw_yn) == XSTYPE_TRUE ? "checked" : "", NULL)),
317 xs_html_sctag("input",
318 xs_html_attr("type", "text"),
319 xs_html_attr("name", "summary"),
320 xs_html_attr("placeholder", L("Sensitive content description")),
321 xs_html_attr("value", xs_is_null(cw_text) ? "" : cw_text))))));
322
323 if (actor_id)
324 xs_html_add(form,
325 xs_html_sctag("input",
326 xs_html_attr("type", "hidden"),
327 xs_html_attr("name", "to"),
328 xs_html_attr("value", actor_id)));
329 else {
330 /* no actor_id; ask for mentioned_only */
331 xs_html_add(form,
332 xs_html_tag("p", NULL),
333 xs_html_text(L("Only for mentioned people: ")),
334 xs_html_sctag("input",
335 xs_html_attr("type", "checkbox"),
336 xs_html_attr("name", "mentioned_only"),
337 xs_html_attr(xs_type(mnt_only) == XSTYPE_TRUE ? "checked" : "", NULL)));
338 }
339
340 if (redir)
341 xs_html_add(form,
342 xs_html_sctag("input",
343 xs_html_attr("type", "hidden"),
344 xs_html_attr("name", "redir"),
345 xs_html_attr("value", redir)));
346
347 if (in_reply_to)
348 xs_html_add(form,
349 xs_html_sctag("input",
350 xs_html_attr("type", "hidden"),
351 xs_html_attr("name", "in_reply_to"),
352 xs_html_attr("value", in_reply_to)));
353 else
354 xs_html_add(form,
355 xs_html_tag("p", NULL),
356 xs_html_text(L("Reply to (URL): ")),
357 xs_html_sctag("input",
358 xs_html_attr("type", "text"),
359 xs_html_attr("name", "in_reply_to"),
360 xs_html_attr("placeholder", "Optional URL to reply to")));
361
362 xs_html_add(form,
363 xs_html_tag("p", NULL),
364 xs_html_attr("title", L("Don't send, but store as a draft")),
365 xs_html_text(L("Draft:")),
366 xs_html_sctag("input",
367 xs_html_attr("type", "checkbox"),
368 xs_html_attr("name", "is_draft"),
369 xs_html_attr(is_draft ? "checked" : "", NULL)));
370
371 if (edit_id)
372 xs_html_add(form,
373 xs_html_sctag("input",
374 xs_html_attr("type", "hidden"),
375 xs_html_attr("name", "edit_id"),
376 xs_html_attr("value", edit_id)));
377
378 /* attachment controls */
379 xs_html *att;
380
381 xs_html_add(form,
382 xs_html_tag("p", NULL),
383 att = xs_html_tag("details",
384 xs_html_tag("summary",
385 xs_html_text(L("Attachment..."))),
386 xs_html_tag("p", NULL)));
387
388 if (att_file && *att_file)
389 xs_html_add(att,
390 xs_html_text(L("File:")),
391 xs_html_sctag("input",
392 xs_html_attr("type", "text"),
393 xs_html_attr("name", "attach_url"),
394 xs_html_attr("title", L("Clear this field to delete the attachment")),
395 xs_html_attr("value", att_file)));
396 else
397 xs_html_add(att,
398 xs_html_sctag("input",
399 xs_html_attr("type", "file"),
400 xs_html_attr("name", "attach")));
401
402 xs_html_add(att,
403 xs_html_text(" "),
404 xs_html_sctag("input",
405 xs_html_attr("type", "text"),
406 xs_html_attr("name", "alt_text"),
407 xs_html_attr("value", att_alt_text),
408 xs_html_attr("placeholder", L("Attachment description"))));
409
410 /* add poll controls */
411 if (poll) {
412 xs_html_add(form,
413 xs_html_tag("p", NULL),
414 xs_html_tag("details",
415 xs_html_tag("summary",
416 xs_html_text(L("Poll..."))),
417 xs_html_tag("p",
418 xs_html_text(L("Poll options (one per line, up to 8):")),
419 xs_html_sctag("br", NULL),
420 xs_html_tag("textarea",
421 xs_html_attr("class", "snac-textarea"),
422 xs_html_attr("name", "poll_options"),
423 xs_html_attr("rows", "4"),
424 xs_html_attr("wrap", "virtual"),
425 xs_html_attr("placeholder", "Option 1...\nOption 2...\nOption 3...\n..."))),
426 xs_html_tag("select",
427 xs_html_attr("name", "poll_multiple"),
428 xs_html_tag("option",
429 xs_html_attr("value", "off"),
430 xs_html_text(L("One choice"))),
431 xs_html_tag("option",
432 xs_html_attr("value", "on"),
433 xs_html_text(L("Multiple choices")))),
434 xs_html_text(" "),
435 xs_html_tag("select",
436 xs_html_attr("name", "poll_end_secs"),
437 xs_html_tag("option",
438 xs_html_attr("value", "300"),
439 xs_html_text(L("End in 5 minutes"))),
440 xs_html_tag("option",
441 xs_html_attr("value", "3600"),
442 xs_html_attr("selected", NULL),
443 xs_html_text(L("End in 1 hour"))),
444 xs_html_tag("option",
445 xs_html_attr("value", "86400"),
446 xs_html_text(L("End in 1 day"))))));
447 }
448
449 xs_html_add(form,
450 xs_html_tag("p", NULL),
451 xs_html_sctag("input",
452 xs_html_attr("type", "submit"),
453 xs_html_attr("class", "button"),
454 xs_html_attr("value", L("Post"))),
455 xs_html_tag("p", NULL));
456
457 return note;
458}
459
460
461static xs_html *html_base_head(void)
462{
463 xs_html *head = xs_html_tag("head",
464 xs_html_sctag("meta",
465 xs_html_attr("name", "viewport"),
466 xs_html_attr("content", "width=device-width, initial-scale=1")),
467 xs_html_sctag("meta",
468 xs_html_attr("name", "generator"),
469 xs_html_attr("content", USER_AGENT)));
470
471 /* add server CSS and favicon */
472 xs *f;
473 f = xs_fmt("%s/favicon.ico", srv_baseurl);
474 const xs_list *p = xs_dict_get(srv_config, "cssurls");
475 const char *v;
476 int c = 0;
477
478 while (xs_list_next(p, &v, &c)) {
479 xs_html_add(head,
480 xs_html_sctag("link",
481 xs_html_attr("rel", "stylesheet"),
482 xs_html_attr("type", "text/css"),
483 xs_html_attr("href", v)));
484 }
485
486 xs_html_add(head,
487 xs_html_sctag("link",
488 xs_html_attr("rel", "icon"),
489 xs_html_attr("type", "image/x-icon"),
490 xs_html_attr("href", f)));
491
492 return head;
493}
494
495
496xs_html *html_instance_head(void)
497{
498 xs_html *head = html_base_head();
499
500 {
501 FILE *f;
502 xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
503
504 if ((f = fopen(g_css_fn, "r")) != NULL) {
505 xs *css = xs_readall(f);
506 fclose(f);
507
508 xs_html_add(head,
509 xs_html_tag("style",
510 xs_html_raw(css)));
511 }
512 }
513
514 const char *host = xs_dict_get(srv_config, "host");
515 const char *title = xs_dict_get(srv_config, "title");
516
517 xs_html_add(head,
518 xs_html_tag("title",
519 xs_html_text(title && *title ? title : host)));
520
521 return head;
522}
523
524
525static xs_html *html_instance_body(void)
526{
527 const char *host = xs_dict_get(srv_config, "host");
528 const char *sdesc = xs_dict_get(srv_config, "short_description");
529 const char *email = xs_dict_get(srv_config, "admin_email");
530 const char *acct = xs_dict_get(srv_config, "admin_account");
531
532 xs *blurb = xs_replace(snac_blurb, "%host%", host);
533
534 xs_html *dl;
535
536 xs_html *body = xs_html_tag("body",
537 xs_html_tag("div",
538 xs_html_attr("class", "snac-instance-blurb"),
539 xs_html_raw(blurb), /* pure html */
540 dl = xs_html_tag("dl", NULL)));
541
542 if (sdesc && *sdesc) {
543 xs_html_add(dl,
544 xs_html_tag("di",
545 xs_html_tag("dt",
546 xs_html_text(L("Site description"))),
547 xs_html_tag("dd",
548 xs_html_text(sdesc))));
549 }
550 if (email && *email) {
551 xs *mailto = xs_fmt("mailto:%s", email);
552
553 xs_html_add(dl,
554 xs_html_tag("di",
555 xs_html_tag("dt",
556 xs_html_text(L("Admin email"))),
557 xs_html_tag("dd",
558 xs_html_tag("a",
559 xs_html_attr("href", mailto),
560 xs_html_text(email)))));
561 }
562 if (acct && *acct) {
563 xs *url = xs_fmt("%s/%s", srv_baseurl, acct);
564 xs *handle = xs_fmt("@%s@%s", acct, host);
565
566 xs_html_add(dl,
567 xs_html_tag("di",
568 xs_html_tag("dt",
569 xs_html_text(L("Admin account"))),
570 xs_html_tag("dd",
571 xs_html_tag("a",
572 xs_html_attr("href", url),
573 xs_html_text(handle)))));
574 }
575
576 return body;
577}
578
579
580xs_html *html_user_head(snac *user, const char *desc, const char *url)
581{
582 xs_html *head = html_base_head();
583
584 /* add the user CSS */
585 {
586 xs *css = NULL;
587 int size;
588
589 /* try to open the user css */
590 if (!valid_status(static_get(user, "style.css", &css, &size, NULL, NULL))) {
591 /* it's not there; try to open the server-wide css */
592 FILE *f;
593 xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
594
595 if ((f = fopen(g_css_fn, "r")) != NULL) {
596 css = xs_readall(f);
597 fclose(f);
598 }
599 }
600
601 if (css != NULL) {
602 xs_html_add(head,
603 xs_html_tag("style",
604 xs_html_raw(css)));
605 }
606 }
607
608 /* title */
609 xs *title = xs_fmt("%s (@%s@%s)", xs_dict_get(user->config, "name"),
610 user->uid, xs_dict_get(srv_config, "host"));
611
612 xs_html_add(head,
613 xs_html_tag("title",
614 xs_html_text(title)));
615
616 xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
617
618 if (avatar == NULL || *avatar == '\0') {
619 xs_free(avatar);
620 avatar = xs_fmt("%s/susie.png", srv_baseurl);
621 }
622
623 /* create a description field */
624 xs *s_desc = NULL;
625 int n;
626
627 if (desc == NULL)
628 s_desc = xs_dup(xs_dict_get(user->config, "bio"));
629 else
630 s_desc = xs_dup(desc);
631
632 /* shorten desc to a reasonable size */
633 for (n = 0; s_desc[n]; n++) {
634 if (n > 512 && (s_desc[n] == ' ' || s_desc[n] == '\n'))
635 break;
636 }
637
638 s_desc[n] = '\0';
639
640 /* og properties */
641 xs_html_add(head,
642 xs_html_sctag("meta",
643 xs_html_attr("property", "og:site_name"),
644 xs_html_attr("content", xs_dict_get(srv_config, "host"))),
645 xs_html_sctag("meta",
646 xs_html_attr("property", "og:title"),
647 xs_html_attr("content", title)),
648 xs_html_sctag("meta",
649 xs_html_attr("property", "og:description"),
650 xs_html_attr("content", s_desc)),
651 xs_html_sctag("meta",
652 xs_html_attr("property", "og:image"),
653 xs_html_attr("content", avatar)),
654 xs_html_sctag("meta",
655 xs_html_attr("property", "og:width"),
656 xs_html_attr("content", "300")),
657 xs_html_sctag("meta",
658 xs_html_attr("property", "og:height"),
659 xs_html_attr("content", "300")));
660
661 /* RSS link */
662 xs *rss_url = xs_fmt("%s.rss", user->actor);
663 xs_html_add(head,
664 xs_html_sctag("link",
665 xs_html_attr("rel", "alternate"),
666 xs_html_attr("type", "application/rss+xml"),
667 xs_html_attr("title", "RSS"),
668 xs_html_attr("href", rss_url)));
669
670 /* ActivityPub alternate link (actor id) */
671 xs_html_add(head,
672 xs_html_sctag("link",
673 xs_html_attr("rel", "alternate"),
674 xs_html_attr("type", "application/activity+json"),
675 xs_html_attr("href", url ? url : user->actor)));
676
677 return head;
678}
679
680
681static xs_html *html_user_body(snac *user, int read_only)
682{
683 xs_html *body = xs_html_tag("body", NULL);
684
685 /* top nav */
686 xs_html *top_nav = xs_html_tag("nav",
687 xs_html_attr("class", "snac-top-nav"));
688
689 xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
690
691 if (avatar == NULL || *avatar == '\0') {
692 xs_free(avatar);
693 avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
694 }
695
696 xs_html_add(top_nav,
697 xs_html_sctag("img",
698 xs_html_attr("src", avatar),
699 xs_html_attr("class", "snac-avatar"),
700 xs_html_attr("alt", "")));
701
702 if (read_only) {
703 xs *rss_url = xs_fmt("%s.rss", user->actor);
704 xs *admin_url = xs_fmt("%s/admin", user->actor);
705
706 xs_html_add(top_nav,
707 xs_html_tag("a",
708 xs_html_attr("href", rss_url),
709 xs_html_text(L("RSS"))),
710 xs_html_text(" - "),
711 xs_html_tag("a",
712 xs_html_attr("href", admin_url),
713 xs_html_attr("rel", "nofollow"),
714 xs_html_text(L("private"))));
715 }
716 else {
717 int n_len = notify_new_num(user);
718 xs_html *notify_count = NULL;
719
720 /* show the number of new notifications, if there are any */
721 if (n_len) {
722 xs *n_len_str = xs_fmt(" %d ", n_len);
723 notify_count = xs_html_tag("sup",
724 xs_html_attr("style", "background-color: red; color: white;"),
725 xs_html_text(n_len_str));
726 }
727 else
728 notify_count = xs_html_text("");
729
730 xs *admin_url = xs_fmt("%s/admin", user->actor);
731 xs *notify_url = xs_fmt("%s/notifications", user->actor);
732 xs *people_url = xs_fmt("%s/people", user->actor);
733 xs *instance_url = xs_fmt("%s/instance", user->actor);
734
735 xs_html_add(top_nav,
736 xs_html_tag("a",
737 xs_html_attr("href", user->actor),
738 xs_html_text(L("public"))),
739 xs_html_text(" - "),
740 xs_html_tag("a",
741 xs_html_attr("href", admin_url),
742 xs_html_text(L("private"))),
743 xs_html_text(" - "),
744 xs_html_tag("a",
745 xs_html_attr("href", notify_url),
746 xs_html_text(L("notifications"))),
747 notify_count,
748 xs_html_text(" - "),
749 xs_html_tag("a",
750 xs_html_attr("href", people_url),
751 xs_html_text(L("people"))),
752 xs_html_text(" - "),
753 xs_html_tag("a",
754 xs_html_attr("href", instance_url),
755 xs_html_text(L("instance"))),
756 xs_html_text(" "),
757 xs_html_tag("form",
758 xs_html_attr("style", "display: inline!important"),
759 xs_html_attr("class", "snac-search-box"),
760 xs_html_attr("action", admin_url),
761 xs_html_sctag("input",
762 xs_html_attr("type", "text"),
763 xs_html_attr("name", "q"),
764 xs_html_attr("title", L("Search posts by content (regular expression) or #tag")),
765 xs_html_attr("placeholder", L("Content search")))));
766 }
767
768 xs_html_add(body,
769 top_nav);
770
771 /* user info */
772 xs_html *top_user = xs_html_tag("div",
773 xs_html_attr("class", "h-card snac-top-user"));
774
775 if (read_only) {
776 const char *header = xs_dict_get(user->config, "header");
777 if (header && *header) {
778 xs_html_add(top_user,
779 xs_html_tag("div",
780 xs_html_attr("class", "snac-top-user-banner"),
781 xs_html_attr("style", "clear: both"),
782 xs_html_sctag("br", NULL),
783 xs_html_sctag("img",
784 xs_html_attr("src", header))));
785 }
786 }
787
788 xs *handle = xs_fmt("@%s@%s",
789 xs_dict_get(user->config, "uid"),
790 xs_dict_get(srv_config, "host"));
791
792 xs_html_add(top_user,
793 xs_html_tag("p",
794 xs_html_attr("class", "p-name snac-top-user-name"),
795 xs_html_text(xs_dict_get(user->config, "name"))),
796 xs_html_tag("p",
797 xs_html_attr("class", "snac-top-user-id"),
798 xs_html_text(handle)));
799
800 /** instance announcement **/
801
802 if (!read_only) {
803 double la = 0.0;
804 xs *user_la = xs_dup(xs_dict_get(user->config, "last_announcement"));
805 if (user_la != NULL)
806 la = xs_number_get(user_la);
807
808 const t_announcement *an = announcement(la);
809 if (an != NULL && (an->text != NULL)) {
810 xs *s = xs_fmt("?da=%.0f", an->timestamp);
811
812 xs_html_add(top_user, xs_html_tag("div",
813 xs_html_attr("class", "snac-announcement"),
814 xs_html_text(an->text),
815 xs_html_text(" "),
816 xs_html_tag("a",
817 xs_html_attr("href", s),
818 xs_html_text("Dismiss"))));
819 }
820 }
821
822 if (read_only) {
823 xs *es1 = encode_html(xs_dict_get(user->config, "bio"));
824 xs *tags = xs_list_new();
825 xs *bio1 = not_really_markdown(es1, NULL, &tags);
826 xs *bio2 = process_tags(user, bio1, &tags);
827
828 bio2 = replace_shortnames(bio2, tags, 2);
829
830 xs_html *top_user_bio = xs_html_tag("div",
831 xs_html_attr("class", "p-note snac-top-user-bio"),
832 xs_html_raw(bio2)); /* already sanitized */
833
834 xs_html_add(top_user,
835 top_user_bio);
836
837 const xs_dict *metadata = xs_dict_get(user->config, "metadata");
838 if (xs_type(metadata) == XSTYPE_DICT) {
839 const xs_str *k;
840 const xs_str *v;
841
842 xs_dict *val_links = user->links;
843 if (xs_is_null(val_links))
844 val_links = xs_stock(XSTYPE_DICT);
845
846 xs_html *snac_metadata = xs_html_tag("div",
847 xs_html_attr("class", "snac-metadata"));
848
849 int c = 0;
850 while (xs_dict_next(metadata, &k, &v, &c)) {
851 xs_html *value;
852
853 if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
854 /* is this link validated? */
855 xs *verified_link = NULL;
856 const xs_number *val_time = xs_dict_get(val_links, v);
857
858 if (xs_type(val_time) == XSTYPE_NUMBER) {
859 time_t t = xs_number_get(val_time);
860
861 if (t > 0) {
862 xs *s1 = xs_str_utctime(t, ISO_DATE_SPEC);
863 verified_link = xs_fmt("%s (%s)", L("verified link"), s1);
864 }
865 }
866
867 if (!xs_is_null(verified_link)) {
868 value = xs_html_tag("span",
869 xs_html_attr("title", verified_link),
870 xs_html_raw("✔ "),
871 xs_html_tag("a",
872 xs_html_attr("rel", "me"),
873 xs_html_attr("href", v),
874 xs_html_text(v)));
875 }
876 else {
877 value = xs_html_tag("a",
878 xs_html_attr("rel", "me"),
879 xs_html_attr("href", v),
880 xs_html_text(v));
881 }
882 }
883 else
884 if (xs_startswith(v, "gemini:/")) {
885 value = xs_html_tag("a",
886 xs_html_attr("rel", "me"),
887 xs_html_attr("href", v),
888 xs_html_text(v));
889 }
890 else
891 value = xs_html_text(v);
892
893 xs_html_add(snac_metadata,
894 xs_html_tag("span",
895 xs_html_attr("class", "snac-property-name"),
896 xs_html_text(k)),
897 xs_html_text(":"),
898 xs_html_raw(" "),
899 xs_html_tag("span",
900 xs_html_attr("class", "snac-property-value"),
901 value),
902 xs_html_sctag("br", NULL));
903 }
904
905 xs_html_add(top_user,
906 snac_metadata);
907 }
908 }
909
910 xs_html_add(body,
911 top_user);
912
913 return body;
914}
915
916
917xs_html *html_top_controls(snac *snac)
918/* generates the top controls */
919{
920 xs *ops_action = xs_fmt("%s/admin/action", snac->actor);
921
922 xs_html *top_controls = xs_html_tag("div",
923 xs_html_attr("class", "snac-top-controls"),
924
925 /** new post **/
926 html_note(snac, L("New Post..."),
927 "new_post_div", "new_post_form",
928 L("What's on your mind?"), "",
929 NULL, NULL,
930 xs_stock(XSTYPE_FALSE), "",
931 xs_stock(XSTYPE_FALSE), NULL,
932 NULL, 1, "", "", 0),
933
934 /** operations **/
935 xs_html_tag("details",
936 xs_html_tag("summary",
937 xs_html_text(L("Operations..."))),
938 xs_html_tag("p", NULL),
939 xs_html_tag("form",
940 xs_html_attr("autocomplete", "off"),
941 xs_html_attr("method", "post"),
942 xs_html_attr("action", ops_action),
943 xs_html_sctag("input",
944 xs_html_attr("type", "text"),
945 xs_html_attr("name", "actor"),
946 xs_html_attr("required", "required"),
947 xs_html_attr("placeholder", "bob@example.com")),
948 xs_html_text(" "),
949 xs_html_sctag("input",
950 xs_html_attr("type", "submit"),
951 xs_html_attr("name", "action"),
952 xs_html_attr("value", L("Follow"))),
953 xs_html_text(" "),
954 xs_html_text(L("(by URL or user@host)"))),
955 xs_html_tag("p", NULL),
956 xs_html_tag("form",
957 xs_html_attr("autocomplete", "off"),
958 xs_html_attr("method", "post"),
959 xs_html_attr("action", ops_action),
960 xs_html_sctag("input",
961 xs_html_attr("type", "text"),
962 xs_html_attr("name", "id"),
963 xs_html_attr("required", "required"),
964 xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
965 xs_html_text(" "),
966 xs_html_sctag("input",
967 xs_html_attr("type", "submit"),
968 xs_html_attr("name", "action"),
969 xs_html_attr("value", L("Boost"))),
970 xs_html_text(" "),
971 xs_html_text(L("(by URL)"))),
972 xs_html_tag("p", NULL)));
973
974 /** user settings **/
975
976 const char *email = "[disabled by admin]";
977
978 if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
979 email = xs_dict_get(snac->config_o, "email");
980 if (xs_is_null(email)) {
981 email = xs_dict_get(snac->config, "email");
982
983 if (xs_is_null(email))
984 email = "";
985 }
986 }
987
988 const char *cw = xs_dict_get(snac->config, "cw");
989 if (xs_is_null(cw))
990 cw = "";
991
992 const char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
993 if (xs_is_null(telegram_bot))
994 telegram_bot = "";
995
996 const char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
997 if (xs_is_null(telegram_chat_id))
998 telegram_chat_id = "";
999
1000 const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
1001 if (xs_is_null(ntfy_server))
1002 ntfy_server = "";
1003
1004 const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
1005 if (xs_is_null(ntfy_token))
1006 ntfy_token = "";
1007
1008 const char *purge_days = xs_dict_get(snac->config, "purge_days");
1009 if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
1010 purge_days = (char *)xs_number_str(purge_days);
1011 else
1012 purge_days = "0";
1013
1014 const xs_val *d_dm_f_u = xs_dict_get(snac->config, "drop_dm_from_unknown");
1015 const xs_val *bot = xs_dict_get(snac->config, "bot");
1016 const xs_val *a_private = xs_dict_get(snac->config, "private");
1017 const xs_val *auto_boost = xs_dict_get(snac->config, "auto_boost");
1018
1019 xs *metadata = xs_str_new(NULL);
1020 const xs_dict *md = xs_dict_get(snac->config, "metadata");
1021 const xs_str *k;
1022 const xs_str *v;
1023
1024 int c = 0;
1025 while (xs_dict_next(md, &k, &v, &c)) {
1026 xs *kp = xs_fmt("%s=%s", k, v);
1027
1028 if (*metadata)
1029 metadata = xs_str_cat(metadata, "\n");
1030 metadata = xs_str_cat(metadata, kp);
1031 }
1032
1033 xs *user_setup_action = xs_fmt("%s/admin/user-setup", snac->actor);
1034
1035 xs_html_add(top_controls,
1036 xs_html_tag("details",
1037 xs_html_tag("summary",
1038 xs_html_text(L("User Settings..."))),
1039 xs_html_tag("div",
1040 xs_html_attr("class", "snac-user-setup"),
1041 xs_html_tag("form",
1042 xs_html_attr("autocomplete", "off"),
1043 xs_html_attr("method", "post"),
1044 xs_html_attr("action", user_setup_action),
1045 xs_html_attr("enctype", "multipart/form-data"),
1046 xs_html_tag("p",
1047 xs_html_text(L("Display name:")),
1048 xs_html_sctag("br", NULL),
1049 xs_html_sctag("input",
1050 xs_html_attr("type", "text"),
1051 xs_html_attr("name", "name"),
1052 xs_html_attr("value", xs_dict_get(snac->config, "name")),
1053 xs_html_attr("placeholder", L("Your name")))),
1054 xs_html_tag("p",
1055 xs_html_text(L("Avatar: ")),
1056 xs_html_sctag("input",
1057 xs_html_attr("type", "file"),
1058 xs_html_attr("name", "avatar_file"))),
1059 xs_html_tag("p",
1060 xs_html_sctag("input",
1061 xs_html_attr("type", "checkbox"),
1062 xs_html_attr("name", "avatar_delete")),
1063 xs_html_text(L("Delete current avatar"))),
1064 xs_html_tag("p",
1065 xs_html_text(L("Header image (banner): ")),
1066 xs_html_sctag("input",
1067 xs_html_attr("type", "file"),
1068 xs_html_attr("name", "header_file"))),
1069 xs_html_tag("p",
1070 xs_html_sctag("input",
1071 xs_html_attr("type", "checkbox"),
1072 xs_html_attr("name", "header_delete")),
1073 xs_html_text(L("Delete current header image"))),
1074 xs_html_tag("p",
1075 xs_html_text(L("Bio:")),
1076 xs_html_sctag("br", NULL),
1077 xs_html_tag("textarea",
1078 xs_html_attr("name", "bio"),
1079 xs_html_attr("cols", "40"),
1080 xs_html_attr("rows", "4"),
1081 xs_html_attr("placeholder", L("Write about yourself here...")),
1082 xs_html_text(xs_dict_get(snac->config, "bio")))),
1083 xs_html_sctag("input",
1084 xs_html_attr("type", "checkbox"),
1085 xs_html_attr("name", "cw"),
1086 xs_html_attr("id", "cw"),
1087 xs_html_attr(strcmp(cw, "open") == 0 ? "checked" : "", NULL)),
1088 xs_html_tag("label",
1089 xs_html_attr("for", "cw"),
1090 xs_html_text(L("Always show sensitive content"))),
1091 xs_html_tag("p",
1092 xs_html_text(L("Email address for notifications:")),
1093 xs_html_sctag("br", NULL),
1094 xs_html_sctag("input",
1095 xs_html_attr("type", "text"),
1096 xs_html_attr("name", "email"),
1097 xs_html_attr("value", email),
1098 xs_html_attr("placeholder", "bob@example.com"))),
1099 xs_html_tag("p",
1100 xs_html_text(L("Telegram notifications (bot key and chat id):")),
1101 xs_html_sctag("br", NULL),
1102 xs_html_sctag("input",
1103 xs_html_attr("type", "text"),
1104 xs_html_attr("name", "telegram_bot"),
1105 xs_html_attr("value", telegram_bot),
1106 xs_html_attr("placeholder", "Bot API key")),
1107 xs_html_text(" "),
1108 xs_html_sctag("input",
1109 xs_html_attr("type", "text"),
1110 xs_html_attr("name", "telegram_chat_id"),
1111 xs_html_attr("value", telegram_chat_id),
1112 xs_html_attr("placeholder", "Chat id"))),
1113 xs_html_tag("p",
1114 xs_html_text(L("ntfy notifications (ntfy server and token):")),
1115 xs_html_sctag("br", NULL),
1116 xs_html_sctag("input",
1117 xs_html_attr("type", "text"),
1118 xs_html_attr("name", "ntfy_server"),
1119 xs_html_attr("value", ntfy_server),
1120 xs_html_attr("placeholder", "ntfy server - full URL (example: https://ntfy.sh/YourTopic)")),
1121 xs_html_text(" "),
1122 xs_html_sctag("input",
1123 xs_html_attr("type", "text"),
1124 xs_html_attr("name", "ntfy_token"),
1125 xs_html_attr("value", ntfy_token),
1126 xs_html_attr("placeholder", "ntfy token - if needed"))),
1127 xs_html_tag("p",
1128 xs_html_text(L("Maximum days to keep posts (0: server settings):")),
1129 xs_html_sctag("br", NULL),
1130 xs_html_sctag("input",
1131 xs_html_attr("type", "number"),
1132 xs_html_attr("name", "purge_days"),
1133 xs_html_attr("value", purge_days))),
1134 xs_html_tag("p",
1135 xs_html_sctag("input",
1136 xs_html_attr("type", "checkbox"),
1137 xs_html_attr("name", "drop_dm_from_unknown"),
1138 xs_html_attr("id", "drop_dm_from_unknown"),
1139 xs_html_attr(xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "", NULL)),
1140 xs_html_tag("label",
1141 xs_html_attr("for", "drop_dm_from_unknown"),
1142 xs_html_text(L("Drop direct messages from people you don't follow")))),
1143 xs_html_tag("p",
1144 xs_html_sctag("input",
1145 xs_html_attr("type", "checkbox"),
1146 xs_html_attr("name", "bot"),
1147 xs_html_attr("id", "bot"),
1148 xs_html_attr(xs_type(bot) == XSTYPE_TRUE ? "checked" : "", NULL)),
1149 xs_html_tag("label",
1150 xs_html_attr("for", "bot"),
1151 xs_html_text(L("This account is a bot")))),
1152 xs_html_tag("p",
1153 xs_html_sctag("input",
1154 xs_html_attr("type", "checkbox"),
1155 xs_html_attr("name", "auto_boost"),
1156 xs_html_attr("id", "auto_boost"),
1157 xs_html_attr(xs_is_true(auto_boost) ? "checked" : "", NULL)),
1158 xs_html_tag("label",
1159 xs_html_attr("for", "auto_boost"),
1160 xs_html_text(L("Auto-boost all mentions to this account")))),
1161 xs_html_tag("p",
1162 xs_html_sctag("input",
1163 xs_html_attr("type", "checkbox"),
1164 xs_html_attr("name", "private"),
1165 xs_html_attr("id", "private"),
1166 xs_html_attr(xs_type(a_private) == XSTYPE_TRUE ? "checked" : "", NULL)),
1167 xs_html_tag("label",
1168 xs_html_attr("for", "private"),
1169 xs_html_text(L("This account is private "
1170 "(posts are not shown through the web)")))),
1171 xs_html_tag("p",
1172 xs_html_text(L("Profile metadata (key=value pairs in each line):")),
1173 xs_html_sctag("br", NULL),
1174 xs_html_tag("textarea",
1175 xs_html_attr("name", "metadata"),
1176 xs_html_attr("cols", "40"),
1177 xs_html_attr("rows", "4"),
1178 xs_html_attr("placeholder", "Blog=https:/"
1179 "/example.com/my-blog\nGPG Key=1FA54\n..."),
1180 xs_html_text(metadata))),
1181
1182 xs_html_tag("p",
1183 xs_html_text(L("New password:")),
1184 xs_html_sctag("br", NULL),
1185 xs_html_sctag("input",
1186 xs_html_attr("type", "password"),
1187 xs_html_attr("name", "passwd1"),
1188 xs_html_attr("value", ""))),
1189 xs_html_tag("p",
1190 xs_html_text(L("Repeat new password:")),
1191 xs_html_sctag("br", NULL),
1192 xs_html_sctag("input",
1193 xs_html_attr("type", "password"),
1194 xs_html_attr("name", "passwd2"),
1195 xs_html_attr("value", ""))),
1196
1197 xs_html_sctag("input",
1198 xs_html_attr("type", "submit"),
1199 xs_html_attr("class", "button"),
1200 xs_html_attr("value", L("Update user info")))))));
1201
1202 return top_controls;
1203}
1204
1205
1206static xs_html *html_button(char *clss, char *label, char *hint)
1207{
1208 xs *c = xs_fmt("snac-btn-%s", clss);
1209
1210 /* use an NULL tag to separate non-css-classed buttons from one another */
1211 return xs_html_container(
1212 xs_html_sctag("input",
1213 xs_html_attr("type", "submit"),
1214 xs_html_attr("name", "action"),
1215 xs_html_attr("class", c),
1216 xs_html_attr("value", label),
1217 xs_html_attr("title", hint)),
1218 xs_html_text("\n"));
1219}
1220
1221
1222xs_str *build_mentions(snac *snac, const xs_dict *msg)
1223/* returns a string with the mentions in msg */
1224{
1225 xs_str *s = xs_str_new(NULL);
1226 const char *list = xs_dict_get(msg, "tag");
1227 const char *v;
1228 int c = 0;
1229
1230 while (xs_list_next(list, &v, &c)) {
1231 const char *type = xs_dict_get(v, "type");
1232 const char *href = xs_dict_get(v, "href");
1233 const char *name = xs_dict_get(v, "name");
1234
1235 if (type && strcmp(type, "Mention") == 0 &&
1236 href && strcmp(href, snac->actor) != 0 && name) {
1237 xs *s1 = NULL;
1238
1239 if (name[0] != '@') {
1240 s1 = xs_fmt("@%s", name);
1241 name = s1;
1242 }
1243
1244 xs *l = xs_split(name, "@");
1245
1246 /* is it a name without a host? */
1247 if (xs_list_len(l) < 3) {
1248 /* split the href and pick the host name LIKE AN ANIMAL */
1249 /* would be better to query the webfinger but *won't do that* here */
1250 xs *l2 = xs_split(href, "/");
1251
1252 if (xs_list_len(l2) >= 3) {
1253 xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
1254
1255 if (xs_str_in(s, s1) == -1)
1256 s = xs_str_cat(s, s1);
1257 }
1258 }
1259 else {
1260 if (xs_str_in(s, name) == -1) {
1261 s = xs_str_cat(s, name);
1262 s = xs_str_cat(s, " ");
1263 }
1264 }
1265 }
1266 }
1267
1268 if (*s) {
1269 xs *s1 = s;
1270 s = xs_fmt("\n\n\nCC: %s", s1);
1271 }
1272
1273 return s;
1274}
1275
1276
1277xs_html *html_entry_controls(snac *snac, const char *actor,
1278 const xs_dict *msg, const char *md5)
1279{
1280 const char *id = xs_dict_get(msg, "id");
1281 const char *group = xs_dict_get(msg, "audience");
1282
1283 xs *likes = object_likes(id);
1284 xs *boosts = object_announces(id);
1285
1286 xs *action = xs_fmt("%s/admin/action", snac->actor);
1287 xs *redir = xs_fmt("%s_entry", md5);
1288
1289 xs_html *form;
1290 xs_html *controls = xs_html_tag("div",
1291 xs_html_attr("class", "snac-controls"),
1292 form = xs_html_tag("form",
1293 xs_html_attr("autocomplete", "off"),
1294 xs_html_attr("method", "post"),
1295 xs_html_attr("action", action),
1296 xs_html_sctag("input",
1297 xs_html_attr("type", "hidden"),
1298 xs_html_attr("name", "id"),
1299 xs_html_attr("value", id)),
1300 xs_html_sctag("input",
1301 xs_html_attr("type", "hidden"),
1302 xs_html_attr("name", "actor"),
1303 xs_html_attr("value", actor)),
1304 xs_html_sctag("input",
1305 xs_html_attr("type", "hidden"),
1306 xs_html_attr("name", "group"),
1307 xs_html_attr("value", xs_is_null(group) ? "" : group)),
1308 xs_html_sctag("input",
1309 xs_html_attr("type", "hidden"),
1310 xs_html_attr("name", "redir"),
1311 xs_html_attr("value", redir))));
1312
1313 if (!xs_startswith(id, snac->actor)) {
1314 if (xs_list_in(likes, snac->md5) == -1) {
1315 /* not already liked; add button */
1316 xs_html_add(form,
1317 html_button("like", L("Like"), L("Say you like this post")));
1318 }
1319 else {
1320 /* not like it anymore */
1321 xs_html_add(form,
1322 html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
1323 }
1324 }
1325 else {
1326 if (is_pinned(snac, id))
1327 xs_html_add(form,
1328 html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
1329 else
1330 xs_html_add(form,
1331 html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
1332 }
1333
1334 if (is_msg_public(msg)) {
1335 if (xs_list_in(boosts, snac->md5) == -1) {
1336 /* not already boosted; add button */
1337 xs_html_add(form,
1338 html_button("boost", L("Boost"), L("Announce this post to your followers")));
1339 }
1340 else {
1341 /* already boosted; add button to regret */
1342 xs_html_add(form,
1343 html_button("unboost", L("Unboost"), L("I regret I boosted this")));
1344 }
1345 }
1346
1347 if (is_bookmarked(snac, id))
1348 xs_html_add(form,
1349 html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
1350 else
1351 xs_html_add(form,
1352 html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
1353
1354 if (strcmp(actor, snac->actor) != 0) {
1355 /* controls for other actors than this one */
1356 if (following_check(snac, actor)) {
1357 xs_html_add(form,
1358 html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
1359 }
1360 else {
1361 xs_html_add(form,
1362 html_button("follow", L("Follow"), L("Start following this user's activity")));
1363 }
1364
1365 if (!xs_is_null(group)) {
1366 if (following_check(snac, group)) {
1367 xs_html_add(form,
1368 html_button("unfollow", L("Unfollow Group"),
1369 L("Stop following this group or channel")));
1370 }
1371 else {
1372 xs_html_add(form,
1373 html_button("follow", L("Follow Group"),
1374 L("Start following this group or channel")));
1375 }
1376 }
1377
1378 xs_html_add(form,
1379 html_button("mute", L("MUTE"),
1380 L("Block any activity from this user forever")));
1381 }
1382
1383 if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
1384 xs_html_add(form,
1385 html_button("delete", L("Delete"), L("Delete this post")));
1386
1387 xs_html_add(form,
1388 html_button("hide", L("Hide"), L("Hide this post and its children")));
1389
1390 const char *prev_src = xs_dict_get(msg, "sourceContent");
1391
1392 if (!xs_is_null(prev_src) && strcmp(actor, snac->actor) == 0) { /** edit **/
1393 /* post can be edited */
1394 xs *div_id = xs_fmt("%s_edit", md5);
1395 xs *form_id = xs_fmt("%s_edit_form", md5);
1396 xs *redir = xs_fmt("%s_entry", md5);
1397
1398 const char *att_file = "";
1399 const char *att_alt_text = "";
1400 const xs_list *att_list = xs_dict_get(msg, "attachment");
1401
1402 /* does it have an attachment? */
1403 if (xs_type(att_list) == XSTYPE_LIST && xs_list_len(att_list)) {
1404 const xs_dict *d = xs_list_get(att_list, 0);
1405
1406 if (xs_type(d) == XSTYPE_DICT) {
1407 att_file = xs_dict_get_def(d, "url", "");
1408 att_alt_text = xs_dict_get_def(d, "name", "");
1409 }
1410 }
1411
1412 xs_html_add(controls, xs_html_tag("div",
1413 xs_html_tag("p", NULL),
1414 html_note(snac, L("Edit..."),
1415 div_id, form_id,
1416 "", prev_src,
1417 id, NULL,
1418 xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
1419 xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
1420 NULL, 0, att_file, att_alt_text, is_draft(snac, id))),
1421 xs_html_tag("p", NULL));
1422 }
1423
1424 { /** reply **/
1425 /* the post textarea */
1426 xs *ct = build_mentions(snac, msg);
1427 xs *div_id = xs_fmt("%s_reply", md5);
1428 xs *form_id = xs_fmt("%s_reply_form", md5);
1429 xs *redir = xs_fmt("%s_entry", md5);
1430
1431 xs_html_add(controls, xs_html_tag("div",
1432 xs_html_tag("p", NULL),
1433 html_note(snac, L("Reply..."),
1434 div_id, form_id,
1435 "", ct,
1436 NULL, NULL,
1437 xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
1438 xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
1439 id, 0, "", "", 0)),
1440 xs_html_tag("p", NULL));
1441 }
1442
1443 return controls;
1444}
1445
1446
1447xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
1448 int level, const char *md5, int hide_children)
1449{
1450 const char *id = xs_dict_get(msg, "id");
1451 const char *type = xs_dict_get(msg, "type");
1452 const char *actor;
1453 const char *v;
1454 int has_title = 0;
1455
1456 /* do not show non-public messages in the public timeline */
1457 if ((read_only || !user) && !is_msg_public(msg))
1458 return NULL;
1459
1460 /* hidden? do nothing more for this conversation */
1461 if (user && is_hidden(user, id)) {
1462 xs *s1 = xs_fmt("%s_entry", md5);
1463
1464 /* return just an dummy anchor, to keep position after hitting 'Hide' */
1465 return xs_html_tag("div",
1466 xs_html_tag("a",
1467 xs_html_attr("name", s1)));
1468 }
1469
1470 /* avoid too deep nesting, as it may be a loop */
1471 if (level >= MAX_CONVERSATION_LEVELS)
1472 return xs_html_tag("mark",
1473 xs_html_text(L("Truncated (too deep)")));
1474
1475 if (strcmp(type, "Follow") == 0) {
1476 return xs_html_tag("div",
1477 xs_html_attr("class", "snac-post"),
1478 xs_html_tag("div",
1479 xs_html_attr("class", "snac-post-header"),
1480 xs_html_tag("div",
1481 xs_html_attr("class", "snac-origin"),
1482 xs_html_text(L("follows you"))),
1483 html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg)));
1484 }
1485 else
1486 if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
1487 /* skip oddities */
1488 snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
1489 return NULL;
1490 }
1491
1492 /* ignore notes with "name", as they are votes to Questions */
1493 if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
1494 return NULL;
1495
1496 /* get the attributedTo */
1497 if ((actor = get_atto(msg)) == NULL)
1498 return NULL;
1499
1500 /* ignore muted morons immediately */
1501 if (user && is_muted(user, actor)) {
1502 xs *s1 = xs_fmt("%s_entry", md5);
1503
1504 /* return just an dummy anchor, to keep position after hitting 'MUTE' */
1505 return xs_html_tag("div",
1506 xs_html_tag("a",
1507 xs_html_attr("name", s1)));
1508 }
1509
1510 if ((user == NULL || strcmp(actor, user->actor) != 0)
1511 && !valid_status(actor_get(actor, NULL)))
1512 return NULL;
1513
1514 /** html_entry top tag **/
1515 xs_html *entry_top = xs_html_tag("div", NULL);
1516
1517 {
1518 xs *s1 = xs_fmt("%s_entry", md5);
1519 xs_html_add(entry_top,
1520 xs_html_tag("a",
1521 xs_html_attr("name", s1)));
1522 }
1523
1524 xs_html *entry = xs_html_tag("div",
1525 xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
1526
1527 xs_html_add(entry_top,
1528 entry);
1529
1530 /** post header **/
1531
1532 xs_html *score;
1533 xs_html *post_header = xs_html_tag("div",
1534 xs_html_attr("class", "snac-post-header"),
1535 score = xs_html_tag("div",
1536 xs_html_attr("class", "snac-score")));
1537
1538 xs_html_add(entry,
1539 post_header);
1540
1541 if (user && is_pinned(user, id)) {
1542 /* add a pin emoji */
1543 xs_html_add(score,
1544 xs_html_tag("span",
1545 xs_html_attr("title", L("Pinned")),
1546 xs_html_raw(" 📌 ")));
1547 }
1548
1549 if (user && is_bookmarked(user, id)) {
1550 /* add a bookmark emoji */
1551 xs_html_add(score,
1552 xs_html_tag("span",
1553 xs_html_attr("title", L("Bookmarked")),
1554 xs_html_raw(" 🔖 ")));
1555 }
1556
1557 if (strcmp(type, "Question") == 0) {
1558 /* add the ballot box emoji */
1559 xs_html_add(score,
1560 xs_html_tag("span",
1561 xs_html_attr("title", L("Poll")),
1562 xs_html_raw(" 🗳 ")));
1563
1564 if (user && was_question_voted(user, id)) {
1565 /* add a check to show this poll was voted */
1566 xs_html_add(score,
1567 xs_html_tag("span",
1568 xs_html_attr("title", L("Voted")),
1569 xs_html_raw(" ✓ ")));
1570 }
1571 }
1572
1573 if (strcmp(type, "Event") == 0) {
1574 /* add the calendar emoji */
1575 xs_html_add(score,
1576 xs_html_tag("span",
1577 xs_html_attr("title", L("Event")),
1578 xs_html_raw(" 📅 ")));
1579 }
1580
1581 /* if it's a user from this same instance, add the score */
1582 if (xs_startswith(id, srv_baseurl)) {
1583 int n_likes = object_likes_len(id);
1584 int n_boosts = object_announces_len(id);
1585
1586 /* alternate emojis: %d 👍 %d 🔁 */
1587 xs *s1 = xs_fmt("%d ★ %d ↺\n", n_likes, n_boosts);
1588
1589 xs_html_add(score,
1590 xs_html_raw(s1));
1591 }
1592
1593 xs *boosts = object_announces(id);
1594
1595 if (xs_list_len(boosts)) {
1596 /* if somebody boosted this, show as origin */
1597 const char *p = xs_list_get(boosts, -1);
1598 xs *actor_r = NULL;
1599
1600 if (user && xs_list_in(boosts, user->md5) != -1) {
1601 /* we boosted this */
1602 xs_html_add(post_header,
1603 xs_html_tag("div",
1604 xs_html_attr("class", "snac-origin"),
1605 xs_html_tag("a",
1606 xs_html_attr("href", user->actor),
1607 xs_html_text(xs_dict_get(user->config, "name"))),
1608 xs_html_text(" "),
1609 xs_html_text(L("boosted"))));
1610 }
1611 else
1612 if (valid_status(object_get_by_md5(p, &actor_r))) {
1613 xs *name = actor_name(actor_r);
1614
1615 if (!xs_is_null(name)) {
1616 xs *href = NULL;
1617 const char *id = xs_dict_get(actor_r, "id");
1618 int fwers = 0;
1619 int fwing = 0;
1620
1621 if (user != NULL) {
1622 fwers = follower_check(user, id);
1623 fwing = following_check(user, id);
1624 }
1625
1626 if (!read_only && (fwers || fwing))
1627 href = xs_fmt("%s/people#%s", user->actor, p);
1628 else
1629 href = xs_dup(id);
1630
1631 xs_html_add(post_header,
1632 xs_html_tag("div",
1633 xs_html_attr("class", "snac-origin"),
1634 xs_html_tag("a",
1635 xs_html_attr("href", href),
1636 xs_html_raw(name)), /* already sanitized */
1637 xs_html_text(" "),
1638 xs_html_text(L("boosted"))));
1639 }
1640 }
1641 }
1642 else
1643 if (strcmp(type, "Note") == 0) {
1644 if (level == 0) {
1645 /* is the parent not here? */
1646 const char *parent = xs_dict_get(msg, "inReplyTo");
1647
1648 if (user && !xs_is_null(parent) && *parent && !timeline_here(user, parent)) {
1649 xs_html_add(post_header,
1650 xs_html_tag("div",
1651 xs_html_attr("class", "snac-origin"),
1652 xs_html_text(L("in reply to")),
1653 xs_html_text(" "),
1654 xs_html_tag("a",
1655 xs_html_attr("href", parent),
1656 xs_html_text("ยป"))));
1657 }
1658 }
1659 }
1660
1661 xs_html_add(post_header,
1662 html_msg_icon(read_only ? NULL : user, actor, msg));
1663
1664 /** post content **/
1665
1666 xs_html *snac_content_wrap = xs_html_tag("div",
1667 xs_html_attr("class", "e-content snac-content"));
1668
1669 xs_html_add(entry,
1670 snac_content_wrap);
1671
1672 if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
1673 xs_html_add(snac_content_wrap,
1674 xs_html_tag("h3",
1675 xs_html_attr("class", "snac-entry-title"),
1676 xs_html_text(v)));
1677
1678 has_title = 1;
1679 }
1680
1681 xs_html *snac_content = NULL;
1682
1683 v = xs_dict_get(msg, "summary");
1684
1685 /* is it sensitive? */
1686 if (xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
1687 if (xs_is_null(v) || *v == '\0')
1688 v = "...";
1689
1690 const char *cw = "";
1691
1692 if (user) {
1693 /* only show it when not in the public timeline and the config setting is "open" */
1694 cw = xs_dict_get(user->config, "cw");
1695 if (xs_is_null(cw) || read_only)
1696 cw = "";
1697 }
1698
1699 snac_content = xs_html_tag("details",
1700 xs_html_attr(cw, NULL),
1701 xs_html_tag("summary",
1702 xs_html_text(v),
1703 xs_html_text(L(" [SENSITIVE CONTENT]"))));
1704 }
1705 else {
1706 /* print the summary as a header (sites like e.g. Friendica can contain one) */
1707 if (!has_title && !xs_is_null(v) && *v) {
1708 xs_html_add(snac_content_wrap,
1709 xs_html_tag("h3",
1710 xs_html_attr("class", "snac-entry-title"),
1711 xs_html_text(v)));
1712
1713 has_title = 1;
1714 }
1715
1716 snac_content = xs_html_tag("div", NULL);
1717 }
1718
1719 xs_html_add(snac_content_wrap,
1720 snac_content);
1721
1722 {
1723 /** build the content string **/
1724 const char *content = xs_dict_get(msg, "content");
1725
1726 if (xs_type(content) != XSTYPE_STRING) {
1727 if (!xs_is_null(content))
1728 srv_archive_error("unexpected_content_xstype",
1729 "content field type", xs_stock(XSTYPE_DICT), msg);
1730
1731 content = "";
1732 }
1733
1734 /* skip ugly line breaks at the beginning */
1735 while (xs_startswith(content, "<br>"))
1736 content += 4;
1737
1738 xs *c = sanitize(content);
1739
1740 /* do some tweaks to the content */
1741 c = xs_replace_i(c, "\r", "");
1742
1743 while (xs_endswith(c, "<br><br>"))
1744 c = xs_crop_i(c, 0, -4);
1745
1746 c = xs_replace_i(c, "<br><br>", "<p>");
1747
1748 c = xs_str_cat(c, "<p>");
1749
1750 /* replace the :shortnames: */
1751 c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2);
1752
1753 /* Peertube videos content is in markdown */
1754 const char *mtype = xs_dict_get(msg, "mediaType");
1755 if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
1756 /* a full conversion could be better */
1757 c = xs_replace_i(c, "\r", "");
1758 c = xs_replace_i(c, "\n", "<br>");
1759 }
1760
1761 /* c contains sanitized HTML */
1762 xs_html_add(snac_content,
1763 xs_html_raw(c));
1764 }
1765
1766 if (strcmp(type, "Question") == 0) { /** question content **/
1767 const xs_list *oo = xs_dict_get(msg, "oneOf");
1768 const xs_list *ao = xs_dict_get(msg, "anyOf");
1769 const xs_list *p;
1770 const xs_dict *v;
1771 int closed = 0;
1772 const char *f_closed = NULL;
1773
1774 xs_html *poll = xs_html_tag("div", NULL);
1775
1776 if (read_only)
1777 closed = 1; /* non-identified page; show as closed */
1778 else
1779 if (user && xs_startswith(id, user->actor))
1780 closed = 1; /* we questioned; closed for us */
1781 else
1782 if (user && was_question_voted(user, id))
1783 closed = 1; /* we already voted; closed for us */
1784
1785 if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
1786 /* it has a closed date... but is it in the past? */
1787 time_t t0 = time(NULL);
1788 time_t t1 = xs_parse_iso_date(f_closed, 0);
1789
1790 if (t1 < t0)
1791 closed = 2;
1792 }
1793
1794 /* get the appropriate list of options */
1795 p = oo != NULL ? oo : ao;
1796
1797 if (closed || user == NULL) {
1798 /* closed poll */
1799 xs_html *poll_result = xs_html_tag("table",
1800 xs_html_attr("class", "snac-poll-result"));
1801 int c = 0;
1802
1803 while (xs_list_next(p, &v, &c)) {
1804 const char *name = xs_dict_get(v, "name");
1805 const xs_dict *replies = xs_dict_get(v, "replies");
1806
1807 if (name && replies) {
1808 char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
1809
1810 xs_html_add(poll_result,
1811 xs_html_tag("tr",
1812 xs_html_tag("td",
1813 xs_html_text(name),
1814 xs_html_text(":")),
1815 xs_html_tag("td",
1816 xs_html_text(ti))));
1817 }
1818 }
1819
1820 xs_html_add(poll,
1821 poll_result);
1822 }
1823 else {
1824 /* poll still active */
1825 xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
1826 xs_html *form;
1827 xs_html *poll_form = xs_html_tag("div",
1828 xs_html_attr("class", "snac-poll-form"),
1829 form = xs_html_tag("form",
1830 xs_html_attr("autocomplete", "off"),
1831 xs_html_attr("method", "post"),
1832 xs_html_attr("action", vote_action),
1833 xs_html_sctag("input",
1834 xs_html_attr("type", "hidden"),
1835 xs_html_attr("name", "actor"),
1836 xs_html_attr("value", actor)),
1837 xs_html_sctag("input",
1838 xs_html_attr("type", "hidden"),
1839 xs_html_attr("name", "irt"),
1840 xs_html_attr("value", id))));
1841
1842 int c = 0;
1843 while (xs_list_next(p, &v, &c)) {
1844 const char *name = xs_dict_get(v, "name");
1845 const xs_dict *replies = xs_dict_get(v, "replies");
1846
1847 if (name) {
1848 char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
1849
1850 xs_html *btn = xs_html_sctag("input",
1851 xs_html_attr("id", name),
1852 xs_html_attr("value", name),
1853 xs_html_attr("name", "question"));
1854
1855 if (!xs_is_null(oo)) {
1856 xs_html_add(btn,
1857 xs_html_attr("type", "radio"),
1858 xs_html_attr("required", "required"));
1859 }
1860 else
1861 xs_html_add(btn,
1862 xs_html_attr("type", "checkbox"));
1863
1864 xs_html_add(form,
1865 btn,
1866 xs_html_text(" "),
1867 xs_html_tag("span",
1868 xs_html_attr("title", ti),
1869 xs_html_text(name)),
1870 xs_html_sctag("br", NULL));
1871 }
1872 }
1873
1874 xs_html_add(form,
1875 xs_html_tag("p", NULL),
1876 xs_html_sctag("input",
1877 xs_html_attr("type", "submit"),
1878 xs_html_attr("class", "button"),
1879 xs_html_attr("value", L("Vote"))));
1880
1881 xs_html_add(poll,
1882 poll_form);
1883 }
1884
1885 /* if it's *really* closed, say it */
1886 if (closed == 2) {
1887 xs_html_add(poll,
1888 xs_html_tag("p",
1889 xs_html_text(L("Closed"))));
1890 }
1891 else {
1892 /* show when the poll closes */
1893 const char *end_time = xs_dict_get(msg, "endTime");
1894
1895 /* Pleroma does not have an endTime field;
1896 it has a closed time in the future */
1897 if (xs_is_null(end_time))
1898 end_time = xs_dict_get(msg, "closed");
1899
1900 if (!xs_is_null(end_time)) {
1901 time_t t0 = time(NULL);
1902 time_t t1 = xs_parse_iso_date(end_time, 0);
1903
1904 if (t1 > 0 && t1 > t0) {
1905 time_t diff_time = t1 - t0;
1906 xs *tf = xs_str_time_diff(diff_time);
1907 char *p = tf;
1908
1909 /* skip leading zeros */
1910 for (; *p == '0' || *p == ':'; p++);
1911
1912 xs_html_add(poll,
1913 xs_html_tag("p",
1914 xs_html_text(L("Closes in")),
1915 xs_html_text(" "),
1916 xs_html_text(p)));
1917 }
1918 }
1919 }
1920
1921 xs_html_add(snac_content,
1922 poll);
1923 }
1924
1925 /** attachments **/
1926 xs *attach = get_attachments(msg);
1927
1928 {
1929 /* make custom css for attachments easier */
1930 xs_html *content_attachments = xs_html_tag("div",
1931 xs_html_attr("class", "snac-content-attachments"));
1932
1933 xs_html_add(snac_content,
1934 content_attachments);
1935
1936 int c = 0;
1937 const xs_dict *a;
1938 while (xs_list_next(attach, &a, &c)) {
1939 const char *type = xs_dict_get(a, "type");
1940 const char *href = xs_dict_get(a, "href");
1941 const char *name = xs_dict_get(a, "name");
1942
1943 if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
1944 xs_html_add(content_attachments,
1945 xs_html_tag("a",
1946 xs_html_attr("href", href),
1947 xs_html_attr("target", "_blank"),
1948 xs_html_sctag("img",
1949 xs_html_attr("loading", "lazy"),
1950 xs_html_attr("src", href),
1951 xs_html_attr("alt", name),
1952 xs_html_attr("title", name))));
1953 }
1954 else
1955 if (xs_startswith(type, "video/")) {
1956 xs_html_add(content_attachments,
1957 xs_html_tag("video",
1958 xs_html_attr("preload", "none"),
1959 xs_html_attr("style", "width: 100%"),
1960 xs_html_attr("class", "snac-embedded-video"),
1961 xs_html_attr("controls", NULL),
1962 xs_html_attr("src", href),
1963 xs_html_text(L("Video")),
1964 xs_html_text(": "),
1965 xs_html_tag("a",
1966 xs_html_attr("href", href),
1967 xs_html_text(name))));
1968 }
1969 else
1970 if (xs_startswith(type, "audio/")) {
1971 xs_html_add(content_attachments,
1972 xs_html_tag("audio",
1973 xs_html_attr("preload", "none"),
1974 xs_html_attr("style", "width: 100%"),
1975 xs_html_attr("class", "snac-embedded-audio"),
1976 xs_html_attr("controls", NULL),
1977 xs_html_attr("src", href),
1978 xs_html_text(L("Audio")),
1979 xs_html_text(": "),
1980 xs_html_tag("a",
1981 xs_html_attr("href", href),
1982 xs_html_text(name))));
1983 }
1984 else
1985 if (strcmp(type, "Link") == 0) {
1986 xs_html_add(content_attachments,
1987 xs_html_tag("p",
1988 xs_html_tag("a",
1989 xs_html_attr("href", href),
1990 xs_html_text(href))));
1991
1992 /* do not generate an Alt... */
1993 name = NULL;
1994 }
1995 else {
1996 xs_html_add(content_attachments,
1997 xs_html_tag("p",
1998 xs_html_tag("a",
1999 xs_html_attr("href", href),
2000 xs_html_text(L("Attachment")),
2001 xs_html_text(": "),
2002 xs_html_text(href))));
2003
2004 /* do not generate an Alt... */
2005 name = NULL;
2006 }
2007
2008 if (name != NULL && *name) {
2009 xs_html_add(content_attachments,
2010 xs_html_tag("p",
2011 xs_html_attr("class", "snac-alt-text"),
2012 xs_html_tag("details",
2013 xs_html_tag("summary",
2014 xs_html_text(L("Alt..."))),
2015 xs_html_text(name))));
2016 }
2017 }
2018 }
2019
2020 /* has this message an audience (i.e., comes from a channel or community)? */
2021 const char *audience = xs_dict_get(msg, "audience");
2022 if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
2023 xs_html *au_tag = xs_html_tag("p",
2024 xs_html_text("("),
2025 xs_html_tag("a",
2026 xs_html_attr("href", audience),
2027 xs_html_attr("title", L("Source channel or community")),
2028 xs_html_text(audience)),
2029 xs_html_text(")"));
2030
2031 xs_html_add(snac_content_wrap,
2032 au_tag);
2033 }
2034
2035 /** controls **/
2036
2037 if (!read_only && user) {
2038 xs_html_add(entry,
2039 html_entry_controls(user, actor, msg, md5));
2040 }
2041
2042 /** children **/
2043 if (!hide_children) {
2044 xs *children = object_children(id);
2045 int left = xs_list_len(children);
2046
2047 if (left) {
2048 xs_html *ch_details = xs_html_tag("details",
2049 xs_html_attr("open", NULL),
2050 xs_html_tag("summary",
2051 xs_html_text("...")));
2052
2053 xs_html_add(entry,
2054 ch_details);
2055
2056 xs_html *ch_container = xs_html_tag("div",
2057 xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
2058
2059 xs_html_add(ch_details,
2060 ch_container);
2061
2062 xs_html *ch_older = NULL;
2063 if (left > 3) {
2064 xs_html_add(ch_container,
2065 ch_older = xs_html_tag("details",
2066 xs_html_tag("summary",
2067 xs_html_text(L("Older...")))));
2068 }
2069
2070 xs_list *p = children;
2071 const char *cmd5;
2072 int cnt = 0;
2073 int o_cnt = 0;
2074
2075 while (xs_list_iter(&p, &cmd5)) {
2076 xs *chd = NULL;
2077
2078 if (user)
2079 timeline_get_by_md5(user, cmd5, &chd);
2080 else
2081 object_get_by_md5(cmd5, &chd);
2082
2083 if (chd != NULL) {
2084 if (xs_is_null(xs_dict_get(chd, "name"))) {
2085 xs_html *che = html_entry(user, chd, read_only,
2086 level + 1, cmd5, hide_children);
2087
2088 if (che != NULL) {
2089 if (left > 3) {
2090 xs_html_add(ch_older,
2091 che);
2092
2093 o_cnt++;
2094 }
2095 else
2096 xs_html_add(ch_container,
2097 che);
2098
2099 cnt++;
2100 }
2101 }
2102
2103 left--;
2104 }
2105 else
2106 srv_debug(2, xs_fmt("cannot read child %s", cmd5));
2107 }
2108
2109 /* if no children were finally added, hide the details */
2110 if (cnt == 0)
2111 xs_html_add(ch_details,
2112 xs_html_attr("style", "display: none"));
2113
2114 if (o_cnt == 0 && ch_older)
2115 xs_html_add(ch_older,
2116 xs_html_attr("style", "display: none"));
2117 }
2118 }
2119
2120 return entry_top;
2121}
2122
2123
2124xs_html *html_footer(void)
2125{
2126 return xs_html_tag("div",
2127 xs_html_attr("class", "snac-footer"),
2128 xs_html_tag("a",
2129 xs_html_attr("href", srv_baseurl),
2130 xs_html_text(L("about this site"))),
2131 xs_html_text(" - "),
2132 xs_html_text(L("powered by ")),
2133 xs_html_tag("a",
2134 xs_html_attr("href", WHAT_IS_SNAC_URL),
2135 xs_html_tag("abbr",
2136 xs_html_attr("title", "Social Networks Are Crap"),
2137 xs_html_text("snac"))));
2138}
2139
2140
2141xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
2142 int skip, int show, int show_more,
2143 const char *title, const char *page,
2144 int utl, const char *error)
2145/* returns the HTML for the timeline */
2146{
2147 xs_list *p = (xs_list *)list;
2148 const char *v;
2149 double t = ftime();
2150
2151 xs *desc = NULL;
2152 xs *alternate = NULL;
2153
2154 if (xs_list_len(list) == 1) {
2155 /* only one element? pick the description from the source */
2156 const char *id = xs_list_get(list, 0);
2157 xs *d = NULL;
2158 object_get_by_md5(id, &d);
2159 const char *sc = xs_dict_get(d, "sourceContent");
2160 if (d && sc != NULL)
2161 desc = xs_dup(sc);
2162
2163 alternate = xs_dup(xs_dict_get(d, "id"));
2164 }
2165
2166 xs_html *head;
2167 xs_html *body;
2168
2169 if (user) {
2170 head = html_user_head(user, desc, alternate);
2171 body = html_user_body(user, read_only);
2172 }
2173 else {
2174 head = html_instance_head();
2175 body = html_instance_body();
2176 }
2177
2178 xs_html *html = xs_html_tag("html",
2179 head,
2180 body);
2181
2182 if (user && !read_only)
2183 xs_html_add(body,
2184 html_top_controls(user));
2185
2186 if (error != NULL) {
2187 xs_html_add(body,
2188 xs_html_tag("dialog",
2189 xs_html_attr("open", NULL),
2190 xs_html_tag("p",
2191 xs_html_text(error)),
2192 xs_html_tag("form",
2193 xs_html_attr("method", "dialog"),
2194 xs_html_sctag("input",
2195 xs_html_attr("type", "submit"),
2196 xs_html_attr("value", L("Dismiss"))))));
2197 }
2198
2199 /* show links to the available lists */
2200 if (user && !read_only) {
2201 xs_html *lol = xs_html_tag("ul",
2202 xs_html_attr("class", "snac-list-of-lists"));
2203 xs_html_add(body, lol);
2204
2205 xs *lists = list_maint(user, NULL, 0); /* get list of lists */
2206
2207 int ct = 0;
2208 const char *v;
2209
2210 while (xs_list_next(lists, &v, &ct)) {
2211 const char *lname = xs_list_get(v, 1);
2212 xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
2213 xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
2214
2215 xs_html_add(lol,
2216 xs_html_tag("li",
2217 xs_html_tag("a",
2218 xs_html_attr("href", url),
2219 xs_html_attr("class", "snac-list-link"),
2220 xs_html_attr("title", ttl),
2221 xs_html_text(lname))));
2222 }
2223
2224 {
2225 /* show the list of pinned posts */
2226 xs *url = xs_fmt("%s/pinned", user->actor);
2227 xs_html_add(lol,
2228 xs_html_tag("li",
2229 xs_html_tag("a",
2230 xs_html_attr("href", url),
2231 xs_html_attr("class", "snac-list-link"),
2232 xs_html_attr("title", L("Pinned posts")),
2233 xs_html_text("pinned"))));
2234 }
2235
2236 {
2237 /* show the list of bookmarked posts */
2238 xs *url = xs_fmt("%s/bookmarks", user->actor);
2239 xs_html_add(lol,
2240 xs_html_tag("li",
2241 xs_html_tag("a",
2242 xs_html_attr("href", url),
2243 xs_html_attr("class", "snac-list-link"),
2244 xs_html_attr("title", L("Bookmarked posts")),
2245 xs_html_text("bookmarks"))));
2246 }
2247
2248 {
2249 /* show the list of drafts */
2250 xs *url = xs_fmt("%s/drafts", user->actor);
2251 xs_html_add(lol,
2252 xs_html_tag("li",
2253 xs_html_tag("a",
2254 xs_html_attr("href", url),
2255 xs_html_attr("class", "snac-list-link"),
2256 xs_html_attr("title", L("Post drafts")),
2257 xs_html_text("drafts"))));
2258 }
2259 }
2260
2261 if (title) {
2262 xs_html_add(body,
2263 xs_html_tag("h2",
2264 xs_html_attr("class", "snac-header"),
2265 xs_html_text(title)));
2266 }
2267
2268 xs_html_add(body,
2269 xs_html_tag("a",
2270 xs_html_attr("name", "snac-posts")));
2271
2272 xs_html *posts = xs_html_tag("div",
2273 xs_html_attr("class", "snac-posts"));
2274
2275 xs_html_add(body,
2276 posts);
2277
2278 while (xs_list_iter(&p, &v)) {
2279 xs *msg = NULL;
2280 int status;
2281
2282 if (utl && user && !is_pinned_by_md5(user, v))
2283 status = timeline_get_by_md5(user, v, &msg);
2284 else
2285 status = object_get_by_md5(v, &msg);
2286
2287 if (!valid_status(status))
2288 continue;
2289
2290 /* if it's an instance page, discard messages from private users */
2291 if (user == NULL && is_msg_from_private_user(msg))
2292 continue;
2293
2294 /* is this message a non-public reply? */
2295 if (user != NULL && !is_msg_public(msg)) {
2296 const char *irt = xs_dict_get(msg, "inReplyTo");
2297
2298 /* is it a reply to something not in the storage? */
2299 if (!xs_is_null(irt) && !object_here(irt)) {
2300 /* is it for me? */
2301 const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
2302 const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
2303
2304 if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
2305 snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
2306 continue;
2307 }
2308 }
2309 }
2310
2311 xs_html *entry = html_entry(user, msg, read_only, 0, v, user ? 0 : 1);
2312
2313 if (entry != NULL)
2314 xs_html_add(posts,
2315 entry);
2316 }
2317
2318 if (list && user && read_only) {
2319 /** history **/
2320 if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE) {
2321 xs_html *ul = xs_html_tag("ul", NULL);
2322
2323 xs_html *history = xs_html_tag("div",
2324 xs_html_attr("class", "snac-history"),
2325 xs_html_tag("p",
2326 xs_html_attr("class", "snac-history-title"),
2327 xs_html_text(L("History"))),
2328 ul);
2329
2330 xs *list = history_list(user);
2331 xs_list *p = list;
2332 const char *v;
2333
2334 while (xs_list_iter(&p, &v)) {
2335 xs *fn = xs_replace(v, ".html", "");
2336 xs *url = xs_fmt("%s/h/%s", user->actor, v);
2337
2338 xs_html_add(ul,
2339 xs_html_tag("li",
2340 xs_html_tag("a",
2341 xs_html_attr("href", url),
2342 xs_html_text(fn))));
2343 }
2344
2345 xs_html_add(body,
2346 history);
2347 }
2348 }
2349
2350 {
2351 xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
2352 xs_html_add(body,
2353 xs_html_raw(s1));
2354 }
2355
2356 if (show_more) {
2357 xs *m = NULL;
2358 xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
2359
2360 xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
2361
2362 if (page != NULL)
2363 url = xs_str_cat(url, page);
2364
2365 if (xs_str_in(url, "?") != -1)
2366 m = xs_fmt("%s&%s", url, ss);
2367 else
2368 m = xs_fmt("%s?%s", url, ss);
2369
2370 xs_html *more_links = xs_html_tag("p",
2371 xs_html_tag("a",
2372 xs_html_attr("href", url),
2373 xs_html_attr("name", "snac-more"),
2374 xs_html_text(L("Back to top"))),
2375 xs_html_text(" - "),
2376 xs_html_tag("a",
2377 xs_html_attr("href", m),
2378 xs_html_attr("name", "snac-more"),
2379 xs_html_text(L("More..."))));
2380
2381 xs_html_add(body,
2382 more_links);
2383 }
2384
2385 xs_html_add(body,
2386 html_footer());
2387
2388 return xs_html_render_s(html, "<!DOCTYPE html>\n");
2389}
2390
2391
2392xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t)
2393{
2394 xs_html *snac_posts;
2395 xs_html *people = xs_html_tag("div",
2396 xs_html_tag("h2",
2397 xs_html_attr("class", "snac-header"),
2398 xs_html_text(header)),
2399 snac_posts = xs_html_tag("details",
2400 xs_html_attr("open", NULL),
2401 xs_html_tag("summary",
2402 xs_html_text("..."))));
2403
2404 xs_list *p = list;
2405 const char *actor_id;
2406
2407 while (xs_list_iter(&p, &actor_id)) {
2408 xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
2409 xs *actor = NULL;
2410
2411 if (valid_status(actor_get(actor_id, &actor))) {
2412 xs_html *snac_post = xs_html_tag("div",
2413 xs_html_attr("class", "snac-post"),
2414 xs_html_tag("a",
2415 xs_html_attr("name", md5)),
2416 xs_html_tag("div",
2417 xs_html_attr("class", "snac-post-header"),
2418 html_actor_icon(snac, actor, xs_dict_get(actor, "published"), NULL, NULL, 0, 1)));
2419
2420 /* content (user bio) */
2421 const char *c = xs_dict_get(actor, "summary");
2422
2423 if (!xs_is_null(c)) {
2424 xs *sc = sanitize(c);
2425
2426 xs_html *snac_content = xs_html_tag("div",
2427 xs_html_attr("class", "snac-content"));
2428
2429 if (xs_startswith(sc, "<p>"))
2430 xs_html_add(snac_content,
2431 xs_html_raw(sc)); /* already sanitized */
2432 else
2433 xs_html_add(snac_content,
2434 xs_html_tag("p",
2435 xs_html_raw(sc))); /* already sanitized */
2436
2437 xs_html_add(snac_post, snac_content);
2438 }
2439
2440 /* buttons */
2441 xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
2442
2443 xs_html *snac_controls = xs_html_tag("div",
2444 xs_html_attr("class", "snac-controls"));
2445
2446 xs_html *form = xs_html_tag("form",
2447 xs_html_attr("autocomplete", "off"),
2448 xs_html_attr("method", "post"),
2449 xs_html_attr("action", btn_form_action),
2450 xs_html_sctag("input",
2451 xs_html_attr("type", "hidden"),
2452 xs_html_attr("name", "actor"),
2453 xs_html_attr("value", actor_id)),
2454 xs_html_sctag("input",
2455 xs_html_attr("type", "hidden"),
2456 xs_html_attr("name", "actor-form"),
2457 xs_html_attr("value", "yes")));
2458
2459 xs_html_add(snac_controls, form);
2460
2461 if (following_check(snac, actor_id)) {
2462 xs_html_add(form,
2463 html_button("unfollow", L("Unfollow"),
2464 L("Stop following this user's activity")));
2465
2466 if (is_limited(snac, actor_id))
2467 xs_html_add(form,
2468 html_button("unlimit", L("Unlimit"),
2469 L("Allow announces (boosts) from this user")));
2470 else
2471 xs_html_add(form,
2472 html_button("limit", L("Limit"),
2473 L("Block announces (boosts) from this user")));
2474 }
2475 else {
2476 xs_html_add(form,
2477 html_button("follow", L("Follow"),
2478 L("Start following this user's activity")));
2479
2480 if (follower_check(snac, actor_id))
2481 xs_html_add(form,
2482 html_button("delete", L("Delete"), L("Delete this user")));
2483 }
2484
2485 if (is_muted(snac, actor_id))
2486 xs_html_add(form,
2487 html_button("unmute", L("Unmute"),
2488 L("Stop blocking activities from this user")));
2489 else
2490 xs_html_add(form,
2491 html_button("mute", L("MUTE"),
2492 L("Block any activity from this user")));
2493
2494 /* the post textarea */
2495 xs *dm_div_id = xs_fmt("%s_%s_dm", md5, t);
2496 xs *dm_form_id = xs_fmt("%s_reply_form", md5);
2497
2498 xs_html_add(snac_controls,
2499 xs_html_tag("p", NULL),
2500 html_note(snac, L("Direct Message..."),
2501 dm_div_id, dm_form_id,
2502 "", "",
2503 NULL, actor_id,
2504 xs_stock(XSTYPE_FALSE), "",
2505 xs_stock(XSTYPE_FALSE), NULL,
2506 NULL, 0, "", "", 0),
2507 xs_html_tag("p", NULL));
2508
2509 xs_html_add(snac_post, snac_controls);
2510
2511 xs_html_add(snac_posts, snac_post);
2512 }
2513 }
2514
2515 return people;
2516}
2517
2518
2519xs_str *html_people(snac *user)
2520{
2521 xs *wing = following_list(user);
2522 xs *wers = follower_list(user);
2523
2524 xs_html *html = xs_html_tag("html",
2525 html_user_head(user, NULL, NULL),
2526 xs_html_add(html_user_body(user, 0),
2527 xs_html_tag("div",
2528 xs_html_attr("class", "snac-posts"),
2529 html_people_list(user, wing, L("People you follow"), "i"),
2530 html_people_list(user, wers, L("People that follow you"), "e")),
2531 html_footer()));
2532
2533 return xs_html_render_s(html, "<!DOCTYPE html>\n");
2534}
2535
2536
2537xs_str *html_notifications(snac *user, int skip, int show)
2538{
2539 xs *n_list = notify_list(user, skip, show);
2540 xs *n_time = notify_check_time(user, 0);
2541
2542 xs_html *body = html_user_body(user, 0);
2543
2544 xs_html *html = xs_html_tag("html",
2545 html_user_head(user, NULL, NULL),
2546 body);
2547
2548 xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
2549
2550 xs_html_add(body,
2551 xs_html_tag("form",
2552 xs_html_attr("autocomplete", "off"),
2553 xs_html_attr("method", "post"),
2554 xs_html_attr("action", clear_all_action),
2555 xs_html_attr("id", "clear"),
2556 xs_html_sctag("input",
2557 xs_html_attr("type", "submit"),
2558 xs_html_attr("class", "snac-btn-like"),
2559 xs_html_attr("value", L("Clear all")))));
2560
2561 xs_html *noti_new = NULL;
2562 xs_html *noti_seen = NULL;
2563
2564 xs_list *p = n_list;
2565 const xs_str *v;
2566 while (xs_list_iter(&p, &v)) {
2567 xs *noti = notify_get(user, v);
2568
2569 if (noti == NULL)
2570 continue;
2571
2572 xs *obj = NULL;
2573 const char *type = xs_dict_get(noti, "type");
2574 const char *utype = xs_dict_get(noti, "utype");
2575 const char *id = xs_dict_get(noti, "objid");
2576 const char *date = xs_dict_get(noti, "date");
2577
2578 if (xs_is_null(id))
2579 continue;
2580
2581 if (is_hidden(user, id))
2582 continue;
2583
2584 object_get(id, &obj);
2585
2586 const char *actor_id = xs_dict_get(noti, "actor");
2587 xs *actor = NULL;
2588
2589 if (!valid_status(actor_get(actor_id, &actor)))
2590 continue;
2591
2592 xs *a_name = actor_name(actor);
2593 const char *label = type;
2594
2595 if (strcmp(type, "Create") == 0)
2596 label = L("Mention");
2597 else
2598 if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
2599 label = L("Finished poll");
2600 else
2601 if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
2602 label = L("Unfollow");
2603
2604 xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
2605
2606 xs_html *entry = xs_html_tag("div",
2607 xs_html_attr("class", "snac-post-with-desc"),
2608 xs_html_tag("p",
2609 xs_html_tag("b",
2610 xs_html_text(label),
2611 xs_html_text(" by "),
2612 xs_html_tag("a",
2613 xs_html_attr("href", actor_id),
2614 xs_html_raw(a_name))), /* a_name is already sanitized */
2615 xs_html_text(" "),
2616 xs_html_tag("time",
2617 xs_html_attr("class", "dt-published snac-pubdate"),
2618 xs_html_attr("title", date),
2619 xs_html_text(s_date))));
2620
2621 if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0 || strcmp(type, "Block") == 0) {
2622 xs_html_add(entry,
2623 xs_html_tag("div",
2624 xs_html_attr("class", "snac-post"),
2625 html_actor_icon(user, actor, NULL, NULL, NULL, 0, 0)));
2626 }
2627 else
2628 if (strcmp(type, "Move") == 0) {
2629 const xs_dict *o_msg = xs_dict_get(noti, "msg");
2630 const char *target;
2631
2632 if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
2633 xs *old_actor = NULL;
2634
2635 if (valid_status(actor_get(target, &old_actor))) {
2636 xs_html_add(entry,
2637 xs_html_tag("div",
2638 xs_html_attr("class", "snac-post"),
2639 html_actor_icon(user, old_actor, NULL, NULL, NULL, 0, 0)));
2640 }
2641 }
2642 }
2643 else
2644 if (obj != NULL) {
2645 xs *md5 = xs_md5_hex(id, strlen(id));
2646
2647 xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
2648
2649 if (h != NULL) {
2650 xs_html_add(entry,
2651 h);
2652 }
2653 }
2654
2655 if (strcmp(v, n_time) > 0) {
2656 /* unseen notification */
2657 if (noti_new == NULL) {
2658 noti_new = xs_html_tag("div",
2659 xs_html_tag("h2",
2660 xs_html_attr("class", "snac-header"),
2661 xs_html_text(L("New"))));
2662
2663 xs_html_add(body,
2664 noti_new);
2665 }
2666
2667 xs_html_add(noti_new,
2668 entry);
2669 }
2670 else {
2671 /* already seen notification */
2672 if (noti_seen == NULL) {
2673 noti_seen = xs_html_tag("div",
2674 xs_html_tag("h2",
2675 xs_html_attr("class", "snac-header"),
2676 xs_html_text(L("Already seen"))));
2677
2678 xs_html_add(body,
2679 noti_seen);
2680 }
2681
2682 xs_html_add(noti_seen,
2683 entry);
2684 }
2685 }
2686
2687 if (noti_new == NULL && noti_seen == NULL)
2688 xs_html_add(body,
2689 xs_html_tag("h2",
2690 xs_html_attr("class", "snac-header"),
2691 xs_html_text(L("None"))));
2692
2693 /* add the navigation footer */
2694 xs *next_p = notify_list(user, skip + show, 1);
2695 if (xs_list_len(next_p)) {
2696 xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
2697 user->actor, skip + show, show);
2698
2699 xs_html_add(body,
2700 xs_html_tag("p",
2701 xs_html_tag("a",
2702 xs_html_attr("href", url),
2703 xs_html_text(L("More...")))));
2704 }
2705
2706 xs_html_add(body,
2707 html_footer());
2708
2709 /* set the check time to now */
2710 xs *dummy = notify_check_time(user, 1);
2711 dummy = xs_free(dummy);
2712
2713 timeline_touch(user);
2714
2715 return xs_html_render_s(html, "<!DOCTYPE html>\n");
2716}
2717
2718
2719int html_get_handler(const xs_dict *req, const char *q_path,
2720 char **body, int *b_size, char **ctype, xs_str **etag)
2721{
2722 const char *accept = xs_dict_get(req, "accept");
2723 int status = HTTP_STATUS_NOT_FOUND;
2724 snac snac;
2725 xs *uid = NULL;
2726 const char *p_path;
2727 int cache = 1;
2728 int save = 1;
2729 const char *v;
2730
2731 xs *l = xs_split_n(q_path, "/", 2);
2732 v = xs_list_get(l, 1);
2733
2734 if (xs_is_null(v)) {
2735 srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
2736 return HTTP_STATUS_NOT_FOUND;
2737 }
2738
2739 uid = xs_dup(v);
2740
2741 /* rss extension? */
2742 if (xs_endswith(uid, ".rss")) {
2743 uid = xs_crop_i(uid, 0, -4);
2744 p_path = ".rss";
2745 }
2746 else
2747 p_path = xs_list_get(l, 2);
2748
2749 if (!uid || !user_open(&snac, uid)) {
2750 /* invalid user */
2751 srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
2752 return HTTP_STATUS_NOT_FOUND;
2753 }
2754
2755 /* return the RSS if requested by Accept header */
2756 if (accept != NULL) {
2757 if (xs_str_in(accept, "text/xml") != -1 ||
2758 xs_str_in(accept, "application/rss+xml") != -1)
2759 p_path = ".rss";
2760 }
2761
2762 /* check if server config variable 'disable_cache' is set */
2763 if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
2764 cache = 0;
2765
2766 int skip = 0;
2767 int def_show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
2768 int show = def_show;
2769
2770 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
2771 if ((v = xs_dict_get(q_vars, "skip")) != NULL)
2772 skip = atoi(v), cache = 0, save = 0;
2773 if ((v = xs_dict_get(q_vars, "show")) != NULL)
2774 show = atoi(v), cache = 0, save = 0;
2775 if ((v = xs_dict_get(q_vars, "da")) != NULL) {
2776 /* user dismissed an announcement */
2777 if (login(&snac, req)) {
2778 double ts = atof(v);
2779 xs *timestamp = xs_number_new(ts);
2780 srv_log(xs_fmt("user dismissed announcements until %d", ts));
2781 snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
2782 user_persist(&snac, 0);
2783 }
2784 }
2785
2786 /* get a possible error message */
2787 const char *error = xs_dict_get(q_vars, "error");
2788 if (error != NULL)
2789 cache = 0;
2790
2791 /* a show of 0 has no sense */
2792 if (show == 0)
2793 show = def_show;
2794
2795 if (p_path == NULL) { /** public timeline **/
2796 xs *h = xs_str_localtime(0, "%Y-%m.html");
2797
2798 if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
2799 /** empty public timeline for private users **/
2800 *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error);
2801 *b_size = strlen(*body);
2802 status = HTTP_STATUS_OK;
2803 }
2804 else
2805 if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
2806 snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
2807
2808 status = history_get(&snac, h, body, b_size,
2809 xs_dict_get(req, "if-none-match"), etag);
2810 }
2811 else {
2812 xs *list = timeline_list(&snac, "public", skip, show);
2813 xs *next = timeline_list(&snac, "public", skip + show, 1);
2814
2815 xs *pins = pinned_list(&snac);
2816 pins = xs_list_cat(pins, list);
2817
2818 *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL, "", 1, error);
2819
2820 *b_size = strlen(*body);
2821 status = HTTP_STATUS_OK;
2822
2823 if (save)
2824 history_add(&snac, h, *body, *b_size, etag);
2825 }
2826 }
2827 else
2828 if (strcmp(p_path, "admin") == 0) { /** private timeline **/
2829 if (!login(&snac, req)) {
2830 *body = xs_dup(uid);
2831 status = HTTP_STATUS_UNAUTHORIZED;
2832 }
2833 else {
2834 const char *q = xs_dict_get(q_vars, "q");
2835
2836 if (q && *q) {
2837 if (*q == '#') {
2838 /** search by tag **/
2839 xs *tl = tag_search(q, skip, show + 1);
2840 int more = 0;
2841 if (xs_list_len(tl) >= show + 1) {
2842 /* drop the last one */
2843 tl = xs_list_del(tl, -1);
2844 more = 1;
2845 }
2846
2847 xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
2848 xs *title = xs_fmt(xs_list_len(tl) ?
2849 L("Search results for tag %s") : L("Nothing found for tag %s"), q);
2850
2851 *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error);
2852 *b_size = strlen(*body);
2853 status = HTTP_STATUS_OK;
2854 }
2855 else {
2856 /** search by content **/
2857 int to = 0;
2858 int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
2859 xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
2860 xs *title = NULL;
2861 xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
2862 int tl_len = xs_list_len(tl);
2863
2864 if (to)
2865 title = xs_fmt(L("Search results for '%s' (may be more)"), q);
2866 else
2867 if (tl_len)
2868 title = xs_fmt(L("Search results for '%s'"), q);
2869 else
2870 if (skip)
2871 title = xs_fmt(L("No more matches for '%s'"), q);
2872 else
2873 title = xs_fmt(L("Nothing found for '%s'"), q);
2874
2875 *body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
2876 title, page, 0, error);
2877 *b_size = strlen(*body);
2878 status = HTTP_STATUS_OK;
2879 }
2880 }
2881 else {
2882 double t = history_mtime(&snac, "timeline.html_");
2883
2884 /* if enabled by admin, return a cached page if its timestamp is:
2885 a) newer than the timeline timestamp
2886 b) newer than the start time of the server
2887 */
2888 if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
2889 snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
2890
2891 status = history_get(&snac, "timeline.html_", body, b_size,
2892 xs_dict_get(req, "if-none-match"), etag);
2893 }
2894 else {
2895 snac_debug(&snac, 1, xs_fmt("building timeline"));
2896
2897 xs *list = timeline_list(&snac, "private", skip, show);
2898 xs *next = timeline_list(&snac, "private", skip + show, 1);
2899
2900 *body = html_timeline(&snac, list, 0, skip, show,
2901 xs_list_len(next), NULL, "/admin", 1, error);
2902
2903 *b_size = strlen(*body);
2904 status = HTTP_STATUS_OK;
2905
2906 if (save)
2907 history_add(&snac, "timeline.html_", *body, *b_size, etag);
2908 }
2909 }
2910 }
2911 }
2912 else
2913 if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
2914 if (!login(&snac, req)) {
2915 *body = xs_dup(uid);
2916 status = HTTP_STATUS_UNAUTHORIZED;
2917 }
2918 else {
2919 xs *l = xs_split(p_path, "/");
2920 const char *md5 = xs_list_get(l, -1);
2921
2922 if (md5 && *md5 && timeline_here(&snac, md5)) {
2923 xs *list = xs_list_append(xs_list_new(), md5);
2924
2925 *body = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error);
2926 *b_size = strlen(*body);
2927 status = HTTP_STATUS_OK;
2928 }
2929 }
2930 }
2931 else
2932 if (strcmp(p_path, "people") == 0) { /** the list of people **/
2933 if (!login(&snac, req)) {
2934 *body = xs_dup(uid);
2935 status = HTTP_STATUS_UNAUTHORIZED;
2936 }
2937 else {
2938 *body = html_people(&snac);
2939 *b_size = strlen(*body);
2940 status = HTTP_STATUS_OK;
2941 }
2942 }
2943 else
2944 if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
2945 if (!login(&snac, req)) {
2946 *body = xs_dup(uid);
2947 status = HTTP_STATUS_UNAUTHORIZED;
2948 }
2949 else {
2950 *body = html_notifications(&snac, skip, show);
2951 *b_size = strlen(*body);
2952 status = HTTP_STATUS_OK;
2953 }
2954 }
2955 else
2956 if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
2957 if (!login(&snac, req)) {
2958 *body = xs_dup(uid);
2959 status = HTTP_STATUS_UNAUTHORIZED;
2960 }
2961 else {
2962 xs *list = timeline_instance_list(skip, show);
2963 xs *next = timeline_instance_list(skip + show, 1);
2964
2965 *body = html_timeline(&snac, list, 0, skip, show,
2966 xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error);
2967 *b_size = strlen(*body);
2968 status = HTTP_STATUS_OK;
2969 }
2970 }
2971 else
2972 if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
2973 if (!login(&snac, req)) {
2974 *body = xs_dup(uid);
2975 status = HTTP_STATUS_UNAUTHORIZED;
2976 }
2977 else {
2978 xs *list = pinned_list(&snac);
2979
2980 *body = html_timeline(&snac, list, 0, skip, show,
2981 0, L("Pinned posts"), "", 0, error);
2982 *b_size = strlen(*body);
2983 status = HTTP_STATUS_OK;
2984 }
2985 }
2986 else
2987 if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
2988 if (!login(&snac, req)) {
2989 *body = xs_dup(uid);
2990 status = HTTP_STATUS_UNAUTHORIZED;
2991 }
2992 else {
2993 xs *list = bookmark_list(&snac);
2994
2995 *body = html_timeline(&snac, list, 0, skip, show,
2996 0, L("Bookmarked posts"), "", 0, error);
2997 *b_size = strlen(*body);
2998 status = HTTP_STATUS_OK;
2999 }
3000 }
3001 else
3002 if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
3003 if (!login(&snac, req)) {
3004 *body = xs_dup(uid);
3005 status = HTTP_STATUS_UNAUTHORIZED;
3006 }
3007 else {
3008 xs *list = draft_list(&snac);
3009
3010 *body = html_timeline(&snac, list, 0, skip, show,
3011 0, L("Post drafts"), "", 0, error);
3012 *b_size = strlen(*body);
3013 status = HTTP_STATUS_OK;
3014 }
3015 }
3016 else
3017 if (xs_startswith(p_path, "list/")) { /** list timelines **/
3018 if (!login(&snac, req)) {
3019 *body = xs_dup(uid);
3020 status = HTTP_STATUS_UNAUTHORIZED;
3021 }
3022 else {
3023 xs *l = xs_split(p_path, "/");
3024 const char *lid = xs_list_get(l, -1);
3025
3026 xs *list = list_timeline(&snac, lid, skip, show);
3027 xs *next = list_timeline(&snac, lid, skip + show, 1);
3028
3029 if (list != NULL) {
3030 xs *ttl = timeline_top_level(&snac, list);
3031
3032 xs *base = xs_fmt("/list/%s", lid);
3033 xs *name = list_maint(&snac, lid, 3);
3034 xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
3035
3036 *body = html_timeline(&snac, ttl, 0, skip, show,
3037 xs_list_len(next), title, base, 1, error);
3038 *b_size = strlen(*body);
3039 status = HTTP_STATUS_OK;
3040 }
3041 }
3042 }
3043 else
3044 if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
3045 if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3046 return HTTP_STATUS_FORBIDDEN;
3047
3048 xs *id = xs_fmt("%s/%s", snac.actor, p_path);
3049 xs *msg = NULL;
3050
3051 if (valid_status(object_get(id, &msg))) {
3052 xs *md5 = xs_md5_hex(id, strlen(id));
3053 xs *list = xs_list_new();
3054
3055 list = xs_list_append(list, md5);
3056
3057 *body = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error);
3058 *b_size = strlen(*body);
3059 status = HTTP_STATUS_OK;
3060 }
3061 }
3062 else
3063 if (xs_startswith(p_path, "s/")) { /** a static file **/
3064 xs *l = xs_split(p_path, "/");
3065 const char *id = xs_list_get(l, 1);
3066 int sz;
3067
3068 if (id && *id) {
3069 status = static_get(&snac, id, body, &sz,
3070 xs_dict_get(req, "if-none-match"), etag);
3071
3072 if (valid_status(status)) {
3073 *b_size = sz;
3074 *ctype = (char *)xs_mime_by_ext(id);
3075 }
3076 }
3077 }
3078 else
3079 if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
3080 if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3081 return HTTP_STATUS_FORBIDDEN;
3082
3083 if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
3084 return HTTP_STATUS_FORBIDDEN;
3085
3086 xs *l = xs_split(p_path, "/");
3087 const char *id = xs_list_get(l, 1);
3088
3089 if (id && *id) {
3090 if (xs_endswith(id, "timeline.html_")) {
3091 /* Don't let them in */
3092 *b_size = 0;
3093 status = HTTP_STATUS_NOT_FOUND;
3094 }
3095 else
3096 status = history_get(&snac, id, body, b_size,
3097 xs_dict_get(req, "if-none-match"), etag);
3098 }
3099 }
3100 else
3101 if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
3102 if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3103 return HTTP_STATUS_FORBIDDEN;
3104
3105 xs *elems = timeline_simple_list(&snac, "public", 0, 20);
3106 xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL, NULL);
3107
3108 xs *rss_title = xs_fmt("%s (@%s@%s)",
3109 xs_dict_get(snac.config, "name"),
3110 snac.uid,
3111 xs_dict_get(srv_config, "host"));
3112 xs *rss_link = xs_fmt("%s.rss", snac.actor);
3113
3114 *body = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
3115 *b_size = strlen(*body);
3116 *ctype = "application/rss+xml; charset=utf-8";
3117 status = HTTP_STATUS_OK;
3118
3119 snac_debug(&snac, 1, xs_fmt("serving RSS"));
3120 }
3121 else
3122 status = HTTP_STATUS_NOT_FOUND;
3123
3124 user_free(&snac);
3125
3126 if (valid_status(status) && *ctype == NULL) {
3127 *ctype = "text/html; charset=utf-8";
3128 }
3129
3130 return status;
3131}
3132
3133
3134int html_post_handler(const xs_dict *req, const char *q_path,
3135 char *payload, int p_size,
3136 char **body, int *b_size, char **ctype)
3137{
3138 (void)p_size;
3139 (void)ctype;
3140
3141 int status = 0;
3142 snac snac;
3143 const char *uid;
3144 const char *p_path;
3145 const xs_dict *p_vars;
3146
3147 xs *l = xs_split_n(q_path, "/", 2);
3148
3149 uid = xs_list_get(l, 1);
3150 if (!uid || !user_open(&snac, uid)) {
3151 /* invalid user */
3152 srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
3153 return HTTP_STATUS_NOT_FOUND;
3154 }
3155
3156 p_path = xs_list_get(l, 2);
3157
3158 /* all posts must be authenticated */
3159 if (!login(&snac, req)) {
3160 user_free(&snac);
3161 *body = xs_dup(uid);
3162 return HTTP_STATUS_UNAUTHORIZED;
3163 }
3164
3165 p_vars = xs_dict_get(req, "p_vars");
3166
3167 if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
3168 snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
3169
3170 /* post note */
3171 const xs_str *content = xs_dict_get(p_vars, "content");
3172 const xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
3173 const xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
3174 const xs_list *attach_file = xs_dict_get(p_vars, "attach");
3175 const xs_str *to = xs_dict_get(p_vars, "to");
3176 const xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
3177 const xs_str *summary = xs_dict_get(p_vars, "summary");
3178 const xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
3179 const xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
3180 int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
3181 int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
3182 xs *attach_list = xs_list_new();
3183
3184 /* default alt text */
3185 if (xs_is_null(alt_text))
3186 alt_text = "";
3187
3188 /* is attach_url set? */
3189 if (!xs_is_null(attach_url) && *attach_url != '\0') {
3190 xs *l = xs_list_new();
3191
3192 l = xs_list_append(l, attach_url);
3193 l = xs_list_append(l, alt_text);
3194
3195 attach_list = xs_list_append(attach_list, l);
3196 }
3197
3198 /* is attach_file set? */
3199 if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
3200 const char *fn = xs_list_get(attach_file, 0);
3201
3202 if (*fn != '\0') {
3203 char *ext = strrchr(fn, '.');
3204 xs *hash = xs_md5_hex(fn, strlen(fn));
3205 xs *id = xs_fmt("%s%s", hash, ext);
3206 xs *url = xs_fmt("%s/s/%s", snac.actor, id);
3207 int fo = xs_number_get(xs_list_get(attach_file, 1));
3208 int fs = xs_number_get(xs_list_get(attach_file, 2));
3209
3210 /* store */
3211 static_put(&snac, id, payload + fo, fs);
3212
3213 xs *l = xs_list_new();
3214
3215 l = xs_list_append(l, url);
3216 l = xs_list_append(l, alt_text);
3217
3218 attach_list = xs_list_append(attach_list, l);
3219 }
3220 }
3221
3222 if (content != NULL) {
3223 xs *msg = NULL;
3224 xs *c_msg = NULL;
3225 xs *content_2 = xs_replace(content, "\r", "");
3226 xs *poll_opts = NULL;
3227
3228 /* is there a valid set of poll options? */
3229 const char *v = xs_dict_get(p_vars, "poll_options");
3230 if (!xs_is_null(v) && *v) {
3231 xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
3232
3233 poll_opts = xs_split(v2, "\n");
3234 }
3235
3236 if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
3237 /* get the rest of poll configuration */
3238 const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
3239 const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
3240 int multiple = 0;
3241
3242 int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
3243
3244 if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
3245 multiple = 1;
3246
3247 msg = msg_question(&snac, content_2, attach_list,
3248 poll_opts, multiple, end_secs);
3249
3250 enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
3251 }
3252 else
3253 msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
3254
3255 if (sensitive != NULL) {
3256 msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
3257 msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
3258 }
3259
3260 if (xs_is_null(edit_id)) {
3261 /* new message */
3262 const char *id = xs_dict_get(msg, "id");
3263
3264 if (store_as_draft) {
3265 draft_add(&snac, id, msg);
3266 }
3267 else {
3268 c_msg = msg_create(&snac, msg);
3269 timeline_add(&snac, id, msg);
3270 }
3271 }
3272 else {
3273 /* an edition of a previous message */
3274 xs *p_msg = NULL;
3275
3276 if (valid_status(object_get(edit_id, &p_msg))) {
3277 /* copy relevant fields from previous version */
3278 char *fields[] = { "id", "context", "url", "published",
3279 "to", "inReplyTo", NULL };
3280 int n;
3281
3282 for (n = 0; fields[n]; n++) {
3283 const char *v = xs_dict_get(p_msg, fields[n]);
3284 msg = xs_dict_set(msg, fields[n], v);
3285 }
3286
3287 if (store_as_draft) {
3288 draft_add(&snac, edit_id, msg);
3289 }
3290 else
3291 if (is_draft(&snac, edit_id)) {
3292 /* message was previously a draft; it's a create activity */
3293
3294 /* set the published field to now */
3295 xs *published = xs_str_utctime(0, ISO_DATE_SPEC);
3296 msg = xs_dict_set(msg, "published", published);
3297
3298 /* overwrite object */
3299 object_add_ow(edit_id, msg);
3300
3301 c_msg = msg_create(&snac, msg);
3302 timeline_add(&snac, edit_id, msg);
3303 draft_del(&snac, edit_id);
3304 }
3305 else {
3306 /* set the updated field */
3307 xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
3308 msg = xs_dict_set(msg, "updated", updated);
3309
3310 /* overwrite object, not updating the indexes */
3311 object_add_ow(edit_id, msg);
3312
3313 /* update message */
3314 c_msg = msg_update(&snac, msg);
3315 }
3316 }
3317 else
3318 snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
3319 }
3320
3321 if (c_msg != NULL)
3322 enqueue_message(&snac, c_msg);
3323
3324 history_del(&snac, "timeline.html_");
3325 }
3326
3327 status = HTTP_STATUS_SEE_OTHER;
3328 }
3329 else
3330 if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
3331 /* action on an entry */
3332 const char *id = xs_dict_get(p_vars, "id");
3333 const char *actor = xs_dict_get(p_vars, "actor");
3334 const char *action = xs_dict_get(p_vars, "action");
3335 const char *group = xs_dict_get(p_vars, "group");
3336
3337 if (action == NULL)
3338 return HTTP_STATUS_NOT_FOUND;
3339
3340 snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
3341
3342 status = HTTP_STATUS_SEE_OTHER;
3343
3344 if (strcmp(action, L("Like")) == 0) { /** **/
3345 xs *msg = msg_admiration(&snac, id, "Like");
3346
3347 if (msg != NULL) {
3348 enqueue_message(&snac, msg);
3349 timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
3350 }
3351 }
3352 else
3353 if (strcmp(action, L("Boost")) == 0) { /** **/
3354 xs *msg = msg_admiration(&snac, id, "Announce");
3355
3356 if (msg != NULL) {
3357 enqueue_message(&snac, msg);
3358 timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
3359 }
3360 }
3361 else
3362 if (strcmp(action, L("Unlike")) == 0) { /** **/
3363 xs *msg = msg_repulsion(&snac, id, "Like");
3364
3365 if (msg != NULL) {
3366 enqueue_message(&snac, msg);
3367 }
3368 }
3369 else
3370 if (strcmp(action, L("Unboost")) == 0) { /** **/
3371 xs *msg = msg_repulsion(&snac, id, "Announce");
3372
3373 if (msg != NULL) {
3374 enqueue_message(&snac, msg);
3375 }
3376 }
3377 else
3378 if (strcmp(action, L("MUTE")) == 0) { /** **/
3379 mute(&snac, actor);
3380 }
3381 else
3382 if (strcmp(action, L("Unmute")) == 0) { /** **/
3383 unmute(&snac, actor);
3384 }
3385 else
3386 if (strcmp(action, L("Hide")) == 0) { /** **/
3387 if (is_draft(&snac, id))
3388 draft_del(&snac, id);
3389 else
3390 hide(&snac, id);
3391 }
3392 else
3393 if (strcmp(action, L("Limit")) == 0) { /** **/
3394 limit(&snac, actor);
3395 }
3396 else
3397 if (strcmp(action, L("Unlimit")) == 0) { /** **/
3398 unlimit(&snac, actor);
3399 }
3400 else
3401 if (strcmp(action, L("Follow")) == 0) { /** **/
3402 xs *msg = msg_follow(&snac, actor);
3403
3404 if (msg != NULL) {
3405 /* reload the actor from the message, in may be different */
3406 actor = xs_dict_get(msg, "object");
3407
3408 following_add(&snac, actor, msg);
3409
3410 enqueue_output_by_actor(&snac, msg, actor, 0);
3411 }
3412 }
3413 else
3414 if (strcmp(action, L("Unfollow")) == 0) { /** **/
3415 /* get the following object */
3416 xs *object = NULL;
3417
3418 if (valid_status(following_get(&snac, actor, &object))) {
3419 xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3420
3421 following_del(&snac, actor);
3422
3423 enqueue_output_by_actor(&snac, msg, actor, 0);
3424
3425 snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
3426 }
3427 else
3428 snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3429 }
3430 else
3431 if (strcmp(action, L("Follow Group")) == 0) { /** **/
3432 xs *msg = msg_follow(&snac, group);
3433
3434 if (msg != NULL) {
3435 /* reload the group from the message, in may be different */
3436 group = xs_dict_get(msg, "object");
3437
3438 following_add(&snac, group, msg);
3439
3440 enqueue_output_by_actor(&snac, msg, group, 0);
3441 }
3442 }
3443 else
3444 if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
3445 /* get the following object */
3446 xs *object = NULL;
3447
3448 if (valid_status(following_get(&snac, group, &object))) {
3449 xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3450
3451 following_del(&snac, group);
3452
3453 enqueue_output_by_actor(&snac, msg, group, 0);
3454
3455 snac_log(&snac, xs_fmt("unfollowed group %s", group));
3456 }
3457 else
3458 snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3459 }
3460 else
3461 if (strcmp(action, L("Delete")) == 0) { /** **/
3462 const char *actor_form = xs_dict_get(p_vars, "actor-form");
3463 if (actor_form != NULL) {
3464 /* delete follower */
3465 if (valid_status(follower_del(&snac, actor)))
3466 snac_log(&snac, xs_fmt("deleted follower %s", actor));
3467 else
3468 snac_log(&snac, xs_fmt("error deleting follower %s", actor));
3469 }
3470 else {
3471 /* delete an entry */
3472 if (xs_startswith(id, snac.actor) && !is_draft(&snac, id)) {
3473 /* it's a post by us: generate a delete */
3474 xs *msg = msg_delete(&snac, id);
3475
3476 enqueue_message(&snac, msg);
3477
3478 snac_log(&snac, xs_fmt("posted tombstone for %s", id));
3479 }
3480
3481 timeline_del(&snac, id);
3482
3483 draft_del(&snac, id);
3484
3485 snac_log(&snac, xs_fmt("deleted entry %s", id));
3486 }
3487 }
3488 else
3489 if (strcmp(action, L("Pin")) == 0) { /** **/
3490 pin(&snac, id);
3491 timeline_touch(&snac);
3492 }
3493 else
3494 if (strcmp(action, L("Unpin")) == 0) { /** **/
3495 unpin(&snac, id);
3496 timeline_touch(&snac);
3497 }
3498 else
3499 if (strcmp(action, L("Bookmark")) == 0) { /** **/
3500 bookmark(&snac, id);
3501 timeline_touch(&snac);
3502 }
3503 else
3504 if (strcmp(action, L("Unbookmark")) == 0) { /** **/
3505 unbookmark(&snac, id);
3506 timeline_touch(&snac);
3507 }
3508 else
3509 status = HTTP_STATUS_NOT_FOUND;
3510
3511 /* delete the cached timeline */
3512 if (status == HTTP_STATUS_SEE_OTHER)
3513 history_del(&snac, "timeline.html_");
3514 }
3515 else
3516 if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
3517 /* change of user data */
3518 const char *v;
3519 const char *p1, *p2;
3520
3521 if ((v = xs_dict_get(p_vars, "name")) != NULL)
3522 snac.config = xs_dict_set(snac.config, "name", v);
3523 if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
3524 snac.config = xs_dict_set(snac.config, "avatar", v);
3525 if ((v = xs_dict_get(p_vars, "bio")) != NULL)
3526 snac.config = xs_dict_set(snac.config, "bio", v);
3527 if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
3528 strcmp(v, "on") == 0) {
3529 snac.config = xs_dict_set(snac.config, "cw", "open");
3530 } else { /* if the checkbox is not set, the parameter is missing */
3531 snac.config = xs_dict_set(snac.config, "cw", "");
3532 }
3533 if ((v = xs_dict_get(p_vars, "email")) != NULL)
3534 snac.config = xs_dict_set(snac.config, "email", v);
3535 if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
3536 snac.config = xs_dict_set(snac.config, "telegram_bot", v);
3537 if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
3538 snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
3539 if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
3540 snac.config = xs_dict_set(snac.config, "ntfy_server", v);
3541 if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
3542 snac.config = xs_dict_set(snac.config, "ntfy_token", v);
3543 if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
3544 xs *days = xs_number_new(atof(v));
3545 snac.config = xs_dict_set(snac.config, "purge_days", days);
3546 }
3547 if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
3548 snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
3549 else
3550 snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
3551 if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
3552 snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
3553 else
3554 snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
3555 if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
3556 snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
3557 else
3558 snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
3559 if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
3560 snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
3561 else
3562 snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
3563
3564 if ((v = xs_dict_get(p_vars, "metadata")) != NULL) {
3565 /* split the metadata and store it as a dict */
3566 xs_dict *md = xs_dict_new();
3567 xs *l = xs_split(v, "\n");
3568 xs_list *p = l;
3569 const xs_str *kp;
3570
3571 while (xs_list_iter(&p, &kp)) {
3572 xs *kpl = xs_split_n(kp, "=", 1);
3573 if (xs_list_len(kpl) == 2) {
3574 xs *k2 = xs_strip_i(xs_dup(xs_list_get(kpl, 0)));
3575 xs *v2 = xs_strip_i(xs_dup(xs_list_get(kpl, 1)));
3576
3577 md = xs_dict_set(md, k2, v2);
3578 }
3579 }
3580
3581 snac.config = xs_dict_set(snac.config, "metadata", md);
3582 }
3583
3584 /* uploads */
3585 const char *uploads[] = { "avatar", "header", NULL };
3586 int n;
3587 for (n = 0; uploads[n]; n++) {
3588 xs *var_name = xs_fmt("%s_file", uploads[n]);
3589
3590 const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
3591 if (xs_type(uploaded_file) == XSTYPE_LIST) {
3592 const char *fn = xs_list_get(uploaded_file, 0);
3593
3594 if (fn && *fn) {
3595 const char *mimetype = xs_mime_by_ext(fn);
3596
3597 if (xs_startswith(mimetype, "image/")) {
3598 const char *ext = strrchr(fn, '.');
3599 xs *hash = xs_md5_hex(fn, strlen(fn));
3600 xs *id = xs_fmt("%s%s", hash, ext);
3601 xs *url = xs_fmt("%s/s/%s", snac.actor, id);
3602 int fo = xs_number_get(xs_list_get(uploaded_file, 1));
3603 int fs = xs_number_get(xs_list_get(uploaded_file, 2));
3604
3605 /* store */
3606 static_put(&snac, id, payload + fo, fs);
3607
3608 snac.config = xs_dict_set(snac.config, uploads[n], url);
3609 }
3610 }
3611 }
3612 }
3613
3614 /* delete images by removing url from user.json */
3615 for (n = 0; uploads[n]; n++) {
3616 xs *var_name = xs_fmt("%s_delete", uploads[n]);
3617 const char *delete_var = xs_dict_get(p_vars, var_name);
3618
3619 if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
3620 snac.config = xs_dict_set(snac.config, uploads[n], "");
3621 }
3622 }
3623
3624 /* password change? */
3625 if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
3626 (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
3627 *p1 && strcmp(p1, p2) == 0) {
3628 xs *pw = hash_password(snac.uid, p1, NULL);
3629 snac.config = xs_dict_set(snac.config, "passwd", pw);
3630 }
3631
3632 user_persist(&snac, 1);
3633
3634 status = HTTP_STATUS_SEE_OTHER;
3635 }
3636 else
3637 if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
3638 notify_clear(&snac);
3639 timeline_touch(&snac);
3640
3641 status = HTTP_STATUS_SEE_OTHER;
3642 }
3643 else
3644 if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
3645 const char *irt = xs_dict_get(p_vars, "irt");
3646 const char *opt = xs_dict_get(p_vars, "question");
3647 const char *actor = xs_dict_get(p_vars, "actor");
3648
3649 xs *ls = NULL;
3650
3651 /* multiple choices? */
3652 if (xs_type(opt) == XSTYPE_LIST)
3653 ls = xs_dup(opt);
3654 else
3655 if (xs_type(opt) == XSTYPE_STRING) {
3656 ls = xs_list_new();
3657 ls = xs_list_append(ls, opt);
3658 }
3659
3660 const xs_str *v;
3661 int c = 0;
3662
3663 while (xs_list_next(ls, &v, &c)) {
3664 xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
3665
3666 /* set the option */
3667 msg = xs_dict_append(msg, "name", v);
3668
3669 xs *c_msg = msg_create(&snac, msg);
3670
3671 enqueue_message(&snac, c_msg);
3672
3673 timeline_add(&snac, xs_dict_get(msg, "id"), msg);
3674 }
3675
3676 if (ls != NULL) {
3677 /* get the poll object */
3678 xs *poll = NULL;
3679
3680 if (valid_status(object_get(irt, &poll))) {
3681 const char *date = xs_dict_get(poll, "endTime");
3682 if (xs_is_null(date))
3683 date = xs_dict_get(poll, "closed");
3684
3685 if (!xs_is_null(date)) {
3686 time_t t = xs_parse_iso_date(date, 0) - time(NULL);
3687
3688 /* request the poll when it's closed;
3689 Pleroma does not send and update when the poll closes */
3690 enqueue_object_request(&snac, irt, t + 2);
3691 }
3692 }
3693 }
3694
3695 status = HTTP_STATUS_SEE_OTHER;
3696 }
3697
3698 if (status == HTTP_STATUS_SEE_OTHER) {
3699 const char *redir = xs_dict_get(p_vars, "redir");
3700
3701 if (xs_is_null(redir))
3702 redir = "top";
3703
3704 *body = xs_fmt("%s/admin#%s", snac.actor, redir);
3705 *b_size = strlen(*body);
3706 }
3707
3708 user_free(&snac);
3709
3710 return status;
3711}
3712
3713
3714xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *link, char *desc)
3715/* converts a timeline to rss */
3716{
3717 xs_html *rss = xs_html_tag("rss",
3718 xs_html_attr("version", "0.91"));
3719
3720 xs_html *channel = xs_html_tag("channel",
3721 xs_html_tag("title",
3722 xs_html_text(title)),
3723 xs_html_tag("language",
3724 xs_html_text("en")),
3725 xs_html_tag("link",
3726 xs_html_text(link)),
3727 xs_html_tag("description",
3728 xs_html_text(desc)));
3729
3730 xs_html_add(rss, channel);
3731
3732 int c = 0;
3733 const char *v;
3734
3735 while (xs_list_next(timeline, &v, &c)) {
3736 xs *msg = NULL;
3737
3738 if (user) {
3739 if (!valid_status(timeline_get_by_md5(user, v, &msg)))
3740 continue;
3741 }
3742 else {
3743 if (!valid_status(object_get_by_md5(v, &msg)))
3744 continue;
3745 }
3746
3747 const char *id = xs_dict_get(msg, "id");
3748 const char *content = xs_dict_get(msg, "content");
3749
3750 if (user && !xs_startswith(id, user->actor))
3751 continue;
3752
3753 /* create a title with the first line of the content */
3754 xs *title = xs_replace(content, "<br>", "\n");
3755 title = xs_regex_replace_i(title, "<[^>]+>", " ");
3756 title = xs_regex_replace_i(title, "&[^;]+;", " ");
3757 int i;
3758
3759 for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
3760
3761 if (title[i] != '\0') {
3762 title[i] = '\0';
3763 title = xs_str_cat(title, "...");
3764 }
3765
3766 title = xs_strip_i(title);
3767
3768 xs_html_add(channel,
3769 xs_html_tag("item",
3770 xs_html_tag("title",
3771 xs_html_text(title)),
3772 xs_html_tag("link",
3773 xs_html_text(id)),
3774 xs_html_tag("description",
3775 xs_html_text(content))));
3776 }
3777
3778 return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
3779}