checkpoint: updated add and remove

This commit is contained in:
Dan Anglin 2024-08-13 12:38:55 +01:00
parent 8cffac2074
commit 22b47e4b40
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 39 additions and 18 deletions

View file

@ -9,7 +9,12 @@ import (
"codeflow.dananglin.me.uk/apollo/enbas/internal/model" "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) account, err := getAccount(gtsClient, myAccount, accountNames, credentialsFile)
if err != nil { if err != nil {
return "", fmt.Errorf("unable to get the account information: %w", err) 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 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
}

View file

@ -58,24 +58,24 @@ func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error {
return NoAccountSpecifiedError{} 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 { accountIDs := make([]string, len(accounts))
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)
}
relationship, err := gtsClient.GetAccountRelationship(accountID) for ind := range accounts {
relationship, err := gtsClient.GetAccountRelationship(accounts[ind].ID)
if err != nil { 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 { 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 { if err := gtsClient.AddAccountsToList(a.listID, accountIDs); err != nil {

View file

@ -56,15 +56,15 @@ func (r *RemoveExecutor) removeAccountsFromList(gtsClient *client.Client) error
return NoAccountSpecifiedError{} 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 { accountIDs := make([]string, len(accounts))
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[ind] = accountID for ind := range accounts {
accountIDs[ind] = accounts[ind].ID
} }
if err := gtsClient.RemoveAccountsFromList(r.listID, accountIDs); err != nil { if err := gtsClient.RemoveAccountsFromList(r.listID, accountIDs); err != nil {