2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-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 {
|
2024-08-18 08:59:44 +01:00
|
|
|
if a.listID == "" {
|
|
|
|
return MissingIDError{
|
|
|
|
resource: resourceList,
|
|
|
|
action: "add to",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceAccount: a.addAccountsToList,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedAddOperationError{
|
2024-08-17 11:23:21 +01:00
|
|
|
resourceType: a.resourceType,
|
|
|
|
addToResourceType: a.toResourceType,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error {
|
2024-08-13 14:53:26 +01:00
|
|
|
if a.accountNames.Empty() {
|
2024-05-23 18:06:49 +01:00
|
|
|
return NoAccountSpecifiedError{}
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
accounts, err := getOtherAccounts(gtsClient, a.accountNames)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the accounts: %w", err)
|
|
|
|
}
|
2024-05-23 18:06:49 +01:00
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
accountIDs := make([]string, len(accounts))
|
2024-06-16 20:29:16 +01:00
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
for ind := range accounts {
|
|
|
|
relationship, err := gtsClient.GetAccountRelationship(accounts[ind].ID)
|
2024-06-16 20:29:16 +01:00
|
|
|
if err != nil {
|
2024-08-13 14:53:26 +01:00
|
|
|
return fmt.Errorf("unable to get your relationship to %s: %w", accounts[ind].Acct, err)
|
2024-06-16 20:29:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !relationship.Following {
|
2024-08-17 11:23:21 +01:00
|
|
|
return NotFollowingError{account: accounts[ind].Acct}
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
accountIDs[ind] = accounts[ind].ID
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2024-08-17 11:23:21 +01:00
|
|
|
resourceType: a.resourceType,
|
|
|
|
addToResourceType: a.toResourceType,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addNoteToAccount(gtsClient *client.Client) error {
|
2024-08-13 23:51:16 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, a.accountNames)
|
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-08-17 11:23:21 +01:00
|
|
|
return Error{"please add content to the note you want to add"}
|
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{
|
2024-08-17 11:23:21 +01:00
|
|
|
resourceType: a.resourceType,
|
|
|
|
addToResourceType: a.toResourceType,
|
2024-06-03 06:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddExecutor) addStatusToBookmarks(gtsClient *client.Client) error {
|
|
|
|
if a.statusID == "" {
|
2024-08-18 08:59:44 +01:00
|
|
|
return MissingIDError{
|
|
|
|
resource: resourceStatus,
|
|
|
|
action: "add to your bookmarks",
|
|
|
|
}
|
2024-06-03 06:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 == "" {
|
2024-08-18 08:59:44 +01:00
|
|
|
return MissingIDError{
|
|
|
|
resource: resourceStatus,
|
|
|
|
action: "add to",
|
|
|
|
}
|
2024-06-04 18:38:47 +01:00
|
|
|
}
|
|
|
|
|
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-08-14 11:18:38 +01:00
|
|
|
resourceVote: a.addVoteToStatus,
|
2024-06-04 17:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedAddOperationError{
|
2024-08-17 11:23:21 +01:00
|
|
|
resourceType: a.resourceType,
|
|
|
|
addToResourceType: a.toResourceType,
|
2024-06-04 17:01:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
func (a *AddExecutor) addVoteToStatus(gtsClient *client.Client) error {
|
|
|
|
if a.votes.Empty() {
|
2024-08-17 11:23:21 +01:00
|
|
|
return Error{"please add your vote(s) to the poll"}
|
2024-06-15 18:40:13 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
status, err := gtsClient.GetStatus(a.statusID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the status: %w", err)
|
2024-06-15 18:40:13 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
if status.Poll == nil {
|
2024-08-17 11:23:21 +01:00
|
|
|
return Error{"this status does not have a poll"}
|
2024-06-15 18:40:13 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
if status.Poll.Expired {
|
2024-08-17 11:23:21 +01:00
|
|
|
return Error{"this poll is closed"}
|
2024-08-14 11:18:38 +01:00
|
|
|
}
|
2024-06-15 18:40:13 +01:00
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
if !status.Poll.Multiple && !a.votes.ExpectedLength(1) {
|
2024-08-17 11:23:21 +01:00
|
|
|
return Error{"this poll does not allow multiple choices"}
|
2024-06-15 18:40:13 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
myAccountID, err := getAccountID(gtsClient, true, nil)
|
2024-06-15 18:40:13 +01:00
|
|
|
if err != nil {
|
2024-08-14 11:18:38 +01:00
|
|
|
return fmt.Errorf("unable to get your account ID: %w", err)
|
2024-06-15 18:40:13 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
if status.Account.ID == myAccountID {
|
2024-08-17 11:23:21 +01:00
|
|
|
return Error{"you cannot vote in your own poll"}
|
2024-06-15 18:40:13 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
pollID := status.Poll.ID
|
2024-06-15 18:40:13 +01:00
|
|
|
|
2024-08-14 11:18:38 +01:00
|
|
|
if err := gtsClient.VoteInPoll(pollID, []int(a.votes)); err != nil {
|
2024-06-15 18:40:13 +01:00
|
|
|
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
|
|
|
|
}
|