diff options
author | 2023-11-09 13:06:37 +0100 | |
---|---|---|
committer | 2023-11-09 12:06:37 +0000 | |
commit | 42a19cf390bc1b3be1331f9bce79c8372f687a77 (patch) | |
tree | ef4c099e81a06af254be2cc491e34d83050444b8 /internal/typeutils | |
parent | [bugfix] actually decrement votes during poll vote delete ... (#2344) (diff) | |
download | gotosocial-42a19cf390bc1b3be1331f9bce79c8372f687a77.tar.xz |
[bugfix/docs] Poll api fixups + swagger docs (#2345)
Diffstat (limited to 'internal/typeutils')
-rw-r--r-- | internal/typeutils/internaltofrontend.go | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 0a79defc7..d5a1dee32 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -1313,8 +1313,10 @@ func (c *Converter) PollToAPIPoll(ctx context.Context, requester *gtsmodel.Accou options []apimodel.PollOption totalVotes int totalVoters int - ownChoices []int + voted *bool + ownChoices *[]int isAuthor bool + emojis []apimodel.Emoji ) // Preallocate a slice of frontend model poll choices. @@ -1337,19 +1339,26 @@ func (c *Converter) PollToAPIPoll(ctx context.Context, requester *gtsmodel.Accou if vote != nil { // Set choices by requester. - ownChoices = vote.Choices + ownChoices = &vote.Choices // Update default totals in the // case that counts are hidden. totalVotes = len(vote.Choices) totalVoters = 1 - for _, choice := range ownChoices { + for _, choice := range *ownChoices { options[choice].VotesCount++ } + } else { + // Requester is defined but hasn't made + // a choice. Init slice to serialize as `[]`. + ownChoices = util.Ptr(make([]int, 0)) } // Check if requester is author of source status. isAuthor = (requester.ID == poll.Status.AccountID) + + // Requester is defined so voted should be defined too. + voted = util.Ptr((isAuthor || len(*ownChoices) > 0)) } if isAuthor || !*poll.HideCounts { @@ -1368,6 +1377,11 @@ func (c *Converter) PollToAPIPoll(ctx context.Context, requester *gtsmodel.Accou } } + // TODO: emojis used in poll options. + // For now init to empty slice to serialize as `[]`. + // In future inherit from parent status. + emojis = make([]apimodel.Emoji, 0) + return &apimodel.Poll{ ID: poll.ID, ExpiresAt: util.FormatISO8601(poll.ExpiresAt), @@ -1375,9 +1389,10 @@ func (c *Converter) PollToAPIPoll(ctx context.Context, requester *gtsmodel.Accou Multiple: (*poll.Multiple), VotesCount: totalVotes, VotersCount: totalVoters, - Voted: (isAuthor || len(ownChoices) > 0), + Voted: voted, OwnVotes: ownChoices, Options: options, + Emojis: emojis, }, nil } |