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"
2024-06-22 01:16:24 +01:00
"path/filepath"
"strings"
2024-02-23 09:44:57 +00:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
2024-06-25 12:39:39 +01:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
2024-02-24 15:53:29 +00:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
2024-06-17 18:59:20 +01:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
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-06-17 18:59:20 +01:00
printer * printer . Printer
2024-06-25 12:39:39 +01:00
config * config . Config
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
2024-06-15 18:40:13 +01:00
pollID string
2024-06-22 01:16:24 +01:00
fromResourceType string
2024-05-20 17:26:06 +01:00
limit int
2024-06-22 01:16:24 +01:00
attachmentIDs MultiStringFlagValue
2024-02-23 09:44:57 +00:00
}
2024-06-25 12:39:39 +01:00
func NewShowExecutor ( printer * printer . Printer , config * config . Config , name , summary string ) * ShowExecutor {
2024-06-03 07:00:39 +01:00
showExe := ShowExecutor {
2024-06-17 18:59:20 +01:00
FlagSet : flag . NewFlagSet ( name , flag . ExitOnError ) ,
2024-06-25 12:39:39 +01:00
printer : printer ,
config : config ,
2024-02-23 09:44:57 +00:00
}
2024-06-09 15:58:10 +01:00
showExe . BoolVar ( & showExe . myAccount , flagMyAccount , false , "Set to true to lookup your account" )
showExe . BoolVar ( & showExe . skipAccountRelationship , flagSkipRelationship , false , "Set to true to skip showing your relationship to the specified account" )
showExe . BoolVar ( & showExe . showUserPreferences , flagShowPreferences , false , "Show your preferences" )
showExe . BoolVar ( & showExe . showInBrowser , flagBrowser , false , "Set to true to view in the browser" )
showExe . StringVar ( & showExe . resourceType , flagType , "" , "Specify the type of resource to display" )
showExe . StringVar ( & showExe . accountName , flagAccountName , "" , "Specify the account name in full (username@domain)" )
showExe . StringVar ( & showExe . statusID , flagStatusID , "" , "Specify the ID of the status to display" )
showExe . StringVar ( & showExe . timelineCategory , flagTimelineCategory , model . TimelineCategoryHome , "Specify the timeline category to view" )
showExe . StringVar ( & showExe . listID , flagListID , "" , "Specify the ID of the list to display" )
showExe . StringVar ( & showExe . tag , flagTag , "" , "Specify the name of the tag to use" )
2024-06-15 18:40:13 +01:00
showExe . StringVar ( & showExe . pollID , flagPollID , "" , "Specify the ID of the poll to display" )
2024-06-22 01:16:24 +01:00
showExe . Var ( & showExe . attachmentIDs , flagAttachmentID , "Specify the ID of the media attachment to display" )
showExe . StringVar ( & showExe . fromResourceType , flagFrom , "" , "Specify the resource type to view the target resource from (e.g. status for viewing media from, etc)" )
2024-06-09 15:58:10 +01:00
showExe . IntVar ( & showExe . limit , flagLimit , 20 , "Specify the limit of items to display" )
2024-02-28 19:35:18 +00:00
2024-06-03 07:00:39 +01:00
showExe . Usage = commandUsageFunc ( name , summary , showExe . FlagSet )
2024-02-23 09:44:57 +00:00
2024-06-03 07:00:39 +01:00
return & showExe
2024-02-23 09:44:57 +00:00
}
2024-06-03 07:00:39 +01:00
func ( s * ShowExecutor ) Execute ( ) error {
if s . 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-06-18 19:58:59 +01:00
resourceInstance : s . showInstance ,
resourceAccount : s . showAccount ,
resourceStatus : s . showStatus ,
resourceTimeline : s . showTimeline ,
resourceList : s . showList ,
resourceFollowers : s . showFollowers ,
resourceFollowing : s . showFollowing ,
resourceBlocked : s . showBlocked ,
resourceBookmarks : s . showBookmarks ,
resourceLiked : s . showLiked ,
resourceStarred : s . showLiked ,
resourceFollowRequest : s . showFollowRequests ,
resourcePoll : s . showPoll ,
resourceMutedAccounts : s . showMutedAccounts ,
2024-06-22 01:16:24 +01:00
resourceMedia : s . showMedia ,
2024-06-18 19:58:59 +01:00
resourceMediaAttachment : s . showMediaAttachment ,
2024-06-03 07:00:39 +01:00
}
doFunc , ok := funcMap [ s . resourceType ]
2024-02-23 09:44:57 +00:00
if ! ok {
2024-06-03 07:00:39 +01:00
return UnsupportedTypeError { resourceType : s . resourceType }
2024-02-28 19:35:18 +00:00
}
2024-06-25 12:39:39 +01:00
gtsClient , err := client . NewClientFromFile ( s . config . CredentialsFile )
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-06-03 07:00:39 +01:00
func ( s * 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-06-17 18:59:20 +01:00
s . printer . PrintInstance ( instance )
2024-02-23 09:44:57 +00:00
return nil
}
2024-06-03 07:00:39 +01:00
func ( s * 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
2024-06-03 07:00:39 +01:00
if s . myAccount {
2024-06-25 12:39:39 +01:00
account , err = getMyAccount ( gtsClient , s . config . CredentialsFile )
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-06-03 07:00:39 +01:00
if s . accountName == "" {
2024-05-23 18:06:49 +01:00
return FlagNotSetError { flagText : flagAccountName }
2024-02-23 09:44:57 +00:00
}
2024-06-03 07:00:39 +01:00
account , err = getAccount ( gtsClient , s . 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-06-03 07:00:39 +01:00
if s . showInBrowser {
2024-06-26 12:38:27 +01:00
if err := utilities . OpenLink ( s . config . Integrations . Browser , account . URL ) ; err != nil {
return fmt . Errorf ( "unable to open link: %w" , err )
}
2024-05-29 20:26:10 +01:00
return nil
}
2024-06-17 18:59:20 +01:00
var (
2024-06-18 19:58:59 +01:00
relationship * model . AccountRelationship
preferences * model . Preferences
2024-06-17 18:59:20 +01:00
)
2024-02-23 09:44:57 +00:00
2024-06-03 07:00:39 +01:00
if ! s . myAccount && ! s . skipAccountRelationship {
2024-06-17 18:59:20 +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-06-03 07:00:39 +01:00
if s . myAccount && s . showUserPreferences {
2024-06-17 18:59:20 +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-06-17 18:59:20 +01:00
s . printer . PrintAccount ( account , relationship , preferences )
2024-02-23 09:44:57 +00:00
return nil
}
2024-06-03 07:00:39 +01:00
func ( s * ShowExecutor ) showStatus ( gtsClient * client . Client ) error {
if s . statusID == "" {
2024-05-23 18:06:49 +01:00
return FlagNotSetError { flagText : flagStatusID }
2024-02-23 13:14:50 +00:00
}
2024-06-03 07:00:39 +01:00
status , err := gtsClient . GetStatus ( s . 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-06-03 07:00:39 +01:00
if s . showInBrowser {
2024-06-26 12:38:27 +01:00
if err := utilities . OpenLink ( s . config . Integrations . Browser , status . URL ) ; err != nil {
return fmt . Errorf ( "unable to open link: %w" , err )
}
2024-05-29 20:26:10 +01:00
return nil
}
2024-06-17 18:59:20 +01:00
s . printer . PrintStatus ( status )
2024-02-23 13:14:50 +00:00
return nil
}
2024-02-24 15:53:29 +00:00
2024-06-03 07:00:39 +01:00
func ( s * 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-06-03 07:00:39 +01:00
switch s . timelineCategory {
2024-06-02 11:35:43 +01:00
case model . TimelineCategoryHome :
2024-06-03 07:00:39 +01:00
timeline , err = gtsClient . GetHomeTimeline ( s . limit )
2024-06-02 11:35:43 +01:00
case model . TimelineCategoryPublic :
2024-06-03 07:00:39 +01:00
timeline , err = gtsClient . GetPublicTimeline ( s . limit )
2024-06-02 11:35:43 +01:00
case model . TimelineCategoryList :
2024-06-03 07:00:39 +01:00
if s . 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
2024-06-03 07:00:39 +01:00
list , err = gtsClient . GetList ( s . listID )
2024-06-03 03:54:27 +01:00
if err != nil {
return fmt . Errorf ( "unable to retrieve the list: %w" , err )
}
2024-06-03 07:00:39 +01:00
timeline , err = gtsClient . GetListTimeline ( list . ID , list . Title , s . limit )
2024-06-02 11:35:43 +01:00
case model . TimelineCategoryTag :
2024-06-03 07:00:39 +01:00
if s . tag == "" {
2024-05-23 18:06:49 +01:00
return FlagNotSetError { flagText : flagTag }
2024-02-24 15:53:29 +00:00
}
2024-06-03 07:00:39 +01:00
timeline , err = gtsClient . GetTagTimeline ( s . tag , s . limit )
2024-02-24 15:53:29 +00:00
default :
2024-06-03 07:00:39 +01:00
return model . InvalidTimelineCategoryError { Value : s . timelineCategory }
2024-02-24 15:53:29 +00:00
}
if err != nil {
2024-06-03 07:00:39 +01:00
return fmt . Errorf ( "unable to retrieve the %s timeline: %w" , s . timelineCategory , err )
2024-02-24 15:53:29 +00:00
}
if len ( timeline . Statuses ) == 0 {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "There are no statuses in this timeline.\n" )
2024-02-24 15:53:29 +00:00
return nil
}
2024-06-17 18:59:20 +01:00
s . printer . PrintStatusList ( timeline )
2024-02-24 15:53:29 +00:00
return nil
}
2024-02-27 19:52:59 +00:00
2024-06-03 07:00:39 +01:00
func ( s * ShowExecutor ) showList ( gtsClient * client . Client ) error {
if s . listID == "" {
return s . showLists ( gtsClient )
2024-05-19 11:48:36 +01:00
}
2024-06-03 07:00:39 +01:00
list , err := gtsClient . GetList ( s . 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-06-03 07:00:39 +01:00
accounts , err := gtsClient . GetAccountsFromList ( s . 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-06-17 18:59:20 +01:00
s . printer . PrintList ( list )
2024-05-19 11:48:36 +01:00
return nil
}
2024-06-03 07:00:39 +01:00
func ( s * 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 {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "You have no lists.\n" )
2024-02-27 19:52:59 +00:00
return nil
}
2024-06-17 18:59:20 +01:00
s . printer . PrintLists ( lists )
2024-02-27 19:52:59 +00:00
return nil
}
2024-05-20 17:26:06 +01:00
2024-06-03 07:00:39 +01:00
func ( s * ShowExecutor ) showFollowers ( gtsClient * client . Client ) error {
2024-07-03 14:20:25 +01:00
if s . fromResourceType == "" {
return FlagNotSetError { flagText : flagFrom }
}
funcMap := map [ string ] func ( * client . Client ) error {
resourceAccount : s . showFollowersFromAccount ,
}
doFunc , ok := funcMap [ s . fromResourceType ]
if ! ok {
return UnsupportedShowOperationError {
ResourceType : s . resourceType ,
ShowFromResourceType : s . fromResourceType ,
}
}
return doFunc ( gtsClient )
}
func ( s * ShowExecutor ) showFollowersFromAccount ( gtsClient * client . Client ) error {
2024-06-25 12:39:39 +01:00
accountID , err := getAccountID ( gtsClient , s . myAccount , s . accountName , s . config . CredentialsFile )
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-06-03 07:00:39 +01:00
followers , err := gtsClient . GetFollowers ( accountID , s . 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-06-17 18:59:20 +01:00
s . printer . PrintAccountList ( followers )
2024-05-20 17:26:06 +01:00
} else {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "There are no followers for this account (or the list is hidden).\n" )
2024-05-20 17:26:06 +01:00
}
return nil
}
2024-06-03 07:00:39 +01:00
func ( s * ShowExecutor ) showFollowing ( gtsClient * client . Client ) error {
2024-07-03 14:20:25 +01:00
if s . fromResourceType == "" {
return FlagNotSetError { flagText : flagFrom }
}
funcMap := map [ string ] func ( * client . Client ) error {
resourceAccount : s . showFollowingFromAccount ,
}
doFunc , ok := funcMap [ s . fromResourceType ]
if ! ok {
return UnsupportedShowOperationError {
ResourceType : s . resourceType ,
ShowFromResourceType : s . fromResourceType ,
}
}
return doFunc ( gtsClient )
}
func ( s * ShowExecutor ) showFollowingFromAccount ( gtsClient * client . Client ) error {
2024-06-25 12:39:39 +01:00
accountID , err := getAccountID ( gtsClient , s . myAccount , s . accountName , s . config . CredentialsFile )
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-06-03 07:00:39 +01:00
following , err := gtsClient . GetFollowing ( accountID , s . 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-06-17 18:59:20 +01:00
s . printer . PrintAccountList ( following )
2024-05-20 17:26:06 +01:00
} else {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "This account is not following anyone or the list is hidden.\n" )
2024-05-20 17:26:06 +01:00
}
return nil
}
2024-05-21 13:19:53 +01:00
2024-06-03 07:00:39 +01:00
func ( s * ShowExecutor ) showBlocked ( gtsClient * client . Client ) error {
blocked , err := gtsClient . GetBlockedAccounts ( s . 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-06-17 18:59:20 +01:00
s . printer . PrintAccountList ( blocked )
2024-05-21 13:19:53 +01:00
} else {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "You have no blocked accounts.\n" )
2024-05-21 13:19:53 +01:00
}
return nil
}
2024-06-03 06:53:31 +01:00
2024-06-03 07:00:39 +01:00
func ( s * ShowExecutor ) showBookmarks ( gtsClient * client . Client ) error {
bookmarks , err := gtsClient . GetBookmarks ( s . limit )
2024-06-03 06:53:31 +01:00
if err != nil {
return fmt . Errorf ( "unable to retrieve the list of bookmarks: %w" , err )
}
if len ( bookmarks . Statuses ) > 0 {
2024-06-17 18:59:20 +01:00
s . printer . PrintStatusList ( bookmarks )
2024-06-03 06:53:31 +01:00
} else {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "You have no bookmarks.\n" )
2024-06-03 06:53:31 +01:00
}
return nil
}
2024-06-04 17:01:33 +01:00
func ( s * ShowExecutor ) showLiked ( gtsClient * client . Client ) error {
liked , err := gtsClient . GetLikedStatuses ( s . limit , s . resourceType )
if err != nil {
return fmt . Errorf ( "unable to retrieve the list of your %s statuses: %w" , s . resourceType , err )
}
if len ( liked . Statuses ) > 0 {
2024-06-17 18:59:20 +01:00
s . printer . PrintStatusList ( liked )
2024-06-04 17:01:33 +01:00
} else {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "You have no " + s . resourceType + " statuses.\n" )
2024-06-04 17:01:33 +01:00
}
return nil
}
2024-06-10 10:58:43 +01:00
func ( s * ShowExecutor ) showFollowRequests ( gtsClient * client . Client ) error {
accounts , err := gtsClient . GetFollowRequests ( s . limit )
if err != nil {
return fmt . Errorf ( "unable to retrieve the list of follow requests: %w" , err )
}
if len ( accounts . Accounts ) > 0 {
2024-06-17 18:59:20 +01:00
s . printer . PrintAccountList ( accounts )
2024-06-10 10:58:43 +01:00
} else {
2024-06-17 18:59:20 +01:00
s . printer . PrintInfo ( "You have no follow requests.\n" )
2024-06-10 10:58:43 +01:00
}
return nil
}
2024-06-15 18:40:13 +01:00
func ( s * ShowExecutor ) showPoll ( gtsClient * client . Client ) error {
if s . pollID == "" {
return FlagNotSetError { flagText : flagPollID }
}
poll , err := gtsClient . GetPoll ( s . pollID )
if err != nil {
return fmt . Errorf ( "unable to retrieve the poll: %w" , err )
}
2024-06-17 18:59:20 +01:00
s . printer . PrintPoll ( poll )
2024-06-15 18:40:13 +01:00
return nil
}
2024-06-18 04:59:32 +01:00
func ( s * ShowExecutor ) showMutedAccounts ( gtsClient * client . Client ) error {
muted , err := gtsClient . GetMutedAccounts ( s . limit )
if err != nil {
return fmt . Errorf ( "unable to retrieve the list of muted accounts: %w" , err )
}
if len ( muted . Accounts ) > 0 {
s . printer . PrintAccountList ( muted )
} else {
s . printer . PrintInfo ( "You have not muted any accounts.\n" )
}
return nil
}
2024-06-18 19:58:59 +01:00
func ( s * ShowExecutor ) showMediaAttachment ( gtsClient * client . Client ) error {
2024-06-22 01:16:24 +01:00
if len ( s . attachmentIDs ) == 0 {
2024-06-18 19:58:59 +01:00
return FlagNotSetError { flagText : flagAttachmentID }
}
2024-06-22 01:16:24 +01:00
if len ( s . attachmentIDs ) != 1 {
return fmt . Errorf (
"unexpected number of attachment IDs received: want 1, got %d" ,
len ( s . attachmentIDs ) ,
)
}
attachment , err := gtsClient . GetMediaAttachment ( s . attachmentIDs [ 0 ] )
2024-06-18 19:58:59 +01:00
if err != nil {
return fmt . Errorf ( "unable to retrieve the media attachment: %w" , err )
}
s . printer . PrintMediaAttachment ( attachment )
return nil
}
2024-06-22 01:16:24 +01:00
func ( s * ShowExecutor ) showMedia ( gtsClient * client . Client ) error {
if s . fromResourceType == "" {
return FlagNotSetError { flagText : flagFrom }
}
funcMap := map [ string ] func ( * client . Client ) error {
resourceStatus : s . showMediaFromStatus ,
}
doFunc , ok := funcMap [ s . fromResourceType ]
if ! ok {
2024-07-03 14:20:25 +01:00
return UnsupportedShowOperationError {
ResourceType : s . resourceType ,
ShowFromResourceType : s . fromResourceType ,
}
2024-06-22 01:16:24 +01:00
}
return doFunc ( gtsClient )
}
func ( s * ShowExecutor ) showMediaFromStatus ( gtsClient * client . Client ) error {
if len ( s . attachmentIDs ) == 0 {
return FlagNotSetError { flagText : flagAttachmentID }
}
if s . statusID == "" {
return FlagNotSetError { flagText : flagStatusID }
}
status , err := gtsClient . GetStatus ( s . statusID )
if err != nil {
return fmt . Errorf ( "unable to retrieve the status: %w" , err )
}
cacheDir := filepath . Join (
2024-06-25 12:39:39 +01:00
utilities . CalculateCacheDir ( s . config . CacheDirectory , utilities . GetFQDN ( gtsClient . Authentication . Instance ) ) ,
2024-06-22 01:16:24 +01:00
"media" ,
)
if err := utilities . EnsureDirectory ( cacheDir ) ; err != nil {
return fmt . Errorf ( "unable to ensure the existence of the directory %q: %w" , cacheDir , err )
}
type media struct {
url string
mediaType string
}
attachmentsHashMap := make ( map [ string ] media )
imageFiles := make ( [ ] string , 0 )
videoFiles := make ( [ ] string , 0 )
for _ , statusAttachment := range status . MediaAttachments {
attachmentsHashMap [ statusAttachment . ID ] = media {
url : statusAttachment . URL ,
mediaType : statusAttachment . Type ,
}
}
for _ , attachmentID := range s . attachmentIDs {
mediaObj , ok := attachmentsHashMap [ attachmentID ]
if ! ok {
2024-06-25 12:39:39 +01:00
return UnknownMediaAttachmentError { AttachmentID : attachmentID }
2024-06-22 01:16:24 +01:00
}
split := strings . Split ( mediaObj . url , "/" )
filename := split [ len ( split ) - 1 ]
filePath := filepath . Join ( cacheDir , filename )
fileExists , err := utilities . FileExists ( filePath )
if err != nil {
return fmt . Errorf (
"unable to check if the media file is already downloaded for %s: %w" ,
attachmentID ,
err ,
)
}
if ! fileExists {
if err := gtsClient . DownloadMedia ( mediaObj . url , filePath ) ; err != nil {
return fmt . Errorf (
"unable to download the media attachment for %s: %w" ,
attachmentID ,
err ,
)
}
}
switch mediaObj . mediaType {
case "image" :
imageFiles = append ( imageFiles , filePath )
case "video" :
videoFiles = append ( videoFiles , filePath )
}
}
if len ( imageFiles ) > 0 {
2024-06-25 12:39:39 +01:00
if err := utilities . OpenMedia ( s . config . Integrations . ImageViewer , imageFiles ) ; err != nil {
2024-06-22 01:16:24 +01:00
return fmt . Errorf ( "unable to open the image viewer: %w" , err )
}
}
if len ( videoFiles ) > 0 {
2024-06-25 12:39:39 +01:00
if err := utilities . OpenMedia ( s . config . Integrations . VideoPlayer , videoFiles ) ; err != nil {
2024-06-22 01:16:24 +01:00
return fmt . Errorf ( "unable to open the video player: %w" , err )
}
}
return nil
}