checkpoint: now vote via the status

This commit is contained in:
Dan Anglin 2024-08-13 22:15:29 +01:00
parent 64e8d6f0ba
commit 2f530bca5a
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 44 additions and 61 deletions

View file

@ -5,36 +5,34 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
) )
const ( const (
pollPath string = "/api/v1/polls" pollPath string = "/api/v1/polls"
) )
func (g *Client) GetPoll(pollID string) (model.Poll, error) { // func (g *Client) GetPoll(pollID string) (model.Poll, error) {
url := g.Authentication.Instance + pollPath + "/" + pollID // url := g.Authentication.Instance + pollPath + "/" + pollID
//
var poll model.Poll // var poll model.Poll
//
params := requestParameters{ // params := requestParameters{
httpMethod: http.MethodGet, // httpMethod: http.MethodGet,
url: url, // url: url,
requestBody: nil, // requestBody: nil,
contentType: "", // contentType: "",
output: &poll, // output: &poll,
} // }
//
if err := g.sendRequest(params); err != nil { // if err := g.sendRequest(params); err != nil {
return model.Poll{}, fmt.Errorf( // return model.Poll{}, fmt.Errorf(
"received an error after sending the request to get the poll: %w", // "received an error after sending the request to get the poll: %w",
err, // err,
) // )
} // }
//
return poll, nil // return poll, nil
} // }
func (g *Client) VoteInPoll(pollID string, choices []int) error { func (g *Client) VoteInPoll(pollID string, choices []int) error {
form := struct { form := struct {

View file

@ -17,7 +17,6 @@ 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]
@ -164,6 +163,7 @@ func (a *AddExecutor) addToStatus(gtsClient *client.Client) error {
resourceStar: a.addStarToStatus, resourceStar: a.addStarToStatus,
resourceLike: a.addStarToStatus, resourceLike: a.addStarToStatus,
resourceBoost: a.addBoostToStatus, resourceBoost: a.addBoostToStatus,
resourceVote: a.addVoteToStatus,
} }
doFunc, ok := funcMap[a.resourceType] doFunc, ok := funcMap[a.resourceType]
@ -197,45 +197,40 @@ func (a *AddExecutor) addBoostToStatus(gtsClient *client.Client) error {
return nil return nil
} }
func (a *AddExecutor) addToPoll(gtsClient *client.Client) error { func (a *AddExecutor) addVoteToStatus(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 a.votes.Empty() { if a.votes.Empty() {
return errors.New("please use --" + flagVote + " to make a choice in this poll") 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 { 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{} return PollClosedError{}
} }
if !poll.Multiple && !a.votes.ExpectedLength(1) { if !status.Poll.Multiple && !a.votes.ExpectedLength(1) {
return MultipleChoiceError{} 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) return fmt.Errorf("unable to add your vote(s) to the poll: %w", err)
} }

View file

@ -56,7 +56,6 @@ type AddExecutor struct {
accountNames internalFlag.StringSliceValue accountNames internalFlag.StringSliceValue
content string content string
listID string listID string
pollID string
statusID string statusID string
toResourceType string toResourceType string
resourceType string resourceType string
@ -80,7 +79,6 @@ func NewAddExecutor(
exe.Var(&exe.accountNames, "account-name", "The name of the account") 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.content, "content", "", "The content of the created resource")
exe.StringVar(&exe.listID, "list-id", "", "The ID of the list in question") 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.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.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)") 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 onlyMedia bool
onlyPinned bool onlyPinned bool
onlyPublic bool onlyPublic bool
pollID string
showUserPreferences bool showUserPreferences bool
showStatuses bool showStatuses bool
skipAccountRelationship 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.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.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.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.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.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") exe.BoolVar(&exe.skipAccountRelationship, "skip-relationship", false, "Set to true to skip showing your relationship to the account that you are viewing")

View file

@ -136,10 +136,6 @@
"type": "bool", "type": "bool",
"description": "Set to true to hide the vote count until the poll is closed" "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": { "poll-option": {
"type": "StringSliceValue", "type": "StringSliceValue",
"description": "A poll option. Use this multiple times to set multiple options" "description": "A poll option. Use this multiple times to set multiple options"
@ -215,7 +211,6 @@
{ "flag": "account-name", "fieldName": "accountNames" }, { "flag": "account-name", "fieldName": "accountNames" },
{ "flag": "content", "default": "" }, { "flag": "content", "default": "" },
{ "flag": "list-id", "fieldName": "listID", "default": "" }, { "flag": "list-id", "fieldName": "listID", "default": "" },
{ "flag": "poll-id", "fieldName": "pollID", "default": "" },
{ "flag": "status-id", "fieldName": "statusID", "default": "" }, { "flag": "status-id", "fieldName": "statusID", "default": "" },
{ "flag": "to", "fieldName": "toResourceType", "default": "" }, { "flag": "to", "fieldName": "toResourceType", "default": "" },
{ "flag": "type", "fieldName": "resourceType", "default": "" }, { "flag": "type", "fieldName": "resourceType", "default": "" },
@ -367,7 +362,6 @@
{ "flag": "only-media", "default": "false" }, { "flag": "only-media", "default": "false" },
{ "flag": "only-pinned", "default": "false" }, { "flag": "only-pinned", "default": "false" },
{ "flag": "only-public", "default": "false" }, { "flag": "only-public", "default": "false" },
{ "flag": "poll-id", "fieldName": "pollID", "default": "" },
{ "flag": "show-preferences", "fieldName": "showUserPreferences", "default": "false" }, { "flag": "show-preferences", "fieldName": "showUserPreferences", "default": "false" },
{ "flag": "show-statuses", "default": "false" }, { "flag": "show-statuses", "default": "false" },
{ "flag": "skip-relationship", "fieldName": "skipAccountRelationship", "default": "false" }, { "flag": "skip-relationship", "fieldName": "skipAccountRelationship", "default": "false" },