checkpoint: add mute and unmute to schema

This commit is contained in:
Dan Anglin 2024-08-10 16:23:00 +01:00
parent 9d509af0df
commit 733a1fc9ac
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
9 changed files with 223 additions and 125 deletions

View file

@ -36,10 +36,11 @@ func main() {
func generateExecutors(schema enbasCLISchema, output string) error { func generateExecutors(schema enbasCLISchema, output string) error {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"capitalise": capitalise, "capitalise": capitalise,
"flagFieldName": flagFieldName, "flagFieldName": flagFieldName,
"getFlagType": schema.Flags.getType, "getFlagType": schema.Flags.getType,
"getFlagDescription": schema.Flags.getDescription, "getFlagDescription": schema.Flags.getDescription,
"customFlagValueType": customFlagValueType,
} }
tmpl := template.Must(template.New("executor-template").Funcs(funcMap).Parse(executorsFileTemplate)) tmpl := template.Must(template.New("executor-template").Funcs(funcMap).Parse(executorsFileTemplate))
@ -107,3 +108,15 @@ func convertFlagToMixedCaps(value string) string {
return builder.String() return builder.String()
} }
func customFlagValueType(flagType string) bool {
customFlagValueTypes := map[string]struct{}{
"MultiStringFlagValue": {},
"MultiIntFlagValue": {},
"TimeDurationFlagValue": {},
}
_, exists := customFlagValueTypes[flagType]
return exists
}

View file

@ -51,6 +51,13 @@ func {{ $new_executor_function_name }}(
{{- if $command.UseConfig }} {{- if $command.UseConfig }}
config: config, config: config,
{{- end }} {{- end }}
{{- range $flag := $command.Flags -}}
{{- $flag_type := getFlagType $flag.Flag -}}
{{- if customFlagValueType $flag_type -}}
{{ print "" }}
{{ flagFieldName $flag }}: New{{ $flag_type }}(),
{{- end -}}
{{- end -}}
{{- range $field := $command.AdditionalFields -}} {{- range $field := $command.AdditionalFields -}}
{{ print "" }} {{ print "" }}
{{ $field.Name }}: {{ $field.Name }}, {{ $field.Name }}: {{ $field.Name }},
@ -61,12 +68,16 @@ func {{ $new_executor_function_name }}(
exe.Usage = commandUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, exe.FlagSet) exe.Usage = commandUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, exe.FlagSet)
{{ print "" }} {{ print "" }}
{{- range $flag := $command.Flags -}} {{- range $flag := $command.Flags -}}
{{- if eq (getFlagType $flag.Flag) "string" -}} {{- $flag_type := getFlagType $flag.Flag -}}
{{- if eq $flag_type "string" -}}
{{ print "" }} {{ print "" }}
exe.StringVar(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ printf "%q" $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }}) exe.StringVar(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ printf "%q" $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
{{- else if eq (getFlagType $flag.Flag) "bool" -}} {{- else if eq $flag_type "bool" -}}
{{ print "" }} {{ print "" }}
exe.BoolVar(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }}) exe.BoolVar(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
{{- else if customFlagValueType $flag_type -}}
{{ print "" }}
exe.Var(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{ print "" }} {{ print "" }}

View file

@ -129,11 +129,9 @@ func run() error {
enbasPrinter, enbasPrinter,
enbasConfig, enbasConfig,
), ),
executor.CommandMute: executor.NewMuteOrUnmuteExecutor( executor.CommandMute: executor.NewMuteExecutor(
enbasPrinter, enbasPrinter,
enbasConfig, enbasConfig,
executor.CommandMute,
executor.CommandSummaryLookup(executor.CommandMute),
), ),
executor.CommandReject: executor.NewRejectExecutor( executor.CommandReject: executor.NewRejectExecutor(
enbasPrinter, enbasPrinter,
@ -153,11 +151,9 @@ func run() error {
enbasPrinter, enbasPrinter,
enbasConfig, enbasConfig,
), ),
executor.CommandUnmute: executor.NewMuteOrUnmuteExecutor( executor.CommandUnmute: executor.NewUnmuteExecutor(
enbasPrinter, enbasPrinter,
enbasConfig, enbasConfig,
executor.CommandUnmute,
executor.CommandSummaryLookup(executor.CommandUnmute),
), ),
executor.CommandUnblock: executor.NewUnblockExecutor( executor.CommandUnblock: executor.NewUnblockExecutor(
enbasPrinter, enbasPrinter,

View file

@ -196,6 +196,37 @@ func NewLoginExecutor(
return &exe return &exe
} }
// MuteExecutor is the executor for the mute command.
type MuteExecutor struct {
*flag.FlagSet
printer *printer.Printer
config *config.Config
accountName string
muteDuration TimeDurationFlagValue
muteNotifications bool
resourceType string
}
func NewMuteExecutor(
printer *printer.Printer,
config *config.Config,
) *MuteExecutor {
exe := MuteExecutor{
FlagSet: flag.NewFlagSet("mute", flag.ExitOnError),
printer: printer,
config: config,
muteDuration: NewTimeDurationFlagValue(),
}
exe.Usage = commandUsageFunc("mute", "Mutes a specific resource (e.g. an account)", exe.FlagSet)
exe.StringVar(&exe.accountName, "account-name", "", "The name of the account")
exe.Var(&exe.muteDuration, "mute-duration", "Specify how long the mute should last for. To mute indefinitely, set this to 0s")
exe.BoolVar(&exe.muteNotifications, "mute-notifications", false, "Set to true to mute notifications as well as posts")
exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)")
return &exe
}
// RejectExecutor is the executor for the reject command. // RejectExecutor is the executor for the reject command.
type RejectExecutor struct { type RejectExecutor struct {
*flag.FlagSet *flag.FlagSet
@ -300,6 +331,32 @@ func NewUnfollowExecutor(
return &exe return &exe
} }
// UnmuteExecutor is the executor for the unmute command.
type UnmuteExecutor struct {
*flag.FlagSet
printer *printer.Printer
config *config.Config
accountName string
resourceType string
}
func NewUnmuteExecutor(
printer *printer.Printer,
config *config.Config,
) *UnmuteExecutor {
exe := UnmuteExecutor{
FlagSet: flag.NewFlagSet("unmute", flag.ExitOnError),
printer: printer,
config: config,
}
exe.Usage = commandUsageFunc("unmute", "Umutes a specific resource (e.g. an account)", exe.FlagSet)
exe.StringVar(&exe.accountName, "account-name", "", "The name of the account")
exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)")
return &exe
}
// VersionExecutor is the executor for the version command. // VersionExecutor is the executor for the version command.
type VersionExecutor struct { type VersionExecutor struct {
*flag.FlagSet *flag.FlagSet

View file

@ -101,8 +101,14 @@ type TimeDurationFlagValue struct {
Duration time.Duration Duration time.Duration
} }
func NewTimeDurationFlagValue() TimeDurationFlagValue {
return TimeDurationFlagValue{
Duration: 0 * time.Second,
}
}
func (v TimeDurationFlagValue) String() string { func (v TimeDurationFlagValue) String() string {
return "" return "Time duration: " + v.Duration.String()
} }
func (v *TimeDurationFlagValue) Set(text string) error { func (v *TimeDurationFlagValue) Set(text string) error {

49
internal/executor/mute.go Normal file
View file

@ -0,0 +1,49 @@
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,
}
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 {
if m.accountName == "" {
return FlagNotSetError{flagText: flagAccountName}
}
accountID, err := getAccountID(gtsClient, false, m.accountName, m.config.CredentialsFile)
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
}

View file

@ -1,108 +0,0 @@
package executor
import (
"flag"
"fmt"
"time"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
)
type MuteOrUnmuteExecutor struct {
*flag.FlagSet
printer *printer.Printer
config *config.Config
accountName string
command string
resourceType string
muteDuration TimeDurationFlagValue
muteNotifications bool
}
func NewMuteOrUnmuteExecutor(printer *printer.Printer, config *config.Config, name, summary string) *MuteOrUnmuteExecutor {
muteDuration := TimeDurationFlagValue{time.Duration(0 * time.Second)}
exe := MuteOrUnmuteExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: printer,
config: config,
command: name,
muteDuration: muteDuration,
}
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.NewClientFromFile(m.config.CredentialsFile)
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.config.CredentialsFile)
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
}

View file

@ -0,0 +1,44 @@
package executor
import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
)
func (m *UnmuteExecutor) Execute() error {
funcMap := map[string]func(*client.Client) error{
resourceAccount: m.unmuteAccount,
}
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 *UnmuteExecutor) unmuteAccount(gtsClient *client.Client) error {
if m.accountName == "" {
return FlagNotSetError{flagText: flagAccountName}
}
accountID, err := getAccountID(gtsClient, false, m.accountName, m.config.CredentialsFile)
if err != nil {
return fmt.Errorf("received an error while getting the account ID: %w", err)
}
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
}

