go-mastodon/polls.go

54 lines
1.4 KiB
Go
Raw Normal View History

package mastodon
import (
"fmt"
"net/http"
"net/url"
"time"
)
2022-11-13 22:14:40 +01:00
// Poll holds information for mastodon polls.
type Poll struct {
ID ID `json:"id"`
ExpiresAt time.Time `json:"expires_at"`
Expired bool `json:"expired"`
Multiple bool `json:"multiple"`
VotesCount int64 `json:"votes_count"`
VotersCount int64 `json:"voters_count"`
Options []PollOption `json:"options"`
Voted bool `json:"voted"`
OwnVotes []int `json:"own_votes"`
Emojis []Emoji `json:"emojis"`
}
2022-11-13 22:14:40 +01:00
// Poll holds information for a mastodon poll option.
type PollOption struct {
Title string `json:"title"`
VotesCount int64 `json:"votes_count"`
}
2022-11-13 22:14:40 +01:00
// GetPoll returns poll specified by id.
2022-11-20 18:03:36 +01:00
func (c *Client) GetPoll(id ID) (*Poll, error) {
var poll Poll
2022-11-20 18:03:36 +01:00
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/polls/%s", id), nil, &poll, nil)
if err != nil {
return nil, err
}
return &poll, nil
}
// PollVote votes on a poll specified by id, choices is the Poll.Options index to vote on
2022-11-20 18:03:36 +01:00
func (c *Client) PollVote(id ID, choices ...int) (*Poll, error) {
params := url.Values{}
for _, c := range choices {
params.Add("choices[]", fmt.Sprintf("%d", c))
}
var poll Poll
2022-11-20 18:03:36 +01:00
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/polls/%s/votes", url.PathEscape(string(id))), params, &poll, nil)
if err != nil {
return nil, err
}
return &poll, nil
}