From 2f530bca5a35c58c089ff84df4b4765afed349e5 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 13 Aug 2024 22:15:29 +0100 Subject: [PATCH] checkpoint: now vote via the status --- internal/client/poll.go | 46 +++++++++++++++---------------- internal/executor/add.go | 49 +++++++++++++++------------------- internal/executor/executors.go | 4 --- schema/enbas_cli_schema.json | 6 ----- 4 files changed, 44 insertions(+), 61 deletions(-) diff --git a/internal/client/poll.go b/internal/client/poll.go index 5d6baa5..783167a 100644 --- a/internal/client/poll.go +++ b/internal/client/poll.go @@ -5,36 +5,34 @@ import ( "encoding/json" "fmt" "net/http" - - "codeflow.dananglin.me.uk/apollo/enbas/internal/model" ) const ( pollPath string = "/api/v1/polls" ) -func (g *Client) GetPoll(pollID string) (model.Poll, error) { - url := g.Authentication.Instance + pollPath + "/" + pollID - - var poll model.Poll - - params := requestParameters{ - httpMethod: http.MethodGet, - url: url, - requestBody: nil, - contentType: "", - output: &poll, - } - - if err := g.sendRequest(params); err != nil { - return model.Poll{}, fmt.Errorf( - "received an error after sending the request to get the poll: %w", - err, - ) - } - - return poll, nil -} +// func (g *Client) GetPoll(pollID string) (model.Poll, error) { +// url := g.Authentication.Instance + pollPath + "/" + pollID +// +// var poll model.Poll +// +// params := requestParameters{ +// httpMethod: http.MethodGet, +// url: url, +// requestBody: nil, +// contentType: "", +// output: &poll, +// } +// +// if err := g.sendRequest(params); err != nil { +// return model.Poll{}, fmt.Errorf( +// "received an error after sending the request to get the poll: %w", +// err, +// ) +// } +// +// return poll, nil +// } func (g *Client) VoteInPoll(pollID string, choices []int) error { form := struct { diff --git a/internal/executor/add.go b/internal/executor/add.go index b46d03b..5d38cf1 100644 --- a/internal/executor/add.go +++ b/internal/executor/add.go @@ -17,7 +17,6 @@ func (a *AddExecutor) Execute() error { resourceAccount: a.addToAccount, resourceBookmarks: a.addToBookmarks, resourceStatus: a.addToStatus, - resourcePoll: a.addToPoll, } doFunc, ok := funcMap[a.toResourceType] @@ -164,6 +163,7 @@ func (a *AddExecutor) addToStatus(gtsClient *client.Client) error { resourceStar: a.addStarToStatus, resourceLike: a.addStarToStatus, resourceBoost: a.addBoostToStatus, + resourceVote: a.addVoteToStatus, } doFunc, ok := funcMap[a.resourceType] @@ -197,45 +197,40 @@ func (a *AddExecutor) addBoostToStatus(gtsClient *client.Client) error { 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 { +func (a *AddExecutor) addVoteToStatus(gtsClient *client.Client) error { if a.votes.Empty() { return errors.New("please use --" + flagVote + " to make a choice in this poll") } - poll, err := gtsClient.GetPoll(a.pollID) + status, err := gtsClient.GetStatus(a.statusID) if err != nil { - return fmt.Errorf("unable to retrieve the poll: %w", err) + return fmt.Errorf("unable to get the status: %w", err) } - if poll.Expired { + if status.Poll == nil { + return errors.New("this status does not have a poll") + } + + if status.Poll.Expired { return PollClosedError{} } - if !poll.Multiple && !a.votes.ExpectedLength(1) { + if !status.Poll.Multiple && !a.votes.ExpectedLength(1) { return MultipleChoiceError{} } - if err := gtsClient.VoteInPoll(a.pollID, []int(a.votes)); err != nil { + myAccountID, err := getAccountID(gtsClient, true, nil, a.config.CredentialsFile) + if err != nil { + return fmt.Errorf("unable to get your account ID: %w", err) + } + + if status.Account.ID == myAccountID { + return errors.New("you cannot vote in your own poll") + } + + pollID := status.Poll.ID + + if err := gtsClient.VoteInPoll(pollID, []int(a.votes)); err != nil { return fmt.Errorf("unable to add your vote(s) to the poll: %w", err) } diff --git a/internal/executor/executors.go b/internal/executor/executors.go index 7b53ed2..4ce5cfc 100644 --- a/internal/executor/executors.go +++ b/internal/executor/executors.go @@ -56,7 +56,6 @@ type AddExecutor struct { accountNames internalFlag.StringSliceValue content string listID string - pollID string statusID string toResourceType string resourceType string @@ -80,7 +79,6 @@ func NewAddExecutor( exe.Var(&exe.accountNames, "account-name", "The name of the account") exe.StringVar(&exe.content, "content", "", "The content of the created resource") exe.StringVar(&exe.listID, "list-id", "", "The ID of the list in question") - exe.StringVar(&exe.pollID, "poll-id", "", "The ID of the poll") exe.StringVar(&exe.statusID, "status-id", "", "The ID of the status") exe.StringVar(&exe.toResourceType, "to", "", "The resource type to action the target resource to (e.g. status)") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") @@ -434,7 +432,6 @@ type ShowExecutor struct { onlyMedia bool onlyPinned bool onlyPublic bool - pollID string showUserPreferences bool showStatuses bool skipAccountRelationship bool @@ -472,7 +469,6 @@ func NewShowExecutor( exe.BoolVar(&exe.onlyMedia, "only-media", false, "Set to true to show only the statuses with media attachments") exe.BoolVar(&exe.onlyPinned, "only-pinned", false, "Set to true to show only the account's pinned statuses") exe.BoolVar(&exe.onlyPublic, "only-public", false, "Set to true to show only the account's public posts") - exe.StringVar(&exe.pollID, "poll-id", "", "The ID of the poll") exe.BoolVar(&exe.showUserPreferences, "show-preferences", false, "Set to true to view your posting preferences when viewing your account information") exe.BoolVar(&exe.showStatuses, "show-statuses", false, "Set to true to view the statuses created from the account you are viewing") exe.BoolVar(&exe.skipAccountRelationship, "skip-relationship", false, "Set to true to skip showing your relationship to the account that you are viewing") diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index b441830..bbed96d 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -136,10 +136,6 @@ "type": "bool", "description": "Set to true to hide the vote count until the poll is closed" }, - "poll-id": { - "type": "string", - "description": "The ID of the poll" - }, "poll-option": { "type": "StringSliceValue", "description": "A poll option. Use this multiple times to set multiple options" @@ -215,7 +211,6 @@ { "flag": "account-name", "fieldName": "accountNames" }, { "flag": "content", "default": "" }, { "flag": "list-id", "fieldName": "listID", "default": "" }, - { "flag": "poll-id", "fieldName": "pollID", "default": "" }, { "flag": "status-id", "fieldName": "statusID", "default": "" }, { "flag": "to", "fieldName": "toResourceType", "default": "" }, { "flag": "type", "fieldName": "resourceType", "default": "" }, @@ -367,7 +362,6 @@ { "flag": "only-media", "default": "false" }, { "flag": "only-pinned", "default": "false" }, { "flag": "only-public", "default": "false" }, - { "flag": "poll-id", "fieldName": "pollID", "default": "" }, { "flag": "show-preferences", "fieldName": "showUserPreferences", "default": "false" }, { "flag": "show-statuses", "default": "false" }, { "flag": "skip-relationship", "fieldName": "skipAccountRelationship", "default": "false" },