commit 140247f

David Vaughan  ·  2026-02-05 02:37:26 +0000 UTC
parent 79cf2ca
Add poll creation to mastoapi
1 files changed,  +37, -9
+37, -9
 1@@ -3258,12 +3258,16 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 2     if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
 3         if (logged_in) {
 4             /* post a new Note */
 5-            const char *content    = xs_dict_get(args, "status");
 6-            const char *mid        = xs_dict_get(args, "in_reply_to_id");
 7-            const char *visibility = xs_dict_get(args, "visibility");
 8-            const char *summary    = xs_dict_get(args, "spoiler_text");
 9-            const char *media_ids  = xs_dict_get(args, "media_ids");
10-            const char *language   = xs_dict_get(args, "language");
11+            const char *content          = xs_dict_get(args, "status");
12+            const char *mid              = xs_dict_get(args, "in_reply_to_id");
13+            const char *visibility       = xs_dict_get(args, "visibility");
14+            const char *summary          = xs_dict_get(args, "spoiler_text");
15+            const char *poll_opts        = xs_dict_get(args, "poll[options][]");
16+            const char *poll_end_secs    = xs_dict_get(args, "poll[expires_in]");
17+            const char *poll_multiple    = xs_dict_get(args, "poll[multiple]");
18+            // const char *poll_hide_totals = xs_dict_get(args, "poll[hide_totals]");
19+            const char *media_ids        = xs_dict_get(args, "media_ids");
20+            const char *language         = xs_dict_get(args, "language");
21 
22             if (xs_is_null(media_ids))
23                 media_ids = xs_dict_get(args, "media_ids[]");
24@@ -3286,8 +3290,8 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
25                     irt = xs_dup(xs_dict_get(r_msg, "id"));
26             }
27 
28-            /* does it have attachments? */
29-            if (!xs_is_null(media_ids)) {
30+            /* does it have attachments (and no poll)? */
31+            if (!xs_is_null(media_ids) && xs_is_null(poll_end_secs) && xs_is_null(poll_opts)) {
32                 xs *mi = NULL;
33 
34                 if (xs_type(media_ids) == XSTYPE_LIST)
35@@ -3313,6 +3317,8 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
36             }
37 
38             /* prepare the message */
39+            xs *msg = NULL;
40+
41             int scope = SCOPE_MENTIONED;
42             if (strcmp(visibility, "unlisted") == 0)
43                 scope = SCOPE_UNLISTED;
44@@ -3323,7 +3329,29 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
45             if (strcmp(visibility, "private") == 0)
46                 scope = SCOPE_FOLLOWERS;
47 
48-            xs *msg = msg_note(&snac, content, NULL, irt, attach_list, scope, language, NULL);
49+            /* does it have a poll? */
50+            if (!xs_is_null(poll_opts) && !xs_is_null(poll_end_secs)) {
51+                xs *po = NULL;
52+                int end_secs = atoi(poll_end_secs);
53+                int multiple = 0;
54+
55+                if (xs_type(poll_opts) == XSTYPE_LIST)
56+                    po = xs_dup(poll_opts);
57+                else {
58+                    po = xs_list_new();
59+                    po = xs_list_append(po, poll_opts);
60+                }
61+
62+                if (!xs_is_null(poll_multiple) && strcmp(poll_multiple, "true") == 0)
63+                    multiple = 1;
64+
65+                msg = msg_question(&snac, content, attach_list,
66+                                   poll_opts, multiple, end_secs);
67+
68+                enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
69+            }
70+            else
71+                msg = msg_note(&snac, content, NULL, irt, attach_list, scope, language, NULL);
72 
73             if (!xs_is_null(summary) && *summary) {
74                 msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));