commit cf0aa96

default  ·  2023-08-20 14:03:46 +0000 UTC
parent b9f4bea
mastoapi: some tweaks for posts from lemmy.
1 files changed,  +26, -4
+26, -4
 1@@ -669,10 +669,16 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
 2         const char *name    = xs_dict_get(msg, "name");
 3         xs *s1 = NULL;
 4 
 5-        if (name)
 6+        if (name && content)
 7             s1 = xs_fmt("%s<br><br>%s", name, content);
 8         else
 9+        if (name)
10+            s1 = xs_dup(name);
11+        else
12+        if (content)
13             s1 = xs_dup(content);
14+        else
15+            s1 = xs_str_new(NULL);
16 
17         st = xs_dict_append(st, "content", s1);
18     }
19@@ -716,6 +722,22 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
20         if (xs_is_null(mtype))
21             mtype = xs_dict_get(aobj, "type");
22 
23+        const char *url = xs_dict_get(aobj, "url");
24+        if (xs_is_null(url))
25+            url = xs_dict_get(aobj, "href");
26+        if (xs_is_null(url))
27+            continue;
28+
29+        /* if it's a plain Link, check if it can be "rewritten" */
30+        if (xs_list_len(attr_list) < 2 && strcmp(mtype, "Link") == 0) {
31+            const char *mt = xs_mime_by_ext(url);
32+
33+            if (xs_startswith(mt, "image/") ||
34+                xs_startswith(mt, "audio/") ||
35+                xs_startswith(mt, "video/"))
36+                mtype = mt;
37+        }
38+
39         if (!xs_is_null(mtype)) {
40             if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/") ||
41                 strcmp(mtype, "Image") == 0) {
42@@ -724,9 +746,9 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
43 
44                 matte = xs_dict_append(matte, "id",          matteid);
45                 matte = xs_dict_append(matte, "type",        *mtype == 'v' ? "video" : "image");
46-                matte = xs_dict_append(matte, "url",         xs_dict_get(aobj, "url"));
47-                matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
48-                matte = xs_dict_append(matte, "remote_url",  xs_dict_get(aobj, "url"));
49+                matte = xs_dict_append(matte, "url",         url);
50+                matte = xs_dict_append(matte, "preview_url", url);
51+                matte = xs_dict_append(matte, "remote_url",  url);
52 
53                 const char *name = xs_dict_get(aobj, "name");
54                 if (xs_is_null(name))