Browse Source

Added mastoapi follow.

default 3 years ago
parent
commit
291f251ced
4 changed files with 96 additions and 35 deletions
  1. 2 2
      activitypub.c
  2. 2 2
      data.c
  3. 88 27
      mastoapi.c
  4. 4 4
      snac.h

+ 2 - 2
activitypub.c

@@ -209,7 +209,7 @@ int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
 }
 
 
-d_char *get_actor_inbox(snac *snac, char *actor)
+d_char *get_actor_inbox(snac *snac, const char *actor)
 /* gets an actor's inbox */
 {
     xs *data = NULL;
@@ -659,7 +659,7 @@ d_char *msg_delete(snac *snac, char *id)
 }
 
 
-d_char *msg_follow(snac *snac, char *url_or_uid)
+xs_dict *msg_follow(snac *snac, const char *url_or_uid)
 /* creates a 'Follow' message */
 {
     xs *actor_o = NULL;

+ 2 - 2
data.c

@@ -1081,7 +1081,7 @@ d_char *_following_fn(snac *snac, const char *actor)
 }
 
 
-int following_add(snac *snac, char *actor, char *msg)
+int following_add(snac *snac, const char *actor, const xs_dict *msg)
 /* adds to the following list */
 {
     int ret = 201; /* created */
@@ -1770,7 +1770,7 @@ void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries)
 }
 
 
-void enqueue_output_by_actor(snac *snac, xs_dict *msg, xs_str *actor, int retries)
+void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int retries)
 /* enqueues an output message for an actor */
 {
     xs *inbox = get_actor_inbox(snac, actor);

+ 88 - 27
mastoapi.c

@@ -622,6 +622,42 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
 }
 
 
