mirror of
https://gitea.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-22 23:13:00 +00:00
501 lines
15 KiB
Go
501 lines
15 KiB
Go
package mastodon
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/url"
|
|
"path/filepath"
|
|
"time"
|
|
"encoding/json"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type StatusPleroma struct {
|
|
InReplyToAccountAcct string `json:"in_reply_to_account_acct"`
|
|
Reactions []*ReactionsPleroma `json:"emoji_reactions"`
|
|
Quote *Status `json:"quote"` // Quoted statuses
|
|
}
|
|
|
|
type ReactionsPleroma struct {
|
|
Accounts []Account `json:"accounts"`
|
|
Count int `json:"count"`
|
|
Me bool `json:"me"`
|
|
Name string `json:"name"`
|
|
|
|
// For support akkoma reactions :)
|
|
Url *string `json:"url"`
|
|
}
|
|
|
|
type ReplyInfo struct {
|
|
ID string `json:"id"`
|
|
Number int `json:"number"`
|
|
}
|
|
|
|
type CreatedAt struct {
|
|
time.Time
|
|
}
|
|
|
|
type EditedAt struct{
|
|
time.Time
|
|
}
|
|
|
|
func (t *CreatedAt) UnmarshalJSON(d []byte) error {
|
|
// Special case to handle retweets from GNU Social
|
|
// which returns empty string ("") in created_at
|
|
if len(d) == 2 && string(d) == `""` {
|
|
return nil
|
|
}
|
|
return t.Time.UnmarshalJSON(d)
|
|
}
|
|
|
|
// Status is struct to hold status.
|
|
type Status struct {
|
|
ID string `json:"id"`
|
|
URI string `json:"uri"`
|
|
URL string `json:"url"`
|
|
Account Account `json:"account"`
|
|
InReplyToID interface{} `json:"in_reply_to_id"`
|
|
InReplyToAccountID interface{} `json:"in_reply_to_account_id"`
|
|
Reblog *Status `json:"reblog"`
|
|
Content string `json:"content"`
|
|
CreatedAt CreatedAt `json:"created_at"`
|
|
EditedAt *EditedAt `json:"edited_at"`
|
|
Emojis []Emoji `json:"emojis"`
|
|
RepliesCount int64 `json:"replies_count"`
|
|
ReblogsCount int64 `json:"reblogs_count"`
|
|
FavouritesCount int64 `json:"favourites_count"`
|
|
Reblogged interface{} `json:"reblogged"`
|
|
Favourited interface{} `json:"favourited"`
|
|
Muted interface{} `json:"muted"`
|
|
Sensitive bool `json:"sensitive"`
|
|
SpoilerText string `json:"spoiler_text"`
|
|
Visibility string `json:"visibility"`
|
|
MediaAttachments []Attachment `json:"media_attachments"`
|
|
Mentions []Mention `json:"mentions"`
|
|
Tags []Tag `json:"tags"`
|
|
Application Application `json:"application"`
|
|
Language string `json:"language"`
|
|
Pinned interface{} `json:"pinned"`
|
|
Bookmarked bool `json:"bookmarked"`
|
|
Poll *Poll `json:"poll"`
|
|
|
|
// Custom fields
|
|
Pleroma StatusPleroma `json:"pleroma"`
|
|
ShowReplies bool `json:"show_replies"`
|
|
IDReplies map[string][]ReplyInfo `json:"id_replies"`
|
|
IDNumbers map[string]int `json:"id_numbers"`
|
|
RetweetedByID string `json:"retweeted_by_id"`
|
|
}
|
|
|
|
// Context hold information for mastodon context.
|
|
type Context struct {
|
|
Ancestors []*Status `json:"ancestors"`
|
|
Descendants []*Status `json:"descendants"`
|
|
}
|
|
|
|
// GetFavourites return the favorite list of the current user.
|
|
func (c *Client) GetFavourites(ctx context.Context, pg *Pagination) ([]*Status, error) {
|
|
var statuses []*Status
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/favourites", nil, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// GetStatus return status specified by id.
|
|
func (c *Client) GetStatus(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// GetStatusContext return status specified by id.
|
|
func (c *Client) GetStatusContext(ctx context.Context, id string) (*Context, error) {
|
|
var context Context
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/context", id), nil, &context, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &context, nil
|
|
}
|
|
|
|
// GetRebloggedBy returns the account list of the user who reblogged the toot of id.
|
|
func (c *Client) GetRebloggedBy(ctx context.Context, id string, pg *Pagination) ([]*Account, error) {
|
|
var accounts []*Account
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/reblogged_by", id), nil, &accounts, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return accounts, nil
|
|
}
|
|
|
|
// GetFavouritedBy returns the account list of the user who liked the toot of id.
|
|
func (c *Client) GetFavouritedBy(ctx context.Context, id string, pg *Pagination) ([]*Account, error) {
|
|
var accounts []*Account
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/favourited_by", id), nil, &accounts, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return accounts, nil
|
|
}
|
|
|
|
// GetReactionBy returns the reactions list of the user who reacted the toot of id. (Pleroma)
|
|
func (c *Client) GetReactedBy(ctx context.Context, id string) ([]*ReactionsPleroma, error) {
|
|
var reactions []*ReactionsPleroma
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/pleroma/statuses/%s/reactions", id), nil, &reactions, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return reactions, nil
|
|
}
|
|
|
|
// PutReaction is reaction on status with unicode emoji (Pleroma)
|
|
func (c *Client) PutReaction(ctx context.Context, id string, emoji string) (*Status, error) {
|
|
var status Status
|
|
err := c.doAPI(ctx, http.MethodPut, fmt.Sprintf("/api/v1/pleroma/statuses/%s/reactions/%s", id, emoji), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// UnReaction is unreaction on status with unicode emoji (Pleroma)
|
|
func (c *Client) UnReaction(ctx context.Context, id string, emoji string) (*Status, error) {
|
|
var status Status
|
|
err := c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/pleroma/statuses/%s/reactions/%s", id, emoji), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Reblog is reblog the toot of id and return status of reblog.
|
|
func (c *Client) Reblog(ctx context.Context, id string, visibility string) (*Status, error) {
|
|
var status Status
|
|
params := url.Values{}
|
|
params.Set("visibility", visibility)
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/reblog", id), params, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Unreblog is unreblog the toot of id and return status of the original toot.
|
|
func (c *Client) Unreblog(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unreblog", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Favourite is favourite the toot of id and return status of the favourite toot.
|
|
func (c *Client) Favourite(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/favourite", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Unfavourite is unfavourite the toot of id and return status of the unfavourite toot.
|
|
func (c *Client) Unfavourite(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unfavourite", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// GetTimelineHome return statuses from home timeline.
|
|
func (c *Client) GetTimelineHome(ctx context.Context, pg *Pagination) ([]*Status, error) {
|
|
var statuses []*Status
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/home", nil, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
type RemoteTimelineInstance struct {
|
|
http.Client
|
|
}
|
|
|
|
// TrueRemoteTimeline get public timeline from remote Mastodon API compatible instance directly
|
|
func (c *Client) TrueRemoteTimeline(ctx context.Context, instance string, pg *Pagination) ([]*Status, error) {
|
|
var httpclient RemoteTimelineInstance
|
|
var publicstatuses []*Status
|
|
params := url.Values{}
|
|
params.Set("local", "true")
|
|
if pg != nil {
|
|
params = pg.setValues(params)
|
|
}
|
|
u, err := url.Parse("https://" + instance)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
u.Path = path.Join(u.Path, "/api/v1/timelines/public")
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), strings.NewReader(params.Encode()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req = req.WithContext(ctx)
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
resp, err := httpclient.Do(req)
|
|
fmt.Println(req)
|
|
fmt.Println(resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, parseAPIError("bad request", resp)
|
|
}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&publicstatuses)
|
|
fmt.Println(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return publicstatuses, nil
|
|
}
|
|
|
|
// GetTimelinePublic return statuses from public timeline.
|
|
func (c *Client) GetTimelinePublic(ctx context.Context, isLocal bool, instance string, pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
if len(instance) > 0 {
|
|
params.Set("instance", instance)
|
|
} else if isLocal {
|
|
params.Set("local", "true")
|
|
}
|
|
|
|
var statuses []*Status
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// GetTimelineHashtag return statuses from tagged timeline.
|
|
func (c *Client) GetTimelineHashtag(ctx context.Context, tag string, isLocal bool, pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
if isLocal {
|
|
params.Set("local", "t")
|
|
}
|
|
|
|
var statuses []*Status
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/timelines/tag/%s", url.PathEscape(tag)), params, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// GetTimelineList return statuses from a list timeline.
|
|
func (c *Client) GetTimelineList(ctx context.Context, id string, pg *Pagination) ([]*Status, error) {
|
|
var statuses []*Status
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/timelines/list/%s", url.PathEscape(string(id))), nil, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// GetTimelineMedia return statuses from media timeline.
|
|
// NOTE: This is an experimental feature of pawoo.net.
|
|
func (c *Client) GetTimelineMedia(ctx context.Context, isLocal bool, pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
params.Set("media", "t")
|
|
if isLocal {
|
|
params.Set("local", "t")
|
|
}
|
|
|
|
var statuses []*Status
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// PostStatus post the toot.
|
|
func (c *Client) PostStatus(ctx context.Context, toot *Toot) (*Status, error) {
|
|
params := url.Values{}
|
|
params.Set("status", toot.Status)
|
|
if toot.InReplyToID != "" {
|
|
params.Set("in_reply_to_id", string(toot.InReplyToID))
|
|
}
|
|
if toot.MediaIDs != nil {
|
|
for _, media := range toot.MediaIDs {
|
|
params.Add("media_ids[]", string(media))
|
|
}
|
|
}
|
|
if toot.Visibility != "" {
|
|
params.Set("visibility", fmt.Sprint(toot.Visibility))
|
|
}
|
|
if toot.Sensitive {
|
|
params.Set("sensitive", "true")
|
|
}
|
|
if toot.SpoilerText != "" {
|
|
params.Set("spoiler_text", toot.SpoilerText)
|
|
}
|
|
if toot.ContentType != "" {
|
|
params.Set("content_type", toot.ContentType)
|
|
}
|
|
var status Status
|
|
if toot.Edit != "" {
|
|
err := c.doAPI(ctx, http.MethodPut, fmt.Sprintf("/api/v1/statuses/%s", toot.Edit), params, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
err := c.doAPI(ctx, http.MethodPost, "/api/v1/statuses", params, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Pin pin your status.
|
|
func (c *Client) Pin(ctx context.Context, id string) error {
|
|
return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/pin", id), nil, nil, nil)
|
|
}
|
|
|
|
// UnPin unpin your status.
|
|
func (c *Client) UnPin(ctx context.Context, id string) error {
|
|
return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unpin", id), nil, nil, nil)
|
|
}
|
|
|
|
// DeleteStatus delete the toot.
|
|
func (c *Client) DeleteStatus(ctx context.Context, id string) error {
|
|
return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/statuses/%s", id), nil, nil, nil)
|
|
}
|
|
|
|
// Search search content with query.
|
|
func (c *Client) Search(ctx context.Context, q string, qType string, limit int, resolve bool, offset int, accountID string, following bool) (*Results, error) {
|
|
var results Results
|
|
params := url.Values{}
|
|
params.Set("q", q)
|
|
params.Set("type", qType)
|
|
params.Set("limit", fmt.Sprint(limit))
|
|
params.Set("resolve", fmt.Sprint(resolve))
|
|
params.Set("offset", fmt.Sprint(offset))
|
|
params.Set("following", fmt.Sprint(following))
|
|
if len(accountID) > 0 {
|
|
params.Set("account_id", accountID)
|
|
}
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v2/search", params, &results, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &results, nil
|
|
}
|
|
|
|
func (c *Client) UploadMediaFromMultipartFileHeader(ctx context.Context, fh *multipart.FileHeader) (*Attachment, error) {
|
|
f, err := fh.Open()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
var buf bytes.Buffer
|
|
mw := multipart.NewWriter(&buf)
|
|
fname := filepath.Base(fh.Filename)
|
|
err = mw.WriteField("description", fname)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
part, err := mw.CreateFormFile("file", fname)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = io.Copy(part, f)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = mw.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
params := &multipartRequest{Data: &buf, ContentType: mw.FormDataContentType()}
|
|
var attachment Attachment
|
|
err = c.doAPI(ctx, http.MethodPost, "/api/v1/media", params, &attachment, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &attachment, nil
|
|
}
|
|
|
|
// GetTimelineDirect return statuses from direct timeline.
|
|
func (c *Client) GetTimelineDirect(ctx context.Context, pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
|
|
var statuses []*Status
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/direct", params, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// MuteConversation mutes status specified by id.
|
|
func (c *Client) MuteConversation(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/mute", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// UnmuteConversation unmutes status specified by id.
|
|
func (c *Client) UnmuteConversation(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unmute", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Bookmark bookmarks status specified by id.
|
|
func (c *Client) Bookmark(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/bookmark", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Unbookmark bookmarks status specified by id.
|
|
func (c *Client) Unbookmark(ctx context.Context, id string) (*Status, error) {
|
|
var status Status
|
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unbookmark", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|