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" )
command . StringVar ( & command . timelineCategory , flagTimelineCategory , "home" , "specify the type of timeline to display (valid values are home, public, list and tag)" )
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 {
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 {
return fmt . Errorf ( "unable to retrieve the instance details; %w" , err )
}
2024-02-23 14:19:12 +00:00
fmt . Println ( instance )
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-05-21 21:06:18 +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-05-21 21:06:18 +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-02-23 14:19:12 +00:00
fmt . Println ( account )
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 {
return fmt . Errorf ( "unable to retrieve the relationship to this account; %w" , err )
}
fmt . Println ( relationship )
}
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 {
return fmt . Errorf ( "unable to retrieve the user preferences; %w" , err )
}
fmt . Println ( preferences )
}
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 {
return fmt . Errorf ( "unable to retrieve the status; %w" , err )
}
2024-05-29 20:26:10 +01:00
if c . showInBrowser {
utilities . OpenLink ( status . URL )
return nil
}
2024-02-23 14:19:12 +00:00
fmt . Println ( status )
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 (
timeline model . Timeline
err error
)
2024-02-28 19:35:18 +00:00
switch c . timelineCategory {
2024-02-24 15:53:29 +00:00
case "home" :
2024-05-21 21:06:18 +01:00
timeline , err = gtsClient . GetHomeTimeline ( c . limit )
2024-02-24 15:53:29 +00:00
case "public" :
2024-05-21 21:06:18 +01:00
timeline , err = gtsClient . GetPublicTimeline ( c . limit )
2024-02-24 15:53:29 +00:00
case "list" :
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-05-21 21:06:18 +01:00
timeline , err = gtsClient . GetListTimeline ( c . listID , c . limit )
2024-02-24 15:53:29 +00:00
case "tag" :
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-05-23 18:06:49 +01:00
return InvalidTimelineCategoryError { category : c . timelineCategory }
2024-02-24 15:53:29 +00:00
}
if err != nil {
2024-02-28 19:35:18 +00: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
}
fmt . Println ( timeline )
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 {
return fmt . Errorf ( "unable to retrieve the list; %w" , err )
}
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 {
return fmt . Errorf ( "unable to retrieve the accounts from the list; %w" , err )
}
if len ( accounts ) > 0 {
accountMap := make ( map [ string ] string )
for i := range accounts {
accountMap [ accounts [ i ] . ID ] = accounts [ i ] . Username
}
2024-05-20 17:26:06 +01:00
2024-05-19 11:48:36 +01:00
list . Accounts = accountMap
}
fmt . Println ( list )
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 {
return fmt . Errorf ( "unable to retrieve the lists; %w" , err )
}
if len ( lists ) == 0 {
fmt . Println ( "You have no lists." )
return nil
}
fmt . Println ( utilities . HeaderFormat ( "LISTS" ) )
2024-05-19 11:48:36 +01:00
fmt . Println ( lists )
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 {
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 {
return fmt . Errorf ( "unable to retrieve the list of followers; %w" , err )
}
2024-05-21 14:30:50 +01:00
if len ( followers . Accounts ) > 0 {
2024-05-20 17:26:06 +01:00
fmt . Println ( followers )
} 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 {
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 {
return fmt . Errorf ( "unable to retrieve the list of followed accounts; %w" , err )
}
2024-05-21 14:30:50 +01:00
if len ( following . Accounts ) > 0 {
2024-05-20 17:26:06 +01:00
fmt . Println ( following )
} 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 {
return fmt . Errorf ( "unable to retrieve the list of blocked accounts; %w" , err )
}
2024-05-21 14:30:50 +01:00
if len ( blocked . Accounts ) > 0 {
2024-05-21 13:19:53 +01:00
fmt . Println ( blocked )
} else {
fmt . Println ( "You have no blocked accounts." )
}
return nil
}