View file

@ -17,12 +17,20 @@
"description": "The ID of the list in question" "description": "The ID of the list in question"
}, },
"list-title": { "list-title": {
"type": "string", "type": "string",
"description": "The title of the list" "description": "The title of the list"
}, },
"list-replies-policy": { "list-replies-policy": {
"type": "string", "type": "string",
"description": "The replies policy of the list" "description": "The replies policy of the list"
},
"mute-duration": {
"type": "TimeDurationFlagValue",
"description": "Specify how long the mute should last for. To mute indefinitely, set this to 0s"
},
"mute-notifications": {
"type": "bool",
"description": "Set to true to mute notifications as well as posts"
}, },
"notify": { "notify": {
"type": "bool", "type": "bool",
@ -115,6 +123,18 @@
"useConfig": true, "useConfig": true,
"usePrinter": true "usePrinter": true
}, },
"mute": {
"additionalFields": [],
"flags": [
{ "flag": "account-name", "default": "" },
{ "flag": "mute-duration" },
{ "flag": "mute-notifications", "default": "false" },
{ "flag": "type", "fieldName": "resourceType", "default": "" }
],
"summary": "Mutes a specific resource (e.g. an account)",
"useConfig": true,
"usePrinter": true
},
"reject": { "reject": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
@ -155,6 +175,16 @@
"useConfig": true, "useConfig": true,
"usePrinter": true "usePrinter": true
}, },
"unmute": {
"additionalFields": [],
"flags": [
{ "flag": "account-name", "default": "" },
{ "flag": "type", "fieldName": "resourceType", "default": "" }
],
"summary": "Umutes a specific resource (e.g. an account)",
"useConfig": true,
"usePrinter": true
},
"version": { "version": {
"additionalFields": [ "additionalFields": [
{ "name": "binaryVersion", "type": "string"}, { "name": "binaryVersion", "type": "string"},