go-mastodon/status.go

264 lines
8.1 KiB
Go
Raw Normal View History

2017-04-14 17:22:44 +02:00
package mastodon
import (
"context"
2017-04-14 17:22:44 +02:00
"fmt"
"net/http"
"net/url"
"strconv"
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"`
2017-04-14 17:22:44 +02:00
CreatedAt time.Time `json:"created_at"`
InReplyToID interface{} `json:"in_reply_to_id"`
InReplyToAccountID interface{} `json:"in_reply_to_account_id"`
Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"`
Visibility string `json:"visibility"`
Application Application `json:"application"`
Account Account `json:"account"`
MediaAttachments []Attachment `json:"media_attachments"`
Mentions []Mention `json:"mentions"`
Tags []Tag `json:"tags"`
URI string `json:"uri"`
Content string `json:"content"`
URL string `json:"url"`
ReblogsCount int64 `json:"reblogs_count"`
FavouritesCount int64 `json:"favourites_count"`
Reblog *Status `json:"reblog"`
Favourited interface{} `json:"favourited"`
Reblogged interface{} `json:"reblogged"`
}
// Context hold information for mastodon context.
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
}
2017-04-14 17:25:35 +02:00
// Card hold information for mastodon card.
type Card struct {
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
}
2017-04-14 20:28:50 +02:00
// GetFavourites return 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
}
2017-04-14 17:22:44 +02:00
// GetStatus return status specified by id.
func (c *Client) GetStatus(ctx context.Context, id int64) (*Status, error) {
2017-04-14 17:22:44 +02:00
var status Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%d", id), nil, &status, nil)
2017-04-14 17:22:44 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
// GetStatusContext return status specified by id.
func (c *Client) GetStatusContext(ctx context.Context, id int64) (*Context, error) {
2017-04-14 17:22:44 +02:00
var context Context
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%d/context", id), nil, &context, nil)
2017-04-14 17:22:44 +02:00
if err != nil {
return nil, err
}
return &context, nil
}
2017-04-14 17:25:35 +02:00
// GetStatusCard return status specified by id.
func (c *Client) GetStatusCard(ctx context.Context, id int64) (*Card, error) {
2017-04-14 17:25:35 +02:00
var card Card
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%d/card", id), nil, &card, nil)
2017-04-14 17:25:35 +02:00
if err != nil {
return nil, err
}
return &card, nil
}
2017-04-16 06:45:59 +02:00
// GetRebloggedBy returns the account list of the user who reblogged the toot of id.
2017-05-06 16:03:19 +02:00
func (c *Client) GetRebloggedBy(ctx context.Context, id int64, pg *Pagination) ([]*Account, error) {
2017-04-16 06:11:56 +02:00
var accounts []*Account
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%d/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-05-06 16:03:19 +02:00
func (c *Client) GetFavouritedBy(ctx context.Context, id int64, pg *Pagination) ([]*Account, error) {
2017-04-16 06:45:59 +02:00
var accounts []*Account
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/statuses/%d/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
}
2017-04-16 08:04:59 +02:00
// Reblog is reblog the toot of id and return status of reblog.
func (c *Client) Reblog(ctx context.Context, id int64) (*Status, error) {
2017-04-16 07:53:15 +02:00
var status Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%d/reblog", id), nil, &status, nil)
2017-04-16 07:53:15 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2017-04-16 08:04:59 +02:00
// Unreblog is unreblog the toot of id and return status of the original toot.
func (c *Client) Unreblog(ctx context.Context, id int64) (*Status, error) {
2017-04-16 07:53:15 +02:00
var status Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%d/unreblog", id), nil, &status, nil)
2017-04-16 07:53:15 +02:00
if err != nil {
return nil, err
}
return &status, nil
}
2017-04-16 08:32:48 +02:00
// Favourite is favourite the toot of id and return status of the favourite toot.
func (c *Client) Favourite(ctx context.Context, id int64) (*Status, error) {
2017-04-16 08:32:48 +02:00
var status Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%d/favourite", id), nil, &status, nil)
2017-04-16 08:32:48 +02:00
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 int64) (*Status, error) {
2017-04-16 08:32:48 +02:00
var status Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%d/unfavourite", id), nil, &status, nil)
2017-04-16 08:32:48 +02:00
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
}
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) {
2017-04-14 17:22:44 +02:00
params := url.Values{}
params.Set("status", toot.Status)
if toot.InReplyToID > 0 {
params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID))
}
if toot.MediaIDs != nil {
for _, media := range toot.MediaIDs {
params.Add("media_ids[]", strconv.FormatInt(media, 10))
}
}
if toot.Visibility != "" {
params.Set("visibility", fmt.Sprint(toot.Visibility))
}
if toot.Sensitive {
params.Set("senstitive", "true")
}
if toot.SpoilerText != "" {
params.Set("spoiler_text", toot.SpoilerText)
}
2017-04-14 17:22:44 +02:00
var status Status
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodPost, "/api/v1/statuses", 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.
func (c *Client) DeleteStatus(ctx context.Context, id int64) error {
2017-05-06 16:03:19 +02:00
return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/statuses/%d", 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
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodGet, "/api/v1/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
2017-04-20 14:20:40 +02:00
// UploadMedia upload a media attachment.
2017-04-17 06:54:36 +02:00
func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, error) {
var attachment Attachment
2017-05-06 16:03:19 +02:00
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", file, &attachment, nil)
2017-04-17 06:54:36 +02:00
if err != nil {
return nil, err
}
return &attachment, nil
}