From 22b47e4b40110864ba4e79add282d2c9ebbcaf2d Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 13 Aug 2024 12:38:55 +0100 Subject: [PATCH] checkpoint: updated add and remove --- internal/executor/account.go | 23 ++++++++++++++++++++++- internal/executor/add.go | 20 ++++++++++---------- internal/executor/remove.go | 14 +++++++------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/internal/executor/account.go b/internal/executor/account.go index 546107b..6660a37 100644 --- a/internal/executor/account.go +++ b/internal/executor/account.go @@ -9,7 +9,12 @@ import ( "codeflow.dananglin.me.uk/apollo/enbas/internal/model" ) -func getAccountID(gtsClient *client.Client, myAccount bool, accountNames internalFlag.StringSliceValue, credentialsFile string) (string, error) { +func getAccountID( + gtsClient *client.Client, + myAccount bool, + accountNames internalFlag.StringSliceValue, + credentialsFile string, +) (string, error) { account, err := getAccount(gtsClient, myAccount, accountNames, credentialsFile) if err != nil { return "", fmt.Errorf("unable to get the account information: %w", err) @@ -79,3 +84,19 @@ func getOtherAccount(gtsClient *client.Client, accountNames internalFlag.StringS return account, nil } + +func getOtherAccounts(gtsClient *client.Client, accountNames internalFlag.StringSliceValue) ([]model.Account, error) { + numAccountNames := len(accountNames) + accounts := make([]model.Account, numAccountNames) + + for ind := 0; ind < numAccountNames; ind++ { + var err error + + accounts[ind], err = gtsClient.GetAccount(accountNames[ind]) + if err != nil { + return nil, fmt.Errorf("unable to retrieve the account information for %s: %w", accountNames[ind], err) + } + } + + return accounts, nil +} diff --git a/internal/executor/add.go b/internal/executor/add.go index b72b3a4..b46d03b 100644 --- a/internal/executor/add.go +++ b/internal/executor/add.go @@ -58,24 +58,24 @@ func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error { return NoAccountSpecifiedError{} } - accountIDs := make([]string, len(a.accountNames)) + accounts, err := getOtherAccounts(gtsClient, a.accountNames) + if err != nil { + return fmt.Errorf("unable to get the accounts: %w", err) + } - for ind := range a.accountNames { - accountID, err := getTheirAccountID(gtsClient, a.accountNames[ind]) - if err != nil { - return fmt.Errorf("unable to get the account ID for %s: %w", a.accountNames[ind], err) - } + accountIDs := make([]string, len(accounts)) - relationship, err := gtsClient.GetAccountRelationship(accountID) + for ind := range accounts { + relationship, err := gtsClient.GetAccountRelationship(accounts[ind].ID) if err != nil { - return fmt.Errorf("unable to get your relationship to %s: %w", a.accountNames[ind], err) + return fmt.Errorf("unable to get your relationship to %s: %w", accounts[ind].Acct, err) } if !relationship.Following { - return NotFollowingError{Account: a.accountNames[ind]} + return NotFollowingError{Account: accounts[ind].Acct} } - accountIDs[ind] = accountID + accountIDs[ind] = accounts[ind].ID } if err := gtsClient.AddAccountsToList(a.listID, accountIDs); err != nil { diff --git a/internal/executor/remove.go b/internal/executor/remove.go index f03060b..34c770d 100644 --- a/internal/executor/remove.go +++ b/internal/executor/remove.go @@ -56,15 +56,15 @@ func (r *RemoveExecutor) removeAccountsFromList(gtsClient *client.Client) error return NoAccountSpecifiedError{} } - accountIDs := make([]string, len(r.accountNames)) + accounts, err := getOtherAccounts(gtsClient, r.accountNames) + if err != nil { + return fmt.Errorf("unable to get the accounts: %w", err) + } - for ind := range r.accountNames { - accountID, err := getTheirAccountID(gtsClient, r.accountNames[ind]) - if err != nil { - return fmt.Errorf("unable to get the account ID for %s: %w", r.accountNames[ind], err) - } + accountIDs := make([]string, len(accounts)) - accountIDs[ind] = accountID + for ind := range accounts { + accountIDs[ind] = accounts[ind].ID } if err := gtsClient.RemoveAccountsFromList(r.listID, accountIDs); err != nil {