commit db1bc85

default  ·  2023-05-31 08:49:08 +0000 UTC
parent 5aa9043
Votes are shown in mastoapi.
1 files changed,  +65, -2
+65, -2
 1@@ -803,6 +803,67 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
 2 
 3     st = xs_dict_append(st, "edited_at", tmp);
 4 
 5+    /* is it a poll? */
 6+    if (strcmp(xs_dict_get(msg, "type"), "Question") == 0) {
 7+        xs *poll = xs_dict_new();
 8+        xs_list *opts = NULL;
 9+        xs_list *p;
10+        xs_val *v;
11+        int num_votes = 0;
12+        xs *options = xs_list_new();
13+
14+        poll = xs_dict_append(poll, "id", id);
15+        poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
16+        poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f);
17+
18+        if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
19+            poll = xs_dict_append(poll, "multiple", f);
20+        else {
21+            opts = xs_dict_get(msg, "anyOf");
22+            poll = xs_dict_append(poll, "multiple", t);
23+        }
24+
25+        while (xs_list_iter(&opts, &v)) {
26+            const char *title   = xs_dict_get(v, "name");
27+            const char *replies = xs_dict_get(v, "replies");
28+
29+            if (title && replies) {
30+                const char *votes_count = xs_dict_get(replies, "totalItems");
31+
32+                if (xs_type(votes_count) == XSTYPE_NUMBER) {
33+                    xs *d = xs_dict_new();
34+                    d = xs_dict_append(d, "title",  title);
35+                    d = xs_dict_append(d, "votes_count", votes_count);
36+
37+                    options = xs_list_append(options, d);
38+                    num_votes += xs_number_get(votes_count);
39+                }
40+            }
41+        }
42+
43+        poll = xs_dict_append(poll, "options", options);
44+        xs *vc = xs_number_new(num_votes);
45+        poll = xs_dict_append(poll, "votes_count", vc);
46+
47+        xs *children = object_children(id);
48+        int voted = 0;
49+        p = children;
50+        while (xs_list_iter(&p, &v)) {
51+            xs *obj = NULL;
52+
53+            if (valid_status(object_get_by_md5(v, &obj))) {
54+                if (strcmp(xs_dict_get(obj, "attributedTo"), snac->actor) == 0) {
55+                    voted = 1;
56+                    break;
57+                }
58+            }
59+        }
60+
61+        poll = xs_dict_append(poll, "voted", voted ? t : f);
62+
63+        st = xs_dict_append(st, "poll", poll);
64+    }
65+
66     return st;
67 }
68 
69@@ -1148,7 +1209,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
70                     continue;
71 
72                 /* discard non-Notes */
73-                if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
74+                const char *type = xs_dict_get(msg, "type");
75+                if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
76                     continue;
77 
78                 /* discard notes from muted morons */
79@@ -1214,7 +1276,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
80                 continue;
81 
82             /* discard non-Notes */
83-            if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
84+            const char *type = xs_dict_get(msg, "type");
85+            if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
86                 continue;
87 
88             /* convert the Note into a Mastodon status */