-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforums.go
275 lines (203 loc) · 8.72 KB
/
forums.go
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package guildedgo
import (
"errors"
"fmt"
)
type ForumTopic struct {
// The ID of the forum topic
ID int `json:"id"`
// The ID of the server
ServerID string `json:"serverId"`
// The ID of the channel
ChannelID string `json:"channelId"`
// The title of the forum topic (min length 1; max length 500)
Title string `json:"title"`
// The ISO 8601 timestamp that the forum topic was created at
CreatedAt string `json:"createdAt"`
// The ID of the user who created this forum topic
// (Note: If this event has createdByWebhookId present, this field will still be populated,
// but can be ignored. In this case, the value of this field will always be Ann6LewA)
CreatedBy string `json:"createdBy"`
// The ID of the webhook who created this forum topic, if it was created by a webhook
CreatedByWebhookID string `json:"createdByWebhookId,omitempty"`
// The ISO 8601 timestamp that the forum topic was updated at, if relevant
UpdatedAt string `json:"updatedAt,omitempty"`
// The ISO 8601 timestamp that the forum topic was bumped at.
// This timestamp is updated whenever there is any activity on the posts within the forum topic.
BumpedAt string `json:"bumpedAt,omitempty"`
// (default false)
IsPinned bool `json:"isPinned,omitempty"`
// (default false)
IsLocked bool `json:"isLocked,omitempty"`
// The content of the forum topic
Content string `json:"content"`
Mentions `json:"mentions,omitempty"`
}
type ForumTopicSummary struct {
// The ID of the forum topic
ID int `json:"id"`
// The ID of the server
ServerID string `json:"serverId"`
// The ID of the channel
ChannelID string `json:"channelId"`
// The title of the forum topic (min length 1; max length 500)
Title string `json:"title"`
// The ISO 8601 timestamp that the forum topic was created at
CreatedAt string `json:"createdAt"`
// The ID of the user who created this forum topic
// (Note: If this event has createdByWebhookId present, this field will still be populated,
// but can be ignored. In this case, the value of this field will always be Ann6LewA)
CreatedBy string `json:"createdBy"`
// The ID of the webhook who created this forum topic, if it was created by a webhook
CreatedByWebhookID string `json:"createdByWebhookId,omitempty"`
// The ISO 8601 timestamp that the forum topic was updated at, if relevant
UpdatedAt string `json:"updatedAt,omitempty"`
// The ISO 8601 timestamp that the forum topic was bumped at.
// This timestamp is updated whenever there is any activity on the posts within the forum topic.
BumpedAt string `json:"bumpedAt,omitempty"`
// (default false)
IsPinned bool `json:"isPinned,omitempty"`
// (default false)
IsLocked bool `json:"isLocked,omitempty"`
}
type ForumTopicComment struct {
ID int `json:"id"`
Content string `json:"content"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
ChannelID string `json:"channelId"`
ForumTopicID int `json:"forumTopicId"`
CreatedBy string `json:"createdBy"`
Mentions `json:"mentions"`
}
type ForumCommentObject struct {
Content string `json:"content"`
}
type ForumTopicObject struct {
Title string `json:"title"`
Content string `json:"content"`
}
type UpdateTopicObject struct {
Title string `json:"title,omitempty"`
Content string `json:"content,omitempty"`
}
type forumEndpoints struct{}
func (e *forumEndpoints) Default(channelId string) string {
return guildedApi + "/channels/" + channelId + "/topics"
}
func (e *forumEndpoints) Get(channelId string, forumTopicId int) string {
return guildedApi + "/channels/" + channelId + "/topics/" + fmt.Sprint(forumTopicId)
}
func (e *forumEndpoints) Pin(channelId string, forumTopicId int) string {
return guildedApi + "/channels/" + channelId + "/topics/" + fmt.Sprint(forumTopicId) + "/pin"
}
func (e *forumEndpoints) Lock(channelId string, forumTopicId int) string {
return guildedApi + "/channels/" + channelId + "/topics/" + fmt.Sprint(forumTopicId) + "/lock"
}
func (e *forumEndpoints) Comments(channelId string, forumTopicId int) string {
return guildedApi + "/channels/" + channelId + "/topics/" + fmt.Sprint(forumTopicId) + "/comments"
}
type forumService struct {
client *Client
endpoints *forumEndpoints
}
type ForumService interface {
CreateForumTopic(channelId string, forumTopicObject *ForumTopicObject) (*ForumTopic, error)
GetForumTopics(channelId string) (*[]ForumTopicSummary, error)
GetForumTopic(channelId string, forumTopicId int) (*ForumTopic, error)
UpdateForumTopic(channelId string, forumTopicId int, updateTopicObject *UpdateTopicObject) (*ForumTopic, error)
DeleteForumTopic(channelId string, forumTopicId int) error
PinForumTopic(channelId string, forumTopicId int) error
UnpinForumTopic(channelId string, forumTopicId int) error
LockForumTopic(channelId string, forumTopicId int) error
UnlockForumTopic(channelId string, forumTopicId int) error
CreateTopicComment(channelId string, forumTopicId int, forumCommentObject *ForumCommentObject) (*ForumTopicComment, error)
}
var _ ForumService = &forumService{
endpoints: &forumEndpoints{},
}
func (service *forumService) CreateForumTopic(channelId string, forumTopicObject *ForumTopicObject) (*ForumTopic, error) {
endpoint := service.endpoints.Default(channelId)
var forumTopic ForumTopic
err := service.client.PostRequestV2(endpoint, &forumTopicObject, &forumTopic)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to create new forum topic. Error: \n%v", err.Error()))
}
return &forumTopic, nil
}
func (service *forumService) GetForumTopics(channelId string) (*[]ForumTopicSummary, error) {
endpoint := service.endpoints.Default(channelId)
var forumTopicSummary []ForumTopicSummary
err := service.client.GetRequestV2(endpoint, &forumTopicSummary)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to get forum topics. Error: \n%v", err.Error()))
}
return &forumTopicSummary, nil
}
func (service *forumService) GetForumTopic(channelId string, forumTopicId int) (*ForumTopic, error) {
endpoint := service.endpoints.Get(channelId, forumTopicId)
var forumTopic ForumTopic
err := service.client.GetRequestV2(endpoint, &forumTopic)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to get forum topic. Error: \n%v", err.Error()))
}
return &forumTopic, nil
}
func (service *forumService) UpdateForumTopic(channelId string, forumTopicId int, topicObject *UpdateTopicObject) (*ForumTopic, error) {
endpoint := service.endpoints.Get(channelId, forumTopicId)
var forumTopic ForumTopic
err := service.client.PatchRequest(endpoint, &topicObject, &forumTopic)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to update forum topic. Error: \n%v", err.Error()))
}
return &forumTopic, nil
}
func (service *forumService) DeleteForumTopic(channelId string, forumTopicId int) error {
endpoint := service.endpoints.Get(channelId, forumTopicId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return errors.New(fmt.Sprintf("Failed to delete forum topic. Error: \n%v", err.Error()))
}
return nil
}
func (service *forumService) PinForumTopic(channelId string, forumTopicId int) error {
endpoint := service.endpoints.Pin(channelId, forumTopicId)
_, err := service.client.PutRequest(endpoint, nil)
if err != nil {
return errors.New(fmt.Sprintf("Failed to pin forum topic. Error: \n%v", err.Error()))
}
return nil
}
func (service *forumService) UnpinForumTopic(channelId string, forumTopicId int) error {
endpoint := service.endpoints.Pin(channelId, forumTopicId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return errors.New(fmt.Sprintf("Failed to unpin forum topic. Error: \n%v", err.Error()))
}
return nil
}
func (service *forumService) LockForumTopic(channelId string, forumTopicId int) error {
endpoint := service.endpoints.Lock(channelId, forumTopicId)
_, err := service.client.PutRequest(endpoint, nil)
if err != nil {
return errors.New(fmt.Sprintf("Failed to lock forum topic. Error: \n%v", err.Error()))
}
return nil
}
func (service *forumService) UnlockForumTopic(channelId string, forumTopicId int) error {
endpoint := service.endpoints.Lock(channelId, forumTopicId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return errors.New(fmt.Sprintf("Failed to unlock forum topic. Error: \n%v", err.Error()))
}
return nil
}
func (service *forumService) CreateTopicComment(channelId string, forumTopicId int, forumCommentObject *ForumCommentObject) (*ForumTopicComment, error) {
endpoint := service.endpoints.Comments(channelId, forumTopicId)
var forumComment ForumTopicComment
err := service.client.PostRequestV2(endpoint, &forumCommentObject, &forumComment)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to create new forum topic comment. Error: \n%v", err.Error()))
}
return &forumComment, nil
}