2024-06-18 04:59:32 +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"
|
2024-07-01 21:27:04 +01:00
|
|
|
"time"
|
2024-06-18 04:59:32 +01:00
|
|
|
|
|
|
|
"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-18 04:59:32 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MuteOrUnmuteExecutor struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
|
|
|
|
printer *printer.Printer
|
2024-06-25 12:39:39 +01:00
|
|
|
config *config.Config
|
2024-06-18 04:59:32 +01:00
|
|
|
accountName string
|
|
|
|
command string
|
|
|
|
resourceType string
|
|
|
|
muteDuration TimeDurationFlagValue
|
|
|
|
muteNotifications bool
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
func NewMuteOrUnmuteExecutor(printer *printer.Printer, config *config.Config, name, summary string) *MuteOrUnmuteExecutor {
|
2024-07-01 21:27:04 +01:00
|
|
|
muteDuration := TimeDurationFlagValue{time.Duration(0 * time.Second)}
|
|
|
|
|
2024-06-18 04:59:32 +01:00
|
|
|
exe := MuteOrUnmuteExecutor{
|
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
|
2024-07-01 21:27:04 +01:00
|
|
|
printer: printer,
|
|
|
|
config: config,
|
|
|
|
command: name,
|
|
|
|
muteDuration: muteDuration,
|
2024-06-18 04:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exe.StringVar(&exe.accountName, flagAccountName, "", "Specify the account name in full (username@domain)")
|
|
|
|
exe.StringVar(&exe.resourceType, flagType, "", "Specify the type of resource to mute or unmute")
|
|
|
|
exe.BoolVar(&exe.muteNotifications, flagMuteNotifications, false, "Mute notifications as well as posts")
|
|
|
|
exe.Var(&exe.muteDuration, flagMuteDuration, "Specify how long the mute should last for. To mute indefinitely, set this to 0s")
|
|
|
|
|
|
|
|
exe.Usage = commandUsageFunc(name, summary, exe.FlagSet)
|
|
|
|
|
|
|
|
return &exe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MuteOrUnmuteExecutor) Execute() error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceAccount: m.muteOrUnmuteAccount,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[m.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: m.resourceType}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
gtsClient, err := client.NewClientFromFile(m.config.CredentialsFile)
|
2024-06-18 04:59:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MuteOrUnmuteExecutor) muteOrUnmuteAccount(gtsClient *client.Client) error {
|
|
|
|
if m.accountName == "" {
|
|
|
|
return FlagNotSetError{flagText: flagAccountName}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, m.accountName, m.config.CredentialsFile)
|
2024-06-18 04:59:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m.command {
|
|
|
|
case CommandMute:
|
|
|
|
return m.muteAccount(gtsClient, accountID)
|
|
|
|
case CommandUnmute:
|
|
|
|
return m.unmuteAccount(gtsClient, accountID)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MuteOrUnmuteExecutor) muteAccount(gtsClient *client.Client, accountID string) error {
|
|
|
|
form := client.MuteAccountForm{
|
|
|
|
Notifications: m.muteNotifications,
|
|
|
|
Duration: int(m.muteDuration.Duration.Seconds()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.MuteAccount(accountID, form); err != nil {
|
|
|
|
return fmt.Errorf("unable to mute the account: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.printer.PrintSuccess("Successfully muted the account.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MuteOrUnmuteExecutor) unmuteAccount(gtsClient *client.Client, accountID string) error {
|
|
|
|
if err := gtsClient.UnmuteAccount(accountID); err != nil {
|
|
|
|
return fmt.Errorf("unable to unmute the account: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.printer.PrintSuccess("Successfully unmuted the account.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|