2024-05-23 18:06:49 +01:00
package executor
2024-02-27 19:52:59 +00:00
import (
"flag"
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
)
2024-05-23 18:06:49 +01:00
type CreateExecutor struct {
2024-02-27 19:52:59 +00:00
* flag . FlagSet
2024-05-23 18:06:49 +01:00
topLevelFlags TopLevelFlags
2024-05-30 03:19:06 +01:00
boostable bool
federated bool
likeable bool
replyable bool
sensitive bool
content string
contentType string
language string
spoilerText string
2024-02-27 19:52:59 +00:00
resourceType string
listTitle string
listRepliesPolicy string
2024-05-30 03:19:06 +01:00
visibility string
2024-02-27 19:52:59 +00:00
}
2024-05-23 18:06:49 +01:00
func NewCreateExecutor ( tlf TopLevelFlags , name , summary string ) * CreateExecutor {
createExe := CreateExecutor {
2024-02-27 19:52:59 +00:00
FlagSet : flag . NewFlagSet ( name , flag . ExitOnError ) ,
2024-05-22 23:30:09 +01:00
topLevelFlags : tlf ,
2024-02-27 19:52:59 +00:00
}
2024-05-30 05:46:28 +01:00
createExe . BoolVar ( & createExe . boostable , flagBoostable , true , "specify if the status can be reposted/boosted by others" )
createExe . BoolVar ( & createExe . federated , flagFederated , true , "specify if the status can be federated beyond the local timelines" )
createExe . BoolVar ( & createExe . likeable , flagLikeable , true , "specify if the status can be liked/favourited" )
createExe . BoolVar ( & createExe . replyable , flagReplyable , true , "specify if the status can be replied to" )
createExe . BoolVar ( & createExe . sensitive , flagSensitive , false , "specify if the status should be marked as sensitive" )
2024-05-30 03:19:06 +01:00
createExe . StringVar ( & createExe . content , flagContent , "" , "the content of the status to create" )
2024-05-30 07:36:27 +01:00
createExe . StringVar ( & createExe . contentType , flagContentType , "plain" , "the type that the contents should be parsed from (valid values are plain and markdown)" )
2024-05-30 05:46:28 +01:00
createExe . StringVar ( & createExe . language , flagLanguage , "" , "the ISO 639 language code for this status" )
createExe . StringVar ( & createExe . spoilerText , flagSpoilerText , "" , "the text to display as the status' warning or subject" )
createExe . StringVar ( & createExe . visibility , flagVisibility , "" , "the visibility of the posted status" )
2024-05-23 18:06:49 +01:00
createExe . StringVar ( & createExe . resourceType , flagType , "" , "specify the type of resource to create" )
createExe . StringVar ( & createExe . listTitle , flagListTitle , "" , "specify the title of the list" )
createExe . StringVar ( & createExe . listRepliesPolicy , flagListRepliesPolicy , "list" , "specify the policy of the replies for this list (valid values are followed, list and none)" )
2024-02-27 19:52:59 +00:00
2024-05-23 18:06:49 +01:00
createExe . Usage = commandUsageFunc ( name , summary , createExe . FlagSet )
2024-02-27 19:52:59 +00:00
2024-05-23 18:06:49 +01:00
return & createExe
2024-02-27 19:52:59 +00:00
}
2024-05-23 18:06:49 +01:00
func ( c * CreateExecutor ) 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-27 19:52:59 +00:00
}
2024-05-23 18:06:49 +01:00
gtsClient , err := client . NewClientFromConfig ( c . topLevelFlags . ConfigDir )
2024-02-27 19:52:59 +00:00
if err != nil {
return fmt . Errorf ( "unable to create the GoToSocial client; %w" , err )
}
funcMap := map [ string ] func ( * client . Client ) error {
2024-05-30 03:19:06 +01:00
resourceList : c . createList ,
resourceStatus : c . createStatus ,
2024-02-27 19:52:59 +00:00
}
doFunc , ok := funcMap [ c . resourceType ]
if ! ok {
2024-05-23 18:06:49 +01:00
return UnsupportedTypeError { resourceType : c . resourceType }
2024-02-27 19:52:59 +00:00
}
return doFunc ( gtsClient )
}
2024-05-23 18:06:49 +01:00
func ( c * CreateExecutor ) createList ( gtsClient * client . Client ) error {
2024-02-27 19:52:59 +00:00
if c . listTitle == "" {
2024-05-23 18:06:49 +01:00
return FlagNotSetError { flagText : flagListTitle }
2024-02-27 19:52:59 +00:00
}
repliesPolicy , err := model . ParseListRepliesPolicy ( c . listRepliesPolicy )
if err != nil {
return fmt . Errorf ( "unable to parse the list replies policy; %w" , err )
}
list , err := gtsClient . CreateList ( c . listTitle , repliesPolicy )
if err != nil {
return fmt . Errorf ( "unable to create the list; %w" , err )
}
fmt . Println ( "Successfully created the following list:" )
fmt . Printf ( "\n%s\n" , list )
return nil
}
2024-05-30 03:19:06 +01:00
func ( c * CreateExecutor ) createStatus ( gtsClient * client . Client ) error {
if c . content == "" {
return FlagNotSetError { flagText : flagContent }
}
2024-05-30 05:46:28 +01:00
var (
language string
visibility string
)
preferences , err := gtsClient . GetUserPreferences ( )
if err != nil {
fmt . Println ( "WARNING: Unable to get your posting preferences; %w" , err )
}
if c . language != "" {
language = c . language
} else {
language = preferences . PostingDefaultLanguage
2024-05-30 03:19:06 +01:00
}
2024-05-30 05:46:28 +01:00
if c . visibility != "" {
visibility = c . visibility
} else {
visibility = preferences . PostingDefaultVisibility
2024-05-30 03:19:06 +01:00
}
2024-05-30 05:46:28 +01:00
parsedVisibility := model . ParseStatusVisibility ( visibility )
if parsedVisibility == model . StatusVisibilityUnknown {
2024-05-30 06:55:01 +01:00
return InvalidStatusVisibilityError { Visibility : visibility }
2024-05-30 03:19:06 +01:00
}
2024-05-30 07:36:27 +01:00
parsedContentType := model . ParseStatusContentType ( c . contentType )
if parsedContentType == model . StatusContentTypeUnknown {
return InvalidStatusContentTypeError { ContentType : c . contentType }
}
2024-05-30 06:55:01 +01:00
form := client . CreateStatusForm {
Content : c . content ,
2024-05-30 07:36:27 +01:00
ContentType : parsedContentType ,
2024-05-30 06:55:01 +01:00
Language : language ,
SpoilerText : c . spoilerText ,
Boostable : c . boostable ,
Federated : c . federated ,
Likeable : c . likeable ,
Replyable : c . replyable ,
Sensitive : c . sensitive ,
Visibility : parsedVisibility ,
}
status , err := gtsClient . CreateStatus ( form )
2024-05-30 03:19:06 +01:00
if err != nil {
return fmt . Errorf ( "unable to create the status; %w" , err )
}
fmt . Println ( "Successfully created the following status:" )
fmt . Println ( status )
return nil
}