~thrrgilag/woodstock

woodstock/posts.go -rw-r--r-- 7.4 KiB
62b10b5cMorgan McMillian Update for new project location and module name 3 months ago
                                                                                
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package woodstock

import (
	"encoding/json"
	"net/url"
	"strconv"
	"strings"
)

// PostResult response definition
type PostResult struct {
	*CommonResponse
	Data Post `json:"data"`
}

// PostsResult response definition
type PostsResult struct {
	*CommonResponse
	Data []Post `json:"data"`
}

// ActionsResult response definition
type ActionsResult struct {
	*CommonResponse
	Data []Action `json:"data"`
}

type NewPost struct {
	Text               string `json:"text"`
	ParseLinks         string `json:"entities.parse_links,omitempty"`
	ParseMarkdownLinks string `json:"entities.parse_markdown_links,omitempty"`
	Nsfw               string `json:"is_nsfw,omitempty"`
	Raw                []Raw  `json:"raw"`
	ReplyTo            string `json:"reply_to,omitempty"`
}

// GetPost retrieves a post
// https://pnut.io/docs/resources/posts/lookup#get-posts-id
func (c *Client) GetPost(id string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetPosts retrieves mutliple posts
// https://pnut.io/docs/resources/posts/lookup#get-posts
func (c *Client) GetPosts(ids []string) (result PostsResult, err error) {
	v := url.Values{}
	v.Set("ids", strings.Join(ids, ","))
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "?" + v.Encode(), data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetPostRevisions retrieves a list of reviesions of a post
// https://pnut.io/docs/resources/posts/lookup#get-posts-id-revisions
func (c *Client) GetPostRevisions(id string) (result PostsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id + "/revisions", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// Post creates a post
// https://pnut.io/docs/resources/posts/lifecycle#post-posts
func (c *Client) Post(params NewPost) (result PostResult, err error) {
	json, err := json.Marshal(params)
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI, data: &result, method: "POST", responseCh: responseCh, json: string(json)}
	return result, (<-responseCh).err
}

// RevisePost updates an existing post
// https://pnut.io/docs/resources/posts/lifecycle#put-posts-id
func (c *Client) RevisePost(id string, params NewPost) (result PostResult, err error) {
	json, err := json.Marshal(params)
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id, data: &result, method: "PUT", responseCh: responseCh, json: string(json)}
	return result, (<-responseCh).err
}

// DeletePost deletes a post
// https://pnut.io/docs/resources/posts/lifecycle#delete-posts-id
func (c *Client) DeletePost(id string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id, data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// MeStream retrieves a personal stream
// https://pnut.io/docs/resources/posts/streams#get-posts-streams-me
func (c *Client) MeStream(count ...int) (result PostsResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: StreamMeAPI + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnifiedStream retrieves a unified stream
// https://pnut.io/docs/resources/posts/streams#get-posts-streams-unified
func (c *Client) UnifiedStream(count ...int) (result PostsResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: StreamUnifiedAPI + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GlobalStream retrieves the global stream
// https://pnut.io/docs/resources/posts/streams#get-posts-streams-unified
func (c *Client) GlobalStream(count ...int) (result PostsResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: StreamGlobalAPI + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// TagStream retrieves a stream for a given tag
// https://pnut.io/docs/resources/posts/streams#get-posts-tag-tag
func (c *Client) TagStream(tag string, count ...int) (result PostsResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: StreamTabBaseURL + "/" + tag + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetThread retrieves post in a given thread
// https://pnut.io/docs/resources/posts/threads#get-posts-id-thread
func (c *Client) GetThread(id string) (result PostsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id + "/thread", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// Repost repeats a post
// https://pnut.io/docs/resources/posts/reposts#put-posts-id-repost
func (c *Client) Repost(id string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id + "/repost", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnRepost removes a repeated post
// https://pnut.io/docs/resources/posts/reposts#delete-posts-id-repost
func (c *Client) UnRepost(id string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id + "/repost", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// Bookmark creates a bookmark (stars) a post
// https://pnut.io/docs/resources/posts/bookmarks#put-posts-id-bookmark
func (c *Client) Bookmark(id string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id + "/bookmark", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnBookmark deletes a bookmark (unstars) a post
// https://pnut.io/docs/resources/posts/bookmarks#put-posts-id-bookmark
func (c *Client) UnBookmark(id string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id + "/bookmark", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetActions retrieves actions
// https://pnut.io/docs/resources/posts/interactions#get-posts-id-interactions
func (c *Client) GetActions(id string, v ...url.Values) (result ActionsResult, err error) {
	param := ""
	if len(v) > 0 {
		param = v[0].Encode()
	}
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id + "/interactions" + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}