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