commit 73e7195

default  ·  2023-04-21 22:17:42 +0000 UTC
parent 70aea72
Added mastoapi support for adding images.
1 files changed,  +70, -0
+70, -0
 1@@ -1271,6 +1271,10 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 2             const char *mid        = xs_dict_get(args, "in_reply_to_id");
 3             const char *visibility = xs_dict_get(args, "visibility");
 4             const char *summary    = xs_dict_get(args, "spoiler_text");
 5+            const char *media_ids  = xs_dict_get(args, "media_ids");
 6+
 7+            if (xs_is_null(media_ids))
 8+                media_ids = xs_dict_get(args, "media_ids[]");
 9 
10             xs *attach_list = xs_list_new();
11             xs *irt         = NULL;
12@@ -1284,6 +1288,31 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
13                     irt = xs_dup(xs_dict_get(r_msg, "id"));
14             }
15 
16+            /* does it have attachments? */
17+            if (!xs_is_null(media_ids)) {
18+                xs *mi = NULL;
19+
20+                if (xs_type(media_ids) == XSTYPE_LIST)
21+                    mi = xs_dup(media_ids);
22+                else {
23+                    mi = xs_list_new();
24+                    mi = xs_list_append(mi, media_ids);
25+                }
26+
27+                xs_list *p = mi;
28+                xs_str *v;
29+
30+                while (xs_list_iter(&p, &v)) {
31+                    xs *l   = xs_list_new();
32+                    xs *url = xs_fmt("%s/s/%s", snac.actor, v);
33+
34+                    l = xs_list_append(l, url);
35+                    l = xs_list_append(l, "");
36+
37+                    attach_list = xs_list_append(attach_list, l);
38+                }
39+            }
40+
41             /* prepare the message */
42             xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
43                         strcmp(visibility, "public") == 0 ? 0 : 1);
44@@ -1444,6 +1473,47 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
45     else
46     if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
47         if (logged_in) {
48+/*    {
49+        xs *j = xs_json_dumps_pp(args, 4);
50+        printf("%s\n", j);
51+    }*/
52+            const xs_list *file = xs_dict_get(args, "file");
53+            const char *desc    = xs_dict_get(args, "description");
54+
55+            if (xs_is_null(desc))
56+                desc = "";
57+
58+            status = 400;
59+
60+            if (xs_type(file) == XSTYPE_LIST) {
61+                const char *fn = xs_list_get(file, 0);
62+
63+                if (*fn != '\0') {
64+                    char *ext = strrchr(fn, '.');
65+                    xs *hash  = xs_md5_hex(fn, strlen(fn));
66+                    xs *id    = xs_fmt("%s%s", hash, ext);
67+                    xs *url   = xs_fmt("%s/s/%s", snac.actor, id);
68+                    int fo    = xs_number_get(xs_list_get(file, 1));
69+                    int fs    = xs_number_get(xs_list_get(file, 2));
70+
71+                    /* store */
72+                    static_put(&snac, id, payload + fo, fs);
73+
74+                    /* prepare a response */
75+                    xs *rsp = xs_dict_new();
76+
77+                    rsp = xs_dict_append(rsp, "id",          id);
78+                    rsp = xs_dict_append(rsp, "type",        "image");
79+                    rsp = xs_dict_append(rsp, "url",         url);
80+                    rsp = xs_dict_append(rsp, "preview_url", url);
81+                    rsp = xs_dict_append(rsp, "remote_url",  url);
82+                    rsp = xs_dict_append(rsp, "description", desc);
83+
84+                    *body  = xs_json_dumps_pp(rsp, 4);
85+                    *ctype = "application/json";
86+                    status = 200;
87+                }
88+            }
89         }
90         else
91             status = 401;