2024-02-27 20:05:03 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-05-30 18:46:33 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2024-02-27 20:05:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
|
|
)
|
|
|
|
|
2024-07-09 00:13:46 +01:00
|
|
|
const (
|
|
|
|
baseStatusesPath string = "/api/v1/statuses"
|
|
|
|
)
|
|
|
|
|
2024-02-27 20:05:03 +00:00
|
|
|
func (g *Client) GetStatus(statusID string) (model.Status, error) {
|
2024-07-09 00:13:46 +01:00
|
|
|
path := baseStatusesPath + "/" + statusID
|
2024-02-27 20:05:03 +00:00
|
|
|
url := g.Authentication.Instance + path
|
|
|
|
|
|
|
|
var status model.Status
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodGet,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: &status,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-02-27 20:05:03 +00:00
|
|
|
return model.Status{}, fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to get the status information: %w",
|
2024-02-27 20:05:03 +00:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status, nil
|
|
|
|
}
|
2024-05-30 18:46:33 +01:00
|
|
|
|
|
|
|
type CreateStatusForm struct {
|
2024-08-15 21:40:17 +01:00
|
|
|
Content string `json:"status"`
|
|
|
|
InReplyTo string `json:"in_reply_to_id"`
|
|
|
|
Language string `json:"language"`
|
|
|
|
SpoilerText string `json:"spoiler_text"`
|
|
|
|
Boostable bool `json:"boostable"`
|
|
|
|
Federated bool `json:"federated"`
|
|
|
|
Likeable bool `json:"likeable"`
|
|
|
|
Replyable bool `json:"replyable"`
|
|
|
|
Sensitive bool `json:"sensitive"`
|
|
|
|
Poll *CreateStatusPollForm `json:"poll,omitempty"`
|
|
|
|
ContentType model.StatusContentType `json:"content_type"`
|
|
|
|
Visibility model.StatusVisibility `json:"visibility"`
|
|
|
|
AttachmentIDs []string `json:"media_ids,omitempty"`
|
2024-05-30 18:46:33 +01:00
|
|
|
}
|
|
|
|
|
2024-06-15 18:40:13 +01:00
|
|
|
type CreateStatusPollForm struct {
|
|
|
|
Options []string `json:"options"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
Multiple bool `json:"multiple"`
|
|
|
|
HideTotals bool `json:"hide_totals"`
|
|
|
|
}
|
|
|
|
|
2024-05-30 18:46:33 +01:00
|
|
|
func (g *Client) CreateStatus(form CreateStatusForm) (model.Status, error) {
|
|
|
|
data, err := json.Marshal(form)
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return model.Status{}, fmt.Errorf("unable to create the JSON form: %w", err)
|
2024-05-30 18:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
2024-07-09 00:13:46 +01:00
|
|
|
url := g.Authentication.Instance + baseStatusesPath
|
2024-05-30 18:46:33 +01:00
|
|
|
|
|
|
|
var status model.Status
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: requestBody,
|
|
|
|
contentType: applicationJSON,
|
|
|
|
output: &status,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-05-30 18:46:33 +01:00
|
|
|
return model.Status{}, fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to create the status: %w",
|
2024-05-30 18:46:33 +01:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status, nil
|
|
|
|
}
|
2024-06-03 06:53:31 +01:00
|
|
|
|
|
|
|
func (g *Client) GetBookmarks(limit int) (model.StatusList, error) {
|
|
|
|
path := fmt.Sprintf("/api/v1/bookmarks?limit=%d", limit)
|
|
|
|
url := g.Authentication.Instance + path
|
|
|
|
|
|
|
|
bookmarks := model.StatusList{
|
2024-06-04 17:01:33 +01:00
|
|
|
Name: "Your Bookmarks",
|
2024-06-03 06:53:31 +01:00
|
|
|
Statuses: nil,
|
|
|
|
}
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodGet,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: &bookmarks.Statuses,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-03 06:53:31 +01:00
|
|
|
return bookmarks, fmt.Errorf(
|
|
|
|
"received an error after sending the request to get the bookmarks: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bookmarks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) AddStatusToBookmarks(statusID string) error {
|
|
|
|
path := fmt.Sprintf("/api/v1/statuses/%s/bookmark", statusID)
|
|
|
|
url := g.Authentication.Instance + path
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-03 06:53:31 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"received an error after sending the request to add the status to the list of bookmarks: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) RemoveStatusFromBookmarks(statusID string) error {
|
|
|
|
path := fmt.Sprintf("/api/v1/statuses/%s/unbookmark", statusID)
|
|
|
|
url := g.Authentication.Instance + path
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-03 06:53:31 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"received an error after sending the request to remove the status from the list of bookmarks: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-04 17:01:33 +01:00
|
|
|
|
|
|
|
func (g *Client) LikeStatus(statusID string) error {
|
2024-07-09 00:13:46 +01:00
|
|
|
url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/favourite"
|
2024-06-04 17:01:33 +01:00
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-04 17:01:33 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"received an error after sending the request to like the status: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) UnlikeStatus(statusID string) error {
|
2024-07-09 00:13:46 +01:00
|
|
|
url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/unfavourite"
|
2024-06-04 17:01:33 +01:00
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-04 17:01:33 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"received an error after sending the request to unlike the status: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) GetLikedStatuses(limit int, resourceName string) (model.StatusList, error) {
|
|
|
|
url := g.Authentication.Instance + fmt.Sprintf("/api/v1/favourites?limit=%d", limit)
|
|
|
|
|
|
|
|
liked := model.StatusList{
|
|
|
|
Name: "Your " + resourceName + " statuses",
|
|
|
|
Statuses: nil,
|
|
|
|
}
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodGet,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: &liked.Statuses,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-04 17:01:33 +01:00
|
|
|
return model.StatusList{}, fmt.Errorf(
|
|
|
|
"received an error after sending the request to get the list of statuses: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return liked, nil
|
|
|
|
}
|
2024-06-04 18:38:47 +01:00
|
|
|
|
|
|
|
func (g *Client) ReblogStatus(statusID string) error {
|
2024-07-09 00:13:46 +01:00
|
|
|
url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/reblog"
|
2024-06-04 18:38:47 +01:00
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-04 18:38:47 +01:00
|
|
|
return fmt.Errorf(
|
2024-08-16 14:42:57 +01:00
|
|
|
"received an error after sending the request to reblog the status: %w",
|
2024-06-04 18:38:47 +01:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) UnreblogStatus(statusID string) error {
|
2024-07-09 00:13:46 +01:00
|
|
|
url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/unreblog"
|
2024-06-04 18:38:47 +01:00
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-04 18:38:47 +01:00
|
|
|
return fmt.Errorf(
|
2024-08-16 14:42:57 +01:00
|
|
|
"received an error after sending the request to un-reblog the status: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) MuteStatus(statusID string) error {
|
|
|
|
url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/mute"
|
|
|
|
|
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"received an error after sending the request to mute the status: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) UnmuteStatus(statusID string) error {
|
|
|
|
url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/unmute"
|
|
|
|
|
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"received an error after sending the request to unmute the status: %w",
|
2024-06-04 18:38:47 +01:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-16 18:53:08 +01:00
|
|
|
|
|
|
|
func (g *Client) DeleteStatus(statusID string) (string, error) {
|
|
|
|
url := g.Authentication.Instance + baseStatusesPath + "/" + statusID
|
|
|
|
|
|
|
|
var status model.Status
|
|
|
|
|
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodDelete,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: &status,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"received an error after sending the request to delete the status: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status.Text, nil
|
|
|
|
}
|