commit 88850bd

default  ·  2023-04-23 04:05:35 +0000 UTC
parent 5b93e90
Attend mastoapi relationships.

Now, the follow/unfollow buttons appear on each account's page.
3 files changed,  +48, -13
M data.c
M snac.h
M data.c
+6, -6
 1@@ -1074,7 +1074,7 @@ d_char *timeline_list(snac *snac, const char *idx_name, int skip, int show)
 2    with a link to a cached author, because we need the Follow object
 3    in case we need to unfollow (Undo + original Follow) */
 4 
 5-d_char *_following_fn(snac *snac, char *actor)
 6+d_char *_following_fn(snac *snac, const char *actor)
 7 {
 8     xs *md5 = xs_md5_hex(actor, strlen(actor));
 9     return xs_fmt("%s/following/%s.json", snac->basedir, md5);
10@@ -1116,7 +1116,7 @@ int following_del(snac *snac, char *actor)
11 }
12 
13 
14-int following_check(snac *snac, char *actor)
15+int following_check(snac *snac, const char *actor)
16 /* checks if we are following this actor */
17 {
18     xs *fn = _following_fn(snac, actor);
19@@ -1185,14 +1185,14 @@ d_char *following_list(snac *snac)
20 }
21 
22 
23-d_char *_muted_fn(snac *snac, char *actor)
24+d_char *_muted_fn(snac *snac, const char *actor)
25 {
26     xs *md5 = xs_md5_hex(actor, strlen(actor));
27     return xs_fmt("%s/muted/%s", snac->basedir, md5);
28 }
29 
30 
31-void mute(snac *snac, char *actor)
32+void mute(snac *snac, const char *actor)
33 /* mutes a moron */
34 {
35     xs *fn = _muted_fn(snac, actor);
36@@ -1207,7 +1207,7 @@ void mute(snac *snac, char *actor)
37 }
38 
39 
40-void unmute(snac *snac, char *actor)
41+void unmute(snac *snac, const char *actor)
42 /* actor is no longer a moron */
43 {
44     xs *fn = _muted_fn(snac, actor);
45@@ -1218,7 +1218,7 @@ void unmute(snac *snac, char *actor)
46 }
47 
48 
49-int is_muted(snac *snac, char *actor)
50+int is_muted(snac *snac, const char *actor)
51 /* check if someone is muted */
52 {
53     xs *fn = _muted_fn(snac, actor);
+37, -2
 1@@ -705,12 +705,47 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 2     if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
 3         /* find if an account is followed, blocked, etc. */
 4         /* the account to get relationships about is in args "id[]" */
 5-        /* dummy by now */
 6+
 7         if (logged_in) {
 8-            *body  = xs_dup("[]");
 9+            xs *res         = xs_list_new();
10+            const char *md5 = xs_dict_get(args, "id[]");
11+            xs *actor_o     = NULL;
12+
13+            if (!xs_is_null(md5) && valid_status(object_get_by_md5(md5, &actor_o))) {
14+                xs *rel = xs_dict_new();
15+                xs *t   = xs_val_new(XSTYPE_TRUE);
16+                xs *f   = xs_val_new(XSTYPE_FALSE);
17+
18+                const char *actor = xs_dict_get(actor_o, "id");
19+
20+                rel = xs_dict_append(rel, "id",                   md5);
21+                rel = xs_dict_append(rel, "following",
22+                    following_check(&snac1, actor) ? t : f);
23+
24+                rel = xs_dict_append(rel, "showing_reblogs",      t);
25+                rel = xs_dict_append(rel, "notifying",            f);
26+                rel = xs_dict_append(rel, "followed_by",
27+                    follower_check(&snac1, actor) ? t : f);
28+
29+                rel = xs_dict_append(rel, "blocking",
30+                    is_muted(&snac1, actor) ? t : f);
31+
32+                rel = xs_dict_append(rel, "muting",               f);
33+                rel = xs_dict_append(rel, "muting_notifications", f);
34+                rel = xs_dict_append(rel, "requested",            f);
35+                rel = xs_dict_append(rel, "domain_blocking",      f);
36+                rel = xs_dict_append(rel, "endorsed",             f);
37+                rel = xs_dict_append(rel, "note",                 "");
38+
39+                res = xs_list_append(res, rel);
40+            }
41+
42+            *body  = xs_json_dumps_pp(res, 4);
43             *ctype = "application/json";
44             status = 200;
45         }
46+        else
47+            status = 422;
48     }
49     else
50     if (xs_startswith(cmd, "/v1/accounts/")) {
M snac.h
+5, -5
 1@@ -1,7 +1,7 @@
 2 /* snac - A simple, minimalistic ActivityPub instance */
 3 /* copyright (c) 2022 - 2023 grunfink / MIT license */
 4 
 5-#define VERSION "2.28"
 6+#define VERSION "2.29-dev"
 7 
 8 #define USER_AGENT "snac/" VERSION
 9 
10@@ -114,13 +114,13 @@ d_char *local_list(snac *snac, int max);
11 
12 int following_add(snac *snac, char *actor, char *msg);
13 int following_del(snac *snac, char *actor);
14-int following_check(snac *snac, char *actor);
15+int following_check(snac *snac, const char *actor);
16 int following_get(snac *snac, char *actor, d_char **data);
17 d_char *following_list(snac *snac);
18 
19-void mute(snac *snac, char *actor);
20-void unmute(snac *snac, char *actor);
21-int is_muted(snac *snac, char *actor);
22+void mute(snac *snac, const char *actor);
23+void unmute(snac *snac, const char *actor);
24+int is_muted(snac *snac, const char *actor);
25 
26 void hide(snac *snac, const char *id);
27 int is_hidden(snac *snac, const char *id);