checkpoint: add ability to follow accounts
This commit is contained in:
parent
b91ebe5864
commit
b1240c42f7
3 changed files with 113 additions and 0 deletions
75
cmd/enbas/follow.go
Normal file
75
cmd/enbas/follow.go
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
type followCommand struct {
|
||||||
|
*flag.FlagSet
|
||||||
|
|
||||||
|
resourceType string
|
||||||
|
accountID string
|
||||||
|
showReposts bool
|
||||||
|
notify bool
|
||||||
|
unfollow bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFollowCommand(name, summary string, unfollow bool) *followCommand {
|
||||||
|
command := followCommand{
|
||||||
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
|
||||||
|
unfollow: unfollow,
|
||||||
|
}
|
||||||
|
|
||||||
|
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to follow")
|
||||||
|
command.StringVar(&command.accountID, accountIDFlag, "", "specify the ID of the account you want to follow")
|
||||||
|
command.BoolVar(&command.showReposts, "show-reposts", true, "show reposts from the account you want to follow")
|
||||||
|
command.BoolVar(&command.notify, "notify", false, "get notifications when the account you want to follow posts a status")
|
||||||
|
|
||||||
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||||
|
|
||||||
|
return &command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *followCommand) Execute() error {
|
||||||
|
funcMap := map[string]func(*client.Client) error{
|
||||||
|
accountResource: c.followAccount,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 *followCommand) followAccount(gts *client.Client) error {
|
||||||
|
if c.accountID == "" {
|
||||||
|
return flagNotSetError{flagText: accountIDFlag}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.unfollow {
|
||||||
|
return c.unfollowAccount(gts)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gts.FollowAccount(c.accountID, c.showReposts, c.notify); err != nil {
|
||||||
|
return fmt.Errorf("unable to follow the account; %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("The follow request was sent successfully.")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *followCommand) unfollowAccount(gts *client.Client) error {
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -57,6 +57,7 @@ func run() error {
|
||||||
whoami string = "whoami"
|
whoami string = "whoami"
|
||||||
add string = "add"
|
add string = "add"
|
||||||
remove string = "remove"
|
remove string = "remove"
|
||||||
|
follow string = "follow"
|
||||||
)
|
)
|
||||||
|
|
||||||
summaries := map[string]string{
|
summaries := map[string]string{
|
||||||
|
@ -70,6 +71,7 @@ func run() error {
|
||||||
whoami: "print the account that you are currently logged in to",
|
whoami: "print the account that you are currently logged in to",
|
||||||
add: "add a resource to another resource",
|
add: "add a resource to another resource",
|
||||||
remove: "remove a resource from another resource",
|
remove: "remove a resource from another resource",
|
||||||
|
follow: "follow a resource (e.g. an account)",
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.Usage = enbasUsageFunc(summaries)
|
flag.Usage = enbasUsageFunc(summaries)
|
||||||
|
@ -108,6 +110,8 @@ func run() error {
|
||||||
executor = newAddCommand(add, summaries[add])
|
executor = newAddCommand(add, summaries[add])
|
||||||
case remove:
|
case remove:
|
||||||
executor = newRemoveCommand(remove, summaries[remove])
|
executor = newRemoveCommand(remove, summaries[remove])
|
||||||
|
case follow:
|
||||||
|
executor = newFollowCommand(follow, summaries[follow], false)
|
||||||
default:
|
default:
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
|
|
||||||
|
|
34
internal/client/follow.go
Normal file
34
internal/client/follow.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in a new issue