commit 08898af

default  ·  2024-01-24 18:13:46 +0000 UTC
parent 24cd0c1
New function get_attachments() (still unused).
1 files changed,  +121, -0
+121, -0
  1@@ -184,6 +184,127 @@ char *get_atto(const xs_dict *msg)
  2 }
  3 
  4 
  5+xs_list *get_attachments(const xs_dict *msg)
  6+/* unify the garbage fire that are the attachments */
  7+{
  8+    xs_list *l = xs_list_new();
  9+    xs_list *p;
 10+
 11+    /* try first the attachments list */
 12+    if (!xs_is_null(p = xs_dict_get(msg, "attachments"))) {
 13+        xs *attach = NULL;
 14+        xs_val *v;
 15+
 16+        /* ensure it's a list */
 17+        if (xs_type(p) == XSTYPE_DICT) {
 18+            attach = xs_list_new();
 19+            attach = xs_list_append(attach, v);
 20+        }
 21+        else
 22+            attach = xs_dup(p);
 23+
 24+        if (xs_type(attach) == XSTYPE_LIST) {
 25+            /* does the message have an image? */
 26+            if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) {
 27+                /* add it to the attachment list */
 28+                attach = xs_list_append(attach, v);
 29+            }
 30+        }
 31+
 32+        /* now iterate the list */
 33+        p = attach;
 34+        while (xs_list_iter(&p, &v)) {
 35+            char *type = xs_dict_get(v, "mediaType");
 36+            if (xs_is_null(type))
 37+                type = xs_dict_get(v, "type");
 38+
 39+            if (xs_is_null(type))
 40+                continue;
 41+
 42+            char *href = xs_dict_get(v, "url");
 43+            if (xs_is_null(href))
 44+                href = xs_dict_get(v, "href");
 45+            if (xs_is_null(href))
 46+                continue;
 47+
 48+            /* infer MIME type from non-specific attachments */
 49+            if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) {
 50+                char *mt = (char *)xs_mime_by_ext(href);
 51+
 52+                if (xs_match(mt, "image/*|audio/*|video/*")) /* */
 53+                    type = mt;
 54+            }
 55+
 56+            char *name = xs_dict_get(v, "name");
 57+            if (xs_is_null(name))
 58+                name = xs_dict_get(msg, "name");
 59+            if (xs_is_null(name))
 60+                name = L("No description");
 61+
 62+            xs *d = xs_dict_new();
 63+            d = xs_dict_append(d, "type", type);
 64+            d = xs_dict_append(d, "href", href);
 65+            d = xs_dict_append(d, "name", name);
 66+
 67+            l = xs_list_append(l, d);
 68+        }
 69+    }
 70+
 71+    /** urls (attachments from Peertube) **/
 72+    p = xs_dict_get(msg, "url");
 73+
 74+    if (xs_type(p) == XSTYPE_LIST) {
 75+        char *href = NULL;
 76+        char *type = NULL;
 77+        xs_val *v;
 78+
 79+        while (href == NULL && xs_list_iter(&p, &v)) {
 80+            if (xs_type(v) == XSTYPE_DICT) {
 81+                char *mtype = xs_dict_get(v, "type");
 82+
 83+                if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Link") == 0) {
 84+                    mtype = xs_dict_get(v, "mediaType");
 85+                    xs_list *tag = xs_dict_get(v, "tag");
 86+
 87+                    if (xs_type(mtype) == XSTYPE_STRING &&
 88+                        strcmp(mtype, "application/x-mpegURL") == 0 &&
 89+                        xs_type(tag) == XSTYPE_LIST) {
 90+                        /* now iterate the tag list, looking for a video URL */
 91+                        xs_dict *d;
 92+
 93+                        while (href == NULL && xs_list_iter(&tag, &d)) {
 94+                            if (xs_type(d) == XSTYPE_DICT) {
 95+                                if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
 96+                                    xs_startswith(mtype, "video/")) {
 97+                                    char *h = xs_dict_get(d, "href");
 98+
 99+                                    /* this is probably it */
100+                                    if (xs_type(h) == XSTYPE_STRING) {
101+                                        href = h;
102+                                        type = mtype;
103+                                    }
104+                                }
105+                            }
106+                        }
107+                    }
108+                }
109+            }
110+        }
111+
112+        if (href && type) {
113+            xs *d = xs_dict_new();
114+            d = xs_dict_append(d, "href", href);
115+            d = xs_dict_append(d, "type", type);
116+            d = xs_dict_append(d, "name", "---");
117+
118+            l = xs_list_append(l, d);
119+        }
120+    }
121+
122+    return l;
123+}
124+
125+
126 int timeline_request(snac *snac, char **id, xs_str **wrk, int level)
127 /* ensures that an entry and its ancestors are in the timeline */
128 {