diff options
| author | 2025-02-07 09:26:54 +0100 | |
|---|---|---|
| committer | 2025-02-07 09:26:54 +0100 | |
| commit | 5de52cb753abaab03e9405441542e73803c3fe08 (patch) | |
| tree | 185a4db949434bb100bcb0ab7cea2d938b5f867f /html.c | |
| parent | Fixed a crash in xs_multipart_form_data(). (diff) | |
| download | penes-snac2-5de52cb753abaab03e9405441542e73803c3fe08.tar.gz penes-snac2-5de52cb753abaab03e9405441542e73803c3fe08.tar.xz penes-snac2-5de52cb753abaab03e9405441542e73803c3fe08.zip | |
Added support for multiple attachments in the same post.
Diffstat (limited to 'html.c')
| -rw-r--r-- | html.c | 168 |
1 files changed, 109 insertions, 59 deletions
| @@ -327,7 +327,7 @@ xs_html *html_note(snac *user, const char *summary, | |||
| 327 | const xs_val *cw_yn, const char *cw_text, | 327 | const xs_val *cw_yn, const char *cw_text, |
| 328 | const xs_val *mnt_only, const char *redir, | 328 | const xs_val *mnt_only, const char *redir, |
| 329 | const char *in_reply_to, int poll, | 329 | const char *in_reply_to, int poll, |
| 330 | const char *att_file, const char *att_alt_text, | 330 | const xs_list *att_files, const xs_list *att_alt_texts, |
| 331 | int is_draft) | 331 | int is_draft) |
| 332 | /* Yes, this is a FUCKTON of arguments and I'm a bit embarrased */ | 332 | /* Yes, this is a FUCKTON of arguments and I'm a bit embarrased */ |
| 333 | { | 333 | { |
| @@ -432,30 +432,71 @@ xs_html *html_note(snac *user, const char *summary, | |||
| 432 | xs_html_tag("p", NULL), | 432 | xs_html_tag("p", NULL), |
| 433 | att = xs_html_tag("details", | 433 | att = xs_html_tag("details", |
| 434 | xs_html_tag("summary", | 434 | xs_html_tag("summary", |
| 435 | xs_html_text(L("Attachment..."))), | 435 | xs_html_text(L("Attachments..."))), |
| 436 | xs_html_tag("p", NULL))); | 436 | xs_html_tag("p", NULL))); |
| 437 | 437 | ||
| 438 | if (att_file && *att_file) | 438 | int max_attachments = xs_number_get(xs_dict_get_def(srv_config, "max_attachments", "4")); |
| 439 | int att_n = 0; | ||
| 440 | |||
| 441 | /* fields for the currently existing attachments */ | ||
| 442 | if (xs_is_list(att_files) && xs_is_list(att_alt_texts)) { | ||
| 443 | while (att_n < max_attachments) { | ||
| 444 | const char *att_file = xs_list_get(att_files, att_n); | ||
| 445 | const char *att_alt_text = xs_list_get(att_alt_texts, att_n); | ||
| 446 | |||
| 447 | if (!xs_is_string(att_file) || !xs_is_string(att_alt_text)) | ||
| 448 | break; | ||
| 449 | |||
| 450 | xs *att_lbl = xs_fmt("attach_url_%d", att_n); | ||
| 451 | xs *alt_lbl = xs_fmt("alt_text_%d", att_n); | ||
| 452 | |||
| 453 | if (att_n) | ||
| 454 | xs_html_add(att, | ||
| 455 | xs_html_sctag("br", NULL)); | ||
| 456 | |||
| 457 | xs_html_add(att, | ||
| 458 | xs_html_text(L("File:")), | ||
| 459 | xs_html_sctag("input", | ||
| 460 | xs_html_attr("type", "text"), | ||
| 461 | xs_html_attr("name", att_lbl), | ||
| 462 | xs_html_attr("title", L("Clear this field to delete the attachment")), | ||
| 463 | xs_html_attr("value", att_file))); | ||
| 464 | |||
| 465 | xs_html_add(att, | ||
| 466 | xs_html_text(" "), | ||
| 467 | xs_html_sctag("input", | ||
| 468 | xs_html_attr("type", "text"), | ||
| 469 | xs_html_attr("name", alt_lbl), | ||
| 470 | xs_html_attr("value", att_alt_text), | ||
| 471 | xs_html_attr("placeholder", L("Attachment description")))); | ||
| 472 | |||
| 473 | att_n++; | ||
| 474 | } | ||
| 475 | } | ||
| 476 | |||
| 477 | /* the rest of possible attachments */ | ||
| 478 | while (att_n < max_attachments) { | ||
| 479 | xs *att_lbl = xs_fmt("attach_%d", att_n); | ||
| 480 | xs *alt_lbl = xs_fmt("alt_text_%d", att_n); | ||
| 481 | |||
| 482 | if (att_n) | ||
| 483 | xs_html_add(att, | ||
| 484 | xs_html_sctag("br", NULL)); | ||
| 485 | |||
| 439 | xs_html_add(att, | 486 | xs_html_add(att, |
| 440 | xs_html_text(L("File:")), | ||
| 441 | xs_html_sctag("input", | 487 | xs_html_sctag("input", |
| 442 | xs_html_attr("type", "text"), | 488 | xs_html_attr("type", "file"), |
| 443 | xs_html_attr("name", "attach_url"), | 489 | xs_html_attr("name", att_lbl))); |
| 444 | xs_html_attr("title", L("Clear this field to delete the attachment")), | 490 | |
| 445 | xs_html_attr("value", att_file))); | ||
| 446 | else | ||
| 447 | xs_html_add(att, | 491 | xs_html_add(att, |
| 492 | xs_html_text(" "), | ||
| 448 | xs_html_sctag("input", | 493 | xs_html_sctag("input", |
| 449 | xs_html_attr("type", "file"), | 494 | xs_html_attr("type", "text"), |
| 450 | xs_html_attr("name", "attach"))); | 495 | xs_html_attr("name", alt_lbl), |
| 496 | xs_html_attr("placeholder", L("Attachment description")))); | ||
| 451 | 497 | ||
| 452 | xs_html_add(att, | 498 | att_n++; |
| 453 | xs_html_text(" "), | 499 | } |
| 454 | xs_html_sctag("input", | ||
| 455 | xs_html_attr("type", "text"), | ||
| 456 | xs_html_attr("name", "alt_text"), | ||
| 457 | xs_html_attr("value", att_alt_text), | ||
| 458 | xs_html_attr("placeholder", L("Attachment description")))); | ||
| 459 | 500 | ||
| 460 | /* add poll controls */ | 501 | /* add poll controls */ |
| 461 | if (poll) { | 502 | if (poll) { |
| @@ -1059,7 +1100,7 @@ xs_html *html_top_controls(snac *snac) | |||
| 1059 | NULL, NULL, | 1100 | NULL, NULL, |
| 1060 | xs_stock(XSTYPE_FALSE), "", | 1101 | xs_stock(XSTYPE_FALSE), "", |
| 1061 | xs_stock(XSTYPE_FALSE), NULL, | 1102 | xs_stock(XSTYPE_FALSE), NULL, |
| 1062 | NULL, 1, "", "", 0), | 1103 | NULL, 1, NULL, NULL, 0), |
| 1063 | 1104 | ||
| 1064 | /** operations **/ | 1105 | /** operations **/ |
| 1065 | xs_html_tag("details", | 1106 | xs_html_tag("details", |
| @@ -1631,17 +1672,22 @@ xs_html *html_entry_controls(snac *snac, const char *actor, | |||
| 1631 | xs *form_id = xs_fmt("%s_edit_form", md5); | 1672 | xs *form_id = xs_fmt("%s_edit_form", md5); |
| 1632 | xs *redir = xs_fmt("%s_entry", md5); | 1673 | xs *redir = xs_fmt("%s_entry", md5); |
| 1633 | 1674 | ||
| 1634 | const char *att_file = ""; | 1675 | xs *att_files = xs_list_new(); |
| 1635 | const char *att_alt_text = ""; | 1676 | xs *att_alt_texts = xs_list_new(); |
| 1677 | |||
| 1636 | const xs_list *att_list = xs_dict_get(msg, "attachment"); | 1678 | const xs_list *att_list = xs_dict_get(msg, "attachment"); |
| 1637 | 1679 | ||
| 1638 | /* does it have an attachment? */ | 1680 | if (xs_is_list(att_list)) { |
| 1639 | if (xs_type(att_list) == XSTYPE_LIST && xs_list_len(att_list)) { | 1681 | const xs_dict *d; |
| 1640 | const xs_dict *d = xs_list_get(att_list, 0); | ||
| 1641 | 1682 | ||
| 1642 | if (xs_type(d) == XSTYPE_DICT) { | 1683 | xs_list_foreach(att_list, d) { |
| 1643 | att_file = xs_dict_get_def(d, "url", ""); | 1684 | const char *att_file = xs_dict_get(d, "url"); |
| 1644 | att_alt_text = xs_dict_get_def(d, "name", ""); | 1685 | const char *att_alt_text = xs_dict_get(d, "name"); |
| 1686 | |||
| 1687 | if (xs_is_string(att_file) && xs_is_string(att_alt_text)) { | ||
| 1688 | att_files = xs_list_append(att_files, att_file); | ||
| 1689 | att_alt_texts = xs_list_append(att_alt_texts, att_alt_text); | ||
| 1690 | } | ||
| 1645 | } | 1691 | } |
| 1646 | } | 1692 | } |
| 1647 | 1693 | ||
| @@ -1653,7 +1699,7 @@ xs_html *html_entry_controls(snac *snac, const char *actor, | |||
| 1653 | id, NULL, | 1699 | id, NULL, |
| 1654 | xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"), | 1700 | xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"), |
| 1655 | xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir, | 1701 | xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir, |
| 1656 | NULL, 0, att_file, att_alt_text, is_draft(snac, id))), | 1702 | NULL, 0, att_files, att_alt_texts, is_draft(snac, id))), |
| 1657 | xs_html_tag("p", NULL)); | 1703 | xs_html_tag("p", NULL)); |
| 1658 | } | 1704 | } |
| 1659 | 1705 | ||
| @@ -1672,7 +1718,7 @@ xs_html *html_entry_controls(snac *snac, const char *actor, | |||
| 1672 | NULL, NULL, | 1718 | NULL, NULL, |
| 1673 | xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"), | 1719 | xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"), |
| 1674 | xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir, | 1720 | xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir, |
| 1675 | id, 0, "", "", 0)), | 1721 | id, 0, NULL, NULL, 0)), |
| 1676 | xs_html_tag("p", NULL)); | 1722 | xs_html_tag("p", NULL)); |
| 1677 | } | 1723 | } |
| 1678 | 1724 | ||
| @@ -2938,7 +2984,7 @@ xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, cons | |||
| 2938 | NULL, actor_id, | 2984 | NULL, actor_id, |
| 2939 | xs_stock(XSTYPE_FALSE), "", | 2985 | xs_stock(XSTYPE_FALSE), "", |
| 2940 | xs_stock(XSTYPE_FALSE), NULL, | 2986 | xs_stock(XSTYPE_FALSE), NULL, |
| 2941 | NULL, 0, "", "", 0), | 2987 | NULL, 0, NULL, NULL, 0), |
| 2942 | xs_html_tag("p", NULL)); | 2988 | xs_html_tag("p", NULL)); |
| 2943 | 2989 | ||
| 2944 | xs_html_add(snac_post, snac_controls); | 2990 | xs_html_add(snac_post, snac_controls); |
| @@ -3966,52 +4012,56 @@ int html_post_handler(const xs_dict *req, const char *q_path, | |||
| 3966 | /* post note */ | 4012 | /* post note */ |
| 3967 | const xs_str *content = xs_dict_get(p_vars, "content"); | 4013 | const xs_str *content = xs_dict_get(p_vars, "content"); |
| 3968 | const xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to"); | 4014 | const xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to"); |
| 3969 | const xs_str *attach_url = xs_dict_get(p_vars, "attach_url"); | ||
| 3970 | const xs_list *attach_file = xs_dict_get(p_vars, "attach"); | ||
| 3971 | const xs_str *to = xs_dict_get(p_vars, "to"); | 4015 | const xs_str *to = xs_dict_get(p_vars, "to"); |
| 3972 | const xs_str *sensitive = xs_dict_get(p_vars, "sensitive"); | 4016 | const xs_str *sensitive = xs_dict_get(p_vars, "sensitive"); |
| 3973 | const xs_str *summary = xs_dict_get(p_vars, "summary"); | 4017 | const xs_str *summary = xs_dict_get(p_vars, "summary"); |
| 3974 | const xs_str *edit_id = xs_dict_get(p_vars, "edit_id"); | 4018 | const xs_str *edit_id = xs_dict_get(p_vars, "edit_id"); |
| 3975 | const xs_str *alt_text = xs_dict_get(p_vars, "alt_text"); | ||
| 3976 | int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only")); | 4019 | int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only")); |
| 3977 | int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft")); | 4020 | int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft")); |
| 3978 | xs *attach_list = xs_list_new(); | 4021 | xs *attach_list = xs_list_new(); |
| 3979 | 4022 | ||
| 3980 | /* default alt text */ | 4023 | /* iterate the attachments */ |
| 3981 | if (xs_is_null(alt_text)) | 4024 | int max_attachments = xs_number_get(xs_dict_get_def(srv_config, "max_attachments", "4")); |
| 3982 | alt_text = ""; | ||
| 3983 | 4025 | ||
| 3984 | /* is attach_url set? */ | 4026 | for (int att_n = 0; att_n < max_attachments; att_n++) { |
| 3985 | if (!xs_is_null(attach_url) && *attach_url != '\0') { | 4027 | xs *url_lbl = xs_fmt("attach_url_%d", att_n); |
| 3986 | xs *l = xs_list_new(); | 4028 | xs *att_lbl = xs_fmt("attach_%d", att_n); |
| 4029 | xs *alt_lbl = xs_fmt("alt_text_%d", att_n); | ||
| 3987 | 4030 | ||
| 3988 | l = xs_list_append(l, attach_url); | 4031 | const char *attach_url = xs_dict_get(p_vars, url_lbl); |
| 3989 | l = xs_list_append(l, alt_text); | 4032 | const xs_list *attach_file = xs_dict_get(p_vars, att_lbl); |
| 4033 | const char *alt_text = xs_dict_get_def(p_vars, alt_lbl, ""); | ||
| 3990 | 4034 | ||
| 3991 | attach_list = xs_list_append(attach_list, l); | 4035 | if (xs_is_string(attach_url) && *attach_url != '\0') { |
| 3992 | } | 4036 | xs *l = xs_list_new(); |
| 3993 | 4037 | ||
| 3994 | /* is attach_file set? */ | 4038 | l = xs_list_append(l, attach_url); |
| 3995 | if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) { | 4039 | l = xs_list_append(l, alt_text); |
| 3996 | const char *fn = xs_list_get(attach_file, 0); | ||
| 3997 | 4040 | ||
| 3998 | if (*fn != '\0') { | 4041 | attach_list = xs_list_append(attach_list, l); |
| 3999 | char *ext = strrchr(fn, '.'); | 4042 | } |
| 4000 | xs *hash = xs_md5_hex(fn, strlen(fn)); | 4043 | else |
| 4001 | xs *id = xs_fmt("%s%s", hash, ext); | 4044 | if (xs_is_list(attach_file)) { |
| 4002 | xs *url = xs_fmt("%s/s/%s", snac.actor, id); | 4045 | const char *fn = xs_list_get(attach_file, 0); |
| 4003 | int fo = xs_number_get(xs_list_get(attach_file, 1)); | ||
| 4004 | int fs = xs_number_get(xs_list_get(attach_file, 2)); | ||
| 4005 | 4046 | ||
| 4006 | /* store */ | 4047 | if (xs_is_string(fn) && *fn != '\0') { |
| 4007 | static_put(&snac, id, payload + fo, fs); | 4048 | char *ext = strrchr(fn, '.'); |
| 4049 | xs *hash = xs_md5_hex(fn, strlen(fn)); | ||
| 4050 | xs *id = xs_fmt("%s%s", hash, ext); | ||
| 4051 | xs *url = xs_fmt("%s/s/%s", snac.actor, id); | ||
| 4052 | int fo = xs_number_get(xs_list_get(attach_file, 1)); | ||
| 4053 | int fs = xs_number_get(xs_list_get(attach_file, 2)); | ||
| 4008 | 4054 | ||
| 4009 | xs *l = xs_list_new(); | 4055 | /* store */ |
| 4056 | static_put(&snac, id, payload + fo, fs); | ||
| 4010 | 4057 | ||
| 4011 | l = xs_list_append(l, url); | 4058 | xs *l = xs_list_new(); |
| 4012 | l = xs_list_append(l, alt_text); | ||
| 4013 | 4059 | ||
| 4014 | attach_list = xs_list_append(attach_list, l); | 4060 | l = xs_list_append(l, url); |
| 4061 | l = xs_list_append(l, alt_text); | ||
| 4062 | |||
| 4063 | attach_list = xs_list_append(attach_list, l); | ||
| 4064 | } | ||
| 4015 | } | 4065 | } |
| 4016 | } | 4066 | } |
| 4017 | 4067 | ||