457 lines
12 KiB
Go
457 lines
12 KiB
Go
package mastodon
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Status struct {
|
|
ID ID `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 time.Time `json:"created_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"`
|
|
Bookmarked interface{} `json:"bookmarked"`
|
|
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"`
|
|
Card *Card `json:"card"`
|
|
Poll *Poll `json:"poll"`
|
|
Application Application `json:"application"`
|
|
Language string `json:"language"`
|
|
Pinned interface{} `json:"pinned"`
|
|
}
|
|
|
|
func (s *Status) GetID() string {
|
|
if s != nil {
|
|
return string(s.ID)
|
|
} else {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
type Context struct {
|
|
Ancestors []*Status `json:"ancestors"`
|
|
Descendants []*Status `json:"descendants"`
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
type Conversation struct {
|
|
ID ID `json:"id"`
|
|
Accounts []*Account `json:"accounts"`
|
|
Unread bool `json:"unread"`
|
|
LastStatus *Status `json:"last_status"`
|
|
}
|
|
|
|
func (c *Conversation) GetID() string {
|
|
if c != nil {
|
|
return string(c.ID)
|
|
} else {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func GetFavourites(pg *Pagination) ([]*Status, error) {
|
|
var statuses []*Status
|
|
err := doAPI(http.MethodGet, "/api/v1/favourites", nil, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
func GetBookmarks(pg *Pagination) ([]*Status, error) {
|
|
var statuses []*Status
|
|
err := doAPI(http.MethodGet, "/api/v1/bookmarks", nil, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
func GetStatus(id ID) (*Status, error) {
|
|
var status Status
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s", id), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func (s *Status) GetContext() (*Context, error) {
|
|
var context Context
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/context", s.GetID()), nil, &context, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &context, nil
|
|
}
|
|
|
|
func (s *Status) GetCard() (*Card, error) {
|
|
var card Card
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/card", s.GetID()), nil, &card, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &card, nil
|
|
}
|
|
|
|
func (s *Status) GetRebloggedBy(pg *Pagination) ([]*Account, error) {
|
|
var accounts []*Account
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/reblogged_by", s.GetID()), nil, &accounts, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return accounts, nil
|
|
}
|
|
|
|
func (s *Status) GetFavouritedBy(pg *Pagination) ([]*Account, error) {
|
|
var accounts []*Account
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/favourited_by", s.GetID()), nil, &accounts, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return accounts, nil
|
|
}
|
|
|
|
func (s *Status) DoReblog() (*Status, error) {
|
|
var status Status
|
|
err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/reblog", s.GetID()), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func (s *Status) Unreblog() (*Status, error) {
|
|
var status Status
|
|
err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unreblog", s.GetID()), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func (s *Status) Favourite() (*Status, error) {
|
|
var status Status
|
|
err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/favourite", s.GetID()), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func (s *Status) Unfavourite() (*Status, error) {
|
|
var status Status
|
|
err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unfavourite", s.GetID()), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func (s *Status) Bookmark() (*Status, error) {
|
|
var status Status
|
|
err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/bookmark", s.GetID()), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func (s *Status) Unbookmark() (*Status, error) {
|
|
var status Status
|
|
err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unbookmark", s.GetID()), nil, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func GetHomeTimeline(pg *Pagination) ([]*Status, error) {
|
|
var statuses []*Status
|
|
err := doAPI(http.MethodGet, "/api/v1/timelines/home", nil, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
func GetPublicTimeline(isLocal bool, pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
if isLocal {
|
|
params.Set("local", "t")
|
|
}
|
|
|
|
var statuses []*Status
|
|
err := doAPI(http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
func GetTaggedTimeline(tag string, isLocal bool, pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
if isLocal {
|
|
params.Set("local", "t")
|
|
}
|
|
|
|
var statuses []*Status
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/timelines/tag/%s", url.PathEscape(tag)), params, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
func GetListTimeline(id ID, pg *Pagination) ([]*Status, error) {
|
|
var statuses []*Status
|
|
err := doAPI(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 GetMediaTimeline(isLocal bool, pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
params.Set("media", "t")
|
|
if isLocal {
|
|
params.Set("local", "t")
|
|
}
|
|
|
|
var statuses []*Status
|
|
err := doAPI(http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
func PostStatus(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))
|
|
}
|
|
}
|
|
// 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))
|
|
}
|
|
if toot.Language != "" {
|
|
params.Set("language", fmt.Sprint(toot.Language))
|
|
}
|
|
if toot.Sensitive {
|
|
params.Set("sensitive", "true")
|
|
}
|
|
if toot.SpoilerText != "" {
|
|
params.Set("spoiler_text", toot.SpoilerText)
|
|
}
|
|
|
|
var status Status
|
|
err := doAPI(http.MethodPost, "/api/v1/statuses", params, &status, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
func (s *Status) Delete() error {
|
|
return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/statuses/%s", s.GetID()), nil, nil, nil)
|
|
}
|
|
|
|
func Search(q string, resolve bool) (*Results, error) {
|
|
params := url.Values{}
|
|
params.Set("q", q)
|
|
params.Set("resolve", fmt.Sprint(resolve))
|
|
var results Results
|
|
err := doAPI(http.MethodGet, "/api/v2/search", params, &results, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &results, nil
|
|
}
|
|
|
|
func UploadMedia(file string) (*Attachment, error) {
|
|
f, err := os.Open(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
return UploadMediaFromMedia(&Media{File: f})
|
|
}
|
|
|
|
func UploadMediaFromBytes(b []byte) (*Attachment, error) {
|
|
return UploadMediaFromReader(bytes.NewReader(b))
|
|
}
|
|
|
|
func UploadMediaFromReader(reader io.Reader) (*Attachment, error) {
|
|
return UploadMediaFromMedia(&Media{File: reader})
|
|
}
|
|
|
|
func UploadMediaFromMedia(media *Media) (*Attachment, error) {
|
|
var attachment Attachment
|
|
if err := doAPI(http.MethodPost, "/api/v1/media", media, &attachment, nil); err != nil {
|
|
return nil, err
|
|
}
|
|
return &attachment, nil
|
|
}
|
|
|
|
func GetDirectTimeline(pg *Pagination) ([]*Status, error) {
|
|
params := url.Values{}
|
|
|
|
var conversations []*Conversation
|
|
err := doAPI(http.MethodGet, "/api/v1/conversations", params, &conversations, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var statuses = []*Status{}
|
|
|
|
for _, c := range conversations {
|
|
s := c.LastStatus
|
|
statuses = append(statuses, s)
|
|
}
|
|
|
|
return statuses, nil
|
|
}
|
|
|
|
func GetConversations(pg *Pagination) ([]*Conversation, error) {
|
|
params := url.Values{}
|
|
|
|
var conversations []*Conversation
|
|
err := doAPI(http.MethodGet, "/api/v1/conversations", params, &conversations, pg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return conversations, nil
|
|
}
|
|
|
|
func (c *Conversation) Delete() error {
|
|
return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/conversations/%s", c.GetID()), nil, nil, nil)
|
|
}
|
|
|
|
func (c *Conversation) MarkAsRead() error {
|
|
return doAPI(http.MethodPost, fmt.Sprintf("/api/v1/conversations/%s/read", c.GetID()), nil, nil, nil)
|
|
}
|