commit 13d4fde

default  ·  2024-12-19 09:05:11 +0000 UTC
parent bde5748
Avoid adding repeated attachments.
2 files changed,  +58, -23
+20, -9
 1@@ -1476,20 +1476,31 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
 2 
 3     /* create the attachment list, if there are any */
 4     if (!xs_is_null(attach)) {
 5-        int c = 0;
 6-        while (xs_list_next(attach, &v, &c)) {
 7-            xs *d            = xs_dict_new();
 8+        xs_list_foreach(attach, v) {
 9             const char *url  = xs_list_get(v, 0);
10             const char *alt  = xs_list_get(v, 1);
11             const char *mime = xs_mime_by_ext(url);
12+            int add = 1;
13+
14+            /* check if it's already here */
15+            const xs_dict *ad;
16+            xs_list_foreach(atls, ad) {
17+                if (strcmp(xs_dict_get_def(ad, "url", ""), url) == 0) {
18+                    add = 0;
19+                    break;
20+                }
21+            }
22 
23-            d = xs_dict_append(d, "mediaType", mime);
24-            d = xs_dict_append(d, "url",       url);
25-            d = xs_dict_append(d, "name",      alt);
26-            d = xs_dict_append(d, "type",
27-                xs_startswith(mime, "image/") ? "Image" : "Document");
28+            if (add) {
29+                xs *d = xs_dict_new();
30+                d = xs_dict_append(d, "mediaType", mime);
31+                d = xs_dict_append(d, "url",       url);
32+                d = xs_dict_append(d, "name",      alt);
33+                d = xs_dict_append(d, "type",
34+                    xs_startswith(mime, "image/") ? "Image" : "Document");
35 
36-            atls = xs_list_append(atls, d);
37+                atls = xs_list_append(atls, d);
38+            }
39         }
40     }
41 
+38, -14
 1@@ -163,14 +163,26 @@ static xs_str *format_line(const char *line, xs_list **attach)
 2                     const char *mime     = xs_mime_by_ext(img_url);
 3 
 4                     if (attach != NULL && xs_startswith(mime, "image/")) {
 5-                        xs *d = xs_dict_new();
 6-
 7-                        d = xs_dict_append(d, "mediaType", mime);
 8-                        d = xs_dict_append(d, "url",       img_url);
 9-                        d = xs_dict_append(d, "name",      alt_text);
10-                        d = xs_dict_append(d, "type",      "Image");
11-
12-                        *attach = xs_list_append(*attach, d);
13+                        const xs_dict *ad;
14+                        int add = 1;
15+
16+                        xs_list_foreach(*attach, ad) {
17+                            if (strcmp(xs_dict_get_def(ad, "url", ""), img_url) == 0) {
18+                                add = 0;
19+                                break;
20+                            }
21+                        }
22+
23+                        if (add) {
24+                            xs *d = xs_dict_new();
25+
26+                            d = xs_dict_append(d, "mediaType", mime);
27+                            d = xs_dict_append(d, "url",       img_url);
28+                            d = xs_dict_append(d, "name",      alt_text);
29+                            d = xs_dict_append(d, "type",      "Image");
30+
31+                            *attach = xs_list_append(*attach, d);
32+                        }
33                     }
34                     else {
35                         xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
36@@ -191,14 +203,26 @@ static xs_str *format_line(const char *line, xs_list **attach)
37 
38                 if (attach != NULL && xs_startswith(mime, "image/")) {
39                     /* if it's a link to an image, insert it as an attachment */
40-                    xs *d = xs_dict_new();
41+                    const xs_dict *ad;
42+                    int add = 1;
43+
44+                    xs_list_foreach(*attach, ad) {
45+                        if (strcmp(xs_dict_get_def(ad, "url", ""), v2) == 0) {
46+                            add = 0;
47+                            break;
48+                        }
49+                    }
50 
51-                    d = xs_dict_append(d, "mediaType", mime);
52-                    d = xs_dict_append(d, "url",       v2);
53-                    d = xs_dict_append(d, "name",      "");
54-                    d = xs_dict_append(d, "type",      "Image");
55+                    if (add) {
56+                        xs *d = xs_dict_new();
57+
58+                        d = xs_dict_append(d, "mediaType", mime);
59+                        d = xs_dict_append(d, "url",       v2);
60+                        d = xs_dict_append(d, "name",      "");
61+                        d = xs_dict_append(d, "type",      "Image");
62 
63-                    *attach = xs_list_append(*attach, d);
64+                        *attach = xs_list_append(*attach, d);
65+                    }
66                 }
67                 else {
68                     xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);