commit 5b3a2fb
default
·
2024-01-24 18:30:01 +0000 UTC
parent 08898af
Use get_attachments() in html_entry().
3 files changed,
+21,
-122
+2,
-2
1@@ -191,7 +191,7 @@ xs_list *get_attachments(const xs_dict *msg)
2 xs_list *p;
3
4 /* try first the attachments list */
5- if (!xs_is_null(p = xs_dict_get(msg, "attachments"))) {
6+ if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) {
7 xs *attach = NULL;
8 xs_val *v;
9
10@@ -262,7 +262,7 @@ xs_list *get_attachments(const xs_dict *msg)
11 if (xs_type(v) == XSTYPE_DICT) {
12 char *mtype = xs_dict_get(v, "type");
13
14- if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Link") == 0) {
15+ if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) {
16 mtype = xs_dict_get(v, "mediaType");
17 xs_list *tag = xs_dict_get(v, "tag");
18
M
html.c
+18,
-120
1@@ -1643,28 +1643,9 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local,
2 }
3
4 /** attachments **/
5- v = xs_dict_get(msg, "attachment");
6-
7- if (!xs_is_null(v)) { /** attachments **/
8- xs *attach = NULL;
9-
10- /* ensure it's a list */
11- if (xs_type(v) == XSTYPE_DICT) {
12- attach = xs_list_new();
13- attach = xs_list_append(attach, v);
14- }
15- else
16- if (xs_type(v) == XSTYPE_LIST)
17- attach = xs_dup(v);
18- else
19- attach = xs_list_new();
20-
21- /* does the message have an image? */
22- if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) {
23- /* add it to the attachment list */
24- attach = xs_list_append(attach, v);
25- }
26+ xs *attach = get_attachments(msg);
27
28+ {
29 /* make custom css for attachments easier */
30 xs_html *content_attachments = xs_html_tag("div",
31 xs_html_attr("class", "snac-content-attachments"));
32@@ -1675,152 +1656,69 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local,
33 xs_list *p = attach;
34
35 while (xs_list_iter(&p, &v)) {
36- char *t = xs_dict_get(v, "mediaType");
37-
38- if (xs_is_null(t))
39- t = xs_dict_get(v, "type");
40-
41- if (xs_is_null(t))
42- continue;
43-
44- char *url = xs_dict_get(v, "url");
45- if (xs_is_null(url))
46- url = xs_dict_get(v, "href");
47- if (xs_is_null(url))
48- continue;
49-
50- /* infer MIME type from non-specific attachments */
51- if (xs_list_len(attach) < 2 && xs_match(t, "Link|Document")) {
52- char *mt = (char *)xs_mime_by_ext(url);
53-
54- if (xs_match(mt, "image/*|audio/*|video/*")) /* */
55- t = mt;
56- }
57-
58+ char *type = xs_dict_get(v, "type");
59+ char *href = xs_dict_get(v, "href");
60 char *name = xs_dict_get(v, "name");
61- if (xs_is_null(name))
62- name = xs_dict_get(msg, "name");
63- if (xs_is_null(name))
64- name = L("No description");
65
66- if (xs_startswith(t, "image/") || strcmp(t, "Image") == 0) {
67+ if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
68 xs_html_add(content_attachments,
69 xs_html_tag("a",
70- xs_html_attr("href", url),
71+ xs_html_attr("href", href),
72 xs_html_attr("target", "_blank"),
73 xs_html_sctag("img",
74 xs_html_attr("loading", "lazy"),
75- xs_html_attr("src", url),
76+ xs_html_attr("src", href),
77 xs_html_attr("alt", name),
78 xs_html_attr("title", name))));
79 }
80 else
81- if (xs_startswith(t, "video/")) {
82+ if (xs_startswith(type, "video/")) {
83 xs_html_add(content_attachments,
84 xs_html_tag("video",
85 xs_html_attr("preload", "none"),
86 xs_html_attr("style", "width: 100%"),
87 xs_html_attr("class", "snac-embedded-video"),
88 xs_html_attr("controls", NULL),
89- xs_html_attr("src", url),
90+ xs_html_attr("src", href),
91 xs_html_text(L("Video")),
92 xs_html_text(": "),
93 xs_html_tag("a",
94- xs_html_attr("href", url),
95+ xs_html_attr("href", href),
96 xs_html_text(name))));
97 }
98 else
99- if (xs_startswith(t, "audio/")) {
100+ if (xs_startswith(type, "audio/")) {
101 xs_html_add(content_attachments,
102 xs_html_tag("audio",
103 xs_html_attr("preload", "none"),
104 xs_html_attr("style", "width: 100%"),
105 xs_html_attr("class", "snac-embedded-audio"),
106 xs_html_attr("controls", NULL),
107- xs_html_attr("src", url),
108+ xs_html_attr("src", href),
109 xs_html_text(L("Audio")),
110 xs_html_text(": "),
111 xs_html_tag("a",
112- xs_html_attr("href", url),
113+ xs_html_attr("href", href),
114 xs_html_text(name))));
115 }
116 else
117- if (strcmp(t, "Link") == 0) {
118+ if (strcmp(type, "Link") == 0) {
119 xs_html_add(content_attachments,
120 xs_html_tag("p",
121 xs_html_tag("a",
122- xs_html_attr("href", url),
123- xs_html_text(url))));
124+ xs_html_attr("href", href),
125+ xs_html_text(href))));
126 }
127 else {
128 xs_html_add(content_attachments,
129 xs_html_tag("p",
130 xs_html_tag("a",
131- xs_html_attr("href", url),
132+ xs_html_attr("href", href),
133 xs_html_text(L("Attachment")),
134 xs_html_text(": "),
135- xs_html_text(url))));
136- }
137- }
138- }
139-
140- /** urls (attachments from Peertube) **/
141- v = xs_dict_get(msg, "url");
142-
143- if (xs_type(v) == XSTYPE_LIST) {
144- xs_list *p = v;
145- char *url = NULL;
146-
147- while (url == NULL && xs_list_iter(&p, &v)) {
148- if (xs_type(v) == XSTYPE_DICT) {
149- char *type = xs_dict_get(v, "type");
150-
151- if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Link") == 0) {
152- char *mtype = xs_dict_get(v, "mediaType");
153- xs_list *tag = xs_dict_get(v, "tag");
154-
155- if (xs_type(mtype) == XSTYPE_STRING &&
156- strcmp(mtype, "application/x-mpegURL") == 0 &&
157- xs_type(tag) == XSTYPE_LIST) {
158- /* now iterate the tag list, looking for a video URL */
159- xs_dict *d;
160-
161- while (url == NULL && xs_list_iter(&tag, &d)) {
162- if (xs_type(d) == XSTYPE_DICT) {
163- if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
164- xs_startswith(mtype, "video/")) {
165-
166- /* this is probably it */
167- if (xs_type(url = xs_dict_get(d, "href")) != XSTYPE_STRING)
168- url = NULL;
169- }
170- }
171- }
172- }
173- }
174+ xs_html_text(href))));
175 }
176 }
177-
178- if (url != NULL) {
179- xs_html *content_attachments = xs_html_tag("div",
180- xs_html_attr("class", "snac-content-attachments"));
181-
182- xs_html_add(snac_content,
183- content_attachments);
184-
185- xs_html_add(content_attachments,
186- xs_html_tag("video",
187- xs_html_attr("preload", "none"),
188- xs_html_attr("style", "width: 100%"),
189- xs_html_attr("class", "snac-embedded-video"),
190- xs_html_attr("controls", NULL),
191- xs_html_attr("src", url),
192- xs_html_text(L("Video")),
193- xs_html_text(": "),
194- xs_html_tag("a",
195- xs_html_attr("href", url),
196- xs_html_text("---"))));
197- }
198 }
199
200 /* has this message an audience (i.e., comes from a channel or community)? */
M
snac.h
+1,
-0
1@@ -246,6 +246,7 @@ const char *default_avatar_base64(void);
2 xs_str *process_tags(snac *snac, const char *content, xs_list **tag);
3
4 char *get_atto(const xs_dict *msg);
5+xs_list *get_attachments(const xs_dict *msg);
6
7 xs_dict *msg_admiration(snac *snac, char *object, char *type);
8 xs_dict *msg_create(snac *snac, const xs_dict *object);