From a6129d14f53f563a2dadf163d59c06b2db10293c Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 21 May 2024 00:00:15 +0100 Subject: [PATCH] checkpoint: implemented blocking and unblocking (ready for testing) --- cmd/enbas/block.go | 77 ++++++++++++++++++++++++++++++++++ cmd/enbas/main.go | 8 ++++ internal/client/accounts.go | 82 +++++++++++++++++++++++++++++++++++++ internal/client/follow.go | 70 ------------------------------- 4 files changed, 167 insertions(+), 70 deletions(-) create mode 100644 cmd/enbas/block.go delete mode 100644 internal/client/follow.go diff --git a/cmd/enbas/block.go b/cmd/enbas/block.go new file mode 100644 index 0000000..6eb6413 --- /dev/null +++ b/cmd/enbas/block.go @@ -0,0 +1,77 @@ +package main + +import ( + "flag" + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/client" +) + +type blockCommand struct { + *flag.FlagSet + + resourceType string + accountID string + unblock bool +} + +func newBlockCommand(name, summary string, unblock bool) *blockCommand { + command := blockCommand{ + FlagSet: flag.NewFlagSet(name, flag.ExitOnError), + + unblock: unblock, + } + + command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to block or unblock") + command.StringVar(&command.accountID, accountIDFlag, "", "specify the ID of the account you want to block or unblock") + + command.Usage = commandUsageFunc(name, summary, command.FlagSet) + + return &command +} + +func (c *blockCommand) Execute() error { + funcMap := map[string]func(*client.Client) error{ + accountResource: c.blockAccount, + } + + doFunc, ok := funcMap[c.resourceType] + if !ok { + return unsupportedResourceTypeError{resourceType: c.resourceType} + } + + gtsClient, err := client.NewClientFromConfig() + if err != nil { + return fmt.Errorf("unable to create the GoToSocial client; %w", err) + } + + return doFunc(gtsClient) +} + +func (c *blockCommand) blockAccount(gts *client.Client) error { + if c.accountID == "" { + return flagNotSetError{flagText: accountIDFlag} + } + + if c.unblock { + return c.unblockAccount(gts) + } + + if err := gts.BlockAccount(c.accountID); err != nil { + return fmt.Errorf("unable to block the account; %w", err) + } + + fmt.Println("Successfully blocked the account.") + + return nil +} + +func (c *blockCommand) unblockAccount(gts *client.Client) error { + if err := gts.UnblockAccount(c.accountID); err != nil { + return fmt.Errorf("unable to unblock the account; %w", err) + } + + fmt.Println("Successfully unblocked the account.") + + return nil +} diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 5bfd4ea..5f6f0c4 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -69,6 +69,8 @@ func run() error { remove string = "remove" follow string = "follow" unfollow string = "unfollow" + block string = "block" + unblock string = "unblock" ) summaries := map[string]string{ @@ -84,6 +86,8 @@ func run() error { remove: "remove a resource from another resource", follow: "follow a resource (e.g. an account)", unfollow: "unfollow a resource (e.g. an account)", + block: "block a resource (e.g. an account)", + unblock: "unblock a resource (e.g. an account)", } flag.Usage = enbasUsageFunc(summaries) @@ -126,6 +130,10 @@ func run() error { executor = newFollowCommand(follow, summaries[follow], false) case unfollow: executor = newFollowCommand(unfollow, summaries[unfollow], true) + case block: + executor = newBlockCommand(block, summaries[block], false) + case unblock: + executor = newBlockCommand(unblock, summaries[unblock], true) default: flag.Usage() diff --git a/internal/client/accounts.go b/internal/client/accounts.go index 2195154..dc0f0cb 100644 --- a/internal/client/accounts.go +++ b/internal/client/accounts.go @@ -1,6 +1,8 @@ package client import ( + "bytes" + "encoding/json" "fmt" "net/http" @@ -49,3 +51,83 @@ func (g *Client) GetAccountRelationship(accountID string) (model.AccountRelation return relationships[0], nil } + +func (g *Client) FollowAccount(accountID string, reblogs, notify bool) error { + form := struct { + ID string `json:"id"` + Reblogs bool `json:"reblogs"` + Notify bool `json:"notify"` + }{ + ID: accountID, + Reblogs: reblogs, + Notify: notify, + } + + 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/follow", accountID) + + if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil { + return fmt.Errorf("received an error after sending the follow request; %w", err) + } + + return nil +} + +func (g *Client) UnfollowAccount(accountID string) error { + url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/unfollow", accountID) + + if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + return fmt.Errorf("received an error after sending the request to unfollow the account; %w", err) + } + + return nil +} + +func (g *Client) GetFollowers(accountID string, limit int) (model.Followers, error) { + url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/followers?limit=%d", accountID, limit) + + var followers model.Followers + + if err := g.sendRequest(http.MethodGet, url, nil, &followers); err != nil { + return nil, fmt.Errorf("received an error after sending the request to get the list of followers; %w", err) + } + + return followers, nil +} + +func (g *Client) GetFollowing(accountID string, limit int) (model.Following, error) { + url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/following?limit=%d", accountID, limit) + + var following model.Following + + if err := g.sendRequest(http.MethodGet, url, nil, &following); err != nil { + return nil, fmt.Errorf("received an error after sending the request to get the list of followed accounts; %w", err) + } + + return following, nil +} + +func (g *Client) BlockAccount(accountID string) error { + url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/block", accountID) + + if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + return fmt.Errorf("received an error after sending the request to block the account; %w", err) + } + + return nil +} + +func (g *Client) UnblockAccount(accountID string) error { + url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/unblock", accountID) + + if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + return fmt.Errorf("received an error after sending the request to unblock the account; %w", err) + } + + return nil +} diff --git a/internal/client/follow.go b/internal/client/follow.go deleted file mode 100644 index cc3e416..0000000 --- a/internal/client/follow.go +++ /dev/null @@ -1,70 +0,0 @@ -package client - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - - "codeflow.dananglin.me.uk/apollo/enbas/internal/model" -) - -func (g *Client) FollowAccount(accountID string, reblogs, notify bool) error { - form := struct { - ID string `json:"id"` - Reblogs bool `json:"reblogs"` - Notify bool `json:"notify"` - }{ - ID: accountID, - Reblogs: reblogs, - Notify: notify, - } - - 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/follow", accountID) - - if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil { - return fmt.Errorf("received an error after sending the follow request; %w", err) - } - - return nil -} - -func (g *Client) UnfollowAccount(accountID string) error { - url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/unfollow", accountID) - - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { - return fmt.Errorf("received an error after sending the request to unfollow the account; %w", err) - } - - return nil -} - -func (g *Client) GetFollowers(accountID string, limit int) (model.Followers, error) { - url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/followers?limit=%d", accountID, limit) - - var followers model.Followers - - if err := g.sendRequest(http.MethodGet, url, nil, &followers); err != nil { - return nil, fmt.Errorf("received an error after sending the request to get the list of followers; %w", err) - } - - return followers, nil -} - -func (g *Client) GetFollowing(accountID string, limit int) (model.Following, error) { - url := g.Authentication.Instance + fmt.Sprintf("/api/v1/accounts/%s/following?limit=%d", accountID, limit) - - var following model.Following - - if err := g.sendRequest(http.MethodGet, url, nil, &following); err != nil { - return nil, fmt.Errorf("received an error after sending the request to get the list of followed accounts; %w", err) - } - - return following, nil -}