From 34d0879c1651d2703f94c1b653227c718aac475f Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Wed, 8 Nov 2023 22:37:35 +0000 Subject: [bugfix] fix poll vote count responses on client and fedi API vote creation (#2343) * increment poll votes *before* enqueuing vote to client API worker * increment vote counts before federating status update after vote in local poll * improved vote count calculation during backend -> frontend model conversion --- internal/typeutils/internaltofrontend.go | 37 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'internal/typeutils/internaltofrontend.go') diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 6a374bbde..0a79defc7 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -1310,13 +1310,21 @@ func (c *Converter) PollToAPIPoll(ctx context.Context, requester *gtsmodel.Accou } var ( + options []apimodel.PollOption totalVotes int totalVoters int - voteCounts []int ownChoices []int isAuthor bool ) + // Preallocate a slice of frontend model poll choices. + options = make([]apimodel.PollOption, len(poll.Options)) + + // Add the titles to all of the options. + for i, title := range poll.Options { + options[i].Title = title + } + if requester != nil { // Get vote by requester in poll (if any). vote, err := c.state.DB.GetPollVoteBy(ctx, @@ -1335,37 +1343,28 @@ func (c *Converter) PollToAPIPoll(ctx context.Context, requester *gtsmodel.Accou // case that counts are hidden. totalVotes = len(vote.Choices) totalVoters = 1 + for _, choice := range ownChoices { + options[choice].VotesCount++ + } } // Check if requester is author of source status. isAuthor = (requester.ID == poll.Status.AccountID) } - // Preallocate a slice of frontend model poll choices. - options := make([]apimodel.PollOption, len(poll.Options)) - - // Add the titles to all of the options. - for i, title := range poll.Options { - options[i].Title = title - } - if isAuthor || !*poll.HideCounts { // A remote status, // the simple route! // // Pull cached remote values. - totalVoters = *poll.Voters - voteCounts = poll.Votes - - // Accumulate total from all counts. - for _, count := range poll.Votes { - totalVotes += count - } + totalVoters = (*poll.Voters) // When this is status author, or hide counts - // is disabled, set the counts known per vote. - for i, count := range voteCounts { + // is disabled, set the counts known per vote, + // and accumulate all the vote totals. + for i, count := range poll.Votes { options[i].VotesCount = count + totalVotes += count } } @@ -1373,7 +1372,7 @@ func (c *Converter) PollToAPIPoll(ctx context.Context, requester *gtsmodel.Accou ID: poll.ID, ExpiresAt: util.FormatISO8601(poll.ExpiresAt), Expired: poll.Closed(), - Multiple: *poll.Multiple, + Multiple: (*poll.Multiple), VotesCount: totalVotes, VotersCount: totalVoters, Voted: (isAuthor || len(ownChoices) > 0), -- cgit v1.2.3