diff options
| author | 2023-04-22 00:17:42 +0200 | |
|---|---|---|
| committer | 2023-04-22 00:17:42 +0200 | |
| commit | 73e7195e6c081f85afc0afdda7efea5017c43d8d (patch) | |
| tree | fa8bdaf64e52dafaf8c65e883cf5efe4d6c4562a | |
| parent | Updated README. (diff) | |
| download | penes-snac2-73e7195e6c081f85afc0afdda7efea5017c43d8d.tar.gz penes-snac2-73e7195e6c081f85afc0afdda7efea5017c43d8d.tar.xz penes-snac2-73e7195e6c081f85afc0afdda7efea5017c43d8d.zip | |
Added mastoapi support for adding images.
| -rw-r--r-- | mastoapi.c | 70 |
1 files changed, 70 insertions, 0 deletions
| @@ -1271,6 +1271,10 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 1271 | const char *mid = xs_dict_get(args, "in_reply_to_id"); | 1271 | const char *mid = xs_dict_get(args, "in_reply_to_id"); |
| 1272 | const char *visibility = xs_dict_get(args, "visibility"); | 1272 | const char *visibility = xs_dict_get(args, "visibility"); |
| 1273 | const char *summary = xs_dict_get(args, "spoiler_text"); | 1273 | const char *summary = xs_dict_get(args, "spoiler_text"); |
| 1274 | const char *media_ids = xs_dict_get(args, "media_ids"); | ||
| 1275 | |||
| 1276 | if (xs_is_null(media_ids)) | ||
| 1277 | media_ids = xs_dict_get(args, "media_ids[]"); | ||
| 1274 | 1278 | ||
| 1275 | xs *attach_list = xs_list_new(); | 1279 | xs *attach_list = xs_list_new(); |
| 1276 | xs *irt = NULL; | 1280 | xs *irt = NULL; |
| @@ -1284,6 +1288,31 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 1284 | irt = xs_dup(xs_dict_get(r_msg, "id")); | 1288 | irt = xs_dup(xs_dict_get(r_msg, "id")); |
| 1285 | } | 1289 | } |
| 1286 | 1290 | ||
| 1291 | /* does it have attachments? */ | ||
| 1292 | if (!xs_is_null(media_ids)) { | ||
| 1293 | xs *mi = NULL; | ||
| 1294 | |||
| 1295 | if (xs_type(media_ids) == XSTYPE_LIST) | ||
| 1296 | mi = xs_dup(media_ids); | ||
| 1297 | else { | ||
| 1298 | mi = xs_list_new(); | ||
| 1299 | mi = xs_list_append(mi, media_ids); | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | xs_list *p = mi; | ||
| 1303 | xs_str *v; | ||
| 1304 | |||
| 1305 | while (xs_list_iter(&p, &v)) { | ||
| 1306 | xs *l = xs_list_new(); | ||
| 1307 | xs *url = xs_fmt("%s/s/%s", snac.actor, v); | ||
| 1308 | |||
| 1309 | l = xs_list_append(l, url); | ||
| 1310 | l = xs_list_append(l, ""); | ||
| 1311 | |||
| 1312 | attach_list = xs_list_append(attach_list, l); | ||
| 1313 | } | ||
| 1314 | } | ||
| 1315 | |||
| 1287 | /* prepare the message */ | 1316 | /* prepare the message */ |
| 1288 | xs *msg = msg_note(&snac, content, NULL, irt, attach_list, | 1317 | xs *msg = msg_note(&snac, content, NULL, irt, attach_list, |
| 1289 | strcmp(visibility, "public") == 0 ? 0 : 1); | 1318 | strcmp(visibility, "public") == 0 ? 0 : 1); |
| @@ -1444,6 +1473,47 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 1444 | else | 1473 | else |
| 1445 | if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { | 1474 | if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { |
| 1446 | if (logged_in) { | 1475 | if (logged_in) { |
| 1476 | /* { | ||
| 1477 | xs *j = xs_json_dumps_pp(args, 4); | ||
| 1478 | printf("%s\n", j); | ||
| 1479 | }*/ | ||
| 1480 | const xs_list *file = xs_dict_get(args, "file"); | ||
| 1481 | const char *desc = xs_dict_get(args, "description"); | ||
| 1482 | |||
| 1483 | if (xs_is_null(desc)) | ||
| 1484 | desc = ""; | ||
| 1485 | |||
| 1486 | status = 400; | ||
| 1487 | |||
| 1488 | if (xs_type(file) == XSTYPE_LIST) { | ||
| 1489 | const char *fn = xs_list_get(file, 0); | ||
| 1490 | |||
| 1491 | if (*fn != '\0') { | ||
| 1492 | char *ext = strrchr(fn, '.'); | ||
| 1493 | xs *hash = xs_md5_hex(fn, strlen(fn)); | ||
| 1494 | xs *id = xs_fmt("%s%s", hash, ext); | ||
| 1495 | xs *url = xs_fmt("%s/s/%s", snac.actor, id); | ||
| 1496 | int fo = xs_number_get(xs_list_get(file, 1)); | ||
| 1497 | int fs = xs_number_get(xs_list_get(file, 2)); | ||
| 1498 | |||
| 1499 | /* store */ | ||
| 1500 | static_put(&snac, id, payload + fo, fs); | ||
| 1501 | |||
| 1502 | /* prepare a response */ | ||
| 1503 | xs *rsp = xs_dict_new(); | ||
| 1504 | |||
| 1505 | rsp = xs_dict_append(rsp, "id", id); | ||
| 1506 | rsp = xs_dict_append(rsp, "type", "image"); | ||
| 1507 | rsp = xs_dict_append(rsp, "url", url); | ||
| 1508 | rsp = xs_dict_append(rsp, "preview_url", url); | ||
| 1509 | rsp = xs_dict_append(rsp, "remote_url", url); | ||
| 1510 | rsp = xs_dict_append(rsp, "description", desc); | ||
| 1511 | |||
| 1512 | *body = xs_json_dumps_pp(rsp, 4); | ||
| 1513 | *ctype = "application/json"; | ||
| 1514 | status = 200; | ||
| 1515 | } | ||
| 1516 | } | ||
| 1447 | } | 1517 | } |
| 1448 | else | 1518 | else |
| 1449 | status = 401; | 1519 | status = 401; |