commit e6baead

default  ·  2023-07-25 10:01:42 +0000 UTC
parent 2ee74c9
Allow the 'attachment' field of a post to be single object instead of a list.

It seems that Gotosocial does this.
1 files changed,  +21, -24
M html.c
M html.c
+21, -24
 1@@ -1153,16 +1153,31 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
 2     s = xs_str_cat(s, "\n");
 3 
 4     /* add the attachments */
 5-    xs_list *attach = xs_dict_get(msg, "attachment");
 6-    xs_dict *image  = xs_dict_get(msg, "image");
 7+    v = xs_dict_get(msg, "attachment");
 8 
 9-    if (!xs_is_null(attach) || !xs_is_null(image)) { /** **/
10-        char *v;
11+    if (!xs_is_null(v)) { /** attachments **/
12+        xs *attach = NULL;
13+
14+        /* ensure it's a list */
15+        if (xs_type(v) == XSTYPE_DICT) {
16+            attach = xs_list_new();
17+            attach = xs_list_append(attach, v);
18+        }
19+        else
20+            attach = xs_dup(v);
21+
22+        /* does the message have an image? */
23+        if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) {
24+            /* add it to the attachment list */
25+            attach = xs_list_append(attach, v);
26+        }
27 
28         /* make custom css for attachments easier */
29         s = xs_str_cat(s, "<p class=\"snac-content-attachments\">\n");
30 
31-        while (xs_list_iter(&attach, &v)) {
32+        xs_list *p = attach;
33+
34+        while (xs_list_iter(&p, &v)) {
35             const char *t = xs_dict_get(v, "mediaType");
36 
37             if (xs_is_null(t))
38@@ -1171,7 +1186,7 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
39             if (xs_is_null(t))
40                 continue;
41 
42-            if (xs_startswith(t, "image/")) {
43+            if (xs_startswith(t, "image/") || strcmp(t, "Image") == 0) {
44                 char *url  = xs_dict_get(v, "url");
45                 char *name = xs_dict_get(v, "name");
46 
47@@ -1248,24 +1263,6 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
48             }
49         }
50 
51-        /* if the message has an image, add it */
52-        if (!xs_is_null(image)) {
53-            if (!xs_is_null(image = xs_dict_get(image, "url"))) {
54-                xs *es1;
55-                if (!xs_is_null(v = xs_dict_get(msg, "name")))
56-                    es1 = encode_html(v);
57-                else
58-                    es1 = xs_str_new(NULL);
59-
60-                xs *s1 = xs_fmt(
61-                    "<a href=\"%s\" target=\"_blank\">"
62-                    "<img src=\"%s\" alt=\"%s\" title=\"%s\" loading=\"lazy\"/></a>\n",
63-                            image, image, es1, es1);
64-
65-                s = xs_str_cat(s, s1);
66-            }
67-        }
68-
69         s = xs_str_cat(s, "</p>\n");
70     }
71