-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel.go
336 lines (262 loc) · 9.9 KB
/
channel.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package guildedgo
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
type ServerChannel struct {
ID string `json:"id"`
// The type of channel. This will determine what routes to use for creating content in a channel.
// For example, if this "chat", then one must use the routes for creating channel messages
Type string `json:"type"`
Name string `json:"name"`
// The topic of the channel. Not applicable to threads (min length 1; max length 512)
Topic string `json:"topic,omitempty"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
UpdatedAt string `json:"updatedAt,omitempty"`
ServerID string `json:"serverId"`
// ID of the root channel or thread in the channel hierarchy.
// Only applicable to "chat", "voice", and "stream" channels and indicates that this channel is a thread, if present
RootID string `json:"rootId,omitempty"`
// ID of the immediate parent channel or thread in the channel hierarchy.
// Only applicable to "chat", "voice", and "stream" channels and indicates that this channel is a thread, if present
ParentID string `json:"parentId,omitempty"`
// The ID of the message that this channel was created off of.
// Only applicable to "chat", "voice", and "stream" channels and indicates that this channel is a thread, if present
MessageID string `json:"messageId,omitempty"`
// The category that the channel exists in. Only relevant for server channels
CategoryID int `json:"categoryId,omitempty"`
GroupID string `json:"groupId"`
IsPublic bool `json:"isPublic,omitempty"`
ArchivedBy string `json:"archivedBy,omitempty"`
ArchivedAt string `json:"archivedAt,omitempty"`
}
const (
ChannelTypeAnnouncements string = "announcements"
ChannelTypeChat string = "chat"
ChannelTypeCalendar string = "calendar"
ChannelTypeForums string = "forums"
ChannelTypeMedia string = "media"
ChannelTypeDocs string = "docs"
ChannelTypeVoice string = "voice"
ChannelTypeList string = "list"
ChannelTypeScheduling string = "scheduling"
ChannelTypeStream string = "stream"
)
type Mentions struct {
// Info on mentioned users (min items 1)
Users []MentionsUser `json:"users,omitempty"`
// Info on mentioned channels (min items 1)
Channels []MentionsChannel `json:"channels,omitempty"`
// Info on mentioned roles (min items 1)
Roles []MentionsRole `json:"roles,omitempty"`
// If @everyone was mentioned
Everyone bool `json:"everyone,omitempty"`
// If @here was mentioned
Here bool `json:"here,omitempty"`
}
type MentionsRole struct {
// The ID of the role
ID int `json:"id"`
}
type MentionsChannel struct {
// The ID of the channel
ID string `json:"id"`
}
type MentionsUser struct {
// The ID of the user
ID string `json:"id"`
}
type NewChannelObject struct {
Name string `json:"name"`
Topic string `json:"topic,omitempty"`
IsPublic bool `json:"isPublic,omitempty"`
Type string `json:"type"`
ServerID string `json:"serverId,omitempty"`
GroupID string `json:"groupId,omitempty"`
CategoryID int `json:"categoryId,omitempty"`
ParentID string `json:"parentId,omitempty"`
MessageID string `json:"messageId,omitempty"`
}
type UpdateChannelObject struct {
Name string `json:"name,omitempty"`
Topic string `json:"topic,omitempty"`
IsPublic bool `json:"isPublic,omitempty"`
}
type ServerChannelResponse struct {
Channel ServerChannel `json:"channel"`
}
type ChannelService interface {
CreateChannel(channelObject *NewChannelObject) (*ServerChannel, error)
GetChannel(channelId string) (*ServerChannel, error)
UpdateChannel(channelId string, channelObject *UpdateChannelObject) (*ServerChannel, error)
DeleteChannel(channelId string) error
SendMessage(channelId string, message *MessageObject) (*ChatMessage, error)
GetMessages(channelId string, getObject *GetMessagesObject) (*[]ChatMessage, error)
GetMessage(channelId string, messageId string) (*ChatMessage, error)
UpdateChannelMessage(channelId string, messageId string, newMessage *MessageObject) (*ChatMessage, error)
DeleteChannelMessage(channelId string, messageId string) error
}
type channelEndpoints struct{}
func (e *channelEndpoints) Default() string {
return guildedApi + "/channels"
}
func (e *channelEndpoints) Get(channelId string) string {
return guildedApi + "/channels/" + channelId
}
func (e *channelEndpoints) Message(channelId string) string {
return guildedApi + "/channels/" + channelId + "/messages"
}
func (e *channelEndpoints) ChannelMessages(channelId string) string {
return guildedApi + "/channels/" + channelId + "/messages"
}
func (e *channelEndpoints) MessageID(channelId string, messageId string) string {
return guildedApi + "/channels/" + channelId + "/messages/" + messageId
}
type channelService struct {
client *Client
endpoints *channelEndpoints
}
var _ ChannelService = &channelService{}
// CreateChannel returns the newly created channel.
// Only server channels are supported at this time (coming soon™: DM Channels!)
func (service *channelService) CreateChannel(channelObject *NewChannelObject) (*ServerChannel, error) {
endpoint := service.endpoints.Default()
channelObject.ServerID = service.client.ServerID
resp, err := service.client.PostRequest(endpoint, &channelObject)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to create new channel. Error: \n%v", err.Error()))
}
var serverChannel ServerChannelCreated
err = json.Unmarshal(resp, &serverChannel)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to unmarshal ServerChannel response. Error: \n%v", err.Error()))
}
return &serverChannel.Channel, nil
}
// GetChannel returns a channel by channelId.
// Only server channels are supported at this time (coming soon™: DM Channels!)
func (service *channelService) GetChannel(channelId string) (*ServerChannel, error) {
endpoint := service.endpoints.Get(channelId)
var serverChannel ServerChannelResponse
err := service.client.GetRequestV2(endpoint, &serverChannel)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to get channel. Error: \n%v", err.Error()))
}
return &serverChannel.Channel, nil
}
// UpdateChannel returns the updated channel.
func (service *channelService) UpdateChannel(channelId string, channelObject *UpdateChannelObject) (*ServerChannel, error) {
endpoint := service.endpoints.Get(channelId)
var serverChannel ServerChannelResponse
err := service.client.PatchRequest(endpoint, &channelObject, &serverChannel)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to update channel. Error: \n%v", err.Error()))
}
return &serverChannel.Channel, nil
}
// DeleteChannel does not return anything
// Only server channels are supported at this time (coming soon™: DM Channels!)
func (cs *channelService) DeleteChannel(channelId string) error {
endpoint := cs.endpoints.Get(channelId)
_, err := cs.client.DeleteRequest(endpoint)
if err != nil {
return errors.New(fmt.Sprintf("Failed to delete channel. Error: \n%v", err.Error()))
}
return nil
}
func (service *channelService) SendMessage(channelId string, message *MessageObject) (*ChatMessage, error) {
endpoint := service.endpoints.Message(channelId)
resp, err := service.client.PostRequest(endpoint, &message)
if err != nil {
return nil, err
}
var msg MessageResponse
err = json.Unmarshal(resp, &msg)
if err != nil {
return nil, err
}
return &msg.Message, err
}
// TODO: only allow for content and embed updates
func (service *channelService) UpdateChannelMessage(channelId string, messageId string, newMessage *MessageObject) (*ChatMessage, error) {
endpoint := service.endpoints.MessageID(channelId, messageId)
resp, err := service.client.PutRequest(endpoint, &newMessage)
if err != nil {
return nil, err
}
var msg MessageResponse
err = json.Unmarshal(resp, &msg)
if err != nil {
return nil, err
}
return &msg.Message, err
}
// GetMessages TODO: add support for params
func (service *channelService) GetMessages(channelId string, getObject *GetMessagesObject) (*[]ChatMessage, error) {
endpoint := service.endpoints.ChannelMessages(channelId)
// create query params with getObject
params := url.Values{}
if getObject != nil {
if getObject.Before != "" {
params.Add("before", getObject.Before)
}
if getObject.After != "" {
params.Add("after", getObject.After)
}
if getObject.Limit != 0 {
params.Add("limit", strconv.Itoa(getObject.Limit))
}
if getObject.IncludePrivate {
params.Add("includePrivate", strconv.FormatBool(getObject.IncludePrivate))
}
}
endpoint = endpoint + "?" + params.Encode()
var msgs AllMessagesResponse
err := service.client.GetRequestV2(endpoint, &msgs)
if err != nil {
return nil, err
}
return &msgs.Messages, nil
}
// GetMessage Get a message from a channel
func (service *channelService) GetMessage(channelId string, messageId string) (*ChatMessage, error) {
endpoint := service.endpoints.MessageID(channelId, messageId)
resp, err := service.client.GetRequest(endpoint)
if err != nil {
return nil, err
}
var msg MessageResponse
err = json.Unmarshal(resp, &msg)
if err != nil {
return nil, err
}
return &msg.Message, nil
}
func (service *channelService) DeleteChannelMessage(channelId string, messageId string) error {
endpoint := service.endpoints.MessageID(channelId, messageId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return err
}
return nil
}
func (service *channelService) ArchiveChannel(channelId string) error {
endpoint := service.endpoints.Get(channelId) + "/archive"
err := service.client.PutRequestV2(endpoint, nil, nil)
if err != nil {
return errors.New(fmt.Sprintf("Failed to archive channel. Error: \n%v", err.Error()))
}
return nil
}
func (service *channelService) RestoreChannel(channelId string) error {
endpoint := service.endpoints.Get(channelId) + "/archive"
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return errors.New(fmt.Sprintf("Failed to restore channel. Error: \n%v", err.Error()))
}
return nil
}