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