2024-08-13 14:53:26 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m *MuteExecutor) Execute() error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceAccount: m.muteAccount,
|
2024-08-16 14:42:57 +01:00
|
|
|
resourceStatus: m.muteStatus,
|
2024-08-13 14:53:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-08-13 23:51:16 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, m.accountName)
|
2024-08-13 14:53:26 +01:00
|
|
|
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
|
|
|
|
}
|
2024-08-16 14:42:57 +01:00
|
|
|
|
|
|
|
func (m *MuteExecutor) muteStatus(gtsClient *client.Client) error {
|
|
|
|
if m.statusID == "" {
|
2024-08-18 08:59:44 +01:00
|
|
|
return MissingIDError{
|
|
|
|
resource: resourceStatus,
|
|
|
|
action: "mute",
|
|
|
|
}
|
2024-08-16 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-08-17 11:23:21 +01:00
|
|
|
|
2024-08-16 14:42:57 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !canMute {
|
2024-08-17 11:23:21 +01:00
|
|
|
return Error{"unable to mute the status because the status does not belong to you nor are you mentioned in it"}
|
2024-08-16 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|