enbas/internal/executor/mute.go
Dan Anglin 3037af60ed
feat: mute and unmute statuses
This commit adds support for muting and unmuting statuses. When viewing
a status the user can now see whether they've muted the status or not.
A status can only be muted by the user if they own it or are mentioned
in it.

PR: apollo/enbas#47
Resolves: apollo/enbas#46
2024-08-16 14:42:57 +01:00

88 lines
2 KiB
Go

package executor
import (
"errors"
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
)
func (m *MuteExecutor) Execute() error {
funcMap := map[string]func(*client.Client) error{
resourceAccount: m.muteAccount,
resourceStatus: m.muteStatus,
}
doFunc, ok := funcMap[m.resourceType]
if !ok {
return UnsupportedTypeError{resourceType: m.resourceType}
}
gtsClient, err := client.NewClientFromFile(m.config.CredentialsFile)
if err != nil {
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
}
return doFunc(gtsClient)
}
func (m *MuteExecutor) muteAccount(gtsClient *client.Client) error {
accountID, err := getAccountID(gtsClient, false, m.accountName)
if err != nil {
return fmt.Errorf("received an error while getting the account ID: %w", err)
}
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 *MuteExecutor) muteStatus(gtsClient *client.Client) error {
if m.statusID == "" {
return FlagNotSetError{flagText: flagStatusID}
}
status, err := gtsClient.GetStatus(m.statusID)
if err != nil {
return fmt.Errorf("unable to retrieve the status: %w", err)
}
myAccountID, err := getAccountID(gtsClient, true, nil)
if err != nil {
return fmt.Errorf("unable to get your account ID: %w", err)
}
canMute := false
if status.Account.ID == myAccountID {
canMute = true
} else {
for _, mention := range status.Mentions {
if mention.ID == myAccountID {
canMute = true
break
}
}
}
if !canMute {
return errors.New("unable to mute the status because you are not the owner and you are not mentioned in it")
}
if err := gtsClient.MuteStatus(m.statusID); err != nil {
return fmt.Errorf("unable to mute the status: %w", err)
}
m.printer.PrintSuccess("Successfully muted the status.")
return nil
}