2024-06-10 10:58:43 +01:00
|
|
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
2024-06-17 18:59:20 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
|
2024-06-10 10:58:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AcceptOrRejectExecutor struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
printer *printer.Printer
|
|
|
|
configDir string
|
|
|
|
resourceType string
|
|
|
|
accountName string
|
|
|
|
command string
|
2024-06-10 10:58:43 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
func NewAcceptOrRejectExecutor(enbasPrinter *printer.Printer, configDir, name, summary string) *AcceptOrRejectExecutor {
|
2024-06-10 10:58:43 +01:00
|
|
|
acceptExe := AcceptOrRejectExecutor{
|
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
printer: enbasPrinter,
|
|
|
|
configDir: configDir,
|
|
|
|
command: name,
|
2024-06-10 10:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
acceptExe.StringVar(&acceptExe.resourceType, flagType, "", "Specify the type of resource to accept or reject")
|
|
|
|
acceptExe.StringVar(&acceptExe.accountName, flagAccountName, "", "Specify the account name in full (username@domain)")
|
|
|
|
|
|
|
|
acceptExe.Usage = commandUsageFunc(name, summary, acceptExe.FlagSet)
|
|
|
|
|
|
|
|
return &acceptExe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AcceptOrRejectExecutor) Execute() error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceFollowRequest: a.acceptOrRejectFollowRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[a.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: a.resourceType}
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
gtsClient, err := client.NewClientFromConfig(a.configDir)
|
2024-06-10 10:58:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AcceptOrRejectExecutor) acceptOrRejectFollowRequest(gtsClient *client.Client) error {
|
2024-06-17 18:59:20 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, a.accountName, a.configDir)
|
2024-06-10 10:58:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
switch a.command {
|
|
|
|
case CommandAccept:
|
2024-06-10 10:58:43 +01:00
|
|
|
return a.acceptFollowRequest(gtsClient, accountID)
|
2024-06-10 18:59:01 +01:00
|
|
|
case CommandReject:
|
2024-06-10 10:58:43 +01:00
|
|
|
return a.rejectFollowRequest(gtsClient, accountID)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AcceptOrRejectExecutor) acceptFollowRequest(gtsClient *client.Client, accountID string) error {
|
|
|
|
if err := gtsClient.AcceptFollowRequest(accountID); err != nil {
|
|
|
|
return fmt.Errorf("unable to accept the follow request: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully accepted the follow request.")
|
2024-06-10 10:58:43 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AcceptOrRejectExecutor) rejectFollowRequest(gtsClient *client.Client, accountID string) error {
|
|
|
|
if err := gtsClient.RejectFollowRequest(accountID); err != nil {
|
|
|
|
return fmt.Errorf("unable to reject the follow request: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
a.printer.PrintSuccess("Successfully rejected the follow request.")
|
2024-06-10 10:58:43 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|