commit 8551c69

default  ·  2023-05-29 06:27:16 +0000 UTC
parent bf96512
New function update_question().
2 files changed,  +77, -1
M snac.h
+76, -0
 1@@ -967,6 +967,82 @@ xs_dict *msg_question(snac *user, const char *content, const xs_list *opts, int
 2 }
 3 
 4 
 5+int update_question(snac *user, const char *id)
 6+/* updates the poll counts */
 7+{
 8+    xs *msg  = NULL;
 9+    xs *rcnt = xs_dict_new();
10+    xs *z    = xs_number_new(0);
11+    xs *chld = NULL;
12+    xs_list *opts;
13+    xs_val *p;
14+    xs_str *k;
15+    xs_val *v;
16+
17+    /* get the object */
18+    if (!valid_status(object_get(id, &msg)))
19+        return -1;
20+
21+    /* get the options */
22+    if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
23+        (opts = xs_dict_get(msg, "anyOf")) == NULL)
24+        return -2;
25+
26+    /* fill the initial count */
27+    p = opts;
28+    while (xs_list_iter(&p, &v)) {
29+        const char *name = xs_dict_get(v, "name");
30+        if (name)
31+            rcnt = xs_dict_set(rcnt, name, z);
32+    }
33+
34+    /* iterate now the children (the votes) */
35+    chld = object_children(id);
36+    p = chld;
37+    while (xs_list_iter(&p, &v)) {
38+        const char *name = xs_dict_get(v, "name");
39+        if (name) {
40+            /* get the current count */
41+            const xs_number *cnt = xs_dict_get(rcnt, "name");
42+
43+            if (xs_type(cnt) == XSTYPE_NUMBER) {
44+                /* if it exists, increment */
45+                xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
46+                rcnt = xs_dict_set(rcnt, "name", ucnt);
47+            }
48+        }
49+    }
50+
51+    /* create a new list of options with their new counts */
52+    xs *nopts = xs_list_new();
53+    p = rcnt;
54+    while (xs_dict_iter(&p, &k, &v)) {
55+        xs *d1 = xs_dict_new();
56+        xs *d2 = xs_dict_new();
57+
58+        d2 = xs_dict_append(d2, "type",       "Collection");
59+        d2 = xs_dict_append(d2, "totalItems", v);
60+
61+        d1 = xs_dict_append(d1, "type",    "Note");
62+        d1 = xs_dict_append(d1, "name",    k);
63+        d1 = xs_dict_append(d1, "replies", d2);
64+
65+        nopts = xs_list_append(nopts, d1);
66+    }
67+
68+    /* update the list */
69+    msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
70+
71+    /* store */
72+    object_add_ow(id, msg);
73+
74+    snac_debug(user, 1, xs_fmt("recounted poll %s", id));
75+    timeline_touch(user);
76+
77+    return 0;
78+}
79+
80+
81 void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg)
82 /* notifies the user of relevant events */
83 {
M snac.h
+1, -1
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.33"
6+#define VERSION "2.34-dev"
7 
8 #define USER_AGENT "snac/" VERSION
9