2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
2024-05-20 17:26:06 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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-06-17 18:59:20 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
|
2024-05-20 17:26:06 +01:00
|
|
|
)
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
type FollowOrUnfollowExecutor struct {
|
2024-05-20 17:26:06 +01: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-06-17 18:59:20 +01:00
|
|
|
resourceType string
|
|
|
|
accountName string
|
|
|
|
showReposts bool
|
|
|
|
notify bool
|
|
|
|
action string
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
func NewFollowOrUnfollowExecutor(printer *printer.Printer, config *config.Config, name, summary string) *FollowOrUnfollowExecutor {
|
2024-06-10 18:59:01 +01:00
|
|
|
command := FollowOrUnfollowExecutor{
|
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
printer: printer,
|
|
|
|
config: config,
|
|
|
|
action: name,
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 15:58:10 +01:00
|
|
|
command.StringVar(&command.resourceType, flagType, "", "Specify the type of resource to follow")
|
|
|
|
command.StringVar(&command.accountName, flagAccountName, "", "Specify the account name in full (username@domain)")
|
|
|
|
command.BoolVar(&command.showReposts, flagShowReposts, true, "Show reposts from the account you want to follow")
|
|
|
|
command.BoolVar(&command.notify, flagNotify, false, "Get notifications when the account you want to follow posts a status")
|
2024-05-20 17:26:06 +01:00
|
|
|
|
|
|
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
|
|
|
|
|
|
|
return &command
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (f *FollowOrUnfollowExecutor) Execute() error {
|
2024-05-20 17:26:06 +01:00
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-06-10 18:59:01 +01:00
|
|
|
resourceAccount: f.followOrUnfollowAccount,
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
doFunc, ok := funcMap[f.resourceType]
|
2024-05-20 17:26:06 +01:00
|
|
|
if !ok {
|
2024-06-10 18:59:01 +01:00
|
|
|
return UnsupportedTypeError{resourceType: f.resourceType}
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
gtsClient, err := client.NewClientFromFile(f.config.CredentialsFile)
|
2024-05-20 17:26:06 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (f *FollowOrUnfollowExecutor) followOrUnfollowAccount(gtsClient *client.Client) error {
|
2024-06-25 12:39:39 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, f.accountName, f.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-10 18:59:01 +01:00
|
|
|
switch f.action {
|
|
|
|
case CommandFollow:
|
|
|
|
return f.followAccount(gtsClient, accountID)
|
|
|
|
case CommandUnfollow:
|
|
|
|
return f.unfollowAccount(gtsClient, accountID)
|
|
|
|
default:
|
|
|
|
return nil
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
2024-06-10 18:59:01 +01:00
|
|
|
}
|
2024-05-20 17:26:06 +01:00
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (f *FollowOrUnfollowExecutor) followAccount(gtsClient *client.Client, accountID string) error {
|
2024-05-30 20:04:55 +01:00
|
|
|
form := client.FollowAccountForm{
|
|
|
|
AccountID: accountID,
|
2024-06-10 18:59:01 +01:00
|
|
|
ShowReposts: f.showReposts,
|
|
|
|
Notify: f.notify,
|
2024-05-30 20:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.FollowAccount(form); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to follow the account: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
f.printer.PrintSuccess("Successfully sent the follow request.")
|
2024-05-20 17:26:06 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (f *FollowOrUnfollowExecutor) unfollowAccount(gtsClient *client.Client, accountID string) error {
|
2024-05-21 21:06:18 +01:00
|
|
|
if err := gtsClient.UnfollowAccount(accountID); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to unfollow the account: %w", err)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
f.printer.PrintSuccess("Successfully unfollowed the account.")
|
2024-05-20 17:26:06 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|