From 9c8476fa97eb2715e3253eee84e682a7fdeb3161 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 4 Jun 2024 18:38:47 +0100 Subject: [PATCH] feat: add reblogging (boosting) support Add support for reblogging (boosting) a status by adding a boost to said status. --- internal/client/statuses.go | 26 ++++++++++++++++++++++++++ internal/executor/add.go | 23 +++++++++++++++++------ internal/executor/const.go | 1 + internal/executor/remove.go | 23 +++++++++++++++++------ 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/internal/client/statuses.go b/internal/client/statuses.go index ea75e12..0a559fc 100644 --- a/internal/client/statuses.go +++ b/internal/client/statuses.go @@ -153,3 +153,29 @@ func (g *Client) GetLikedStatuses(limit int, resourceName string) (model.StatusL return liked, nil } + +func (g *Client) ReblogStatus(statusID string) error { + url := g.Authentication.Instance + "/api/v1/statuses/" + statusID + "/reblog" + + if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + return fmt.Errorf( + "received an error after sending the request to reblog the status; %w", + err, + ) + } + + return nil +} + +func (g *Client) UnreblogStatus(statusID string) error { + url := g.Authentication.Instance + "/api/v1/statuses/" + statusID + "/unreblog" + + if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + return fmt.Errorf( + "received an error after sending the request to un-reblog the status; %w", + err, + ) + } + + return nil +} diff --git a/internal/executor/add.go b/internal/executor/add.go index 9c2b317..d48e909 100644 --- a/internal/executor/add.go +++ b/internal/executor/add.go @@ -187,9 +187,14 @@ func (a *AddExecutor) addStatusToBookmarks(gtsClient *client.Client) error { } func (a *AddExecutor) addToStatus(gtsClient *client.Client) error { + if a.statusID == "" { + return FlagNotSetError{flagText: flagStatusID} + } + funcMap := map[string]func(*client.Client) error{ - resourceStar: a.addStarToStatus, - resourceLike: a.addStarToStatus, + resourceStar: a.addStarToStatus, + resourceLike: a.addStarToStatus, + resourceBoost: a.addBoostToStatus, } doFunc, ok := funcMap[a.resourceType] @@ -204,10 +209,6 @@ func (a *AddExecutor) addToStatus(gtsClient *client.Client) error { } func (a *AddExecutor) addStarToStatus(gtsClient *client.Client) error { - if a.statusID == "" { - return FlagNotSetError{flagText: flagStatusID} - } - if err := gtsClient.LikeStatus(a.statusID); err != nil { return fmt.Errorf("unable to add the %s to the status: %w", a.resourceType, err) } @@ -216,3 +217,13 @@ func (a *AddExecutor) addStarToStatus(gtsClient *client.Client) error { return nil } + +func (a *AddExecutor) addBoostToStatus(gtsClient *client.Client) error { + if err := gtsClient.ReblogStatus(a.statusID); err != nil { + return fmt.Errorf("unable to add the boost to the status: %w", err) + } + + fmt.Println("Successfully added the boost to the status.") + + return nil +} diff --git a/internal/executor/const.go b/internal/executor/const.go index c020684..4ff4af2 100644 --- a/internal/executor/const.go +++ b/internal/executor/const.go @@ -38,6 +38,7 @@ const ( resourceAccount = "account" resourceBlocked = "blocked" resourceBookmarks = "bookmarks" + resourceBoost = "boost" resourceFollowers = "followers" resourceFollowing = "following" resourceInstance = "instance" diff --git a/internal/executor/remove.go b/internal/executor/remove.go index 2f249e8..0018ad0 100644 --- a/internal/executor/remove.go +++ b/internal/executor/remove.go @@ -178,9 +178,14 @@ func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) err } func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error { + if r.statusID == "" { + return FlagNotSetError{flagText: flagStatusID} + } + funcMap := map[string]func(*client.Client) error{ - resourceStar: r.removeStarFromStatus, - resourceLike: r.removeStarFromStatus, + resourceStar: r.removeStarFromStatus, + resourceLike: r.removeStarFromStatus, + resourceBoost: r.removeBoostFromStatus, } doFunc, ok := funcMap[r.resourceType] @@ -195,10 +200,6 @@ func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error { } func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error { - if r.statusID == "" { - return FlagNotSetError{flagText: flagStatusID} - } - if err := gtsClient.UnlikeStatus(r.statusID); err != nil { return fmt.Errorf("unable to remove the %s from the status: %w", r.resourceType, err) } @@ -207,3 +208,13 @@ func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error { return nil } + +func (r *RemoveExecutor) removeBoostFromStatus(gtsClient *client.Client) error { + if err := gtsClient.UnreblogStatus(r.statusID); err != nil { + return fmt.Errorf("unable to remove the boost from the status: %w", err) + } + + fmt.Println("Successfully removed the boost from the status.") + + return nil +}