Avoid adding repeated attachments.

This commit is contained in:
default 2024-12-19 10:05:11 +01:00
parent bde5748a6e
commit 13d4fde316
2 changed files with 56 additions and 21 deletions

View file

@ -1476,13 +1476,23 @@ 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;
}
}
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);
@ -1492,6 +1502,7 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
atls = xs_list_append(atls, d);
}
}
}
if (ctxt == NULL)
ctxt = xs_fmt("%s#ctxt", id);

View file

@ -163,6 +163,17 @@ 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/")) {
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);
@ -172,6 +183,7 @@ static xs_str *format_line(const char *line, xs_list **attach)
*attach = xs_list_append(*attach, d);
}
}
else {
xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
@ -191,6 +203,17 @@ 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 */
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;
}
}
if (add) {
xs *d = xs_dict_new();
d = xs_dict_append(d, "mediaType", mime);
@ -200,6 +223,7 @@ static xs_str *format_line(const char *line, xs_list **attach)
*attach = xs_list_append(*attach, d);
}
}
else {
xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
s = xs_str_cat(s, s1);