From ae6cc118205477dc1fd9dee5c2a997df6e8118b2 Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Fri, 5 Nov 2021 09:21:53 +0100 Subject: [PATCH] Add support for creating polls --- mastodon.go | 9 +++++++++ status.go | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/mastodon.go b/mastodon.go index d642cf3..3523713 100644 --- a/mastodon.go +++ b/mastodon.go @@ -219,6 +219,15 @@ type Toot struct { SpoilerText string `json:"spoiler_text"` Visibility string `json:"visibility"` ScheduledAt *time.Time `json:"scheduled_at,omitempty"` + Poll *TootPoll `json:"poll"` +} + +// TootPoll holds information for creating a poll in Toot. +type TootPoll struct { + Options []string `json:"options"` + ExpiresInSeconds int64 `json:"expires_in"` + Multiple bool `json:"multiple"` + HideTotals bool `json:"hide_totals"` } // Mention hold information for mention. diff --git a/status.go b/status.go index bd86a15..5cdbf22 100644 --- a/status.go +++ b/status.go @@ -349,6 +349,19 @@ func (c *Client) PostStatus(ctx context.Context, toot *Toot) (*Status, error) { params.Add("media_ids[]", string(media)) } } + // Can't use Media and Poll at the same time. + if toot.Poll != nil && toot.Poll.Options != nil && toot.MediaIDs == nil { + for _, opt := range toot.Poll.Options { + params.Add("poll[options][]", string(opt)) + } + params.Add("poll[expires_in]", fmt.Sprintf("%d", toot.Poll.ExpiresInSeconds)) + if toot.Poll.Multiple { + params.Add("poll[multiple]", "true") + } + if toot.Poll.HideTotals { + params.Add("poll[hide_totals]", "true") + } + } if toot.Visibility != "" { params.Set("visibility", fmt.Sprint(toot.Visibility)) }