-
Notifications
You must be signed in to change notification settings - Fork 5
/
groupchannels.go
378 lines (282 loc) · 10.6 KB
/
groupchannels.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package sendbirdclient
import (
"errors"
"net/url"
"strings"
"github.com/blazingorb/sendbirdclient/templates"
)
type GroupChannel struct {
channel
IsDistinct bool `json:"is_distinct"`
MemberCount int `json:"member_count"`
Members []User `json:"members"`
ReadReceipt map[string]int64 `json:"read_receipt"`
UnreadMessageCount int `json:"unread_message_count"`
LastMessage LastMessage `json:"last_message"`
}
type LastMessage struct {
CreatedAt int64 `json:"created_at"`
User User `json:"user"`
}
type groupChannelsTemplateData struct {
ChannelURL string
UserID string
}
func (c *Client) CreateAGroupChannelWithURL(r *CreateAGroupChannelWithURLRequest) (GroupChannel, error) {
result := GroupChannel{}
if err := c.postAndReturnJSON(c.PrepareUrl(SendbirdURLGroupChannels), r, &result); err != nil {
return GroupChannel{}, err
}
return result, nil
}
type CreateAGroupChannelWithURLRequest struct {
Name string `json:"name,omitempty"`
CoverURL string `json:"cover_url,omitempty"`
CustomType string `json:"custom_type,omitempty"`
Data string `json:"data,omitempty"`
UserIDs []string `json:"user_ids,omitempty"`
IsDistinct bool `json:"is_distinct,omitempty"`
}
func (c *Client) CreateAGroupChannelWithFile(r *CreateAGroupChannelWithFileRequest) (GroupChannel, error) {
return GroupChannel{}, errors.New(SendbirdClientErrorNotImplemented)
}
type CreateAGroupChannelWithFileRequest struct {
Name string `json:"name,omitempty"`
CoverFile []byte `json:"cover_file,omitempty"`
CustomType string `json:"custom_type,omitempty"`
Data string `json:"data,omitempty"`
UserIDs []string `json:"user_ids,omitempty"`
IsDistinct bool `json:"is_distinct,omitempty"`
}
func (c *Client) ListGroupChannels(r *ListGroupChannelsRequest) (ListGroupChannelsResponse, error) {
raw := r.params().Encode()
//#HACK special case handling for sendbird API
raw = strings.Replace(raw, "%2C", ",", -1)
result := ListGroupChannelsResponse{}
err := c.getAndReturnJSON(c.PrepareUrl(SendbirdURLGroupChannels), raw, &result)
if err != nil {
return ListGroupChannelsResponse{}, err
}
return result, nil
}
func (r *ListGroupChannelsRequest) params() url.Values {
q := make(url.Values)
if r.Token != "" {
q.Set("token", r.Token)
}
if r.Limit >= ListLimitLowerBound && r.Limit <= ListLimitUpperBound {
q.Set("limit", string(r.Limit))
}
if r.ShowMember {
q.Set("show_member", "true")
}
if r.ShowReadReceipt {
q.Set("show_read_receipt", "true")
}
switch r.DistinctMode {
case "distinct":
q.Set("distinct_mode", "distinct")
case "nondistinct":
q.Set("distinct_mode", "nondistinct")
case "all":
q.Set("distinct_mode", "all")
default:
q.Set("distinct_mode", "all")
}
if r.MembersExactlyIn != nil && len(r.MembersExactlyIn) > 0 {
q.Set("members_exactly_in", strings.Join(r.MembersExactlyIn, ","))
}
if r.MembersNicknameContains != nil && len(r.MembersIncludeIn) > 0 {
q.Set("members_nickname_contains", strings.Join(r.MembersNicknameContains, ","))
}
if r.MembersIncludeIn != nil && len(r.MembersIncludeIn) > 0 {
q.Set("members_include_in", strings.Join(r.MembersIncludeIn, ","))
}
if r.QueryType != "" {
q.Set("query_type", r.QueryType)
}
if r.CustomType != "" {
q.Set("custom_type", r.CustomType)
}
if r.ChannelURLs != nil && len(r.ChannelURLs) > 0 {
q.Set("channel_urls", strings.Join(r.ChannelURLs, ","))
}
if r.CreatedAfter > 0 {
q.Set("created_after", string(r.CreatedAfter))
}
if r.CreatedBefore > 0 {
q.Set("created_before", string(r.CreatedBefore))
}
return q
}
type ListGroupChannelsRequest struct {
Token string `json:"token,omitempty"`
Limit int `json:"limit,omitempty"`
ShowMember bool `json:"show_member,omitempty"`
ShowReadReceipt bool `json:"show_read_receipt,omitempty"`
DistinctMode string `json:"distinct_mode,omitempty"`
MembersExactlyIn []string `json:"members_exactly_in,omitempty"`
MembersIncludeIn []string `json:"members_include_in,omitempty"`
MembersNicknameContains []string `json:"members_nickname_contains,omitempty"`
QueryType string `json:"query_type,omitempty"`
CustomType string `json:"custom_type,omitempty"`
ChannelURLs []string `json:"channel_urls,omitempty"`
CreatedAfter int64 `json:"created_after,omitempty"`
CreatedBefore int64 `json:"created_before,omitempty"`
}
type ListGroupChannelsResponse struct {
Channels []GroupChannel `json:"channels"`
Next string `json:"next"`
}
func (c *Client) UpdateAGroupChannel(channelURL string, r *UpdateAGroupChannelRequest) (GroupChannel, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{ChannelURL: url.PathEscape(channelURL)}, templates.SendbirdURLGroupChannelsWithChannelURL)
if err != nil {
return GroupChannel{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := GroupChannel{}
if err := c.putAndReturnJSON(parsedURL, r, &result); err != nil {
return GroupChannel{}, err
}
return result, nil
}
type UpdateAGroupChannelRequest struct {
Name string `json:"name,omitempty"`
CoverURL string `json:"cover_url,omitempty"`
CustomType string `json:"custom_type,omitempty"`
Data string `json:"data,omitempty"`
IsDistinct bool `json:"is_distinct,omitempty"`
}
func (c *Client) ViewAGroupChannel(channelURL string, r *ViewAGroupChannelRequest) (GroupChannel, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{ChannelURL: url.PathEscape(channelURL)}, templates.SendbirdURLGroupChannelsWithChannelURL)
if err != nil {
return GroupChannel{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := GroupChannel{}
raw := r.params().Encode()
if err := c.getAndReturnJSON(parsedURL, raw, &result); err != nil {
return GroupChannel{}, err
}
return result, nil
}
func (r *ViewAGroupChannelRequest) params() url.Values {
q := make(url.Values)
if r.ShowReadReceipt {
q.Set("show_read_receipt", "true")
}
if r.ShowMember {
q.Set("show_member", "true")
}
return q
}
type ViewAGroupChannelRequest struct {
ShowReadReceipt bool `json:"show_read_receipt,omitempty"`
ShowMember bool `json:"show_member,omitempty"`
}
func (c *Client) DeleteAGroupChannel(channelURL string) (sendbirdErrorResponse, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{ChannelURL: url.PathEscape(channelURL)}, templates.SendbirdURLGroupChannelsWithChannelURL)
if err != nil {
return sendbirdErrorResponse{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := sendbirdErrorResponse{}
if err := c.deleteAndReturnJSON(parsedURL, "", &result); err != nil {
return sendbirdErrorResponse{}, err
}
return result, nil
}
func (c *Client) ListMembersInGroupChannel(channelURL string, r *ListMembersInGroupChannelRequest) (ListMembersInGroupChannelResponse, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{ChannelURL: url.PathEscape(channelURL)}, templates.SendbirdURLGroupChannelsMembersWithChannelURL)
if err != nil {
return ListMembersInGroupChannelResponse{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := ListMembersInGroupChannelResponse{}
raw := r.params().Encode()
if err := c.getAndReturnJSON(parsedURL, raw, &result); err != nil {
return ListMembersInGroupChannelResponse{}, err
}
return result, nil
}
func (r *ListMembersInGroupChannelRequest) params() url.Values {
q := make(url.Values)
if r.Token != "" {
q.Set("token", r.Token)
}
if r.Limit >= 1 && r.Limit <= 100 {
q.Set("limit", string(r.Limit))
}
return q
}
type ListMembersInGroupChannelRequest struct {
Token string `json:"token,omitempty"`
Limit int `json:"limit,omitempty"`
}
type ListMembersInGroupChannelResponse struct {
Members []User `json:"members"`
Next string `json:"next"`
}
func (c *Client) CheckIfMemberInGroupChannel(channelURL string, userID string) (CheckIfMemberInGroupChannelResponse, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{
ChannelURL: url.PathEscape(channelURL),
UserID: url.PathEscape(userID),
}, templates.SendbirdURLGroupChannelsMembersWithChannelURLAndUserID)
if err != nil {
return CheckIfMemberInGroupChannelResponse{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := CheckIfMemberInGroupChannelResponse{}
if err := c.getAndReturnJSON(parsedURL, "", &result); err != nil {
return CheckIfMemberInGroupChannelResponse{}, err
}
return result, nil
}
type CheckIfMemberInGroupChannelResponse struct {
IsMember bool `json:"is_member"`
}
func (c *Client) InviteMembersToGroupChannel(channelURL string, r *InviteMembersToGroupChannelRequest) (GroupChannel, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{ChannelURL: url.PathEscape(channelURL)}, templates.SendbirdURLGroupChannelsInviteWithChannelURL)
if err != nil {
return GroupChannel{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := GroupChannel{}
if err := c.postAndReturnJSON(parsedURL, r, &result); err != nil {
return GroupChannel{}, err
}
return result, nil
}
type InviteMembersToGroupChannelRequest struct {
UserIDs []string `json:"user_ids"`
}
func (c *Client) HideFromAGroupChannel(channelURL string, r *HideFromAGroupChannelRequest) (sendbirdErrorResponse, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{ChannelURL: url.PathEscape(channelURL)}, templates.SendbirdURLGroupChannelsHideWithChannelURL)
if err != nil {
return sendbirdErrorResponse{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := sendbirdErrorResponse{}
if err := c.putAndReturnJSON(parsedURL, r, &result); err != nil {
return sendbirdErrorResponse{}, err
}
return result, nil
}
type HideFromAGroupChannelRequest struct {
UserID string `json:"user_id"`
}
func (c *Client) LeaveFromAGroupChannel(channelURL string, r *LeaveFromAGroupChannelRequest) (sendbirdErrorResponse, error) {
pathString, err := templates.GetGroupChannelTemplate(groupChannelsTemplateData{ChannelURL: url.PathEscape(channelURL)}, templates.SendbirdURLGroupChannelsLeaveWithChannelURL)
if err != nil {
return sendbirdErrorResponse{}, err
}
parsedURL := c.PrepareUrl(pathString)
result := sendbirdErrorResponse{}
if err := c.putAndReturnJSON(parsedURL, r, &result); err != nil {
return sendbirdErrorResponse{}, err
}
return result, nil
}
type LeaveFromAGroupChannelRequest struct {
UserIDs []string `json:"user_ids"`
}