2024-06-02 07:53:13 +01:00
|
|
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
2024-02-24 15:53:29 +00:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
2024-02-27 19:52:59 +00:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
2024-02-23 09:44:57 +00:00
|
|
|
)
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
type ShowExecutor struct {
|
2024-02-23 09:44:57 +00:00
|
|
|
*flag.FlagSet
|
2024-05-23 18:06:49 +01:00
|
|
|
topLevelFlags TopLevelFlags
|
2024-05-20 17:26:06 +01:00
|
|
|
myAccount bool
|
2024-05-23 18:22:13 +01:00
|
|
|
skipAccountRelationship bool
|
2024-05-20 19:46:05 +01:00
|
|
|
showUserPreferences bool
|
2024-05-29 20:26:10 +01:00
|
|
|
showInBrowser bool
|
2024-05-20 17:26:06 +01:00
|
|
|
resourceType string
|
2024-05-21 21:06:18 +01:00
|
|
|
accountName string
|
2024-05-20 17:26:06 +01:00
|
|
|
statusID string
|
|
|
|
timelineCategory string
|
|
|
|
listID string
|
|
|
|
tag string
|
|
|
|
limit int
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func NewShowExecutor(tlf TopLevelFlags, name, summary string) *ShowExecutor {
|
|
|
|
command := ShowExecutor{
|
2024-05-22 23:30:09 +01:00
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
topLevelFlags: tlf,
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
command.BoolVar(&command.myAccount, flagMyAccount, false, "set to true to lookup your account")
|
2024-05-23 18:22:13 +01:00
|
|
|
command.BoolVar(&command.skipAccountRelationship, flagSkipRelationship, false, "set to true to skip showing your relationship to the specified account")
|
2024-05-23 18:06:49 +01:00
|
|
|
command.BoolVar(&command.showUserPreferences, flagShowPreferences, false, "show your preferences")
|
2024-05-29 20:26:10 +01:00
|
|
|
command.BoolVar(&command.showInBrowser, flagBrowser, false, "set to true to view in the browser")
|
2024-05-23 18:06:49 +01:00
|
|
|
command.StringVar(&command.resourceType, flagType, "", "specify the type of resource to display")
|
|
|
|
command.StringVar(&command.accountName, flagAccountName, "", "specify the account name in full (username@domain)")
|
|
|
|
command.StringVar(&command.statusID, flagStatusID, "", "specify the ID of the status to display")
|
2024-06-02 11:35:43 +01:00
|
|
|
command.StringVar(&command.timelineCategory, flagTimelineCategory, model.TimelineCategoryHome, "specify the timeline category to view")
|
2024-05-23 18:06:49 +01:00
|
|
|
command.StringVar(&command.listID, flagListID, "", "specify the ID of the list to display")
|
|
|
|
command.StringVar(&command.tag, flagTag, "", "specify the name of the tag to use")
|
|
|
|
command.IntVar(&command.limit, flagLimit, 20, "specify the limit of items to display")
|
2024-02-28 19:35:18 +00:00
|
|
|
|
2024-02-23 09:44:57 +00:00
|
|
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
|
|
|
|
|
|
|
return &command
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) Execute() error {
|
2024-02-27 19:52:59 +00:00
|
|
|
if c.resourceType == "" {
|
2024-05-23 18:06:49 +01:00
|
|
|
return FlagNotSetError{flagText: flagType}
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-05-23 18:06:49 +01:00
|
|
|
resourceInstance: c.showInstance,
|
|
|
|
resourceAccount: c.showAccount,
|
|
|
|
resourceStatus: c.showStatus,
|
|
|
|
resourceTimeline: c.showTimeline,
|
|
|
|
resourceList: c.showList,
|
|
|
|
resourceFollowers: c.showFollowers,
|
|
|
|
resourceFollowing: c.showFollowing,
|
|
|
|
resourceBlocked: c.showBlocked,
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 19:52:59 +00:00
|
|
|
doFunc, ok := funcMap[c.resourceType]
|
2024-02-23 09:44:57 +00:00
|
|
|
if !ok {
|
2024-05-23 18:06:49 +01:00
|
|
|
return UnsupportedTypeError{resourceType: c.resourceType}
|
2024-02-28 19:35:18 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.ConfigDir)
|
2024-02-28 19:35:18 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showInstance(gtsClient *client.Client) error {
|
2024-05-21 21:06:18 +01:00
|
|
|
instance, err := gtsClient.GetInstance()
|
2024-02-23 09:44:57 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the instance details: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(instance, *c.topLevelFlags.NoColor)
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showAccount(gtsClient *client.Client) error {
|
2024-05-20 18:37:29 +01:00
|
|
|
var (
|
|
|
|
account model.Account
|
|
|
|
err error
|
|
|
|
)
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
if c.myAccount {
|
2024-05-23 18:06:49 +01:00
|
|
|
account, err = getMyAccount(gtsClient, c.topLevelFlags.ConfigDir)
|
2024-02-23 09:44:57 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error while getting the account details: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-05-21 21:06:18 +01:00
|
|
|
if c.accountName == "" {
|
2024-05-23 18:06:49 +01:00
|
|
|
return FlagNotSetError{flagText: flagAccountName}
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:06:18 +01:00
|
|
|
account, err = getAccount(gtsClient, c.accountName)
|
2024-05-20 18:37:29 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error while getting the account details: %w", err)
|
2024-05-20 18:37:29 +01:00
|
|
|
}
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 20:26:10 +01:00
|
|
|
if c.showInBrowser {
|
|
|
|
utilities.OpenLink(account.URL)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(account, *c.topLevelFlags.NoColor)
|
2024-02-23 09:44:57 +00:00
|
|
|
|
2024-05-23 18:22:13 +01:00
|
|
|
if !c.myAccount && !c.skipAccountRelationship {
|
2024-05-21 21:06:18 +01:00
|
|
|
relationship, err := gtsClient.GetAccountRelationship(account.ID)
|
2024-05-20 17:26:06 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the relationship to this account: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(relationship, *c.topLevelFlags.NoColor)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-20 19:46:05 +01:00
|
|
|
if c.myAccount && c.showUserPreferences {
|
2024-05-21 21:06:18 +01:00
|
|
|
preferences, err := gtsClient.GetUserPreferences()
|
2024-05-20 19:46:05 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the user preferences: %w", err)
|
2024-05-20 19:46:05 +01:00
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(preferences, *c.topLevelFlags.NoColor)
|
2024-05-20 19:46:05 +01:00
|
|
|
}
|
|
|
|
|
2024-02-23 09:44:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showStatus(gtsClient *client.Client) error {
|
2024-02-23 13:14:50 +00:00
|
|
|
if c.statusID == "" {
|
2024-05-23 18:06:49 +01:00
|
|
|
return FlagNotSetError{flagText: flagStatusID}
|
2024-02-23 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:06:18 +01:00
|
|
|
status, err := gtsClient.GetStatus(c.statusID)
|
2024-02-23 13:14:50 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the status: %w", err)
|
2024-02-23 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 20:26:10 +01:00
|
|
|
if c.showInBrowser {
|
|
|
|
utilities.OpenLink(status.URL)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(status, *c.topLevelFlags.NoColor)
|
2024-02-23 13:14:50 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-24 15:53:29 +00:00
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showTimeline(gtsClient *client.Client) error {
|
2024-02-24 15:53:29 +00:00
|
|
|
var (
|
2024-06-03 03:54:27 +01:00
|
|
|
timeline model.StatusList
|
2024-02-24 15:53:29 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2024-02-28 19:35:18 +00:00
|
|
|
switch c.timelineCategory {
|
2024-06-02 11:35:43 +01:00
|
|
|
case model.TimelineCategoryHome:
|
2024-05-21 21:06:18 +01:00
|
|
|
timeline, err = gtsClient.GetHomeTimeline(c.limit)
|
2024-06-02 11:35:43 +01:00
|
|
|
case model.TimelineCategoryPublic:
|
2024-05-21 21:06:18 +01:00
|
|
|
timeline, err = gtsClient.GetPublicTimeline(c.limit)
|
2024-06-02 11:35:43 +01:00
|
|
|
case model.TimelineCategoryList:
|
2024-02-28 19:35:18 +00:00
|
|
|
if c.listID == "" {
|
2024-05-23 18:06:49 +01:00
|
|
|
return FlagNotSetError{flagText: flagListID}
|
2024-02-24 15:53:29 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 03:54:27 +01:00
|
|
|
var list model.List
|
|
|
|
|
|
|
|
list, err = gtsClient.GetList(c.listID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to retrieve the list: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timeline, err = gtsClient.GetListTimeline(list.ID, list.Title, c.limit)
|
2024-06-02 11:35:43 +01:00
|
|
|
case model.TimelineCategoryTag:
|
2024-02-28 19:35:18 +00:00
|
|
|
if c.tag == "" {
|
2024-05-23 18:06:49 +01:00
|
|
|
return FlagNotSetError{flagText: flagTag}
|
2024-02-24 15:53:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:06:18 +01:00
|
|
|
timeline, err = gtsClient.GetTagTimeline(c.tag, c.limit)
|
2024-02-24 15:53:29 +00:00
|
|
|
default:
|
2024-06-02 11:35:43 +01:00
|
|
|
return model.InvalidTimelineCategoryError{Value: c.timelineCategory}
|
2024-02-24 15:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the %s timeline: %w", c.timelineCategory, err)
|
2024-02-24 15:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(timeline.Statuses) == 0 {
|
|
|
|
fmt.Println("There are no statuses in this timeline.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(timeline, *c.topLevelFlags.NoColor)
|
2024-02-24 15:53:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-27 19:52:59 +00:00
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showList(gtsClient *client.Client) error {
|
2024-05-19 11:48:36 +01:00
|
|
|
if c.listID == "" {
|
2024-05-21 21:06:18 +01:00
|
|
|
return c.showLists(gtsClient)
|
2024-05-19 11:48:36 +01:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:06:18 +01:00
|
|
|
list, err := gtsClient.GetList(c.listID)
|
2024-05-19 11:48:36 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the list: %w", err)
|
2024-05-19 11:48:36 +01:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:06:18 +01:00
|
|
|
accounts, err := gtsClient.GetAccountsFromList(c.listID, 0)
|
2024-05-19 11:48:36 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the accounts from the list: %w", err)
|
2024-05-19 11:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(accounts) > 0 {
|
|
|
|
accountMap := make(map[string]string)
|
|
|
|
for i := range accounts {
|
2024-05-31 21:41:10 +01:00
|
|
|
accountMap[accounts[i].Acct] = accounts[i].Username
|
2024-05-19 11:48:36 +01:00
|
|
|
}
|
2024-05-20 17:26:06 +01:00
|
|
|
|
2024-05-19 11:48:36 +01:00
|
|
|
list.Accounts = accountMap
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(list, *c.topLevelFlags.NoColor)
|
2024-05-19 11:48:36 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showLists(gtsClient *client.Client) error {
|
2024-05-21 21:06:18 +01:00
|
|
|
lists, err := gtsClient.GetAllLists()
|
2024-02-27 19:52:59 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the lists: %w", err)
|
2024-02-27 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(lists) == 0 {
|
|
|
|
fmt.Println("You have no lists.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(lists, *c.topLevelFlags.NoColor)
|
2024-02-27 19:52:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-20 17:26:06 +01:00
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showFollowers(gtsClient *client.Client) error {
|
|
|
|
accountID, err := getAccountID(gtsClient, c.myAccount, c.accountName, c.topLevelFlags.ConfigDir)
|
2024-05-21 21:06:18 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:06:18 +01:00
|
|
|
followers, err := gtsClient.GetFollowers(accountID, c.limit)
|
2024-05-20 17:26:06 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the list of followers: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:30:50 +01:00
|
|
|
if len(followers.Accounts) > 0 {
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(followers, *c.topLevelFlags.NoColor)
|
2024-05-20 17:26:06 +01:00
|
|
|
} else {
|
|
|
|
fmt.Println("There are no followers for this account or the list is hidden.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showFollowing(gtsClient *client.Client) error {
|
|
|
|
accountID, err := getAccountID(gtsClient, c.myAccount, c.accountName, c.topLevelFlags.ConfigDir)
|
2024-05-21 21:06:18 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:06:18 +01:00
|
|
|
following, err := gtsClient.GetFollowing(accountID, c.limit)
|
2024-05-20 17:26:06 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the list of followed accounts: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:30:50 +01:00
|
|
|
if len(following.Accounts) > 0 {
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(following, *c.topLevelFlags.NoColor)
|
2024-05-20 17:26:06 +01:00
|
|
|
} else {
|
|
|
|
fmt.Println("This account is not following anyone or the list is hidden.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-21 13:19:53 +01:00
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *ShowExecutor) showBlocked(gtsClient *client.Client) error {
|
2024-05-21 21:06:18 +01:00
|
|
|
blocked, err := gtsClient.GetBlockedAccounts(c.limit)
|
2024-05-21 13:19:53 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to retrieve the list of blocked accounts: %w", err)
|
2024-05-21 13:19:53 +01:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:30:50 +01:00
|
|
|
if len(blocked.Accounts) > 0 {
|
2024-05-31 21:18:11 +01:00
|
|
|
utilities.Display(blocked, *c.topLevelFlags.NoColor)
|
2024-05-21 13:19:53 +01:00
|
|
|
} else {
|
|
|
|
fmt.Println("You have no blocked accounts.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|