Minor refactoring to mastoapi_poll().

This commit is contained in:
default 2023-06-01 08:13:58 +02:00
parent 91ed98df0d
commit 182ba33c76

View file

@ -590,57 +590,51 @@ xs_dict *mastoapi_account(const xs_dict *actor)
xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg) xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
/* creates a mastoapi Poll object */ /* creates a mastoapi Poll object */
{ {
xs_dict *poll = NULL; xs_dict *poll = xs_dict_new();
xs *mid = mastoapi_id(msg);
xs *f = xs_val_new(XSTYPE_FALSE);
xs *t = xs_val_new(XSTYPE_TRUE);
xs_list *opts = NULL;
xs_val *v;
int num_votes = 0;
xs *options = xs_list_new();
/* is it a poll? */ poll = xs_dict_append(poll, "id", mid);
if (strcmp(xs_dict_get(msg, "type"), "Question") == 0) { poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
xs *mid = mastoapi_id(msg); poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f);
xs *f = xs_val_new(XSTYPE_FALSE);
xs *t = xs_val_new(XSTYPE_TRUE);
xs_list *opts = NULL;
xs_val *v;
int num_votes = 0;
xs *options = xs_list_new();
poll = xs_dict_new(); if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
poll = xs_dict_append(poll, "multiple", f);
else {
opts = xs_dict_get(msg, "anyOf");
poll = xs_dict_append(poll, "multiple", t);
}
poll = xs_dict_append(poll, "id", mid); while (xs_list_iter(&opts, &v)) {
poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime")); const char *title = xs_dict_get(v, "name");
poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f); const char *replies = xs_dict_get(v, "replies");
if ((opts = xs_dict_get(msg, "oneOf")) != NULL) if (title && replies) {
poll = xs_dict_append(poll, "multiple", f); const char *votes_count = xs_dict_get(replies, "totalItems");
else {
opts = xs_dict_get(msg, "anyOf");
poll = xs_dict_append(poll, "multiple", t);
}
while (xs_list_iter(&opts, &v)) { if (xs_type(votes_count) == XSTYPE_NUMBER) {
const char *title = xs_dict_get(v, "name"); xs *d = xs_dict_new();
const char *replies = xs_dict_get(v, "replies"); d = xs_dict_append(d, "title", title);
d = xs_dict_append(d, "votes_count", votes_count);
if (title && replies) { options = xs_list_append(options, d);
const char *votes_count = xs_dict_get(replies, "totalItems"); num_votes += xs_number_get(votes_count);
if (xs_type(votes_count) == XSTYPE_NUMBER) {
xs *d = xs_dict_new();
d = xs_dict_append(d, "title", title);
d = xs_dict_append(d, "votes_count", votes_count);
options = xs_list_append(options, d);
num_votes += xs_number_get(votes_count);
}
} }
} }
poll = xs_dict_append(poll, "options", options);
xs *vc = xs_number_new(num_votes);
poll = xs_dict_append(poll, "votes_count", vc);
poll = xs_dict_append(poll, "voted",
was_question_voted(snac, xs_dict_get(msg, "id")) ? t : f);
} }
poll = xs_dict_append(poll, "options", options);
xs *vc = xs_number_new(num_votes);
poll = xs_dict_append(poll, "votes_count", vc);
poll = xs_dict_append(poll, "voted",
was_question_voted(snac, xs_dict_get(msg, "id")) ? t : f);
return poll; return poll;
} }
@ -655,6 +649,9 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
if (actor == NULL) if (actor == NULL)
return NULL; return NULL;
const char *type = xs_dict_get(msg, "type");
const char *id = xs_dict_get(msg, "id");
xs *acct = mastoapi_account(actor); xs *acct = mastoapi_account(actor);
xs *f = xs_val_new(XSTYPE_FALSE); xs *f = xs_val_new(XSTYPE_FALSE);
@ -662,9 +659,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
xs *n = xs_val_new(XSTYPE_NULL); xs *n = xs_val_new(XSTYPE_NULL);
xs *idx = NULL; xs *idx = NULL;
xs *ixc = NULL; xs *ixc = NULL;
char *tmp; char *tmp;
char *id = xs_dict_get(msg, "id");
xs *mid = mastoapi_id(msg); xs *mid = mastoapi_id(msg);
xs_dict *st = xs_dict_new(); xs_dict *st = xs_dict_new();
@ -861,11 +856,10 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
st = xs_dict_append(st, "edited_at", tmp); st = xs_dict_append(st, "edited_at", tmp);
/* build the poll object, if applicable */ if (strcmp(type, "Question") == 0) {
xs *poll = mastoapi_poll(snac, msg); xs *poll = mastoapi_poll(snac, msg);
if (poll)
st = xs_dict_append(st, "poll", poll); st = xs_dict_append(st, "poll", poll);
}
return st; return st;
} }