diff options
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 67 |
1 files changed, 65 insertions, 2 deletions
| @@ -803,6 +803,67 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 803 | 803 | ||
| 804 | st = xs_dict_append(st, "edited_at", tmp); | 804 | st = xs_dict_append(st, "edited_at", tmp); |
| 805 | 805 | ||
| 806 | /* is it a poll? */ | ||
| 807 | if (strcmp(xs_dict_get(msg, "type"), "Question") == 0) { | ||
| 808 | xs *poll = xs_dict_new(); | ||
| 809 | xs_list *opts = NULL; | ||
| 810 | xs_list *p; | ||
| 811 | xs_val *v; | ||
| 812 | int num_votes = 0; | ||
| 813 | xs *options = xs_list_new(); | ||
| 814 | |||
| 815 | poll = xs_dict_append(poll, "id", id); | ||
| 816 | poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime")); | ||
| 817 | poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f); | ||
| 818 | |||
| 819 | if ((opts = xs_dict_get(msg, "oneOf")) != NULL) | ||
| 820 | poll = xs_dict_append(poll, "multiple", f); | ||
| 821 | else { | ||
| 822 | opts = xs_dict_get(msg, "anyOf"); | ||
| 823 | poll = xs_dict_append(poll, "multiple", t); | ||
| 824 | } | ||
| 825 | |||
| 826 | while (xs_list_iter(&opts, &v)) { | ||
| 827 | const char *title = xs_dict_get(v, "name"); | ||
| 828 | const char *replies = xs_dict_get(v, "replies"); | ||
| 829 | |||
| 830 | if (title && replies) { | ||
| 831 | const char *votes_count = xs_dict_get(replies, "totalItems"); | ||
| 832 | |||
| 833 | if (xs_type(votes_count) == XSTYPE_NUMBER) { | ||
| 834 | xs *d = xs_dict_new(); | ||
| 835 | d = xs_dict_append(d, "title", title); | ||
| 836 | d = xs_dict_append(d, "votes_count", votes_count); | ||
| 837 | |||
| 838 | options = xs_list_append(options, d); | ||
| 839 | num_votes += xs_number_get(votes_count); | ||
| 840 | } | ||
| 841 | } | ||
| 842 | } | ||
| 843 | |||
| 844 | poll = xs_dict_append(poll, "options", options); | ||
| 845 | xs *vc = xs_number_new(num_votes); | ||
| 846 | poll = xs_dict_append(poll, "votes_count", vc); | ||
| 847 | |||
| 848 | xs *children = object_children(id); | ||
| 849 | int voted = 0; | ||
| 850 | p = children; | ||
| 851 | while (xs_list_iter(&p, &v)) { | ||
| 852 | xs *obj = NULL; | ||
| 853 | |||
| 854 | if (valid_status(object_get_by_md5(v, &obj))) { | ||
| 855 | if (strcmp(xs_dict_get(obj, "attributedTo"), snac->actor) == 0) { | ||
| 856 | voted = 1; | ||
| 857 | break; | ||
| 858 | } | ||
| 859 | } | ||
| 860 | } | ||
| 861 | |||
| 862 | poll = xs_dict_append(poll, "voted", voted ? t : f); | ||
| 863 | |||
| 864 | st = xs_dict_append(st, "poll", poll); | ||
| 865 | } | ||
| 866 | |||
| 806 | return st; | 867 | return st; |
| 807 | } | 868 | } |
| 808 | 869 | ||
| @@ -1148,7 +1209,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1148 | continue; | 1209 | continue; |
| 1149 | 1210 | ||
| 1150 | /* discard non-Notes */ | 1211 | /* discard non-Notes */ |
| 1151 | if (strcmp(xs_dict_get(msg, "type"), "Note") != 0) | 1212 | const char *type = xs_dict_get(msg, "type"); |
| 1213 | if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0) | ||
| 1152 | continue; | 1214 | continue; |
| 1153 | 1215 | ||
| 1154 | /* discard notes from muted morons */ | 1216 | /* discard notes from muted morons */ |
| @@ -1214,7 +1276,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1214 | continue; | 1276 | continue; |
| 1215 | 1277 | ||
| 1216 | /* discard non-Notes */ | 1278 | /* discard non-Notes */ |
| 1217 | if (strcmp(xs_dict_get(msg, "type"), "Note") != 0) | 1279 | const char *type = xs_dict_get(msg, "type"); |
| 1280 | if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0) | ||
| 1218 | continue; | 1281 | continue; |
| 1219 | 1282 | ||
| 1220 | /* convert the Note into a Mastodon status */ | 1283 | /* convert the Note into a Mastodon status */ |