commit 13d4fde316c8281e3100017b4b8eca4fc0dfc925
parent bde5748a6e52e6f124e708a64e95827d87c8850c
Author: default <nobody@localhost>
Date: Thu, 19 Dec 2024 10:05:11 +0100
Avoid adding repeated attachments.
Diffstat:
M | activitypub.c | | | 29 | ++++++++++++++++++++--------- |
M | format.c | | | 52 | ++++++++++++++++++++++++++++++++++++++-------------- |
2 files changed, 58 insertions(+), 23 deletions(-)
diff --git a/activitypub.c b/activitypub.c
@@ -1476,20 +1476,31 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
/* create the attachment list, if there are any */
if (!xs_is_null(attach)) {
- int c = 0;
- while (xs_list_next(attach, &v, &c)) {
- xs *d = xs_dict_new();
+ xs_list_foreach(attach, v) {
const char *url = xs_list_get(v, 0);
const char *alt = xs_list_get(v, 1);
const char *mime = xs_mime_by_ext(url);
+ int add = 1;
+
+ /* check if it's already here */
+ const xs_dict *ad;
+ xs_list_foreach(atls, ad) {
+ if (strcmp(xs_dict_get_def(ad, "url", ""), url) == 0) {
+ add = 0;
+ break;
+ }
+ }
- d = xs_dict_append(d, "mediaType", mime);
- d = xs_dict_append(d, "url", url);
- d = xs_dict_append(d, "name", alt);
- d = xs_dict_append(d, "type",
- xs_startswith(mime, "image/") ? "Image" : "Document");
+ if (add) {
+ xs *d = xs_dict_new();
+ d = xs_dict_append(d, "mediaType", mime);
+ d = xs_dict_append(d, "url", url);
+ d = xs_dict_append(d, "name", alt);
+ d = xs_dict_append(d, "type",
+ xs_startswith(mime, "image/") ? "Image" : "Document");
- atls = xs_list_append(atls, d);
+ atls = xs_list_append(atls, d);
+ }
}
}
diff --git a/format.c b/format.c
@@ -163,14 +163,26 @@ static xs_str *format_line(const char *line, xs_list **attach)
const char *mime = xs_mime_by_ext(img_url);
if (attach != NULL && xs_startswith(mime, "image/")) {
- xs *d = xs_dict_new();
-
- d = xs_dict_append(d, "mediaType", mime);
- d = xs_dict_append(d, "url", img_url);
- d = xs_dict_append(d, "name", alt_text);
- d = xs_dict_append(d, "type", "Image");
-
- *attach = xs_list_append(*attach, d);
+ const xs_dict *ad;
+ int add = 1;
+
+ xs_list_foreach(*attach, ad) {
+ if (strcmp(xs_dict_get_def(ad, "url", ""), img_url) == 0) {
+ add = 0;
+ break;
+ }
+ }
+
+ if (add) {
+ xs *d = xs_dict_new();
+
+ d = xs_dict_append(d, "mediaType", mime);
+ d = xs_dict_append(d, "url", img_url);
+ d = xs_dict_append(d, "name", alt_text);
+ d = xs_dict_append(d, "type", "Image");
+
+ *attach = xs_list_append(*attach, d);
+ }
}
else {
xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
@@ -191,14 +203,26 @@ static xs_str *format_line(const char *line, xs_list **attach)
if (attach != NULL && xs_startswith(mime, "image/")) {
/* if it's a link to an image, insert it as an attachment */
- xs *d = xs_dict_new();
+ const xs_dict *ad;
+ int add = 1;
+
+ xs_list_foreach(*attach, ad) {
+ if (strcmp(xs_dict_get_def(ad, "url", ""), v2) == 0) {
+ add = 0;
+ break;
+ }
+ }
- d = xs_dict_append(d, "mediaType", mime);
- d = xs_dict_append(d, "url", v2);
- d = xs_dict_append(d, "name", "");
- d = xs_dict_append(d, "type", "Image");
+ if (add) {
+ xs *d = xs_dict_new();
+
+ d = xs_dict_append(d, "mediaType", mime);
+ d = xs_dict_append(d, "url", v2);
+ d = xs_dict_append(d, "name", "");
+ d = xs_dict_append(d, "type", "Image");
- *attach = xs_list_append(*attach, d);
+ *attach = xs_list_append(*attach, d);
+ }
}
else {
xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);