2019-05-13 01:46:11 +02:00
|
|
|
package mastodon
|
|
|
|
|
2021-11-04 14:50:16 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
2019-05-13 01:46:11 +02:00
|
|
|
|
|
|
|
type Poll struct {
|
2021-11-04 14:50:16 +01:00
|
|
|
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"`
|
2019-05-13 01:46:11 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 21:31:59 +01:00
|
|
|
func (p *Poll) GetID() string {
|
|
|
|
if p != nil {
|
|
|
|
return string(p.ID)
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:46:11 +02:00
|
|
|
type PollOption struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
VotesCount int64 `json:"votes_count"`
|
|
|
|
}
|
2021-11-04 14:50:16 +01:00
|
|
|
|
2022-11-20 20:36:27 +01:00
|
|
|
func GetPoll(id ID) (*Poll, error) {
|
2021-11-04 14:50:16 +01:00
|
|
|
var poll Poll
|
2022-11-20 20:36:27 +01:00
|
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/polls/%s", id), nil, &poll, nil)
|
2021-11-04 14:50:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &poll, nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 21:31:59 +01:00
|
|
|
func (p *Poll) Vote(choices ...int) (*Poll, error) {
|
2021-11-04 14:50:16 +01:00
|
|
|
params := url.Values{}
|
|
|
|
for _, c := range choices {
|
|
|
|
params.Add("choices[]", fmt.Sprintf("%d", c))
|
|
|
|
}
|
|
|
|
|
|
|
|
var poll Poll
|
2022-11-20 21:31:59 +01:00
|
|
|
err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/polls/%s/votes", url.PathEscape(p.GetID())), params, &poll, nil)
|
2021-11-04 14:50:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &poll, nil
|
|
|
|
}
|