From 0a7620a16edca31a308d604bae934d70044077a2 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 15 Aug 2024 23:22:33 +0100 Subject: [PATCH] checkpoint: check if user can mute or unmute a status --- internal/executor/mute.go | 28 ++++++++++++++++++++++++++++ internal/executor/unmute.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/internal/executor/mute.go b/internal/executor/mute.go index 1696e45..f8cd30d 100644 --- a/internal/executor/mute.go +++ b/internal/executor/mute.go @@ -1,6 +1,7 @@ package executor import ( + "errors" "fmt" "codeflow.dananglin.me.uk/apollo/enbas/internal/client" @@ -50,6 +51,33 @@ func (m *MuteExecutor) muteStatus(gtsClient *client.Client) error { 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) } diff --git a/internal/executor/unmute.go b/internal/executor/unmute.go index 487451d..d643e46 100644 --- a/internal/executor/unmute.go +++ b/internal/executor/unmute.go @@ -1,6 +1,7 @@ package executor import ( + "errors" "fmt" "codeflow.dananglin.me.uk/apollo/enbas/internal/client" @@ -45,6 +46,33 @@ func (m *UnmuteExecutor) unmuteStatus(gtsClient *client.Client) error { 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) + } + + canUnmute := false + + if status.Account.ID == myAccountID { + canUnmute = true + } else { + for _, mention := range status.Mentions { + if mention.ID == myAccountID { + canUnmute = true + break + } + } + } + + if !canUnmute { + return errors.New("unable to unmute the status because you are not the owner and you are not mentioned in it") + } + if err := gtsClient.UnmuteStatus(m.statusID); err != nil { return fmt.Errorf("unable to unmute the status: %w", err) }