refactor: add new AccountList type for accounts

Add a new AccountList type for handling the printing of different list
of accounts (such as following, followers and blocked accounts).
This commit is contained in:
Dan Anglin 2024-05-21 14:30:50 +01:00
parent dffd324243
commit 9d8cb2c68e
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
5 changed files with 78 additions and 80 deletions

View file

@ -260,7 +260,7 @@ func (c *showCommand) showFollowers(gts *client.Client) error {
return fmt.Errorf("unable to retrieve the list of followers; %w", err) return fmt.Errorf("unable to retrieve the list of followers; %w", err)
} }
if len(followers) > 0 { if len(followers.Accounts) > 0 {
fmt.Println(followers) fmt.Println(followers)
} else { } else {
fmt.Println("There are no followers for this account or the list is hidden.") 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) 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) fmt.Println(following)
} else { } else {
fmt.Println("This account is not following anyone or the list is hidden.") 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) 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) fmt.Println(blocked)
} else { } else {
fmt.Println("You have no blocked accounts.") fmt.Println("You have no blocked accounts.")

View file

@ -88,25 +88,35 @@ func (g *Client) UnfollowAccount(accountID string) error {
return nil 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) 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 { if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil {
return nil, fmt.Errorf("received an error after sending the request to get the list of followers; %w", err) 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 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) 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 { if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil {
return nil, fmt.Errorf("received an error after sending the request to get the list of followed accounts; %w", err) 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 return following, nil
@ -132,13 +142,18 @@ func (g *Client) UnblockAccount(accountID string) error {
return nil 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) 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 { if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil {
return nil, fmt.Errorf("received an error after sending the request to get the list of blocked accounts; %w", err) 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 return blocked, nil

View file

@ -112,3 +112,51 @@ func (a Account) String() string {
a.URL, 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
}

View file

@ -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
}

View file

@ -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
}