Compare commits

...

2 commits

Author SHA1 Message Date
b12f2328b3
add client method to add a private note 2024-05-21 23:08:11 +01:00
6e40792fec
fix: redesign the add and remove commands
Redesigned the add and remove subcommands so that it is easier to add
logic when adding and removing additional resources.
2024-05-21 23:05:23 +01:00
4 changed files with 60 additions and 6 deletions

View file

@ -10,6 +10,7 @@ import (
type addCommand struct { type addCommand struct {
*flag.FlagSet *flag.FlagSet
resourceType string
toResourceType string toResourceType string
listID string listID string
accountNames accountNames accountNames accountNames
@ -23,7 +24,8 @@ func newAddCommand(name, summary string) *addCommand {
accountNames: accountNames(emptyArr), accountNames: accountNames(emptyArr),
} }
command.StringVar(&command.toResourceType, addToFlag, "", "specify the type of resource to add to") command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the resource type to add (e.g. account, note)")
command.StringVar(&command.toResourceType, addToFlag, "", "specify the target resource type to add to (e.g. list, account, etc)")
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to add to") command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to add to")
command.Var(&command.accountNames, accountNameFlag, "the name of the account to add to the resource") command.Var(&command.accountNames, accountNameFlag, "the name of the account to add to the resource")
@ -38,7 +40,7 @@ func (c *addCommand) Execute() error {
} }
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{
listResource: c.addAccountsToList, listResource: c.addToList,
} }
doFunc, ok := funcMap[c.toResourceType] doFunc, ok := funcMap[c.toResourceType]
@ -54,6 +56,19 @@ func (c *addCommand) Execute() error {
return doFunc(gtsClient) return doFunc(gtsClient)
} }
func (c *addCommand) addToList(gtsClient *client.Client) error {
funcMap := map[string]func(*client.Client) error{
accountResource: c.addAccountsToList,
}
doFunc, ok := funcMap[c.resourceType]
if !ok {
return unsupportedResourceTypeError{resourceType: c.resourceType}
}
return doFunc(gtsClient)
}
func (c *addCommand) addAccountsToList(gtsClient *client.Client) error { func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
if c.listID == "" { if c.listID == "" {
return flagNotSetError{flagText: listIDFlag} return flagNotSetError{flagText: listIDFlag}
@ -70,6 +85,7 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
if err != nil { if err != nil {
return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err) return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err)
} }
accountIDs[i] = accountID accountIDs[i] = accountID
} }

View file

@ -8,13 +8,13 @@ import (
const ( const (
accountNameFlag = "account-name" accountNameFlag = "account-name"
addToFlag = "add-to" addToFlag = "to"
instanceFlag = "instance" instanceFlag = "instance"
listIDFlag = "list-id" listIDFlag = "list-id"
listTitleFlag = "list-title" listTitleFlag = "list-title"
listRepliesPolicyFlag = "list-replies-policy" listRepliesPolicyFlag = "list-replies-policy"
myAccountFlag = "my-account" myAccountFlag = "my-account"
removeFromFlag = "remove-from" removeFromFlag = "from"
resourceTypeFlag = "type" resourceTypeFlag = "type"
statusIDFlag = "status-id" statusIDFlag = "status-id"
tagFlag = "tag" tagFlag = "tag"

View file

@ -10,6 +10,7 @@ import (
type removeCommand struct { type removeCommand struct {
*flag.FlagSet *flag.FlagSet
resourceType string
fromResourceType string fromResourceType string
listID string listID string
accountNames accountNames accountNames accountNames
@ -23,7 +24,8 @@ func newRemoveCommand(name, summary string) *removeCommand {
accountNames: accountNames(emptyArr), accountNames: accountNames(emptyArr),
} }
command.StringVar(&command.fromResourceType, removeFromFlag, "", "specify the type of resource to remove from") command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the resource type to remove (e.g. account, note)")
command.StringVar(&command.fromResourceType, removeFromFlag, "", "specify the resource type to remove from (e.g. list, account, etc)")
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to remove from") command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to remove from")
command.Var(&command.accountNames, accountNameFlag, "the name of the account to remove from the resource") command.Var(&command.accountNames, accountNameFlag, "the name of the account to remove from the resource")
@ -38,7 +40,7 @@ func (c *removeCommand) Execute() error {
} }
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{
listResource: c.removeAccountsFromList, listResource: c.removeFromList,
} }
doFunc, ok := funcMap[c.fromResourceType] doFunc, ok := funcMap[c.fromResourceType]
@ -54,6 +56,19 @@ func (c *removeCommand) Execute() error {
return doFunc(gtsClient) return doFunc(gtsClient)
} }
func (c *removeCommand) removeFromList(gtsClient *client.Client) error {
funcMap := map[string]func(*client.Client) error{
accountResource: c.removeAccountsFromList,
}
doFunc, ok := funcMap[c.resourceType]
if !ok {
return unsupportedResourceTypeError{resourceType: c.resourceType}
}
return doFunc(gtsClient)
}
func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error { func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error {
if c.listID == "" { if c.listID == "" {
return flagNotSetError{flagText: listIDFlag} return flagNotSetError{flagText: listIDFlag}
@ -70,6 +85,7 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error {
if err != nil { if err != nil {
return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err) return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err)
} }
accountIDs[i] = accountID accountIDs[i] = accountID
} }

View file

@ -158,3 +158,25 @@ func (g *Client) GetBlockedAccounts(limit int) (model.AccountList, error) {
return blocked, nil return blocked, nil
} }
func (g *Client) SetPrivateNote(accountID, note string) error {
form := struct {
Comment string `json:"comment"`
}{
Comment: note,
}
data, err := json.Marshal(form)
if err != nil {
return fmt.Errorf("unable to marshal the form; %w", err)
}
requestBody := bytes.NewBuffer(data)
url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/note", accountID)
if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil {
return fmt.Errorf("received an error after sending the request to set the private note; %w", err)
}
return nil
}