forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.go
498 lines (382 loc) · 12.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package stream_chat //nolint: golint
import (
"errors"
"io"
"net/http"
"net/url"
"path"
"time"
)
type ChannelRead struct {
User *User `json:"user"`
LastRead time.Time `json:"last_read"`
}
type ChannelMember struct {
UserID string `json:"user_id,omitempty"`
User *User `json:"user,omitempty"`
IsModerator bool `json:"is_moderator,omitempty"`
Invited bool `json:"invited,omitempty"`
InviteAcceptedAt *time.Time `json:"invite_accepted_at,omitempty"`
InviteRejectedAt *time.Time `json:"invite_rejected_at,omitempty"`
Role string `json:"role,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
type Channel struct {
ID string `json:"id"`
Type string `json:"type"`
CID string `json:"cid"` // full id in format channel_type:channel_ID
Config ChannelConfig `json:"config"`
CreatedBy *User `json:"created_by"`
Frozen bool `json:"frozen"`
MemberCount int `json:"member_count"`
Members []*ChannelMember `json:"members"`
Messages []*Message `json:"messages"`
Read []*ChannelRead `json:"read"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LastMessageAt time.Time `json:"last_message_at"`
client *Client
}
type queryResponse struct {
Channel *Channel `json:"channel,omitempty"`
Messages []*Message `json:"messages,omitempty"`
Members []*ChannelMember `json:"members,omitempty"`
Read []*ChannelRead `json:"read,omitempty"`
}
func (q queryResponse) updateChannel(ch *Channel) {
if q.Channel != nil {
// save client pointer but update channel information
client := ch.client
*ch = *q.Channel
ch.client = client
}
if q.Members != nil {
ch.Members = q.Members
}
if q.Messages != nil {
ch.Messages = q.Messages
}
if q.Read != nil {
ch.Read = q.Read
}
}
// query makes request to channel api and updates channel internal state
func (ch *Channel) query(options, data map[string]interface{}) (err error) {
payload := map[string]interface{}{
"state": true,
}
for k, v := range options {
payload[k] = v
}
if data == nil {
data = map[string]interface{}{}
}
payload["data"] = data
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "query")
var resp queryResponse
err = ch.client.makeRequest(http.MethodPost, p, nil, payload, &resp)
if err != nil {
return err
}
resp.updateChannel(ch)
return nil
}
// Update edits the channel's custom properties
//
// options: the object to update the custom properties of this channel with
// message: optional update message
func (ch *Channel) Update(options map[string]interface{}, message *Message) error {
payload := map[string]interface{}{
"data": options,
}
if message != nil {
payload["message"] = message
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, payload, nil)
}
// Delete removes the channel. Messages are permanently removed.
func (ch *Channel) Delete() error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodDelete, p, nil, nil, nil)
}
// Truncate removes all messages from the channel
func (ch *Channel) Truncate() error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "truncate")
return ch.client.makeRequest(http.MethodPost, p, nil, nil, nil)
}
// AddMembers adds members with given user IDs to the channel
func (ch *Channel) AddMembers(userIDs []string, message *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"add_members": userIDs,
}
if message != nil {
data["message"] = message
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// RemoveMembers deletes members with given IDs from the channel
func (ch *Channel) RemoveMembers(userIDs []string, message *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"remove_members": userIDs,
}
if message != nil {
data["message"] = message
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
var resp queryResponse
err := ch.client.makeRequest(http.MethodPost, p, nil, data, &resp)
if err != nil {
return err
}
resp.updateChannel(ch)
return nil
}
// AddModerators adds moderators with given IDs to the channel
func (ch *Channel) AddModerators(userIDs ...string) error {
return ch.addModerators(userIDs, nil)
}
// AddModerators adds moderators with given IDs to the channel and produce system message
func (ch *Channel) AddModeratorsWithMessage(userIDs []string, msg *Message) error {
return ch.addModerators(userIDs, msg)
}
// AddModerators adds moderators with given IDs to the channel
func (ch *Channel) addModerators(userIDs []string, msg *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"add_moderators": userIDs,
}
if msg != nil {
data["message"] = msg
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// InviteMembers invites users with given IDs to the channel
func (ch *Channel) InviteMembers(userIDs ...string) error {
return ch.inviteMembers(userIDs, nil)
}
// InviteMembers invites users with given IDs to the channel and produce system message
func (ch *Channel) InviteMembersWithMessage(userIDs []string, msg *Message) error {
return ch.inviteMembers(userIDs, msg)
}
// InviteMembers invites users with given IDs to the channel
func (ch *Channel) inviteMembers(userIDs []string, msg *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"invites": userIDs,
}
if msg != nil {
data["message"] = msg
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// DemoteModerators moderators with given IDs from the channel
func (ch *Channel) DemoteModerators(userIDs ...string) error {
return ch.demoteModerators(userIDs, nil)
}
// DemoteModerators moderators with given IDs from the channel and produce system message
func (ch *Channel) DemoteModeratorsWithMessage(userIDs []string, msg *Message) error {
return ch.demoteModerators(userIDs, msg)
}
// DemoteModerators moderators with given IDs from the channel
func (ch *Channel) demoteModerators(userIDs []string, msg *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"demote_moderators": userIDs,
}
if msg != nil {
data["message"] = msg
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// MarkRead send the mark read event for user with given ID, only works if the `read_events` setting is enabled
// options: additional data, ie {"messageID": last_messageID}
func (ch *Channel) MarkRead(userID string, options map[string]interface{}) error {
switch {
case userID == "":
return errors.New("user ID must be not empty")
case options == nil:
options = map[string]interface{}{}
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "read")
options["user"] = map[string]interface{}{"id": userID}
return ch.client.makeRequest(http.MethodPost, p, nil, options, nil)
}
// BanUser bans target user ID from this channel
// userID: user who bans target
// options: additional ban options, ie {"timeout": 3600, "reason": "offensive language is not allowed here"}
func (ch *Channel) BanUser(targetID, userID string, options map[string]interface{}) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case userID == "":
return errors.New("user ID is empty")
case options == nil:
options = map[string]interface{}{}
}
options["type"] = ch.Type
options["id"] = ch.ID
return ch.client.BanUser(targetID, userID, options)
}
// UnBanUser removes the ban for target user ID on this channel
func (ch *Channel) UnBanUser(targetID string, options map[string]string) error {
switch {
case targetID == "":
return errors.New("target ID must be not empty")
case options == nil:
options = map[string]string{}
}
options["type"] = ch.Type
options["id"] = ch.ID
return ch.client.UnBanUser(targetID, options)
}
// Query fills channel info without state (messages, members, reads)
func (ch *Channel) Query(data map[string]interface{}) error {
options := map[string]interface{}{
"watch": false,
"state": false,
"presence": false,
}
return ch.query(options, data)
}
// Show makes channel visible for userID
func (ch *Channel) Show(userID string) error {
data := map[string]interface{}{
"user_id": userID,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "show")
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// Hide makes channel hidden for userID
func (ch *Channel) Hide(userID string) error {
return ch.hide(userID, false)
}
// HideWithHistoryClear clear marks channel as hidden and remove all messages for user
func (ch *Channel) HideWithHistoryClear(userID string) error {
return ch.hide(userID, true)
}
func (ch *Channel) hide(userID string, clearHistory bool) error {
data := map[string]interface{}{
"user_id": userID,
"clear_history": clearHistory,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "hide")
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// CreateChannel creates new channel of given type and id or returns already created one
func (c *Client) CreateChannel(chanType, chanID, userID string, data map[string]interface{}) (*Channel, error) {
_, membersPresent := data["members"]
switch {
case chanType == "":
return nil, errors.New("channel type is empty")
case chanID == "" && !membersPresent:
return nil, errors.New("either channel ID or members must be provided")
case userID == "":
return nil, errors.New("user ID is empty")
}
ch := &Channel{
Type: chanType,
ID: chanID,
client: c,
CreatedBy: &User{ID: userID},
}
options := map[string]interface{}{
"watch": false,
"state": true,
"presence": false,
}
if data == nil {
data = make(map[string]interface{}, 1)
}
data["created_by"] = map[string]string{"id": userID}
err := ch.query(options, data)
return ch, err
}
type SendFileRequest struct {
Reader io.Reader `json:"-"`
// name of the file would be stored
FileName string
// User object; required
User *User
// file content type, required for SendImage
ContentType string
}
// SendFile sends file to the channel. Returns file url or error
func (ch *Channel) SendFile(request SendFileRequest) (string, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "file")
return ch.client.sendFile(p, request)
}
// SendFile sends image to the channel. Returns file url or error
func (ch *Channel) SendImage(request SendFileRequest) (string, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "image")
return ch.client.sendFile(p, request)
}
// DeleteFile removes uploaded file
func (ch *Channel) DeleteFile(location string) error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "file")
var params = url.Values{}
params.Set("url", location)
return ch.client.makeRequest(http.MethodDelete, p, params, nil, nil)
}
// DeleteImage removes uploaded image
func (ch *Channel) DeleteImage(location string) error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "image")
var params = url.Values{}
params.Set("url", location)
return ch.client.makeRequest(http.MethodDelete, p, params, nil, nil)
}
func (ch *Channel) AcceptInvite(userID string, message *Message) error {
if userID == "" {
return errors.New("user ID must be not empty")
}
data := map[string]interface{}{
"accept_invite": true,
"user_id": userID,
}
if message != nil {
data["message"] = message
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
func (ch *Channel) RejectInvite(userID string, message *Message) error {
if userID == "" {
return errors.New("user ID must be not empty")
}
data := map[string]interface{}{
"reject_invite": true,
"user_id": userID,
}
if message != nil {
data["message"] = message
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
//nolint: godox
// todo: cleanup this
func (ch *Channel) refresh() error {
options := map[string]interface{}{
"watch": false,
"state": true,
"presence": false,
}
return ch.query(options, nil)
}