poll.go 995 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package mastodon
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "time"
  8. )
  9. type Poll struct {
  10. ID string `json:"id"`
  11. ExpiresAt *time.Time `json:"expires_at"`
  12. Expired bool `json:"expired"`
  13. Multiple bool `json:"multiple"`
  14. VotesCount int64 `json:"votes_count"`
  15. Voted bool `json:"voted"`
  16. Emojis []Emoji `json:"emojis"`
  17. Options []PollOption `json:"options"`
  18. }
  19. // Poll hold information for a mastodon poll option.
  20. type PollOption struct {
  21. Title string `json:"title"`
  22. VotesCount int64 `json:"votes_count"`
  23. }
  24. // Vote submits a vote with given choices to the poll specified by id.
  25. func (c *Client) Vote(ctx context.Context, id string, choices []string) (*Poll, error) {
  26. var poll Poll
  27. params := make(url.Values)
  28. params["choices[]"] = choices
  29. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/polls/%s/votes", id), params, &poll, nil)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &poll, nil
  34. }