enbas/internal/executor/mute_or_unmute.go
Dan Anglin 56445601a3
feat: add support for muting and unmuting accounts
Now that muting and unmuting accounts are now supported in GoToSocial
(as of version 0.16.0) Enbas can now allow users to do that.

Users can also see a list of accounts that they've muted.
2024-06-18 04:59:32 +01:00

107 lines
2.9 KiB
Go

// 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"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
)
type MuteOrUnmuteExecutor struct {
*flag.FlagSet
printer *printer.Printer
accountName string
configDir string
command string
resourceType string
muteDuration TimeDurationFlagValue
muteNotifications bool
}
func NewMuteOrUnmuteExecutor(printer *printer.Printer, configDir, name, summary string) *MuteOrUnmuteExecutor {
exe := MuteOrUnmuteExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: printer,
configDir: configDir,
command: name,
}
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}
}
gtsClient, err := client.NewClientFromConfig(m.configDir)
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}
}
accountID, err := getAccountID(gtsClient, false, m.accountName, m.configDir)
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
}