2024-06-02 07:53:13 +01:00
|
|
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2024-06-15 18:40:13 +01:00
|
|
|
"errors"
|
2024-05-23 18:06:49 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
2024-06-25 12:39:39 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
2024-06-17 18:59:20 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
|
2024-05-23 18:06:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AddExecutor struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
printer *printer.Printer
|
2024-06-25 12:39:39 +01:00
|
|
|
config *config.Config
|
2024-05-23 18:06:49 +01:00
|
|
|
resourceType string
|
|
|
|
toResourceType string
|
|
|
|
listID string
|
2024-06-03 06:53:31 +01:00
|
|
|
statusID string
|
2024-06-15 18:40:13 +01:00
|
|
|
pollID string
|
|
|
|
choices MultiIntFlagValue
|
|
|
|
accountNames MultiStringFlagValue
|
2024-05-23 18:06:49 +01:00
|
|
|
content string
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
func NewAddExecutor(printer *printer.Printer, config *config.Config, name, summary string) *AddExecutor {
|
2024-05-23 18:06:49 +01:00
|
|
|
emptyArr := make([]string, 0, 3)
|
|
|
|
|
|
|
|
addExe := AddExecutor{
|
2024-06-17 18:59:20 +01:00
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
|
|
|
|
printer: printer,
|
2024-06-25 12:39:39 +01:00
|
|
|
config: config,
|
2024-06-17 18:59:20 +01:00
|
|
|
accountNames: MultiStringFlagValue(emptyArr),
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 15:58:10 +01:00
|
|
|
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)")
|
2024-06-15 18:40:13 +01:00
|
|
|
addExe.StringVar(&addExe.listID, flagListID, "", "The ID of the list")
|
2024-06-09 15:58:10 +01:00
|
|
|
addExe.StringVar(&addExe.statusID, flagStatusID, "", "The ID of the status")
|
|
|
|
addExe.StringVar(&addExe.content, flagContent, "", "The content of the resource")
|
2024-06-15 18:40:13 +01:00
|
|
|
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 ")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
addExe.Usage = commandUsageFunc(name, summary, addExe.FlagSet)
|
|
|
|
|
|
|
|
return &addExe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) Execute() error {
|
|
|
|
if a.toResourceType == "" {
|
|
|
|
return FlagNotSetError{flagText: flagTo}
|
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-06-03 06:53:31 +01:00
|
|
|
resourceList: a.addToList,
|
|
|
|
resourceAccount: a.addToAccount,
|
|
|
|
resourceBookmarks: a.addToBookmarks,
|
2024-06-04 17:01:33 +01:00
|
|
|
resourceStatus: a.addToStatus,
|
2024-06-15 18:40:13 +01:00
|
|
|
resourcePoll: a.addToPoll,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.toResourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: a.toResourceType}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
gtsClient, err := client.NewClientFromFile(a.config.CredentialsFile)
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addToList(gtsClient *client.Client) error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceAccount: a.addAccountsToList,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedAddOperationError{
|
|
|
|
ResourceType: a.resourceType,
|
|
|
|
AddToResourceType: a.toResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error {
|
|
|
|
if a.listID == "" {
|
|
|
|
return FlagNotSetError{flagText: flagListID}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.accountNames) == 0 {
|
|
|
|
return NoAccountSpecifiedError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
accountIDs := make([]string, len(a.accountNames))
|
|
|
|
|
|
|
|
for ind := range a.accountNames {
|
|
|
|
accountID, err := getTheirAccountID(gtsClient, a.accountNames[ind])
|
|
|
|
if err != nil {
|
2024-06-16 20:29:16 +01:00
|
|
|
return fmt.Errorf("unable to get the account ID for %s: %w", a.accountNames[ind], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship, err := gtsClient.GetAccountRelationship(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get your relationship to %s: %w", a.accountNames[ind], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !relationship.Following {
|
|
|
|
return NotFollowingError{Account: a.accountNames[ind]}
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
accountIDs[ind] = accountID
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.AddAccountsToList(a.listID, accountIDs); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to add the accounts to the list: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully added the account(s) to the list.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addToAccount(gtsClient *client.Client) error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceNote: a.addNoteToAccount,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedAddOperationError{
|
|
|
|
ResourceType: a.resourceType,
|
|
|
|
AddToResourceType: a.toResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addNoteToAccount(gtsClient *client.Client) error {
|
|
|
|
if len(a.accountNames) != 1 {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unexpected number of accounts specified: want 1, got %d", len(a.accountNames))
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, a.accountNames[0], a.config.CredentialsFile)
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.content == "" {
|
2024-05-31 00:11:02 +01:00
|
|
|
return EmptyContentError{
|
|
|
|
ResourceType: resourceNote,
|
|
|
|
Hint: "please use --" + flagContent,
|
|
|
|
}
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.SetPrivateNote(accountID, a.content); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to add the private note to the account: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully added the private note to the account.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-03 06:53:31 +01:00
|
|
|
|
|
|
|
func (a *AddExecutor) addToBookmarks(gtsClient *client.Client) error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceStatus: a.addStatusToBookmarks,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedAddOperationError{
|
|
|
|
ResourceType: a.resourceType,
|
|
|
|
AddToResourceType: a.toResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addStatusToBookmarks(gtsClient *client.Client) error {
|
|
|
|
if a.statusID == "" {
|
|
|
|
return FlagNotSetError{flagText: flagStatusID}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.AddStatusToBookmarks(a.statusID); err != nil {
|
|
|
|
return fmt.Errorf("unable to add the status to your bookmarks: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully added the status to your bookmarks.")
|
2024-06-03 06:53:31 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-04 17:01:33 +01:00
|
|
|
|
|
|
|
func (a *AddExecutor) addToStatus(gtsClient *client.Client) error {
|
2024-06-04 18:38:47 +01:00
|
|
|
if a.statusID == "" {
|
|
|
|
return FlagNotSetError{flagText: flagStatusID}
|
|
|
|
}
|
|
|
|
|
2024-06-04 17:01:33 +01:00
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-06-04 18:38:47 +01:00
|
|
|
resourceStar: a.addStarToStatus,
|
|
|
|
resourceLike: a.addStarToStatus,
|
|
|
|
resourceBoost: a.addBoostToStatus,
|
2024-06-04 17:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedAddOperationError{
|
|
|
|
ResourceType: a.resourceType,
|
|
|
|
AddToResourceType: a.toResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addStarToStatus(gtsClient *client.Client) error {
|
|
|
|
if err := gtsClient.LikeStatus(a.statusID); err != nil {
|
|
|
|
return fmt.Errorf("unable to add the %s to the status: %w", a.resourceType, err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully added a " + a.resourceType + " to the status.")
|
2024-06-04 17:01:33 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-04 18:38:47 +01:00
|
|
|
|
|
|
|
func (a *AddExecutor) addBoostToStatus(gtsClient *client.Client) error {
|
|
|
|
if err := gtsClient.ReblogStatus(a.statusID); err != nil {
|
|
|
|
return fmt.Errorf("unable to add the boost to the status: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully added the boost to the status.")
|
2024-06-04 18:38:47 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-15 18:40:13 +01:00
|
|
|
|
|
|
|
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 PollClosedError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !poll.Multiple && len(a.choices) > 1 {
|
|
|
|
return MultipleChoiceError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully added your vote(s) to the poll.")
|
2024-06-15 18:40:13 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|