commit 65040b6

default  ·  2024-01-18 22:04:37 +0000 UTC
parent 1aff468
New function get_atto(), to get the attributedTo field.
5 files changed,  +49, -43
M data.c
M html.c
M snac.h
+36, -8
  1@@ -156,6 +156,34 @@ int actor_request(snac *user, const char *actor, xs_dict **data)
  2 }
  3 
  4 
  5+char *get_atto(const xs_dict *msg)
  6+/* gets the attributedTo field (an actor) */
  7+{
  8+    char *actor = xs_dict_get(msg, "attributedTo");
  9+
 10+    /* if the actor is a list of objects (like on Peertube videos), pick the Person */
 11+    if (xs_type(actor) == XSTYPE_LIST) {
 12+        xs_list *p = actor;
 13+        xs_dict *v;
 14+        actor = NULL;
 15+
 16+        while (actor == NULL && xs_list_iter(&p, &v)) {
 17+            if (xs_type(v) == XSTYPE_DICT) {
 18+                char *type = xs_dict_get(v, "type");
 19+                if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
 20+                    actor = xs_dict_get(v, "id");
 21+
 22+                    if (xs_type(actor) != XSTYPE_STRING)
 23+                        actor = NULL;
 24+                }
 25+            }
 26+        }
 27+    }
 28+
 29+    return actor;
 30+}
 31+
 32+
 33 int timeline_request(snac *snac, char **id, xs_str **wrk, int level)
 34 /* ensures that an entry and its ancestors are in the timeline */
 35 {
 36@@ -203,7 +231,7 @@ int timeline_request(snac *snac, char **id, xs_str **wrk, int level)
 37                 }
 38 
 39                 if (xs_match(type, "Note|Page|Article")) {
 40-                    const char *actor = xs_dict_get(object, "attributedTo");
 41+                    const char *actor = get_atto(object);
 42 
 43                     /* request (and drop) the actor for this entry */
 44                     if (!xs_is_null(actor))
 45@@ -531,7 +559,7 @@ int is_msg_for_me(snac *snac, const xs_dict *c_msg)
 46     }
 47 
 48     /* accept if it's by someone we follow */
 49-    char *atto = xs_dict_get(msg, "attributedTo");
 50+    char *atto = get_atto(msg);
 51 
 52     if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
 53         return 3;
 54@@ -543,7 +571,7 @@ int is_msg_for_me(snac *snac, const xs_dict *c_msg)
 55 
 56         /* try to get the replied message */
 57         if (valid_status(object_get(irt, &r_msg))) {
 58-            atto = xs_dict_get(r_msg, "attributedTo");
 59+            atto = get_atto(r_msg);
 60 
 61             /* accept if the replied message is from someone we follow */
 62             if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
 63@@ -958,7 +986,7 @@ xs_dict *msg_admiration(snac *snac, char *object, char *type)
 64         if (is_msg_public(a_msg))
 65             rcpts = xs_list_append(rcpts, public_address);
 66 
 67-        rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
 68+        rcpts = xs_list_append(rcpts, get_atto(a_msg));
 69 
 70         msg = xs_dict_append(msg, "to", rcpts);
 71     }
 72@@ -1085,7 +1113,7 @@ xs_dict *msg_create(snac *snac, const xs_dict *object)
 73     xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
 74     xs_val *v;
 75 
 76-    if ((v = xs_dict_get(object, "attributedTo")))
 77+    if ((v = get_atto(object)))
 78         msg = xs_dict_append(msg, "attributedTo", v);
 79 
 80     if ((v = xs_dict_get(object, "cc")))
 81@@ -1213,7 +1241,7 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
 82             /* add this author as recipient */
 83             char *a, *v;
 84 
 85-            if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
 86+            if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
 87                 to = xs_list_append(to, a);
 88 
 89             /* add this author to the tag list as a mention */
 90@@ -1447,7 +1475,7 @@ int update_question(snac *user, const char *id)
 91             continue;
 92 
 93         const char *name = xs_dict_get(obj, "name");
 94-        const char *atto = xs_dict_get(obj, "attributedTo");
 95+        const char *atto = get_atto(obj);
 96 
 97         if (name && atto) {
 98             /* get the current count */
 99@@ -1799,7 +1827,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
100             timeline_request(snac, &object, &wrk, 0);
101 
102             if (valid_status(object_get(object, &a_msg))) {
103-                const char *who = xs_dict_get(a_msg, "attributedTo");
104+                const char *who = get_atto(a_msg);
105 
106                 if (who && !is_muted(snac, who)) {
107                     /* bring the actor */
M data.c
+2, -1
 1@@ -2345,7 +2345,8 @@ int was_question_voted(snac *user, const char *id)
 2         xs *obj = NULL;
 3 
 4         if (valid_status(object_get_by_md5(md5, &obj))) {
 5-            if (strcmp(xs_dict_get(obj, "attributedTo"), user->actor) == 0 &&
 6+            char *atto = get_atto(obj);
 7+            if (atto && strcmp(atto, user->actor) == 0 &&
 8                 !xs_is_null(xs_dict_get(obj, "name"))) {
 9                 voted = 1;
10                 break;
M html.c
+3, -28
 1@@ -1275,35 +1275,10 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local,
 2     if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
 3         return NULL;
 4 
 5-    /* bring the main actor */
 6-    if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
 7+    /* get the attributedTo */
 8+    if ((actor = get_atto(msg)) == NULL)
 9         return NULL;
10 
11-    /* if the actor is a list (like on Peertube videos), pick the Person */
12-    if (xs_type(actor) == XSTYPE_LIST) {
13-        char *e_actor = NULL;
14-        xs_list *p = actor;
15-
16-        while (xs_list_iter(&p, &v)) {
17-            if (xs_type(v) == XSTYPE_DICT) {
18-                char *type = xs_dict_get(v, "type");
19-                if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
20-                    e_actor = xs_dict_get(v, "id");
21-
22-                    if (xs_type(e_actor) == XSTYPE_STRING)
23-                        break;
24-                    else
25-                        e_actor = NULL;
26-                }
27-            }
28-        }
29-
30-        if (e_actor != NULL)
31-            actor = e_actor;
32-        else
33-            return NULL;
34-    }
35-
36     /* ignore muted morons immediately */
37     if (user && is_muted(user, actor))
38         return NULL;
39@@ -1979,7 +1954,7 @@ xs_str *html_timeline(snac *user, const xs_list *list, int local,
40 
41         /* if it's an instance page, discard private users */
42         if (user == NULL && xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
43-            const char *atto = xs_dict_get(msg, "attributedTo");
44+            const char *atto = get_atto(msg);
45             xs *l = xs_split(atto, "/");
46             const char *uid = xs_list_get(l, -1);
47             snac user;
+6, -6
 1@@ -701,7 +701,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
 2 /* converts an ActivityPub note to a Mastodon status */
 3 {
 4     xs *actor = NULL;
 5-    actor_get(xs_dict_get(msg, "attributedTo"), &actor);
 6+    actor_get(get_atto(msg), &actor);
 7 
 8     /* if the author is not here, discard */
 9     if (actor == NULL)
10@@ -967,7 +967,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
11             st = xs_dict_set(st, "in_reply_to_id", irt_mid);
12 
13             char *at = NULL;
14-            if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
15+            if (!xs_is_null(at = get_atto(irto))) {
16                 xs *at_md5 = xs_md5_hex(at, strlen(at));
17                 st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
18             }
19@@ -1443,7 +1443,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
20                     from = xs_dict_get(msg, "audience");
21 
22                 if (from == NULL)
23-                    from = xs_dict_get(msg, "attributedTo");
24+                    from = get_atto(msg);
25 
26                 if (from == NULL)
27                     continue;
28@@ -1525,7 +1525,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
29 
30             /* discard private users */
31             {
32-                const char *atto = xs_dict_get(msg, "attributedTo");
33+                const char *atto = get_atto(msg);
34                 xs *l = xs_split(atto, "/");
35                 const char *uid = xs_list_get(l, -1);
36                 snac p_user;
37@@ -1825,7 +1825,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
38 
39                 if (valid_status(object_get_by_md5(id, &msg))) {
40                     if (op == NULL) {
41-                        if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
42+                        if (!is_muted(&snac1, get_atto(msg))) {
43                             /* return the status itself */
44                             out = mastoapi_status(&snac1, msg);
45                         }
46@@ -2435,7 +2435,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
47 
48                 if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
49                     const char *id   = xs_dict_get(msg, "id");
50-                    const char *atto = xs_dict_get(msg, "attributedTo");
51+                    const char *atto = get_atto(msg);
52 
53                     xs_list *opts = xs_dict_get(msg, "oneOf");
54                     if (opts == NULL)
M snac.h
+2, -0
1@@ -245,6 +245,8 @@ const char *default_avatar_base64(void);
2 
3 xs_str *process_tags(snac *snac, const char *content, xs_list **tag);
4 
5+char *get_atto(const xs_dict *msg);
6+
7 xs_dict *msg_admiration(snac *snac, char *object, char *type);
8 xs_dict *msg_create(snac *snac, const xs_dict *object);
9 xs_dict *msg_follow(snac *snac, const char *actor);