Allow the 'attachment' field of a post to be single object instead of a list.

It seems that Gotosocial does this.
This commit is contained in:
default 2023-07-25 12:01:42 +02:00
parent 2ee74c9a6a
commit e6baeadf5f

45
html.c
View file

@ -1153,16 +1153,31 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
s = xs_str_cat(s, "\n"); s = xs_str_cat(s, "\n");
/* add the attachments */ /* add the attachments */
xs_list *attach = xs_dict_get(msg, "attachment"); v = xs_dict_get(msg, "attachment");
xs_dict *image = xs_dict_get(msg, "image");
if (!xs_is_null(attach) || !xs_is_null(image)) { /** **/ if (!xs_is_null(v)) { /** attachments **/
char *v; xs *attach = NULL;
/* ensure it's a list */
if (xs_type(v) == XSTYPE_DICT) {
attach = xs_list_new();
attach = xs_list_append(attach, v);
}
else
attach = xs_dup(v);
/* does the message have an image? */
if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) {
/* add it to the attachment list */
attach = xs_list_append(attach, v);
}
/* make custom css for attachments easier */ /* make custom css for attachments easier */
s = xs_str_cat(s, "<p class=\"snac-content-attachments\">\n"); s = xs_str_cat(s, "<p class=\"snac-content-attachments\">\n");
while (xs_list_iter(&attach, &v)) { xs_list *p = attach;
while (xs_list_iter(&p, &v)) {
const char *t = xs_dict_get(v, "mediaType"); const char *t = xs_dict_get(v, "mediaType");
if (xs_is_null(t)) if (xs_is_null(t))
@ -1171,7 +1186,7 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
if (xs_is_null(t)) if (xs_is_null(t))
continue; continue;
if (xs_startswith(t, "image/")) { if (xs_startswith(t, "image/") || strcmp(t, "Image") == 0) {
char *url = xs_dict_get(v, "url"); char *url = xs_dict_get(v, "url");
char *name = xs_dict_get(v, "name"); char *name = xs_dict_get(v, "name");
@ -1248,24 +1263,6 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
} }
} }
/* if the message has an image, add it */
if (!xs_is_null(image)) {
if (!xs_is_null(image = xs_dict_get(image, "url"))) {
xs *es1;
if (!xs_is_null(v = xs_dict_get(msg, "name")))
es1 = encode_html(v);
else
es1 = xs_str_new(NULL);
xs *s1 = xs_fmt(
"<a href=\"%s\" target=\"_blank\">"
"<img src=\"%s\" alt=\"%s\" title=\"%s\" loading=\"lazy\"/></a>\n",
image, image, es1, es1);
s = xs_str_cat(s, s1);
}
}
s = xs_str_cat(s, "</p>\n"); s = xs_str_cat(s, "</p>\n");
} }