Use get_attachments() in mastoapi_status().

This commit is contained in:
default 2024-01-24 19:58:51 +01:00
parent 5b3a2fbd88
commit 2d4860d57e

View file

@ -768,72 +768,38 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
st = xs_dict_append(st, "spoiler_text", tmp); st = xs_dict_append(st, "spoiler_text", tmp);
/* create the list of attachments */ /* create the list of attachments */
xs *matt = xs_list_new(); xs *attach = get_attachments(msg);
xs_list *att = xs_dict_get(msg, "attachment");
xs_str *aobj;
xs *attr_list = NULL;
if (xs_type(att) == XSTYPE_DICT) { {
attr_list = xs_list_new(); xs_list *p = attach;
attr_list = xs_list_append(attr_list, att); xs_dict *v;
}
else
if (xs_type(att) == XSTYPE_LIST)
attr_list = xs_dup(att);
else
attr_list = xs_list_new();
/* if it has an image, add it as an attachment */ xs *matt = xs_list_new();
xs_dict *image = xs_dict_get(msg, "image");
if (!xs_is_null(image))
attr_list = xs_list_append(attr_list, image);
att = attr_list; while (xs_list_iter(&p, &v)) {
while (xs_list_iter(&att, &aobj)) { char *type = xs_dict_get(v, "type");
const char *mtype = xs_dict_get(aobj, "mediaType"); char *href = xs_dict_get(v, "href");
if (xs_is_null(mtype)) char *name = xs_dict_get(v, "name");
mtype = xs_dict_get(aobj, "type");
const char *url = xs_dict_get(aobj, "url"); if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
if (xs_is_null(url))
url = xs_dict_get(aobj, "href");
if (xs_is_null(url))
continue;
/* if it's a plain Link, check if it can be "rewritten" */
if (xs_list_len(attr_list) < 2 && strcmp(mtype, "Link") == 0) {
const char *mt = xs_mime_by_ext(url);
if (xs_startswith(mt, "image/") ||
xs_startswith(mt, "audio/") ||
xs_startswith(mt, "video/"))
mtype = mt;
}
if (!xs_is_null(mtype)) {
if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/") ||
strcmp(mtype, "Image") == 0 || strcmp(mtype, "Document") == 0) {
xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt)); xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
xs *matte = xs_dict_new();
matte = xs_dict_append(matte, "id", matteid); xs *d = xs_dict_new();
matte = xs_dict_append(matte, "type", *mtype == 'v' ? "video" : "image");
matte = xs_dict_append(matte, "url", url);
matte = xs_dict_append(matte, "preview_url", url);
matte = xs_dict_append(matte, "remote_url", url);
const char *name = xs_dict_get(aobj, "name"); d = xs_dict_append(d, "id", matteid);
if (xs_is_null(name)) d = xs_dict_append(d, "url", href);
name = ""; d = xs_dict_append(d, "preview_url", href);
d = xs_dict_append(d, "remote_url", href);
d = xs_dict_append(d, "description", name);
matte = xs_dict_append(matte, "description", name); d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" : "image");
matt = xs_list_append(matt, matte); matt = xs_list_append(matt, d);
} }
} }
}
st = xs_dict_append(st, "media_attachments", matt); st = xs_dict_append(st, "media_attachments", matt);
}
{ {
xs *ml = xs_list_new(); xs *ml = xs_list_new();