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 *attach = get_attachments(msg);
{
xs_list *p = attach;
xs_dict *v;
xs *matt = xs_list_new(); xs *matt = xs_list_new();
xs_list *att = xs_dict_get(msg, "attachment");
xs_str *aobj;
xs *attr_list = NULL;
if (xs_type(att) == XSTYPE_DICT) { while (xs_list_iter(&p, &v)) {
attr_list = xs_list_new(); char *type = xs_dict_get(v, "type");
attr_list = xs_list_append(attr_list, att); char *href = xs_dict_get(v, "href");
} char *name = xs_dict_get(v, "name");
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 */ if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
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(&att, &aobj)) {
const char *mtype = xs_dict_get(aobj, "mediaType");
if (xs_is_null(mtype))
mtype = xs_dict_get(aobj, "type");
const char *url = xs_dict_get(aobj, "url");
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();