+xs_dict *mastoapi_relationship(snac *snac, const char *md5)
+{
+    xs_dict *rel = NULL;
+    xs *actor_o  = NULL;
+
+    if (valid_status(object_get_by_md5(md5, &actor_o))) {
+        xs *t   = xs_val_new(XSTYPE_TRUE);
+        xs *f   = xs_val_new(XSTYPE_FALSE);
+        rel = xs_dict_new();
+
+        const char *actor = xs_dict_get(actor_o, "id");
+
+        rel = xs_dict_append(rel, "id",                   md5);
+        rel = xs_dict_append(rel, "following",
+            following_check(snac, actor) ? t : f);
+
+        rel = xs_dict_append(rel, "showing_reblogs",      t);
+        rel = xs_dict_append(rel, "notifying",            f);
+        rel = xs_dict_append(rel, "followed_by",
+            follower_check(snac, actor) ? t : f);
+
+        rel = xs_dict_append(rel, "blocking",
+            is_muted(snac, actor) ? t : f);
+
+        rel = xs_dict_append(rel, "muting",               f);
+        rel = xs_dict_append(rel, "muting_notifications", f);
+        rel = xs_dict_append(rel, "requested",            f);
+        rel = xs_dict_append(rel, "domain_blocking",      f);
+        rel = xs_dict_append(rel, "endorsed",             f);
+        rel = xs_dict_append(rel, "note",                 "");
+    }
+
+    return rel;
+}
+
+
 int process_auth_token(snac *snac, const xs_dict *req)
 /* processes an authorization token, if there is one */
 {
@@ -709,35 +745,12 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
         if (logged_in) {
             xs *res         = xs_list_new();
             const char *md5 = xs_dict_get(args, "id[]");
-            xs *actor_o     = NULL;
-
-            if (!xs_is_null(md5) && valid_status(object_get_by_md5(md5, &actor_o))) {
-                xs *rel = xs_dict_new();
-                xs *t   = xs_val_new(XSTYPE_TRUE);
-                xs *f   = xs_val_new(XSTYPE_FALSE);
-
-                const char *actor = xs_dict_get(actor_o, "id");
-
-                rel = xs_dict_append(rel, "id",                   md5);
-                rel = xs_dict_append(rel, "following",
-                    following_check(&snac1, actor) ? t : f);
-
-                rel = xs_dict_append(rel, "showing_reblogs",      t);
-                rel = xs_dict_append(rel, "notifying",            f);
-                rel = xs_dict_append(rel, "followed_by",
-                    follower_check(&snac1, actor) ? t : f);
 
-                rel = xs_dict_append(rel, "blocking",
-                    is_muted(&snac1, actor) ? t : f);
+            if (!xs_is_null(md5)) {
+                xs *rel = mastoapi_relationship(&snac1, md5);
 
-                rel = xs_dict_append(rel, "muting",               f);
-                rel = xs_dict_append(rel, "muting_notifications", f);
-                rel = xs_dict_append(rel, "requested",            f);
-                rel = xs_dict_append(rel, "domain_blocking",      f);
-                rel = xs_dict_append(rel, "endorsed",             f);
-                rel = xs_dict_append(rel, "note",                 "");
-
-                res = xs_list_append(res, rel);
+                if (rel != NULL)
+                    res = xs_list_append(res, rel);
             }
 
             *body  = xs_json_dumps_pp(res, 4);
@@ -1595,6 +1608,54 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
         else
             status = 401;
     }
+    else
+    if (xs_startswith(cmd, "/v1/accounts")) {
+        if (logged_in) {
+            /* account-related information */
+            xs *l = xs_split(cmd, "/");
+            const char *md5 = xs_list_get(l, 3);
+            const char *opt = xs_list_get(l, 4);
+            xs *rsp = NULL;
+
+            if (!xs_is_null(md5) && *md5) {
+                xs *actor_o = NULL;
+
+                if (xs_is_null(opt)) {
+                    /* ? */
+                }
+                else
+                if (strcmp(opt, "follow") == 0) {
+                    if (valid_status(object_get_by_md5(md5, &actor_o))) {
+                        const char *actor = xs_dict_get(actor_o, "id");
+
+                        xs *msg = msg_follow(&snac, actor);
+
+                        if (msg != NULL) {
+                            /* reload the actor from the message, in may be different */
+                            actor = xs_dict_get(msg, "object");
+
+                            following_add(&snac, actor, msg);
+
+                            enqueue_output_by_actor(&snac, msg, actor, 0);
+
+                            rsp = mastoapi_relationship(&snac, md5);
+                        }
+                    }
+                }
+                else
+                if (strcmp(opt, "unfollow") == 0) {
+                }
+            }
+
+            if (rsp != NULL) {
+                *body  = xs_json_dumps_pp(rsp, 4);
+                *ctype = "application/json";
+                status = 200;
+            }
+        }
+        else
+            status = 401;
+    }
 
     /* user cleanup */
     if (logged_in)

+ 4 - 4
snac.h

@@ -112,7 +112,7 @@ xs_list *timeline_top_level(snac *snac, xs_list *list);
 
 d_char *local_list(snac *snac, int max);
 
-int following_add(snac *snac, char *actor, char *msg);
+int following_add(snac *snac, const char *actor, const xs_dict *msg);
 int following_del(snac *snac, char *actor);
 int following_check(snac *snac, const char *actor);
 int following_get(snac *snac, char *actor, d_char **data);
@@ -156,7 +156,7 @@ void enqueue_input(snac *snac, xs_dict *msg, xs_dict *req, int retries);
 void enqueue_output_raw(const char *keyid, const char *seckey,
                         xs_dict *msg, xs_str *inbox, int retries);
 void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries);
-void enqueue_output_by_actor(snac *snac, xs_dict *msg, xs_str *actor, int retries);
+void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int retries);
 void enqueue_email(xs_str *msg, int retries);
 void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id);
 void enqueue_message(snac *snac, char *msg);
@@ -191,7 +191,7 @@ const char *default_avatar_base64(void);
 
 d_char *msg_admiration(snac *snac, char *object, char *type);
 d_char *msg_create(snac *snac, char *object);
-d_char *msg_follow(snac *snac, char *actor);
+xs_dict *msg_follow(snac *snac, const char *actor);
 
 xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
                   xs_str *in_reply_to, xs_list *attach, int priv);
@@ -208,7 +208,7 @@ int send_to_inbox_raw(const char *keyid, const char *seckey,
                   xs_val **payload, int *p_size, int timeout);
 int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
                   xs_val **payload, int *p_size, int timeout);
-d_char *get_actor_inbox(snac *snac, char *actor);
+d_char *get_actor_inbox(snac *snac, const char *actor);
 int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size, int timeout);
 int is_msg_public(snac *snac, const xs_dict *msg);
 int is_msg_for_me(snac *snac, const xs_dict *msg);