diff options
| author | 2023-05-31 18:35:50 +0200 | |
|---|---|---|
| committer | 2023-05-31 18:35:50 +0200 | |
| commit | 796a111de81df9e7c8bc2e72947f27e97fa85dae (patch) | |
| tree | b17d25f6625e7a84ceaac9c5c60d6d9958de7c7e | |
| parent | Started voting on mastoapi (non-working). (diff) | |
| download | snac2-796a111de81df9e7c8bc2e72947f27e97fa85dae.tar.gz snac2-796a111de81df9e7c8bc2e72947f27e97fa85dae.tar.xz snac2-796a111de81df9e7c8bc2e72947f27e97fa85dae.zip | |
New function mastoapi_poll().
| -rw-r--r-- | mastoapi.c | 133 |
1 files changed, 75 insertions, 58 deletions
| @@ -587,6 +587,78 @@ xs_dict *mastoapi_account(const xs_dict *actor) | |||
| 587 | } | 587 | } |
| 588 | 588 | ||
| 589 | 589 | ||
| 590 | xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg) | ||
| 591 | /* creates a mastoapi Poll object */ | ||
| 592 | { | ||
| 593 | xs_dict *poll = NULL; | ||
| 594 | |||
| 595 | /* is it a poll? */ | ||
| 596 | if (strcmp(xs_dict_get(msg, "type"), "Question") == 0) { | ||
| 597 | xs *mid = mastoapi_id(msg); | ||
| 598 | xs *f = xs_val_new(XSTYPE_FALSE); | ||
| 599 | xs *t = xs_val_new(XSTYPE_TRUE); | ||
| 600 | xs_list *opts = NULL; | ||
| 601 | xs_list *p; | ||
| 602 | xs_val *v; | ||
| 603 | int num_votes = 0; | ||
| 604 | xs *options = xs_list_new(); | ||
| 605 | |||
| 606 | poll = xs_dict_new(); | ||
| 607 | |||
| 608 | poll = xs_dict_append(poll, "id", mid); | ||
| 609 | poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime")); | ||
| 610 | poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f); | ||
| 611 | |||
| 612 | if ((opts = xs_dict_get(msg, "oneOf")) != NULL) | ||
| 613 | poll = xs_dict_append(poll, "multiple", f); | ||
| 614 | else { | ||
| 615 | opts = xs_dict_get(msg, "anyOf"); | ||
| 616 | poll = xs_dict_append(poll, "multiple", t); | ||
| 617 | } | ||
| 618 | |||
| 619 | while (xs_list_iter(&opts, &v)) { | ||
| 620 | const char *title = xs_dict_get(v, "name"); | ||
| 621 | const char *replies = xs_dict_get(v, "replies"); | ||
| 622 | |||
| 623 | if (title && replies) { | ||
| 624 | const char *votes_count = xs_dict_get(replies, "totalItems"); | ||
| 625 | |||
| 626 | if (xs_type(votes_count) == XSTYPE_NUMBER) { | ||
| 627 | xs *d = xs_dict_new(); | ||
| 628 | d = xs_dict_append(d, "title", title); | ||
| 629 | d = xs_dict_append(d, "votes_count", votes_count); | ||
| 630 | |||
| 631 | options = xs_list_append(options, d); | ||
| 632 | num_votes += xs_number_get(votes_count); | ||
| 633 | } | ||
| 634 | } | ||
| 635 | } | ||
| 636 | |||
| 637 | poll = xs_dict_append(poll, "options", options); | ||
| 638 | xs *vc = xs_number_new(num_votes); | ||
| 639 | poll = xs_dict_append(poll, "votes_count", vc); | ||
| 640 | |||
| 641 | xs *children = object_children(xs_dict_get(msg, "id")); | ||
| 642 | int voted = 0; | ||
| 643 | p = children; | ||
| 644 | while (xs_list_iter(&p, &v)) { | ||
| 645 | xs *obj = NULL; | ||
| 646 | |||
| 647 | if (valid_status(object_get_by_md5(v, &obj))) { | ||
| 648 | if (strcmp(xs_dict_get(obj, "attributedTo"), snac->actor) == 0) { | ||
| 649 | voted = 1; | ||
| 650 | break; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | } | ||
| 654 | |||
| 655 | poll = xs_dict_append(poll, "voted", voted ? t : f); | ||
| 656 | } | ||
| 657 | |||
| 658 | return poll; | ||
| 659 | } | ||
| 660 | |||
| 661 | |||
| 590 | xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | 662 | xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) |
| 591 | /* converts an ActivityPub note to a Mastodon status */ | 663 | /* converts an ActivityPub note to a Mastodon status */ |
| 592 | { | 664 | { |
| @@ -803,66 +875,11 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 803 | 875 | ||
| 804 | st = xs_dict_append(st, "edited_at", tmp); | 876 | st = xs_dict_append(st, "edited_at", tmp); |
| 805 | 877 | ||
| 806 | /* is it a poll? */ | 878 | /* build the poll object, if applicable */ |
| 807 | if (strcmp(xs_dict_get(msg, "type"), "Question") == 0) { | 879 | xs *poll = mastoapi_poll(snac, msg); |
| 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", mid); | ||
| 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 | 880 | ||
| 881 | if (poll) | ||
| 864 | st = xs_dict_append(st, "poll", poll); | 882 | st = xs_dict_append(st, "poll", poll); |
| 865 | } | ||
| 866 | 883 | ||
| 867 | return st; | 884 | return st; |
| 868 | } | 885 | } |