checkpoint: user's can now vote on polls
This commit is contained in:
parent
d3b541dc86
commit
d3d3936704
4 changed files with 90 additions and 4 deletions
|
@ -45,7 +45,7 @@ func (g *Client) VoteInPoll(pollID string, choices []int) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
requestBody := bytes.NewBuffer(data)
|
requestBody := bytes.NewBuffer(data)
|
||||||
url := g.Authentication.Instance + pollPath + "/" + pollID + "/vote"
|
url := g.Authentication.Instance + pollPath + "/" + pollID + "/votes"
|
||||||
|
|
||||||
if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil {
|
if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil {
|
||||||
return fmt.Errorf("received an error after sending the request to vote in the poll: %w", err)
|
return fmt.Errorf("received an error after sending the request to vote in the poll: %w", err)
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package executor
|
package executor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
@ -19,6 +20,8 @@ type AddExecutor struct {
|
||||||
toResourceType string
|
toResourceType string
|
||||||
listID string
|
listID string
|
||||||
statusID string
|
statusID string
|
||||||
|
pollID string
|
||||||
|
choices PollChoices
|
||||||
accountNames AccountNames
|
accountNames AccountNames
|
||||||
content string
|
content string
|
||||||
}
|
}
|
||||||
|
@ -34,10 +37,12 @@ func NewAddExecutor(tlf TopLevelFlags, name, summary string) *AddExecutor {
|
||||||
|
|
||||||
addExe.StringVar(&addExe.resourceType, flagType, "", "Specify the resource type to add (e.g. account, note)")
|
addExe.StringVar(&addExe.resourceType, flagType, "", "Specify the resource type to add (e.g. account, note)")
|
||||||
addExe.StringVar(&addExe.toResourceType, flagTo, "", "Specify the target resource type to add to (e.g. list, account, etc)")
|
addExe.StringVar(&addExe.toResourceType, flagTo, "", "Specify the target resource type to add to (e.g. list, account, etc)")
|
||||||
addExe.StringVar(&addExe.listID, flagListID, "", "The ID of the list to add to")
|
addExe.StringVar(&addExe.listID, flagListID, "", "The ID of the list")
|
||||||
addExe.StringVar(&addExe.statusID, flagStatusID, "", "The ID of the status")
|
addExe.StringVar(&addExe.statusID, flagStatusID, "", "The ID of the status")
|
||||||
addExe.Var(&addExe.accountNames, flagAccountName, "The name of the account")
|
|
||||||
addExe.StringVar(&addExe.content, flagContent, "", "The content of the resource")
|
addExe.StringVar(&addExe.content, flagContent, "", "The content of the resource")
|
||||||
|
addExe.StringVar(&addExe.pollID, flagPollID, "", "The ID of the poll")
|
||||||
|
addExe.Var(&addExe.accountNames, flagAccountName, "The name of the account")
|
||||||
|
addExe.Var(&addExe.choices, flagChoose, "Specify your choice ")
|
||||||
|
|
||||||
addExe.Usage = commandUsageFunc(name, summary, addExe.FlagSet)
|
addExe.Usage = commandUsageFunc(name, summary, addExe.FlagSet)
|
||||||
|
|
||||||
|
@ -54,6 +59,7 @@ func (a *AddExecutor) Execute() error {
|
||||||
resourceAccount: a.addToAccount,
|
resourceAccount: a.addToAccount,
|
||||||
resourceBookmarks: a.addToBookmarks,
|
resourceBookmarks: a.addToBookmarks,
|
||||||
resourceStatus: a.addToStatus,
|
resourceStatus: a.addToStatus,
|
||||||
|
resourcePoll: a.addToPoll,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[a.toResourceType]
|
doFunc, ok := funcMap[a.toResourceType]
|
||||||
|
@ -227,3 +233,50 @@ func (a *AddExecutor) addBoostToStatus(gtsClient *client.Client) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AddExecutor) addToPoll(gtsClient *client.Client) error {
|
||||||
|
if a.pollID == "" {
|
||||||
|
return FlagNotSetError{flagText: flagPollID}
|
||||||
|
}
|
||||||
|
|
||||||
|
funcMap := map[string]func(*client.Client) error{
|
||||||
|
resourceVote: a.addVoteToPoll,
|
||||||
|
}
|
||||||
|
|
||||||
|
doFunc, ok := funcMap[a.resourceType]
|
||||||
|
if !ok {
|
||||||
|
return UnsupportedAddOperationError{
|
||||||
|
ResourceType: a.resourceType,
|
||||||
|
AddToResourceType: a.toResourceType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return doFunc(gtsClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AddExecutor) addVoteToPoll(gtsClient *client.Client) error {
|
||||||
|
if len(a.choices) == 0 {
|
||||||
|
return errors.New("please use --" + flagChoose + " to make a choice in this poll")
|
||||||
|
}
|
||||||
|
|
||||||
|
poll, err := gtsClient.GetPoll(a.pollID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to retrieve the poll: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if poll.Expired {
|
||||||
|
return errors.New("this poll has expired")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !poll.Multiple && len(a.choices) > 1 {
|
||||||
|
return errors.New("this poll does not allow multiple choices")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gtsClient.VoteInPoll(a.pollID, []int(a.choices)); err != nil {
|
||||||
|
return fmt.Errorf("unable to add your vote(s) to the poll: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Successfully added your vote(s) to the poll.")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -4,11 +4,16 @@
|
||||||
|
|
||||||
package executor
|
package executor
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
flagAccountName = "account-name"
|
flagAccountName = "account-name"
|
||||||
flagBrowser = "browser"
|
flagBrowser = "browser"
|
||||||
|
flagChoose = "choose"
|
||||||
flagContentType = "content-type"
|
flagContentType = "content-type"
|
||||||
flagContent = "content"
|
flagContent = "content"
|
||||||
flagEnableFederation = "enable-federation"
|
flagEnableFederation = "enable-federation"
|
||||||
|
@ -59,3 +64,30 @@ type TopLevelFlags struct {
|
||||||
NoColor *bool
|
NoColor *bool
|
||||||
Pager string
|
Pager string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PollChoices []int
|
||||||
|
|
||||||
|
func (p *PollChoices) String() string {
|
||||||
|
value := "Choices: "
|
||||||
|
|
||||||
|
for ind, vote := range *p {
|
||||||
|
if ind == len(*p)-1 {
|
||||||
|
value += strconv.Itoa(vote)
|
||||||
|
} else {
|
||||||
|
value += strconv.Itoa(vote) + ", "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PollChoices) Set(text string) error {
|
||||||
|
value, err := strconv.Atoi(text)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to parse the value to an integer: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
*p = append(*p, value)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -22,4 +22,5 @@ const (
|
||||||
resourceStar = "star"
|
resourceStar = "star"
|
||||||
resourceStarred = "starred"
|
resourceStarred = "starred"
|
||||||
resourceTimeline = "timeline"
|
resourceTimeline = "timeline"
|
||||||
|
resourceVote = "vote"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue