commit 31b0f75

grunfink  ·  2025-05-18 07:11:40 +0000 UTC
parent 0900f69
mastoapi: added post endpoints for 'authorize' and 'reject' follow requests.
1 files changed,  +51, -0
+51, -0
 1@@ -3244,6 +3244,57 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 2         *ctype = "application/json";
 3         status = HTTP_STATUS_OK;
 4     }
 5+    else
 6+    if (xs_startswith(cmd, "/v1/follow_requests")) { /** **/
 7+        if (logged_in) {
 8+            /* "authorize" or "reject" */
 9+            xs *rel = NULL;
10+            xs *l = xs_split(cmd, "/");
11+            const char *md5 = xs_list_get(l, -2);
12+            const char *s_cmd = xs_list_get(l, -1);
13+
14+            if (xs_is_string(md5) && xs_is_string(s_cmd)) {
15+                xs *actor = NULL;
16+
17+                if (valid_status(object_get_by_md5(md5, &actor))) {
18+                    const char *actor_id = xs_dict_get(actor, "id");
19+
20+                    if (strcmp(s_cmd, "authorize") == 0) {
21+                        xs *fwreq = pending_get(&snac, actor_id);
22+
23+                        if (fwreq != NULL) {
24+                            xs *reply = msg_accept(&snac, fwreq, actor_id);
25+
26+                            enqueue_message(&snac, reply);
27+
28+                            if (xs_is_null(xs_dict_get(fwreq, "published"))) {
29+                                xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
30+                                fwreq = xs_dict_set(fwreq, "published", date);
31+                            }
32+
33+                            timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
34+
35+                            follower_add(&snac, actor_id);
36+
37+                            pending_del(&snac, actor_id);
38+                            rel = mastoapi_relationship(&snac, md5);
39+                        }
40+                    }
41+                    else
42+                    if (strcmp(s_cmd, "reject") == 0) {
43+                        pending_del(&snac, actor_id);
44+                        rel = mastoapi_relationship(&snac, md5);
45+                    }
46+                }
47+            }
48+
49+            if (rel != NULL) {
50+                *body = xs_json_dumps(rel, 4);
51+                *ctype = "application/json";
52+                status = HTTP_STATUS_OK;
53+            }
54+        }
55+    }
56     else
57         status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
58