From 1a90ca666f3620ee541875f82877842ee7b17e73 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 10 Oct 2024 20:29:16 +0200 Subject: [PATCH] Update vote counts after voting --- src/mastodon/api/poll.c | 16 ++++++++++++---- src/mastodon/api/poll.h | 4 ++++ src/mastodon/api/status.c | 2 +- src/mastodon/cli/tl.c | 5 +++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/mastodon/api/poll.c b/src/mastodon/api/poll.c index 07b10097..32d341d1 100644 --- a/src/mastodon/api/poll.c +++ b/src/mastodon/api/poll.c @@ -34,14 +34,16 @@ static const char *poll_selector = ".reblog.poll|(.multiple,.votes_count," "(.own_votes|join(\",\"))," "(.options[]|(.title,.votes_count)))"; -void poll_fill(poll *p, char from_reblog) { +void poll_fill(poll *p, char source) { int r; char n_lines; - if (from_reblog) + if (source == POLL_FROM_REBLOG) n_lines = 0; - else + else if (source == POLL_FROM_STATUS) n_lines = 7; /* strlen(".reblog") */ + else if (source == POLL_FROM_VOTE) + n_lines = 13; /* strlen(".reblog.poll|") */ memset(p->own_votes, 0, MAX_POLL_OPTIONS); @@ -63,7 +65,10 @@ void poll_fill(poll *p, char from_reblog) { for (r = 0; r < p->options_count; r ++) { char i = NUM_POLL_LINES + (r * 2); - p->options[r].title = strdup(lines[i]); + if (p->options[r].title == NULL) { + p->options[r].title = strdup(lines[i]); + } /* otherwise, it's a reload from votes, and + * titles won't have changed. */ p->options[r].votes_count = (size_t)atoi(lines[i + 1]); } } @@ -96,6 +101,9 @@ void poll_update_vote(poll *p) { surl_send_data(params, i); surl_read_response_header(); + if (surl_response_ok()) { + poll_fill(p, POLL_FROM_VOTE); + } } #pragma code-name(pop) diff --git a/src/mastodon/api/poll.h b/src/mastodon/api/poll.h index 82a458ca..28cee4d0 100644 --- a/src/mastodon/api/poll.h +++ b/src/mastodon/api/poll.h @@ -7,6 +7,10 @@ typedef struct _poll_option poll_option; #define MAX_POLL_OPTIONS 4 #define MAX_POLL_OPTION_LEN 49 +#define POLL_FROM_STATUS 0 +#define POLL_FROM_REBLOG 1 +#define POLL_FROM_VOTE 2 + struct _poll_option { char *title; size_t votes_count; diff --git a/src/mastodon/api/status.c b/src/mastodon/api/status.c index 6ac9fe40..1e77fd58 100644 --- a/src/mastodon/api/status.c +++ b/src/mastodon/api/status.c @@ -147,7 +147,7 @@ static __fastcall__ char status_fill_from_json(status *s, char *id, char full, c if (n_lines == 17) { s->poll = poll_new(); s->poll->id = strdup(lines[16]); - poll_fill(s->poll, is_reblog); + poll_fill(s->poll, is_reblog /* POLL_FROM_REBLOG == 1, POLL_FROM_STATUS == 0 */); } } diff --git a/src/mastodon/cli/tl.c b/src/mastodon/cli/tl.c index 9f8baaf1..f1bb3378 100644 --- a/src/mastodon/cli/tl.c +++ b/src/mastodon/cli/tl.c @@ -983,6 +983,11 @@ static void do_vote (status *status) { } vote: poll_update_vote(status->poll); + /* update display */ + gotoxy(0, 0); + writable_lines = 23; + print_status(status, 0, 1); + out: set_hscrollwindow(0, scrw); return;