Documentation
¶
Overview ¶
Package mastodon is a dependency-free read client for the Mastodon REST API.
It uses only the Go standard library (CGO_ENABLED=0) and exposes a small surface for reading public, hashtag and per-account timelines.
Index ¶
- type Account
- type Client
- func (c *Client) AccountStatuses(ctx context.Context, acct string, opts TimelineOptions) (*Timeline, error)
- func (c *Client) Following(ctx context.Context, accountID string, opts TimelineOptions) (*FollowingPage, error)
- func (c *Client) HashtagTimeline(ctx context.Context, tag string, opts TimelineOptions) (*Timeline, error)
- func (c *Client) HomeTimeline(ctx context.Context, opts TimelineOptions) (*Timeline, error)
- func (c *Client) PublicTimeline(ctx context.Context, opts TimelineOptions) (*Timeline, error)
- func (c *Client) SearchAccounts(ctx context.Context, q string, limit int) ([]Account, error)
- func (c *Client) VerifyCredentials(ctx context.Context) (*Account, error)
- type FollowingPage
- type Media
- type Option
- type Status
- type Tag
- type Timeline
- type TimelineOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Account ¶
type Account struct {
ID string `json:"id"`
Username string `json:"username"`
Acct string `json:"acct"`
DisplayName string `json:"display_name"`
URL string `json:"url"`
Avatar string `json:"avatar"`
Note string `json:"note"` // HTML bio
FollowersCount int `json:"followers_count"`
}
Account is a Mastodon account.
type Client ¶
type Client struct {
// Instance is the base URL of the Mastodon instance,
// e.g. https://mastodon.social.
Instance string
// Token is an optional bearer token. When set, requests are
// authenticated with an Authorization header.
Token string
// HTTPClient is the underlying HTTP client. When nil, http.DefaultClient
// is used.
HTTPClient *http.Client
// UserAgent is the value sent in the User-Agent header.
UserAgent string
}
Client is a read-only Mastodon REST API client.
func (*Client) AccountStatuses ¶
func (c *Client) AccountStatuses(ctx context.Context, acct string, opts TimelineOptions) (*Timeline, error)
AccountStatuses resolves acct via GET /api/v1/accounts/lookup and then fetches that account's statuses (GET /api/v1/accounts/:id/statuses).
func (*Client) Following ¶ added in v0.2.0
func (c *Client) Following(ctx context.Context, accountID string, opts TimelineOptions) (*FollowingPage, error)
Following fetches one page of the accounts that accountID follows (GET /api/v1/accounts/:id/following). Feed the returned FollowingPage.MaxID back through TimelineOptions.MaxID to page the rest. A bearer token authenticates the request (required when the target hides their follows); a public follows list is readable anonymously.
func (*Client) HashtagTimeline ¶
func (c *Client) HashtagTimeline(ctx context.Context, tag string, opts TimelineOptions) (*Timeline, error)
HashtagTimeline fetches the timeline for a hashtag (GET /api/v1/timelines/tag/:hashtag).
func (*Client) HomeTimeline ¶ added in v0.2.0
HomeTimeline fetches the authenticated user's home timeline — the statuses from the accounts they follow (GET /api/v1/timelines/home). A bearer token is required; without one the instance returns 401.
func (*Client) PublicTimeline ¶
PublicTimeline fetches the public timeline (GET /api/v1/timelines/public).
func (*Client) SearchAccounts ¶ added in v0.3.0
SearchAccounts returns the accounts matching q via GET /api/v2/search (type=accounts) — used to discover accounts to follow. It reads the "accounts" slice of the search result and ignores statuses/hashtags. limit caps the page (0 = server default). A token is not required on most instances for account search, but one (via WithToken) broadens the results (remote resolution).
func (*Client) VerifyCredentials ¶ added in v0.2.0
VerifyCredentials fetches the account associated with the configured bearer token (GET /api/v1/accounts/verify_credentials), returning at least its ID — the handle a caller needs to then page the account's own Client.Following list. A bearer token is required; without one the instance returns 401.
type FollowingPage ¶ added in v0.2.0
FollowingPage is one page of the accounts a target follows, plus the pagination cursor. MaxID is parsed from the Link header's rel="next" URL and passed back via TimelineOptions.MaxID to fetch the following page; it is empty when the list is exhausted. Mastodon keys following pagination on an internal relationship id it exposes only through the Link header, so the cursor is opaque and must be round-tripped rather than derived from an account id.
type Media ¶
type Media struct {
Type string `json:"type"` // image, video, gifv, audio
URL string `json:"url"`
PreviewURL string `json:"preview_url"`
Description string `json:"description"`
}
Media is a media attachment on a status.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used to perform requests.
func WithUserAgent ¶
WithUserAgent sets the User-Agent header value.
type Status ¶
type Status struct {
ID string `json:"id"`
URL string `json:"url"`
Content string `json:"content"` // HTML
CreatedAt time.Time `json:"created_at"`
Account Account `json:"account"`
Favourites int `json:"favourites_count"`
Reblogs int `json:"reblogs_count"`
Replies int `json:"replies_count"`
Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"`
Media []Media `json:"media_attachments"`
Tags []Tag `json:"tags"`
}
Status is a Mastodon status (toot).
type Timeline ¶
type Timeline struct {
Statuses []Status
// MaxID is extracted from the Link header rel="next" and can be passed
// as TimelineOptions.MaxID to fetch the following page. It is empty when
// there is no next page.
MaxID string
}
Timeline is a page of statuses plus the pagination cursor.
