go-mastodon/status.go

528 lines
16 KiB
Go
Raw Permalink Normal View History

2017-04-14 17:22:44 +02:00
package mastodon
import (
"bytes"
"context"
2017-04-14 17:22:44 +02:00
"fmt"
2019-04-27 03:56:47 +02:00
"io"
"mime/multipart"
2017-04-14 17:22:44 +02:00
"net/http"
"net/url"
"os"
"strings"
2017-04-25 20:16:32 +02:00
"time"
2017-04-14 17:22:44 +02:00
)
// Status is struct to hold status.
type Status struct {
ID ID `json:"id"`
URI string `json:"uri"`
URL string `json:"url"`
Account Account `json:"account"`
2017-04-14 17:22:44 +02:00
InReplyToID interface{} `json:"in_reply_to_id"`
InReplyToAccountID interface{} `json:"in_reply_to_account_id"`
Reblog *Status `json:"reblog"`
Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
EditedAt time.Time `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"`
2021-06-29 17:02:28 +02:00
Bookmarked interface{} `json:"bookmarked"`
Muted interface{} `json:"muted"`
2017-04-14 17:22:44 +02:00
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"`
Card *Card `json:"card"`
Poll *Poll `json:"poll"`
Application Application `json:"application"`
Language string `json:"language"`
Pinned interface{} `json:"pinned"`
2017-04-14 17:22:44 +02:00
}
// StatusHistory is a struct to hold status history data.
type StatusHistory struct {
Content string `json:"content"`
SpoilerText string `json:"spoiler_text"`
Account Account `json:"account"`
Sensitive bool `json:"sensitive"`
CreatedAt time.Time `json:"created_at"`
Emojis []Emoji `json:"emojis"`
MediaAttachments []Attachment `json:"media_attachments"`
}
2022-11-13 22:14:40 +01:00
// Context holds information for a mastodon context.
2017-04-14 17:22:44 +02:00
type Context struct {
2017-04-16 16:38:53 +02:00
Ancestors []*Status `json:"ancestors"`
Descendants []*Status `json:"descendants"`
2017-04-14 17:22:44 +02:00
}
2022-11-13 22:14:40 +01:00
// Card holds information for a mastodon card.
2017-04-14 17:25:35 +02:00
type Card struct {
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
Type string `json:"type"`
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
ProviderName string `json:"provider_name"`
ProviderURL string `json:"provider_url"`
HTML string `json:"html"`
Width int64 `json:"width"`
Height int64 `json:"height"`
2017-04-14 17:25:35 +02:00
}
// Source holds source properties so a status can be edited.
type Source struct {
ID ID `json:"id"`
Text string `json:"text"`
SpoilerText string `json:"spoiler_text"`
}
2022-11-13 22:14:40 +01:00
// Conversation holds information for a mastodon conversation.
type Conversation struct {
ID ID `json:"id"`
Accounts []*Account `json:"accounts"`
Unread bool `json:"unread"`
LastStatus *Status `json:"last_status"`
}
// Media is struct to hold media.
type Media struct {
File io.Reader
Thumbnail io.Reader
Description string
Focus string
}
func (m *Media) bodyAndContentType() (io.Reader, string, error) {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
fileName := "upload"
if f, ok := m.File.(*os.File); ok {
fileName = f.Name()
}
file, err := mw.CreateFormFile("file", fileName)
if err != nil {
return nil, "", err
}
if _, err := io.Copy(file, m.File); err != nil {
return nil, "", err
}
if m.Thumbnail != nil {
thumbName := "upload"
if f, ok := m.Thumbnail.(*os.File); ok {
thumbName = f.Name()
}
thumb, err := mw.CreateFormFile("thumbnail", thumbName)
if err != nil {
return nil, "", err
}
if _, err := io.Copy(thumb, m.Thumbnail); err != nil {
return nil, "", err
}
}
if m.Description != "" {
desc, err := mw.CreateFormField("description")
if err != nil {
return nil, "", err
}
if _, err := io.Copy(desc, strings.NewReader(m.Description)); err != nil {
return nil, "", err
}
}
if m.Focus != "" {
focus, err := mw.CreateFormField("focus")
if err != nil {
return nil, "", err
}
if _, err := io.Copy(focus, strings.NewReader(m.Focus)); err != nil {
return nil, "", err
}
}
if err := mw.Close(); err != nil {
return nil, "", err
}
return &buf, mw.FormDataContentType(), nil
}
2022-11-13 22:14:40 +01:00
// GetFavourites returns the favorite list of the current user.
2017-05-06 16:03:19 +02:00
func (c *Client) GetFavourites(ctx context.Context, pg *Pagination) ([]*Status, error) {
2017-04-14 20:28:50 +02:00
var statuses []*Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, "/api/v1/favourites", nil, &statuses, pg)
2017-04-14 20:28:50 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return nil, err
2017-04-14 20:28:50 +02:00
}
2017-05-06 16:03:19 +02:00
return statuses, nil
2017-04-14 20:28:50 +02:00
}
2022-11-13 22:14:40 +01:00
// GetBookmarks returns the bookmark list of the current user.
2021-06-29 17:02:28 +02:00
func (c *Client) GetBookmarks(ctx context.Context, pg *Pagination) ([]*Status, error) {
var statuses []*Status
err := c.doAPI(ctx, http.MethodGet, "/api/v1/bookmarks", nil, &statuses, pg)
if err != nil {
return nil, err
}
return statuses, nil
}
2022-11-13 22:14:40 +01:00
// GetStatus returns status specified by id.
2017-10-25 08:22:17 +02:00
func (c *Client) GetStatus(ctx context.Context, id ID) (*Status, error) {
2017-04-14 17:22:44 +02:00
var status Status
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s", id), nil, &status, nil)
2017-04-14 17:22:44 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2022-11-13 22:14:40 +01:00
// GetStatusContext returns status specified by id.
2017-10-25 08:22:17 +02:00
func (c *Client) GetStatusContext(ctx context.Context, id ID) (*Context, error) {
2017-04-14 17:22:44 +02:00
var context Context
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/context", id), nil, &context, nil)
2017-04-14 17:22:44 +02:00
if err != nil {
return nil, err
}
return &context, nil
}
2022-11-13 22:14:40 +01:00
// GetStatusCard returns status specified by id.
2017-10-25 08:22:17 +02:00
func (c *Client) GetStatusCard(ctx context.Context, id ID) (*Card, error) {
2017-04-14 17:25:35 +02:00
var card Card
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/card", id), nil, &card, nil)
2017-04-14 17:25:35 +02:00
if err != nil {
return nil, err
}
return &card, nil
}
2022-11-20 20:25:15 +01:00
// GetStatusSource returns source data specified by id.
func (c *Client) GetStatusSource(ctx context.Context, id ID) (*Source, error) {
var source Source
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/source", id), nil, &source, nil)
if err != nil {
return nil, err
}
return &source, nil
}
// GetStatusHistory returns the status history specified by id.
func (c *Client) GetStatusHistory(ctx context.Context, id ID) ([]*StatusHistory, error) {
var statuses []*StatusHistory
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/history", id), nil, &statuses, nil)
if err != nil {
return nil, err
}
return statuses, nil
}
2017-04-16 06:45:59 +02:00
// GetRebloggedBy returns the account list of the user who reblogged the toot of id.
2017-10-25 08:22:17 +02:00
func (c *Client) GetRebloggedBy(ctx context.Context, id ID, pg *Pagination) ([]*Account, error) {
2017-04-16 06:11:56 +02:00
var accounts []*Account
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/reblogged_by", id), nil, &accounts, pg)
2017-04-16 06:11:56 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return nil, err
2017-04-16 06:11:56 +02:00
}
2017-05-06 16:03:19 +02:00
return accounts, nil
2017-04-16 06:11:56 +02:00
}
2017-04-16 06:45:59 +02:00
// GetFavouritedBy returns the account list of the user who liked the toot of id.
2017-10-25 08:22:17 +02:00
func (c *Client) GetFavouritedBy(ctx context.Context, id ID, pg *Pagination) ([]*Account, error) {
2017-04-16 06:45:59 +02:00
var accounts []*Account
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/favourited_by", id), nil, &accounts, pg)
2017-04-16 06:45:59 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return nil, err
2017-04-16 06:45:59 +02:00
}
2017-05-06 16:03:19 +02:00
return accounts, nil
2017-04-16 06:45:59 +02:00
}
2022-11-13 22:14:40 +01:00
// Reblog reblogs the toot of id and returns status of reblog.
2017-10-25 08:22:17 +02:00
func (c *Client) Reblog(ctx context.Context, id ID) (*Status, error) {
2017-04-16 07:53:15 +02:00
var status Status
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/reblog", id), nil, &status, nil)
2017-04-16 07:53:15 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2022-11-13 22:14:40 +01:00
// Unreblog unreblogs the toot of id and returns status of the original toot.
2017-10-25 08:22:17 +02:00
func (c *Client) Unreblog(ctx context.Context, id ID) (*Status, error) {
2017-04-16 07:53:15 +02:00
var status Status
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unreblog", id), nil, &status, nil)
2017-04-16 07:53:15 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2022-11-13 22:14:40 +01:00
// Favourite favourites the toot of id and returns status of the favourite toot.
2017-10-25 08:22:17 +02:00
func (c *Client) Favourite(ctx context.Context, id ID) (*Status, error) {
2017-04-16 08:32:48 +02:00
var status Status
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/favourite", id), nil, &status, nil)
2017-04-16 08:32:48 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2022-11-13 22:14:40 +01:00
// Unfavourite unfavourites the toot of id and returns status of the unfavourite toot.
2017-10-25 08:22:17 +02:00
func (c *Client) Unfavourite(ctx context.Context, id ID) (*Status, error) {
2017-04-16 08:32:48 +02:00
var status Status
2017-10-25 08:22:17 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unfavourite", id), nil, &status, nil)
2017-04-16 08:32:48 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2022-11-13 22:14:40 +01:00
// Bookmark bookmarks the toot of id and returns status of the bookmark toot.
2021-06-29 17:02:28 +02:00
func (c *Client) Bookmark(ctx context.Context, id ID) (*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 is unbookmark the toot of id and return status of the unbookmark toot.
func (c *Client) Unbookmark(ctx context.Context, id ID) (*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
}
2017-04-14 17:22:44 +02:00
// GetTimelineHome return statuses from home timeline.
2017-05-06 16:03:19 +02:00
func (c *Client) GetTimelineHome(ctx context.Context, pg *Pagination) ([]*Status, error) {
2017-04-14 17:22:44 +02:00
var statuses []*Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/home", nil, &statuses, pg)
2017-04-14 17:22:44 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return nil, err
2017-04-14 17:22:44 +02:00
}
2017-05-06 16:03:19 +02:00
return statuses, nil
2017-04-25 20:16:32 +02:00
}
// GetTimelinePublic return statuses from public timeline.
2017-05-06 16:03:19 +02:00
func (c *Client) GetTimelinePublic(ctx context.Context, isLocal bool, pg *Pagination) ([]*Status, error) {
2017-04-25 20:16:32 +02:00
params := url.Values{}
if isLocal {
params.Set("local", "t")
}
var statuses []*Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
2017-04-25 20:16:32 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return nil, err
2017-04-25 20:16:32 +02:00
}
2017-05-06 16:03:19 +02:00
return statuses, nil
2017-04-14 17:22:44 +02:00
}
2017-04-15 14:02:55 +02:00
// GetTimelineHashtag return statuses from tagged timeline.
2017-05-06 16:03:19 +02:00
func (c *Client) GetTimelineHashtag(ctx context.Context, tag string, isLocal bool, pg *Pagination) ([]*Status, error) {
2017-04-27 19:29:11 +02:00
params := url.Values{}
if isLocal {
params.Set("local", "t")
}
2017-04-15 14:02:55 +02:00
var statuses []*Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/timelines/tag/%s", url.PathEscape(tag)), params, &statuses, pg)
2017-04-15 14:02:55 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return nil, err
2017-04-15 14:02:55 +02:00
}
2017-05-06 16:03:19 +02:00
return statuses, nil
2017-04-15 14:02:55 +02:00
}
2019-04-27 03:56:47 +02:00
// GetTimelineList return statuses from a list timeline.
func (c *Client) GetTimelineList(ctx context.Context, id ID, 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
}
2017-04-28 17:36:13 +02:00
// GetTimelineMedia return statuses from media timeline.
// NOTE: This is an experimental feature of pawoo.net.
2017-05-06 16:03:19 +02:00
func (c *Client) GetTimelineMedia(ctx context.Context, isLocal bool, pg *Pagination) ([]*Status, error) {
2017-04-28 17:36:13 +02:00
params := url.Values{}
params.Set("media", "t")
if isLocal {
params.Set("local", "t")
}
var statuses []*Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
2017-04-28 17:36:13 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return nil, err
2017-04-28 17:36:13 +02:00
}
2017-05-06 16:03:19 +02:00
return statuses, nil
2017-04-28 17:36:13 +02:00
}
2017-04-14 17:22:44 +02:00
// PostStatus post the toot.
func (c *Client) PostStatus(ctx context.Context, toot *Toot) (*Status, error) {
return c.postStatus(ctx, toot, false, ID("none"))
}
// UpdateStatus updates the toot.
func (c *Client) UpdateStatus(ctx context.Context, toot *Toot, id ID) (*Status, error) {
return c.postStatus(ctx, toot, true, id)
}
func (c *Client) postStatus(ctx context.Context, toot *Toot, update bool, updateID ID) (*Status, error) {
2017-04-14 17:22:44 +02:00
params := url.Values{}
params.Set("status", toot.Status)
if toot.InReplyToID != "" {
params.Set("in_reply_to_id", string(toot.InReplyToID))
2017-04-14 17:22:44 +02:00
}
if toot.MediaIDs != nil {
for _, media := range toot.MediaIDs {
params.Add("media_ids[]", string(media))
}
}
2021-11-05 09:21:53 +01:00
// Can't use Media and Poll at the same time.
if toot.Poll != nil && toot.Poll.Options != nil && toot.MediaIDs == nil {
for _, opt := range toot.Poll.Options {
params.Add("poll[options][]", string(opt))
}
params.Add("poll[expires_in]", fmt.Sprintf("%d", toot.Poll.ExpiresInSeconds))
if toot.Poll.Multiple {
params.Add("poll[multiple]", "true")
}
if toot.Poll.HideTotals {
params.Add("poll[hide_totals]", "true")
}
}
if toot.Visibility != "" {
params.Set("visibility", fmt.Sprint(toot.Visibility))
}
2022-11-16 20:20:18 +01:00
if toot.Language != "" {
params.Set("language", fmt.Sprint(toot.Language))
}
if toot.Sensitive {
2018-07-22 19:36:42 +02:00
params.Set("sensitive", "true")
}
if toot.SpoilerText != "" {
params.Set("spoiler_text", toot.SpoilerText)
}
2017-04-14 17:22:44 +02:00
var status Status
var err error
if !update {
err = c.doAPI(ctx, http.MethodPost, "/api/v1/statuses", params, &status, nil)
} else {
err = c.doAPI(ctx, http.MethodPut, fmt.Sprintf("/api/v1/statuses/%s", updateID), params, &status, nil)
}
2017-04-14 17:22:44 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2017-04-15 14:07:11 +02:00
// DeleteStatus delete the toot.
2017-10-25 08:22:17 +02:00
func (c *Client) DeleteStatus(ctx context.Context, id ID) error {
return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/statuses/%s", id), nil, nil, nil)
2017-04-15 14:07:11 +02:00
}
2017-04-15 16:21:16 +02:00
// Search search content with query.
func (c *Client) Search(ctx context.Context, q string, resolve bool) (*Results, error) {
2017-04-15 16:21:16 +02:00
params := url.Values{}
params.Set("q", q)
params.Set("resolve", fmt.Sprint(resolve))
var results Results
err := c.doAPI(ctx, http.MethodGet, "/api/v2/search", params, &results, nil)
2017-04-15 16:21:16 +02:00
if err != nil {
return nil, err
}
return &results, nil
}
2017-04-17 06:54:36 +02:00
2019-03-11 03:55:15 +01:00
// UploadMedia upload a media attachment from a file.
2017-04-17 06:54:36 +02:00
func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, error) {
f, err := os.Open(file)
2017-04-17 06:54:36 +02:00
if err != nil {
return nil, err
}
defer f.Close()
return c.UploadMediaFromMedia(ctx, &Media{File: f})
2017-04-17 06:54:36 +02:00
}
2019-03-11 03:55:15 +01:00
2022-11-15 03:54:56 +01:00
// UploadMediaFromBytes uploads a media attachment from a byte slice.
func (c *Client) UploadMediaFromBytes(ctx context.Context, b []byte) (*Attachment, error) {
return c.UploadMediaFromReader(ctx, bytes.NewReader(b))
}
// UploadMediaFromReader uploads a media attachment from an io.Reader.
2019-03-11 03:55:15 +01:00
func (c *Client) UploadMediaFromReader(ctx context.Context, reader io.Reader) (*Attachment, error) {
return c.UploadMediaFromMedia(ctx, &Media{File: reader})
}
// UploadMediaFromMedia uploads a media attachment from a Media struct.
func (c *Client) UploadMediaFromMedia(ctx context.Context, media *Media) (*Attachment, error) {
2019-03-11 03:55:15 +01:00
var attachment Attachment
if err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", media, &attachment, nil); err != nil {
2019-03-11 03:55:15 +01:00
return nil, err
}
return &attachment, nil
}
2019-06-21 18:44:24 +02:00
// GetTimelineDirect return statuses from direct timeline.
func (c *Client) GetTimelineDirect(ctx context.Context, pg *Pagination) ([]*Status, error) {
params := url.Values{}
2021-04-14 13:42:14 +02:00
var conversations []*Conversation
err := c.doAPI(ctx, http.MethodGet, "/api/v1/conversations", params, &conversations, pg)
2019-06-21 18:44:24 +02:00
if err != nil {
return nil, err
}
2021-04-14 13:42:14 +02:00
2021-04-15 15:02:19 +02:00
var statuses = []*Status{}
2021-04-14 13:42:14 +02:00
for _, c := range conversations {
s := c.LastStatus
statuses = append(statuses, s)
}
2019-06-21 18:44:24 +02:00
return statuses, nil
}
// GetConversations return direct conversations.
func (c *Client) GetConversations(ctx context.Context, pg *Pagination) ([]*Conversation, error) {
params := url.Values{}
var conversations []*Conversation
err := c.doAPI(ctx, http.MethodGet, "/api/v1/conversations", params, &conversations, pg)
if err != nil {
return nil, err
}
return conversations, nil
}
// DeleteConversation delete the conversation specified by id.
func (c *Client) DeleteConversation(ctx context.Context, id ID) error {
return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/conversations/%s", id), nil, nil, nil)
}
// MarkConversationAsRead mark the conversation as read.
func (c *Client) MarkConversationAsRead(ctx context.Context, id ID) error {
return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/conversations/%s/read", id), nil, nil, nil)
}