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:
parent
dffd324243
commit
9d8cb2c68e
5 changed files with 78 additions and 80 deletions
|
@ -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.")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue