commit 291f251

default  ·  2023-04-23 06:44:26 +0000 UTC
parent ed4bf22
Added mastoapi follow.
4 files changed,  +96, -35
M data.c
M snac.h
+2, -2
 1@@ -209,7 +209,7 @@ int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
 2 }
 3 
 4 
 5-d_char *get_actor_inbox(snac *snac, char *actor)
 6+d_char *get_actor_inbox(snac *snac, const char *actor)
 7 /* gets an actor's inbox */
 8 {
 9     xs *data = NULL;
10@@ -659,7 +659,7 @@ d_char *msg_delete(snac *snac, char *id)
11 }
12 
13 
14-d_char *msg_follow(snac *snac, char *url_or_uid)
15+xs_dict *msg_follow(snac *snac, const char *url_or_uid)
16 /* creates a 'Follow' message */
17 {
18     xs *actor_o = NULL;
M data.c
+2, -2
 1@@ -1081,7 +1081,7 @@ d_char *_following_fn(snac *snac, const char *actor)
 2 }
 3 
 4 
 5-int following_add(snac *snac, char *actor, char *msg)
 6+int following_add(snac *snac, const char *actor, const xs_dict *msg)
 7 /* adds to the following list */
 8 {
 9     int ret = 201; /* created */
10@@ -1770,7 +1770,7 @@ void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries)
11 }
12 
13 
14-void enqueue_output_by_actor(snac *snac, xs_dict *msg, xs_str *actor, int retries)
15+void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int retries)
16 /* enqueues an output message for an actor */
17 {
18     xs *inbox = get_actor_inbox(snac, actor);
+88, -27
  1@@ -622,6 +622,42 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  2 }
  3 
  4 
  5+xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  6+{
  7+    xs_dict *rel = NULL;
  8+    xs *actor_o  = NULL;
  9+
 10+    if (valid_status(object_get_by_md5(md5, &actor_o))) {
 11+        xs *t   = xs_val_new(XSTYPE_TRUE);
 12+        xs *f   = xs_val_new(XSTYPE_FALSE);
 13+        rel = xs_dict_new();
 14+
 15+        const char *actor = xs_dict_get(actor_o, "id");
 16+
 17+        rel = xs_dict_append(rel, "id",                   md5);
 18+        rel = xs_dict_append(rel, "following",
 19+            following_check(snac, actor) ? t : f);
 20+
 21+        rel = xs_dict_append(rel, "showing_reblogs",      t);
 22+        rel = xs_dict_append(rel, "notifying",            f);
 23+        rel = xs_dict_append(rel, "followed_by",
 24+            follower_check(snac, actor) ? t : f);
 25+
 26+        rel = xs_dict_append(rel, "blocking",
 27+            is_muted(snac, actor) ? t : f);
 28+
 29+        rel = xs_dict_append(rel, "muting",               f);
 30+        rel = xs_dict_append(rel, "muting_notifications", f);
 31+        rel = xs_dict_append(rel, "requested",            f);
 32+        rel = xs_dict_append(rel, "domain_blocking",      f);
 33+        rel = xs_dict_append(rel, "endorsed",             f);
 34+        rel = xs_dict_append(rel, "note",                 "");
 35+    }
 36+
 37+    return rel;
 38+}
 39+
 40+
 41 int process_auth_token(snac *snac, const xs_dict *req)
 42 /* processes an authorization token, if there is one */
 43 {
 44@@ -709,35 +745,12 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 45         if (logged_in) {
 46             xs *res         = xs_list_new();
 47             const char *md5 = xs_dict_get(args, "id[]");
 48-            xs *actor_o     = NULL;
 49-
 50-            if (!xs_is_null(md5) && valid_status(object_get_by_md5(md5, &actor_o))) {
 51-                xs *rel = xs_dict_new();
 52-                xs *t   = xs_val_new(XSTYPE_TRUE);
 53-                xs *f   = xs_val_new(XSTYPE_FALSE);
 54-
 55-                const char *actor = xs_dict_get(actor_o, "id");
 56-
 57-                rel = xs_dict_append(rel, "id",                   md5);
 58-                rel = xs_dict_append(rel, "following",
 59-                    following_check(&snac1, actor) ? t : f);
 60-
 61-                rel = xs_dict_append(rel, "showing_reblogs",      t);
 62-                rel = xs_dict_append(rel, "notifying",            f);
 63-                rel = xs_dict_append(rel, "followed_by",
 64-                    follower_check(&snac1, actor) ? t : f);
 65 
 66-                rel = xs_dict_append(rel, "blocking",
 67-                    is_muted(&snac1, actor) ? t : f);
 68+            if (!xs_is_null(md5)) {
 69+                xs *rel = mastoapi_relationship(&snac1, md5);
 70 
 71-                rel = xs_dict_append(rel, "muting",               f);
 72-                rel = xs_dict_append(rel, "muting_notifications", f);
 73-                rel = xs_dict_append(rel, "requested",            f);
 74-                rel = xs_dict_append(rel, "domain_blocking",      f);
 75-                rel = xs_dict_append(rel, "endorsed",             f);
 76-                rel = xs_dict_append(rel, "note",                 "");
 77-
 78-                res = xs_list_append(res, rel);
 79+                if (rel != NULL)
 80+                    res = xs_list_append(res, rel);
 81             }
 82 
 83             *body  = xs_json_dumps_pp(res, 4);
 84@@ -1595,6 +1608,54 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 85         else
 86             status = 401;
 87     }
 88+    else
 89+    if (xs_startswith(cmd, "/v1/accounts")) {
 90+        if (logged_in) {
 91+            /* account-related information */
 92+            xs *l = xs_split(cmd, "/");
 93+            const char *md5 = xs_list_get(l, 3);
 94+            const char *opt = xs_list_get(l, 4);
 95+            xs *rsp = NULL;
 96+
 97+            if (!xs_is_null(md5) && *md5) {
 98+                xs *actor_o = NULL;
 99+
100+                if (xs_is_null(opt)) {
101+                    /* ? */
102+                }
103+                else
104+                if (strcmp(opt, "follow") == 0) {
105+                    if (valid_status(object_get_by_md5(md5, &actor_o))) {
106+                        const char *actor = xs_dict_get(actor_o, "id");
107+
108+                        xs *msg = msg_follow(&snac, actor);
109+
110+                        if (msg != NULL) {
111+                            /* reload the actor from the message, in may be different */
112+                            actor = xs_dict_get(msg, "object");
113+
114+                            following_add(&snac, actor, msg);
115+
116+                            enqueue_output_by_actor(&snac, msg, actor, 0);
117+
118+                            rsp = mastoapi_relationship(&snac, md5);
119+                        }
120+                    }
121+                }
122+                else
123+                if (strcmp(opt, "unfollow") == 0) {
124+                }
125+            }
126+
127+            if (rsp != NULL) {
128+                *body  = xs_json_dumps_pp(rsp, 4);
129+                *ctype = "application/json";
130+                status = 200;
131+            }
132+        }
133+        else
134+            status = 401;
135+    }
136 
137     /* user cleanup */
138     if (logged_in)
M snac.h
+4, -4
 1@@ -112,7 +112,7 @@ xs_list *timeline_top_level(snac *snac, xs_list *list);
 2 
 3 d_char *local_list(snac *snac, int max);
 4 
 5-int following_add(snac *snac, char *actor, char *msg);
 6+int following_add(snac *snac, const char *actor, const xs_dict *msg);
 7 int following_del(snac *snac, char *actor);
 8 int following_check(snac *snac, const char *actor);
 9 int following_get(snac *snac, char *actor, d_char **data);
10@@ -156,7 +156,7 @@ void enqueue_input(snac *snac, xs_dict *msg, xs_dict *req, int retries);
11 void enqueue_output_raw(const char *keyid, const char *seckey,
12                         xs_dict *msg, xs_str *inbox, int retries);
13 void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries);
14-void enqueue_output_by_actor(snac *snac, xs_dict *msg, xs_str *actor, int retries);
15+void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int retries);
16 void enqueue_email(xs_str *msg, int retries);
17 void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id);
18 void enqueue_message(snac *snac, char *msg);
19@@ -191,7 +191,7 @@ const char *default_avatar_base64(void);
20 
21 d_char *msg_admiration(snac *snac, char *object, char *type);
22 d_char *msg_create(snac *snac, char *object);
23-d_char *msg_follow(snac *snac, char *actor);
24+xs_dict *msg_follow(snac *snac, const char *actor);
25 
26 xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
27                   xs_str *in_reply_to, xs_list *attach, int priv);
28@@ -208,7 +208,7 @@ int send_to_inbox_raw(const char *keyid, const char *seckey,
29                   xs_val **payload, int *p_size, int timeout);
30 int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
31                   xs_val **payload, int *p_size, int timeout);
32-d_char *get_actor_inbox(snac *snac, char *actor);
33+d_char *get_actor_inbox(snac *snac, const char *actor);
34 int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size, int timeout);
35 int is_msg_public(snac *snac, const xs_dict *msg);
36 int is_msg_for_me(snac *snac, const xs_dict *msg);