[bugfix/docs] Poll api fixups + swagger docs (#2345)

This commit is contained in:
tobi
2023-11-09 13:06:37 +01:00
committed by GitHub
parent b1c65ed9ac
commit 42a19cf390
5 changed files with 93 additions and 14 deletions

View File

@ -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
}