checkpoint: show followers

This commit is contained in:
Dan Anglin 2024-05-20 14:59:03 +01:00
parent 0e8f83f4e0
commit c080568c2d
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 78 additions and 17 deletions

View file

@ -20,16 +20,17 @@ const (
statusIDFlag = "status-id" statusIDFlag = "status-id"
tagFlag = "tag" tagFlag = "tag"
timelineCategoryFlag = "timeline-category" timelineCategoryFlag = "timeline-category"
timelineLimitFlag = "timeline-limit" limitFlag = "limit"
toAccountFlag = "to-account" toAccountFlag = "to-account"
) )
const ( const (
accountResource = "account" accountResource = "account"
instanceResource = "instance" instanceResource = "instance"
listResource = "list" listResource = "list"
statusResource = "status" statusResource = "status"
timelineResource = "timeline" timelineResource = "timeline"
followersResource = "followers"
) )
type Executor interface { type Executor interface {

View file

@ -16,11 +16,12 @@ type showCommand struct {
showAccountRelationship bool showAccountRelationship bool
resourceType string resourceType string
account string account string
accountID string
statusID string statusID string
timelineCategory string timelineCategory string
listID string listID string
tag string tag string
timelineLimit int limit int
} }
func newShowCommand(name, summary string) *showCommand { func newShowCommand(name, summary string) *showCommand {
@ -32,11 +33,12 @@ func newShowCommand(name, summary string) *showCommand {
command.BoolVar(&command.showAccountRelationship, "show-account-relationship", false, "show your relationship to the specified account") command.BoolVar(&command.showAccountRelationship, "show-account-relationship", false, "show your relationship to the specified account")
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to display") command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to display")
command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup") command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup")
command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID")
command.StringVar(&command.statusID, statusIDFlag, "", "specify the ID of the status to display") command.StringVar(&command.statusID, statusIDFlag, "", "specify the ID of the status to display")
command.StringVar(&command.timelineCategory, timelineCategoryFlag, "home", "specify the type of timeline to display (valid values are home, public, list and tag)") command.StringVar(&command.timelineCategory, timelineCategoryFlag, "home", "specify the type of timeline to display (valid values are home, public, list and tag)")
command.StringVar(&command.listID, listIDFlag, "", "specify the ID of the list to display") command.StringVar(&command.listID, listIDFlag, "", "specify the ID of the list to display")
command.StringVar(&command.tag, tagFlag, "", "specify the name of the tag to use") command.StringVar(&command.tag, tagFlag, "", "specify the name of the tag to use")
command.IntVar(&command.timelineLimit, timelineLimitFlag, 5, "specify the number of statuses to display") command.IntVar(&command.limit, limitFlag, 20, "specify the limit of items to display")
command.Usage = commandUsageFunc(name, summary, command.FlagSet) command.Usage = commandUsageFunc(name, summary, command.FlagSet)
@ -49,11 +51,12 @@ func (c *showCommand) Execute() error {
} }
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{
instanceResource: c.showInstance, instanceResource: c.showInstance,
accountResource: c.showAccount, accountResource: c.showAccount,
statusResource: c.showStatus, statusResource: c.showStatus,
timelineResource: c.showTimeline, timelineResource: c.showTimeline,
listResource: c.showList, listResource: c.showList,
followersResource: c.showFollowers,
} }
doFunc, ok := funcMap[c.resourceType] doFunc, ok := funcMap[c.resourceType]
@ -140,21 +143,21 @@ func (c *showCommand) showTimeline(gts *client.Client) error {
switch c.timelineCategory { switch c.timelineCategory {
case "home": case "home":
timeline, err = gts.GetHomeTimeline(c.timelineLimit) timeline, err = gts.GetHomeTimeline(c.limit)
case "public": case "public":
timeline, err = gts.GetPublicTimeline(c.timelineLimit) timeline, err = gts.GetPublicTimeline(c.limit)
case "list": case "list":
if c.listID == "" { if c.listID == "" {
return flagNotSetError{flagText: listIDFlag} return flagNotSetError{flagText: listIDFlag}
} }
timeline, err = gts.GetListTimeline(c.listID, c.timelineLimit) timeline, err = gts.GetListTimeline(c.listID, c.limit)
case "tag": case "tag":
if c.tag == "" { if c.tag == "" {
return flagNotSetError{flagText: tagFlag} return flagNotSetError{flagText: tagFlag}
} }
timeline, err = gts.GetTagTimeline(c.tag, c.timelineLimit) timeline, err = gts.GetTagTimeline(c.tag, c.limit)
default: default:
return invalidTimelineCategoryError{category: c.timelineCategory} return invalidTimelineCategoryError{category: c.timelineCategory}
} }
@ -219,3 +222,22 @@ func (c *showCommand) showLists(gts *client.Client) error {
return nil return nil
} }
func (c *showCommand) showFollowers(gts *client.Client) error {
if c.accountID == "" {
return flagNotSetError{flagText: accountIDFlag}
}
followers, err := gts.GetFollowers(c.accountID, c.limit)
if err != nil {
return fmt.Errorf("unable to retrieve the list of followers; %w", err)
}
if len(followers) > 0 {
fmt.Println(followers)
} else {
fmt.Println("There are no followers for this account or the list is hidden.")
}
return nil
}

View file

@ -5,6 +5,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
) )
func (g *Client) FollowAccount(accountID string, reblogs, notify bool) error { func (g *Client) FollowAccount(accountID string, reblogs, notify bool) error {
@ -42,3 +44,15 @@ func (g *Client) UnfollowAccount(accountID string) error {
return nil return nil
} }
func (g *Client) GetFollowers(accountID string, limit int) (model.Followers, error) {
url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/followers?limit=%d", accountID, limit)
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 followers, nil
}

24
internal/model/follows.go Normal file
View file

@ -0,0 +1,24 @@
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("FOLLOWERS:")
for i := range f {
output += fmt.Sprintf(
"\n • %s (%s)",
utilities.DisplayNameFormat(f[i].DisplayName),
f[i].Acct,
)
}
return output
}