From 73e7195e6c081f85afc0afdda7efea5017c43d8d Mon Sep 17 00:00:00 2001 From: default Date: Sat, 22 Apr 2023 00:17:42 +0200 Subject: [PATCH] Added mastoapi support for adding images. --- mastoapi.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/mastoapi.c b/mastoapi.c index 3858e13..bf28fa4 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1271,6 +1271,10 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, const char *mid = xs_dict_get(args, "in_reply_to_id"); const char *visibility = xs_dict_get(args, "visibility"); const char *summary = xs_dict_get(args, "spoiler_text"); + const char *media_ids = xs_dict_get(args, "media_ids"); + + if (xs_is_null(media_ids)) + media_ids = xs_dict_get(args, "media_ids[]"); xs *attach_list = xs_list_new(); xs *irt = NULL; @@ -1284,6 +1288,31 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, irt = xs_dup(xs_dict_get(r_msg, "id")); } + /* does it have attachments? */ + if (!xs_is_null(media_ids)) { + xs *mi = NULL; + + if (xs_type(media_ids) == XSTYPE_LIST) + mi = xs_dup(media_ids); + else { + mi = xs_list_new(); + mi = xs_list_append(mi, media_ids); + } + + xs_list *p = mi; + xs_str *v; + + while (xs_list_iter(&p, &v)) { + xs *l = xs_list_new(); + xs *url = xs_fmt("%s/s/%s", snac.actor, v); + + l = xs_list_append(l, url); + l = xs_list_append(l, ""); + + attach_list = xs_list_append(attach_list, l); + } + } + /* prepare the message */ xs *msg = msg_note(&snac, content, NULL, irt, attach_list, strcmp(visibility, "public") == 0 ? 0 : 1); @@ -1444,6 +1473,47 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, else if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { if (logged_in) { +/* { + xs *j = xs_json_dumps_pp(args, 4); + printf("%s\n", j); + }*/ + const xs_list *file = xs_dict_get(args, "file"); + const char *desc = xs_dict_get(args, "description"); + + if (xs_is_null(desc)) + desc = ""; + + status = 400; + + if (xs_type(file) == XSTYPE_LIST) { + const char *fn = xs_list_get(file, 0); + + if (*fn != '\0') { + char *ext = strrchr(fn, '.'); + xs *hash = xs_md5_hex(fn, strlen(fn)); + xs *id = xs_fmt("%s%s", hash, ext); + xs *url = xs_fmt("%s/s/%s", snac.actor, id); + int fo = xs_number_get(xs_list_get(file, 1)); + int fs = xs_number_get(xs_list_get(file, 2)); + + /* store */ + static_put(&snac, id, payload + fo, fs); + + /* prepare a response */ + xs *rsp = xs_dict_new(); + + rsp = xs_dict_append(rsp, "id", id); + rsp = xs_dict_append(rsp, "type", "image"); + rsp = xs_dict_append(rsp, "url", url); + rsp = xs_dict_append(rsp, "preview_url", url); + rsp = xs_dict_append(rsp, "remote_url", url); + rsp = xs_dict_append(rsp, "description", desc); + + *body = xs_json_dumps_pp(rsp, 4); + *ctype = "application/json"; + status = 200; + } + } } else status = 401;