diff options
| author | 2023-06-01 08:13:58 +0200 | |
|---|---|---|
| committer | 2023-06-01 08:13:58 +0200 | |
| commit | 182ba33c76f34ecd092978246aa58be48742a61b (patch) | |
| tree | dadbb9ed1c2fc71339d3e4cec5997a319f6d3b1b /mastoapi.c | |
| parent | Always notify about our own closed polls. (diff) | |
| download | snac2-182ba33c76f34ecd092978246aa58be48742a61b.tar.gz snac2-182ba33c76f34ecd092978246aa58be48742a61b.tar.xz snac2-182ba33c76f34ecd092978246aa58be48742a61b.zip | |
Minor refactoring to mastoapi_poll().
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 90 |
1 files changed, 42 insertions, 48 deletions
| @@ -590,56 +590,50 @@ xs_dict *mastoapi_account(const xs_dict *actor) | |||
| 590 | xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg) | 590 | xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg) |
| 591 | /* creates a mastoapi Poll object */ | 591 | /* creates a mastoapi Poll object */ |
| 592 | { | 592 | { |
| 593 | xs_dict *poll = NULL; | 593 | xs_dict *poll = xs_dict_new(); |
| 594 | 594 | xs *mid = mastoapi_id(msg); | |
| 595 | /* is it a poll? */ | 595 | xs *f = xs_val_new(XSTYPE_FALSE); |
| 596 | if (strcmp(xs_dict_get(msg, "type"), "Question") == 0) { | 596 | xs *t = xs_val_new(XSTYPE_TRUE); |
| 597 | xs *mid = mastoapi_id(msg); | 597 | xs_list *opts = NULL; |
| 598 | xs *f = xs_val_new(XSTYPE_FALSE); | 598 | xs_val *v; |
| 599 | xs *t = xs_val_new(XSTYPE_TRUE); | 599 | int num_votes = 0; |
| 600 | xs_list *opts = NULL; | 600 | xs *options = xs_list_new(); |
| 601 | xs_val *v; | 601 | |
| 602 | int num_votes = 0; | 602 | poll = xs_dict_append(poll, "id", mid); |
| 603 | xs *options = xs_list_new(); | 603 | poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime")); |
| 604 | 604 | poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f); | |
| 605 | poll = xs_dict_new(); | 605 | |
| 606 | 606 | if ((opts = xs_dict_get(msg, "oneOf")) != NULL) | |
| 607 | poll = xs_dict_append(poll, "id", mid); | 607 | poll = xs_dict_append(poll, "multiple", f); |
| 608 | poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime")); | 608 | else { |
| 609 | poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f); | 609 | opts = xs_dict_get(msg, "anyOf"); |
| 610 | 610 | poll = xs_dict_append(poll, "multiple", t); | |
| 611 | if ((opts = xs_dict_get(msg, "oneOf")) != NULL) | 611 | } |
| 612 | poll = xs_dict_append(poll, "multiple", f); | ||
| 613 | else { | ||
| 614 | opts = xs_dict_get(msg, "anyOf"); | ||
| 615 | poll = xs_dict_append(poll, "multiple", t); | ||
| 616 | } | ||
| 617 | 612 | ||
| 618 | while (xs_list_iter(&opts, &v)) { | 613 | while (xs_list_iter(&opts, &v)) { |
| 619 | const char *title = xs_dict_get(v, "name"); | 614 | const char *title = xs_dict_get(v, "name"); |
| 620 | const char *replies = xs_dict_get(v, "replies"); | 615 | const char *replies = xs_dict_get(v, "replies"); |
| 621 | 616 | ||
| 622 | if (title && replies) { | 617 | if (title && replies) { |
| 623 | const char *votes_count = xs_dict_get(replies, "totalItems"); | 618 | const char *votes_count = xs_dict_get(replies, "totalItems"); |
| 624 | 619 | ||
| 625 | if (xs_type(votes_count) == XSTYPE_NUMBER) { | 620 | if (xs_type(votes_count) == XSTYPE_NUMBER) { |
| 626 | xs *d = xs_dict_new(); | 621 | xs *d = xs_dict_new(); |
| 627 | d = xs_dict_append(d, "title", title); | 622 | d = xs_dict_append(d, "title", title); |
| 628 | d = xs_dict_append(d, "votes_count", votes_count); | 623 | d = xs_dict_append(d, "votes_count", votes_count); |
| 629 | 624 | ||
| 630 | options = xs_list_append(options, d); | 625 | options = xs_list_append(options, d); |
| 631 | num_votes += xs_number_get(votes_count); | 626 | num_votes += xs_number_get(votes_count); |
| 632 | } | ||
| 633 | } | 627 | } |
| 634 | } | 628 | } |
| 629 | } | ||
| 635 | 630 | ||
| 636 | poll = xs_dict_append(poll, "options", options); | 631 | poll = xs_dict_append(poll, "options", options); |
| 637 | xs *vc = xs_number_new(num_votes); | 632 | xs *vc = xs_number_new(num_votes); |
| 638 | poll = xs_dict_append(poll, "votes_count", vc); | 633 | poll = xs_dict_append(poll, "votes_count", vc); |
| 639 | 634 | ||
| 640 | poll = xs_dict_append(poll, "voted", | 635 | poll = xs_dict_append(poll, "voted", |
| 641 | was_question_voted(snac, xs_dict_get(msg, "id")) ? t : f); | 636 | was_question_voted(snac, xs_dict_get(msg, "id")) ? t : f); |
| 642 | } | ||
| 643 | 637 | ||
| 644 | return poll; | 638 | return poll; |
| 645 | } | 639 | } |
| @@ -655,6 +649,9 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 655 | if (actor == NULL) | 649 | if (actor == NULL) |
| 656 | return NULL; | 650 | return NULL; |
| 657 | 651 | ||
| 652 | const char *type = xs_dict_get(msg, "type"); | ||
| 653 | const char *id = xs_dict_get(msg, "id"); | ||
| 654 | |||
| 658 | xs *acct = mastoapi_account(actor); | 655 | xs *acct = mastoapi_account(actor); |
| 659 | 656 | ||
| 660 | xs *f = xs_val_new(XSTYPE_FALSE); | 657 | xs *f = xs_val_new(XSTYPE_FALSE); |
| @@ -662,9 +659,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 662 | xs *n = xs_val_new(XSTYPE_NULL); | 659 | xs *n = xs_val_new(XSTYPE_NULL); |
| 663 | xs *idx = NULL; | 660 | xs *idx = NULL; |
| 664 | xs *ixc = NULL; | 661 | xs *ixc = NULL; |
| 665 | |||
| 666 | char *tmp; | 662 | char *tmp; |
| 667 | char *id = xs_dict_get(msg, "id"); | ||
| 668 | xs *mid = mastoapi_id(msg); | 663 | xs *mid = mastoapi_id(msg); |
| 669 | 664 | ||
| 670 | xs_dict *st = xs_dict_new(); | 665 | xs_dict *st = xs_dict_new(); |
| @@ -861,11 +856,10 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 861 | 856 | ||
| 862 | st = xs_dict_append(st, "edited_at", tmp); | 857 | st = xs_dict_append(st, "edited_at", tmp); |
| 863 | 858 | ||
| 864 | /* build the poll object, if applicable */ | 859 | if (strcmp(type, "Question") == 0) { |
| 865 | xs *poll = mastoapi_poll(snac, msg); | 860 | xs *poll = mastoapi_poll(snac, msg); |
| 866 | |||
| 867 | if (poll) | ||
| 868 | st = xs_dict_append(st, "poll", poll); | 861 | st = xs_dict_append(st, "poll", poll); |
| 862 | } | ||
| 869 | 863 | ||
| 870 | return st; | 864 | return st; |
| 871 | } | 865 | } |