diff options
| author | 2024-01-24 19:13:46 +0100 | |
|---|---|---|
| committer | 2024-01-24 19:13:46 +0100 | |
| commit | 08898af45c3ec5f212af873998c7f7c61091031f (patch) | |
| tree | 5ad3b188d4c0f5d91c8c27c9b5ba3671568dd258 | |
| parent | Don't send anything to the collected inboxes if collection is disabled. (diff) | |
| download | snac2-08898af45c3ec5f212af873998c7f7c61091031f.tar.gz snac2-08898af45c3ec5f212af873998c7f7c61091031f.tar.xz snac2-08898af45c3ec5f212af873998c7f7c61091031f.zip | |
New function get_attachments() (still unused).
| -rw-r--r-- | activitypub.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/activitypub.c b/activitypub.c index 91195cd..79b8350 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -184,6 +184,127 @@ char *get_atto(const xs_dict *msg) | |||
| 184 | } | 184 | } |
| 185 | 185 | ||
| 186 | 186 | ||
| 187 | xs_list *get_attachments(const xs_dict *msg) | ||
| 188 | /* unify the garbage fire that are the attachments */ | ||
| 189 | { | ||
| 190 | xs_list *l = xs_list_new(); | ||
| 191 | xs_list *p; | ||
| 192 | |||
| 193 | /* try first the attachments list */ | ||
| 194 | if (!xs_is_null(p = xs_dict_get(msg, "attachments"))) { | ||
| 195 | xs *attach = NULL; | ||
| 196 | xs_val *v; | ||
| 197 | |||
| 198 | /* ensure it's a list */ | ||
| 199 | if (xs_type(p) == XSTYPE_DICT) { | ||
| 200 | attach = xs_list_new(); | ||
| 201 | attach = xs_list_append(attach, v); | ||
| 202 | } | ||
| 203 | else | ||
| 204 | attach = xs_dup(p); | ||
| 205 | |||
| 206 | if (xs_type(attach) == XSTYPE_LIST) { | ||
| 207 | /* does the message have an image? */ | ||
| 208 | if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) { | ||
| 209 | /* add it to the attachment list */ | ||
| 210 | attach = xs_list_append(attach, v); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | /* now iterate the list */ | ||
| 215 | p = attach; | ||
| 216 | while (xs_list_iter(&p, &v)) { | ||
| 217 | char *type = xs_dict_get(v, "mediaType"); | ||
| 218 | if (xs_is_null(type)) | ||
| 219 | type = xs_dict_get(v, "type"); | ||
| 220 | |||
| 221 | if (xs_is_null(type)) | ||
| 222 | continue; | ||
| 223 | |||
| 224 | char *href = xs_dict_get(v, "url"); | ||
| 225 | if (xs_is_null(href)) | ||
| 226 | href = xs_dict_get(v, "href"); | ||
| 227 | if (xs_is_null(href)) | ||
| 228 | continue; | ||
| 229 | |||
| 230 | /* infer MIME type from non-specific attachments */ | ||
| 231 | if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) { | ||
| 232 | char *mt = (char *)xs_mime_by_ext(href); | ||
| 233 | |||
| 234 | if (xs_match(mt, "image/*|audio/*|video/*")) /* */ | ||
| 235 | type = mt; | ||
| 236 | } | ||
| 237 | |||
| 238 | char *name = xs_dict_get(v, "name"); | ||
| 239 | if (xs_is_null(name)) | ||
| 240 | name = xs_dict_get(msg, "name"); | ||
| 241 | if (xs_is_null(name)) | ||
| 242 | name = L("No description"); | ||
| 243 | |||
| 244 | xs *d = xs_dict_new(); | ||
| 245 | d = xs_dict_append(d, "type", type); | ||
| 246 | d = xs_dict_append(d, "href", href); | ||
| 247 | d = xs_dict_append(d, "name", name); | ||
| 248 | |||
| 249 | l = xs_list_append(l, d); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | /** urls (attachments from Peertube) **/ | ||
| 254 | p = xs_dict_get(msg, "url"); | ||
| 255 | |||
| 256 | if (xs_type(p) == XSTYPE_LIST) { | ||
| 257 | char *href = NULL; | ||
| 258 | char *type = NULL; | ||
| 259 | xs_val *v; | ||
| 260 | |||
| 261 | while (href == NULL && xs_list_iter(&p, &v)) { | ||
| 262 | if (xs_type(v) == XSTYPE_DICT) { | ||
| 263 | char *mtype = xs_dict_get(v, "type"); | ||
| 264 | |||
| 265 | if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Link") == 0) { | ||
| 266 | mtype = xs_dict_get(v, "mediaType"); | ||
| 267 | xs_list *tag = xs_dict_get(v, "tag"); | ||
| 268 | |||
| 269 | if (xs_type(mtype) == XSTYPE_STRING && | ||
| 270 | strcmp(mtype, "application/x-mpegURL") == 0 && | ||
| 271 | xs_type(tag) == XSTYPE_LIST) { | ||
| 272 | /* now iterate the tag list, looking for a video URL */ | ||
| 273 | xs_dict *d; | ||
| 274 | |||
| 275 | while (href == NULL && xs_list_iter(&tag, &d)) { | ||
| 276 | if (xs_type(d) == XSTYPE_DICT) { | ||
| 277 | if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING && | ||
| 278 | xs_startswith(mtype, "video/")) { | ||
| 279 | char *h = xs_dict_get(d, "href"); | ||
| 280 | |||
| 281 | /* this is probably it */ | ||
| 282 | if (xs_type(h) == XSTYPE_STRING) { | ||
| 283 | href = h; | ||
| 284 | type = mtype; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | if (href && type) { | ||
| 295 | xs *d = xs_dict_new(); | ||
| 296 | d = xs_dict_append(d, "href", href); | ||
| 297 | d = xs_dict_append(d, "type", type); | ||
| 298 | d = xs_dict_append(d, "name", "---"); | ||
| 299 | |||
| 300 | l = xs_list_append(l, d); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | return l; | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 187 | int timeline_request(snac *snac, char **id, xs_str **wrk, int level) | 308 | int timeline_request(snac *snac, char **id, xs_str **wrk, int level) |
| 188 | /* ensures that an entry and its ancestors are in the timeline */ | 309 | /* ensures that an entry and its ancestors are in the timeline */ |
| 189 | { | 310 | { |