commit e146c2a

default  ·  2023-04-12 18:20:46 +0000 UTC
parent 5366bd1
Also return (some) information for non-local users.
1 files changed,  +47, -29
+47, -29
  1@@ -412,7 +412,7 @@ xs_str *mastoapi_id(const xs_dict *msg)
  2 #define MID_TO_MD5(id) (id + 10)
  3 
  4 
  5-xs_dict *mastoapi_account(snac *snac, const xs_dict *actor)
  6+xs_dict *mastoapi_account(const xs_dict *actor)
  7 /* converts an ActivityPub actor to a Mastodon account */
  8 {
  9     xs_dict *acct = xs_dict_new();
 10@@ -459,7 +459,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
 11     if (actor == NULL)
 12         return NULL;
 13 
 14-    xs *acct = mastoapi_account(snac, actor);
 15+    xs *acct = mastoapi_account(actor);
 16 
 17     /** shave the yak converting an ActivityPub Note to a Mastodon status **/
 18 
 19@@ -682,38 +682,58 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 20         xs *l = xs_split(cmd, "/");
 21         const char *uid = xs_list_get(l, 2);
 22         const char *opt = xs_list_get(l, 3);
 23-        snac snac2;
 24 
 25-        if (uid != NULL && user_open(&snac2, uid)) {
 26-            xs *out = NULL;
 27-
 28-            if (opt == NULL) {
 29-                /* account information */
 30-                xs *actor = msg_actor(&snac2);
 31-                out = mastoapi_account(&snac2, actor);
 32-            }
 33-            else
 34-            if (strcmp(opt, "statuses") == 0) {
 35-                /* the public list of posts of a user */
 36-                xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
 37-                xs_list *p   = timeline;
 38-                xs_str *v;
 39+        if (uid != NULL) {
 40+            snac snac2;
 41+            xs *out   = NULL;
 42+            xs *actor = NULL;
 43+
 44+            /* is it a local user? */
 45+            if (user_open(&snac2, uid)) {
 46+                if (opt == NULL) {
 47+                    /* account information */
 48+                    actor = msg_actor(&snac2);
 49+                    out   = mastoapi_account(actor);
 50+                }
 51+                else
 52+                if (strcmp(opt, "statuses") == 0) {
 53+                    /* the public list of posts of a user */
 54+                    xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
 55+                    xs_list *p   = timeline;
 56+                    xs_str *v;
 57 
 58-                out = xs_list_new();
 59+                    out = xs_list_new();
 60 
 61-                while (xs_list_iter(&p, &v)) {
 62-                    xs *msg = NULL;
 63+                    while (xs_list_iter(&p, &v)) {
 64+                        xs *msg = NULL;
 65 
 66-                    if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
 67-                        /* add only posts by the author */
 68-                        if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
 69-                            xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
 70-                            xs *st = mastoapi_status(&snac2, msg);
 71+                        if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
 72+                            /* add only posts by the author */
 73+                            if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
 74+                                xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
 75+                                xs *st = mastoapi_status(&snac2, msg);
 76 
 77-                            out = xs_list_append(out, st);
 78+                                out = xs_list_append(out, st);
 79+                            }
 80                         }
 81                     }
 82                 }
 83+
 84+                user_free(&snac2);
 85+            }
 86+            else {
 87+                /* try the uid as the md5 of a possibly loaded actor */
 88+                if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
 89+                    if (opt == NULL) {
 90+                        /* account information */
 91+                        out = mastoapi_account(actor);
 92+                    }
 93+                    else
 94+                    if (strcmp(opt, "statuses") == 0) {
 95+                        /* we don't serve statuses of others; return the empty list */
 96+                        out = xs_list_new();
 97+                    }
 98+                }
 99             }
100 
101             if (out != NULL) {
102@@ -721,8 +741,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
103                 *ctype = "application/json";
104                 status = 200;
105             }
106-
107-            user_free(&snac2);
108         }
109     }
110     else
111@@ -993,7 +1011,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
112                         xs *actor2 = NULL;
113 
114                         if (valid_status(object_get_by_md5(v, &actor2))) {
115-                            xs *acct2 = mastoapi_account(&snac1, actor2);
116+                            xs *acct2 = mastoapi_account(actor2);
117 
118                             out = xs_list_append(out, acct2);
119                         }