feat: block and unblock accounts #9

Manually merged
dananglin merged 1 commit from account-blocking into main 2024-05-21 13:27:10 +01:00
6 changed files with 220 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

@ -40,6 +40,7 @@ const (
timelineResource = "timeline"
followersResource = "followers"
followingResource = "following"
blockedResource = "blocked"
)
type Executor interface {
@ -69,6 +70,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 +87,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 +131,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()

View file

@ -59,6 +59,7 @@ func (c *showCommand) Execute() error {
listResource: c.showList,
followersResource: c.showFollowers,
followingResource: c.showFollowing,
blockedResource: c.showBlocked,
}
doFunc, ok := funcMap[c.resourceType]
@ -299,3 +300,18 @@ func (c *showCommand) showFollowing(gts *client.Client) error {
return nil
}
func (c *showCommand) showBlocked(gts *client.Client) error {
blocked, err := gts.GetBlockedAccounts(c.limit)
if err != nil {
return fmt.Errorf("unable to retrieve the list of blocked accounts; %w", err)
}
if len(blocked) > 0 {
fmt.Println(blocked)
} else {
fmt.Println("You have no blocked accounts.")
}
return nil
}

View file

@ -1,6 +1,8 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
@ -49,3 +51,95 @@ 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
}
func (g *Client) GetBlockedAccounts(limit int) (model.BlockedAccounts, error) {
url := g.Authentication.Instance + fmt.Sprintf("/api/v1/blocks?limit=%d", limit)
var blocked model.BlockedAccounts
if err := g.sendRequest(http.MethodGet, url, nil, &blocked); err != nil {
return nil, fmt.Errorf("received an error after sending the request to get the list of blocked accounts; %w", err)
}
return blocked, 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
}

24
internal/model/blocked.go Normal file
View file

@ -0,0 +1,24 @@
package model
import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
type BlockedAccounts []Account
func (b BlockedAccounts) String() string {
output := "\n"
output += utilities.HeaderFormat("BLOCKED ACCOUNTS:")
for i := range b {
output += fmt.Sprintf(
"\n • %s (%s)",
b[i].Acct,
b[i].ID,
)
}
return output
}