checkpoint: implemented blocking and unblocking (ready for testing)

This commit is contained in:
Dan Anglin 2024-05-21 00:00:15 +01:00
parent a77f3741a0
commit a6129d14f5
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 167 additions and 70 deletions

77
cmd/enbas/block.go Normal file
View file

@ -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
}

View file

@ -69,6 +69,8 @@ func run() error {
remove string = "remove" remove string = "remove"
follow string = "follow" follow string = "follow"
unfollow string = "unfollow" unfollow string = "unfollow"
block string = "block"
unblock string = "unblock"
) )
summaries := map[string]string{ summaries := map[string]string{
@ -84,6 +86,8 @@ func run() error {
remove: "remove a resource from another resource", remove: "remove a resource from another resource",
follow: "follow a resource (e.g. an account)", follow: "follow a resource (e.g. an account)",
unfollow: "unfollow 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) flag.Usage = enbasUsageFunc(summaries)
@ -126,6 +130,10 @@ func run() error {
executor = newFollowCommand(follow, summaries[follow], false) executor = newFollowCommand(follow, summaries[follow], false)
case unfollow: case unfollow:
executor = newFollowCommand(unfollow, summaries[unfollow], true) executor = newFollowCommand(unfollow, summaries[unfollow], true)
case block:
executor = newBlockCommand(block, summaries[block], false)
case unblock:
executor = newBlockCommand(unblock, summaries[unblock], true)
default: default:
flag.Usage() flag.Usage()

View file

@ -1,6 +1,8 @@
package client package client
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -49,3 +51,83 @@ func (g *Client) GetAccountRelationship(accountID string) (model.AccountRelation
return relationships[0], nil 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
}

View file

@ -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
}