From 031dd9a0b0517c05a373bd57b0c35017f7ca5860 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 20 May 2024 15:41:45 +0100 Subject: [PATCH] checkpoint: display followed accounts --- cmd/enbas/main.go | 1 + cmd/enbas/show.go | 21 +++++++++++++++++++++ internal/client/follow.go | 14 +++++++++++++- internal/model/follows.go | 19 ++++++++++++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index fe5ff8b..3c6f1fb 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -31,6 +31,7 @@ const ( statusResource = "status" timelineResource = "timeline" followersResource = "followers" + followingResource = "following" ) type Executor interface { diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index 79643b3..15e987d 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -57,6 +57,7 @@ func (c *showCommand) Execute() error { timelineResource: c.showTimeline, listResource: c.showList, followersResource: c.showFollowers, + followingResource: c.showFollowing, } doFunc, ok := funcMap[c.resourceType] @@ -197,6 +198,7 @@ func (c *showCommand) showList(gts *client.Client) error { for i := range accounts { accountMap[accounts[i].ID] = accounts[i].Username } + list.Accounts = accountMap } @@ -241,3 +243,22 @@ func (c *showCommand) showFollowers(gts *client.Client) error { return nil } + +func (c *showCommand) showFollowing(gts *client.Client) error { + if c.accountID == "" { + return flagNotSetError{flagText: accountIDFlag} + } + + following, err := gts.GetFollowing(c.accountID, c.limit) + if err != nil { + return fmt.Errorf("unable to retrieve the list of followed accounts; %w", err) + } + + if len(following) > 0 { + fmt.Println(following) + } else { + fmt.Println("This account is not following anyone or the list is hidden.") + } + + return nil +} diff --git a/internal/client/follow.go b/internal/client/follow.go index cf1b4a5..cc3e416 100644 --- a/internal/client/follow.go +++ b/internal/client/follow.go @@ -51,8 +51,20 @@ func (g *Client) GetFollowers(accountID string, limit int) (model.Followers, err var followers model.Followers if err := g.sendRequest(http.MethodGet, url, nil, &followers); err != nil { - return nil, fmt.Errorf("received an error after sending the request to get followers; %w", err) + return nil, fmt.Errorf("received an error after sending the request to get the list of followers; %w", err) } return followers, nil } + +func (g *Client) GetFollowing(accountID string, limit int) (model.Following, error) { + url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/following?limit=%d", accountID, limit) + + var following model.Following + + 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) + } + + return following, nil +} diff --git a/internal/model/follows.go b/internal/model/follows.go index 77a5d74..18beeed 100644 --- a/internal/model/follows.go +++ b/internal/model/follows.go @@ -10,7 +10,24 @@ type Followers []Account func (f Followers) String() string { output := "\n" - output += utilities.HeaderFormat("FOLLOWERS:") + 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(