diff options
| author | 2023-05-29 08:27:16 +0200 | |
|---|---|---|
| committer | 2023-05-29 08:27:16 +0200 | |
| commit | 8551c691fb0baaa502bbd6cc4ec3291ed58354e5 (patch) | |
| tree | 813097757a91fff8dc2ef99c0adc3bd407a94e0d /activitypub.c | |
| parent | Added the 'replies' dict to each question. (diff) | |
| download | snac2-8551c691fb0baaa502bbd6cc4ec3291ed58354e5.tar.gz snac2-8551c691fb0baaa502bbd6cc4ec3291ed58354e5.tar.xz snac2-8551c691fb0baaa502bbd6cc4ec3291ed58354e5.zip | |
New function update_question().
Diffstat (limited to 'activitypub.c')
| -rw-r--r-- | activitypub.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/activitypub.c b/activitypub.c index 59b82c1..1af6d63 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -967,6 +967,82 @@ xs_dict *msg_question(snac *user, const char *content, const xs_list *opts, int | |||
| 967 | } | 967 | } |
| 968 | 968 | ||
| 969 | 969 | ||
| 970 | int update_question(snac *user, const char *id) | ||
| 971 | /* updates the poll counts */ | ||
| 972 | { | ||
| 973 | xs *msg = NULL; | ||
| 974 | xs *rcnt = xs_dict_new(); | ||
| 975 | xs *z = xs_number_new(0); | ||
| 976 | xs *chld = NULL; | ||
| 977 | xs_list *opts; | ||
| 978 | xs_val *p; | ||
| 979 | xs_str *k; | ||
| 980 | xs_val *v; | ||
| 981 | |||
| 982 | /* get the object */ | ||
| 983 | if (!valid_status(object_get(id, &msg))) | ||
| 984 | return -1; | ||
| 985 | |||
| 986 | /* get the options */ | ||
| 987 | if ((opts = xs_dict_get(msg, "oneOf")) == NULL && | ||
| 988 | (opts = xs_dict_get(msg, "anyOf")) == NULL) | ||
| 989 | return -2; | ||
| 990 | |||
| 991 | /* fill the initial count */ | ||
| 992 | p = opts; | ||
| 993 | while (xs_list_iter(&p, &v)) { | ||
| 994 | const char *name = xs_dict_get(v, "name"); | ||
| 995 | if (name) | ||
| 996 | rcnt = xs_dict_set(rcnt, name, z); | ||
| 997 | } | ||
| 998 | |||
| 999 | /* iterate now the children (the votes) */ | ||
| 1000 | chld = object_children(id); | ||
| 1001 | p = chld; | ||
| 1002 | while (xs_list_iter(&p, &v)) { | ||
| 1003 | const char *name = xs_dict_get(v, "name"); | ||
| 1004 | if (name) { | ||
| 1005 | /* get the current count */ | ||
| 1006 | const xs_number *cnt = xs_dict_get(rcnt, "name"); | ||
| 1007 | |||
| 1008 | if (xs_type(cnt) == XSTYPE_NUMBER) { | ||
| 1009 | /* if it exists, increment */ | ||
| 1010 | xs *ucnt = xs_number_new(xs_number_get(cnt) + 1); | ||
| 1011 | rcnt = xs_dict_set(rcnt, "name", ucnt); | ||
| 1012 | } | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | /* create a new list of options with their new counts */ | ||
| 1017 | xs *nopts = xs_list_new(); | ||
| 1018 | p = rcnt; | ||
| 1019 | while (xs_dict_iter(&p, &k, &v)) { | ||
| 1020 | xs *d1 = xs_dict_new(); | ||
| 1021 | xs *d2 = xs_dict_new(); | ||
| 1022 | |||
| 1023 | d2 = xs_dict_append(d2, "type", "Collection"); | ||
| 1024 | d2 = xs_dict_append(d2, "totalItems", v); | ||
| 1025 | |||
| 1026 | d1 = xs_dict_append(d1, "type", "Note"); | ||
| 1027 | d1 = xs_dict_append(d1, "name", k); | ||
| 1028 | d1 = xs_dict_append(d1, "replies", d2); | ||
| 1029 | |||
| 1030 | nopts = xs_list_append(nopts, d1); | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | /* update the list */ | ||
| 1034 | msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts); | ||
| 1035 | |||
| 1036 | /* store */ | ||
| 1037 | object_add_ow(id, msg); | ||
| 1038 | |||
| 1039 | snac_debug(user, 1, xs_fmt("recounted poll %s", id)); | ||
| 1040 | timeline_touch(user); | ||
| 1041 | |||
| 1042 | return 0; | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | |||
| 970 | void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg) | 1046 | void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg) |
| 971 | /* notifies the user of relevant events */ | 1047 | /* notifies the user of relevant events */ |
| 972 | { | 1048 | { |