diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index 01dc729..ad0939d 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -260,7 +260,7 @@ func (c *showCommand) showFollowers(gts *client.Client) error { return fmt.Errorf("unable to retrieve the list of followers; %w", err) } - if len(followers) > 0 { + if len(followers.Accounts) > 0 { fmt.Println(followers) } else { fmt.Println("There are no followers for this account or the list is hidden.") @@ -292,7 +292,7 @@ func (c *showCommand) showFollowing(gts *client.Client) error { return fmt.Errorf("unable to retrieve the list of followed accounts; %w", err) } - if len(following) > 0 { + if len(following.Accounts) > 0 { fmt.Println(following) } else { fmt.Println("This account is not following anyone or the list is hidden.") @@ -307,7 +307,7 @@ func (c *showCommand) showBlocked(gts *client.Client) error { return fmt.Errorf("unable to retrieve the list of blocked accounts; %w", err) } - if len(blocked) > 0 { + if len(blocked.Accounts) > 0 { fmt.Println(blocked) } else { fmt.Println("You have no blocked accounts.") diff --git a/internal/client/accounts.go b/internal/client/accounts.go index 43411bc..490e8a3 100644 --- a/internal/client/accounts.go +++ b/internal/client/accounts.go @@ -88,25 +88,35 @@ func (g *Client) UnfollowAccount(accountID string) error { return nil } -func (g *Client) GetFollowers(accountID string, limit int) (model.Followers, error) { +func (g *Client) GetFollowers(accountID string, limit int) (model.AccountList, error) { url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/followers?limit=%d", accountID, limit) - var followers model.Followers + accounts := make([]model.Account, limit) - if err := g.sendRequest(http.MethodGet, url, nil, &followers); err != nil { - return nil, fmt.Errorf("received an error after sending the request to get the list of followers; %w", err) + if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of followers; %w", err) + } + + followers := model.AccountList{ + Type: model.AccountListFollowers, + Accounts: accounts, } return followers, nil } -func (g *Client) GetFollowing(accountID string, limit int) (model.Following, error) { +func (g *Client) GetFollowing(accountID string, limit int) (model.AccountList, error) { url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/following?limit=%d", accountID, limit) - var following model.Following + accounts := make([]model.Account, limit) - if err := g.sendRequest(http.MethodGet, url, nil, &following); err != nil { - return nil, fmt.Errorf("received an error after sending the request to get the list of followed accounts; %w", err) + if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of followed accounts; %w", err) + } + + following := model.AccountList{ + Type: model.AccountListFollowing, + Accounts: accounts, } return following, nil @@ -132,13 +142,18 @@ func (g *Client) UnblockAccount(accountID string) error { return nil } -func (g *Client) GetBlockedAccounts(limit int) (model.BlockedAccounts, error) { +func (g *Client) GetBlockedAccounts(limit int) (model.AccountList, error) { url := g.Authentication.Instance + fmt.Sprintf("/api/v1/blocks?limit=%d", limit) - var blocked model.BlockedAccounts + accounts := make([]model.Account, limit) - if err := g.sendRequest(http.MethodGet, url, nil, &blocked); err != nil { - return nil, fmt.Errorf("received an error after sending the request to get the list of blocked accounts; %w", err) + if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of blocked accounts; %w", err) + } + + blocked := model.AccountList{ + Type: model.AccountListBlockedAccount, + Accounts: accounts, } return blocked, nil diff --git a/internal/model/account.go b/internal/model/account.go index febb024..368953b 100644 --- a/internal/model/account.go +++ b/internal/model/account.go @@ -112,3 +112,51 @@ func (a Account) String() string { a.URL, ) } + +type AccountListType int + +const ( + AccountListFollowers AccountListType = iota + AccountListFollowing + AccountListBlockedAccount +) + +type AccountList struct { + Type AccountListType + Accounts []Account +} + +func (a AccountList) String() string { + output := "\n" + + switch a.Type { + case AccountListFollowers: + output += utilities.HeaderFormat("FOLLOWED BY:") + case AccountListFollowing: + output += utilities.HeaderFormat("FOLLOWING:") + case AccountListBlockedAccount: + output += utilities.HeaderFormat("BLOCKED ACCOUNTS:") + default: + output += utilities.HeaderFormat("ACCOUNTS:") + } + + if a.Type == AccountListBlockedAccount { + for i := range a.Accounts { + output += fmt.Sprintf( + "\n • %s (%s)", + a.Accounts[i].Acct, + a.Accounts[i].ID, + ) + } + } else { + for i := range a.Accounts { + output += fmt.Sprintf( + "\n • %s (%s)", + utilities.DisplayNameFormat(a.Accounts[i].DisplayName), + a.Accounts[i].Acct, + ) + } + } + + return output +} diff --git a/internal/model/blocked.go b/internal/model/blocked.go deleted file mode 100644 index 7bebb70..0000000 --- a/internal/model/blocked.go +++ /dev/null @@ -1,24 +0,0 @@ -package model - -import ( - "fmt" - - "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" -) - -type BlockedAccounts []Account - -func (b BlockedAccounts) String() string { - output := "\n" - output += utilities.HeaderFormat("BLOCKED ACCOUNTS:") - - for i := range b { - output += fmt.Sprintf( - "\n • %s (%s)", - b[i].Acct, - b[i].ID, - ) - } - - return output -} diff --git a/internal/model/follows.go b/internal/model/follows.go deleted file mode 100644 index 18beeed..0000000 --- a/internal/model/follows.go +++ /dev/null @@ -1,41 +0,0 @@ -package model - -import ( - "fmt" - - "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" -) - -type Followers []Account - -func (f Followers) String() string { - output := "\n" - output += utilities.HeaderFormat("FOLLOWED BY:") - - for i := range f { - output += fmt.Sprintf( - "\n • %s (%s)", - utilities.DisplayNameFormat(f[i].DisplayName), - f[i].Acct, - ) - } - - return output -} - -type Following []Account - -func (f Following) String() string { - output := "\n" - output += utilities.HeaderFormat("FOLLOWING:") - - for i := range f { - output += fmt.Sprintf( - "\n • %s (%s)", - utilities.DisplayNameFormat(f[i].DisplayName), - f[i].Acct, - ) - } - - return output -}