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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
|
|
)
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
type BlockOrUnblockExecutor struct {
|
2024-05-23 18:06:49 +01:00
|
|
|
*flag.FlagSet
|
|
|
|
|
|
|
|
topLevelFlags TopLevelFlags
|
|
|
|
resourceType string
|
|
|
|
accountName string
|
2024-06-10 18:59:01 +01:00
|
|
|
command string
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func NewBlockOrUnblockExecutor(tlf TopLevelFlags, name, summary string) *BlockOrUnblockExecutor {
|
|
|
|
blockExe := BlockOrUnblockExecutor{
|
2024-05-23 18:06:49 +01:00
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
|
|
|
|
topLevelFlags: tlf,
|
2024-06-10 18:59:01 +01:00
|
|
|
command: name,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 15:58:10 +01:00
|
|
|
blockExe.StringVar(&blockExe.resourceType, flagType, "", "Specify the type of resource to block or unblock")
|
|
|
|
blockExe.StringVar(&blockExe.accountName, flagAccountName, "", "Specify the account name in full (username@domain)")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
blockExe.Usage = commandUsageFunc(name, summary, blockExe.FlagSet)
|
|
|
|
|
|
|
|
return &blockExe
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) Execute() error {
|
2024-05-23 18:06:49 +01:00
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-06-10 18:59:01 +01:00
|
|
|
resourceAccount: b.blockOrUnblockAccount,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[b.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: b.resourceType}
|
|
|
|
}
|
|
|
|
|
|
|
|
gtsClient, err := client.NewClientFromConfig(b.topLevelFlags.ConfigDir)
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) blockOrUnblockAccount(gtsClient *client.Client) error {
|
2024-05-23 18:06:49 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, b.accountName, b.topLevelFlags.ConfigDir)
|
|
|
|
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-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
switch b.command {
|
|
|
|
case CommandBlock:
|
|
|
|
return b.blockAccount(gtsClient, accountID)
|
|
|
|
case CommandUnblock:
|
2024-05-23 18:06:49 +01:00
|
|
|
return b.unblockAccount(gtsClient, accountID)
|
2024-06-10 18:59:01 +01:00
|
|
|
default:
|
|
|
|
return nil
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
2024-06-10 18:59:01 +01:00
|
|
|
}
|
2024-05-23 18:06:49 +01:00
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) blockAccount(gtsClient *client.Client, accountID string) error {
|
2024-05-23 18:06:49 +01:00
|
|
|
if err := gtsClient.BlockAccount(accountID); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to block the account: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Successfully blocked the account.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) unblockAccount(gtsClient *client.Client, accountID string) error {
|
2024-05-23 18:06:49 +01:00
|
|
|
if err := gtsClient.UnblockAccount(accountID); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to unblock the account: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Successfully unblocked the account.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|