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-05-20 17:26:06 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
|
|
)
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
type FollowExecutor struct {
|
2024-05-20 17:26:06 +01:00
|
|
|
*flag.FlagSet
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
topLevelFlags TopLevelFlags
|
2024-05-22 23:30:09 +01:00
|
|
|
resourceType string
|
|
|
|
accountName string
|
|
|
|
showReposts bool
|
|
|
|
notify bool
|
|
|
|
unfollow bool
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func NewFollowExecutor(tlf TopLevelFlags, name, summary string, unfollow bool) *FollowExecutor {
|
|
|
|
command := FollowExecutor{
|
2024-05-22 23:30:09 +01:00
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
unfollow: unfollow,
|
|
|
|
topLevelFlags: tlf,
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +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-05-23 18:06:49 +01:00
|
|
|
func (c *FollowExecutor) Execute() error {
|
2024-05-20 17:26:06 +01:00
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-05-23 18:06:49 +01:00
|
|
|
resourceAccount: c.followAccount,
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[c.resourceType]
|
|
|
|
if !ok {
|
2024-05-23 18:06:49 +01:00
|
|
|
return UnsupportedTypeError{resourceType: c.resourceType}
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.ConfigDir)
|
2024-05-20 17:26:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *FollowExecutor) followAccount(gtsClient *client.Client) error {
|
|
|
|
accountID, err := getAccountID(gtsClient, false, 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
|
|
|
}
|
|
|
|
|
|
|
|
if c.unfollow {
|
2024-05-21 21:06:18 +01:00
|
|
|
return c.unfollowAccount(gtsClient, accountID)
|
2024-05-20 17:26:06 +01:00
|
|
|
}
|
|
|
|
|
2024-05-30 20:04:55 +01:00
|
|
|
form := client.FollowAccountForm{
|
|
|
|
AccountID: accountID,
|
|
|
|
ShowReposts: c.showReposts,
|
|
|
|
Notify: c.notify,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.FollowAccount(form); err != nil {
|
2024-05-20 17:26:06 +01:00
|
|
|
return fmt.Errorf("unable to follow the account; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("The follow request was sent successfully.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *FollowExecutor) unfollowAccount(gtsClient *client.Client, accountID string) error {
|
2024-05-21 21:06:18 +01:00
|
|
|
if err := gtsClient.UnfollowAccount(accountID); err != nil {
|
2024-05-20 17:26:06 +01:00
|
|
|
return fmt.Errorf("unable to unfollow the account; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Successfully unfollowed the account.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|