aboutsummaryrefslogtreecommitdiff
path: root/web/mastodon/status.go
blob: bdb0bbf9007ca1ad9d73d16ddad35579e6efe731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package mastodon

import (
	"context"
	"encoding/json"
	"io"
	"net/http"
	"net/url"
	"time"
)

type CreateStatusParams struct {
	Status      string     `json:"status"`
	InReplyTo   string     `json:"in_reply_to_id"`
	MediaIDs    []string   `json:"media_ids"`
	SpoilerText string     `json:"spoiler_text"`
	Visibility  string     `json:"visibility"`
	ScheduledAt *time.Time `json:"scheduled_at,omitempty"`
}

func (csp CreateStatusParams) Values() url.Values {
	result := url.Values{}

	result.Set("status", csp.Status)

	if csp.Visibility != "" {
		result.Set("visibility", csp.Visibility)
	}

	if csp.InReplyTo != "" {
		result.Set("in_reply_to_id", csp.InReplyTo)
	}

	if csp.SpoilerText != "" {
		result.Set("spoiler_text", csp.SpoilerText)
	}

	for _, id := range csp.MediaIDs {
		result.Add("media_ids[]", id)
	}

	return result
}

func (c *Client) CreateStatus(ctx context.Context, csp CreateStatusParams) (*Status, error) {
	vals := csp.Values()

	u, err := c.server.Parse("/api/v1/statuses")
	if err != nil {
		return nil, err
	}

	resp, err := c.cli.PostForm(u.String(), vals)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	var result Status
	err = json.NewDecoder(resp.Body).Decode(&result)
	if err != nil {
		return nil, err
	}

	return &result, nil
}

// FetchStatus fetches a Mastodon status over the internet using the federation protocol.
//
// This will not work if the target server has "secure" mode enabled.
func FetchStatus(ctx context.Context, statusURL string) (*Status, error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, statusURL, nil)
	if err != nil {
		return nil, err
	}

	req.Header.Set("Accept", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var result Status
	if err := json.NewDecoder(io.LimitReader(resp.Body, 1024*1024*2)).Decode(&result); err != nil {
		return nil, err
	}

	return &result, nil
}