-
Notifications
You must be signed in to change notification settings - Fork 31
/
channel.go
857 lines (685 loc) · 24.5 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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package stream_chat
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
)
type ChannelRead struct {
User *User `json:"user"`
LastRead time.Time `json:"last_read"`
UnreadMessages int `json:"unread_messages"`
}
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
Team string `json:"team"`
Config ChannelConfig `json:"config"`
CreatedBy *User `json:"created_by"`
Disabled bool `json:"disabled"`
Frozen bool `json:"frozen"`
MemberCount int `json:"member_count"`
Members []*ChannelMember `json:"members"`
Messages []*Message `json:"messages"`
PinnedMessages []*Message `json:"pinned_messages"`
PendingMessages []*Message `json:"pending_messages"`
Read []*ChannelRead `json:"read"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LastMessageAt time.Time `json:"last_message_at"`
TruncatedBy *User `json:"truncated_by"`
TruncatedAt *time.Time `json:"truncated_at"`
ExtraData map[string]interface{} `json:"-"`
client *Client
}
func (ch Channel) cid() string {
if ch.CID != "" {
return ch.CID
}
return ch.Type + ":" + ch.ID
}
type PartialUpdate struct {
Set map[string]interface{} `json:"set"`
Unset []string `json:"unset"`
}
type channelForJSON Channel
// UnmarshalJSON implements json.Unmarshaler.
func (ch *Channel) UnmarshalJSON(data []byte) error {
var ch2 channelForJSON
if err := json.Unmarshal(data, &ch2); err != nil {
return err
}
*ch = Channel(ch2)
if err := json.Unmarshal(data, &ch.ExtraData); err != nil {
return err
}
removeFromMap(ch.ExtraData, *ch)
return nil
}
// MarshalJSON implements json.Marshaler.
func (ch Channel) MarshalJSON() ([]byte, error) {
return addToMapAndMarshal(ch.ExtraData, channelForJSON(ch))
}
type QueryResponse struct {
Channel *Channel `json:"channel,omitempty"`
Messages []*Message `json:"messages,omitempty"`
PinnedMessages []*Message `json:"pinned_messages,omitempty"`
Members []*ChannelMember `json:"members,omitempty"`
Read []*ChannelRead `json:"read,omitempty"`
Response
}
type ChannelRequest struct {
CreatedBy *User `json:"created_by,omitempty"`
Team string `json:"team,omitempty"`
AutoTranslationEnabled bool `json:"auto_translation_enabled,omitempty"`
AutoTranslationLanguage string `json:"auto_translation_language,omitempty"`
Frozen *bool `json:"frozen,omitempty"`
Disabled *bool `json:"disabled,omitempty"`
Members []string `json:"members,omitempty"`
Invites []string `json:"invites,omitempty"`
ExtraData map[string]interface{} `json:"-"`
}
type channelRequestForJSON ChannelRequest
// UnmarshalJSON implements json.Unmarshaler.
func (c *ChannelRequest) UnmarshalJSON(data []byte) error {
var ch2 channelRequestForJSON
if err := json.Unmarshal(data, &ch2); err != nil {
return err
}
*c = ChannelRequest(ch2)
if err := json.Unmarshal(data, &c.ExtraData); err != nil {
return err
}
removeFromMap(c.ExtraData, *c)
return nil
}
// MarshalJSON implements json.Marshaler.
func (c ChannelRequest) MarshalJSON() ([]byte, error) {
return addToMapAndMarshal(c.ExtraData, channelRequestForJSON(c))
}
type PaginationParamsRequest struct {
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
IDGTE string `json:"id_gte,omitempty"`
IDGT string `json:"id_gt,omitempty"`
IDLTE string `json:"id_lte,omitempty"`
IDLT string `json:"id_lt,omitempty"`
}
type MessagePaginationParamsRequest struct {
PaginationParamsRequest
CreatedAtAfterEq *time.Time `json:"created_at_after_or_equal,omitempty"`
CreatedAtAfter *time.Time `json:"created_at_after,omitempty"`
CreatedAtBeforeEq *time.Time `json:"created_at_before_or_equal,omitempty"`
CreatedAtBefore *time.Time `json:"created_at_before,omitempty"`
IDAround string `json:"id_around,omitempty"`
CreatedAtAround *time.Time `json:"created_at_around,omitempty"`
}
type QueryRequest struct {
Data *ChannelRequest `json:"data,omitempty"`
Watch bool `json:"watch,omitempty"`
State bool `json:"state,omitempty"`
Presence bool `json:"presence,omitempty"`
Messages *MessagePaginationParamsRequest `json:"messages,omitempty"`
Members *PaginationParamsRequest `json:"members,omitempty"`
Watchers *PaginationParamsRequest `json:"watchers,omitempty"`
HideForCreator bool `json:"hide_for_creator,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
}
if q.PinnedMessages != nil {
ch.PinnedMessages = q.PinnedMessages
}
}
// Query makes request to channel api and updates channel internal state.
func (ch *Channel) Query(ctx context.Context, q *QueryRequest) (*QueryResponse, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "query")
var resp QueryResponse
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, &q, &resp)
if err != nil {
return nil, err
}
resp.updateChannel(ch)
return &resp, nil
}
// Update edits the channel's custom properties.
//
// properties: the object to update the custom properties of this channel with
// message: optional update message
func (ch *Channel) Update(ctx context.Context, properties map[string]interface{}, message *Message) (*Response, error) {
payload := map[string]interface{}{
"data": properties,
}
if message != nil {
payload["message"] = message
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, payload, &resp)
return &resp, err
}
// PartialUpdate set and unset specific fields when it is necessary to retain additional custom data fields on the object. AKA a patch style update.
func (ch *Channel) PartialUpdate(ctx context.Context, update PartialUpdate) (*Response, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPatch, p, nil, update, &resp)
return &resp, err
}
// Delete removes the channel. Messages are permanently removed.
func (ch *Channel) Delete(ctx context.Context) (*Response, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodDelete, p, nil, nil, &resp)
return &resp, err
}
type truncateOptions struct {
HardDelete bool `json:"hard_delete,omitempty"`
SkipPush bool `json:"skip_push,omitempty"`
TruncatedAt *time.Time `json:"truncated_at,omitempty"`
Message *Message `json:"message,omitempty"`
UserID string `json:"user_id,omitempty"`
User *User `json:"user,omitempty"`
}
type TruncateOption func(*truncateOptions)
func TruncateWithHardDelete() func(*truncateOptions) {
return func(o *truncateOptions) {
o.HardDelete = true
}
}
func TruncateWithSkipPush() func(*truncateOptions) {
return func(o *truncateOptions) {
o.SkipPush = true
}
}
func TruncateWithMessage(message *Message) func(*truncateOptions) {
return func(o *truncateOptions) {
o.Message = message
}
}
func TruncateWithUserID(userID string) func(*truncateOptions) {
return func(o *truncateOptions) {
o.UserID = userID
}
}
func TruncateWithUser(user *User) func(*truncateOptions) {
return func(o *truncateOptions) {
o.User = user
}
}
func TruncateWithTruncatedAt(truncatedAt *time.Time) func(*truncateOptions) {
return func(o *truncateOptions) {
o.TruncatedAt = truncatedAt
}
}
type TruncateResponse struct {
Response
Channel *Channel `json:"channel"`
Message *Message `json:"message"`
}
// Truncate removes all messages from the channel.
// You can pass in options such as hard_delete, skip_push
// or a custom message.
func (ch *Channel) Truncate(ctx context.Context, options ...TruncateOption) (*Response, error) {
option := &truncateOptions{}
for _, fn := range options {
fn(option)
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "truncate")
var resp TruncateResponse
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, option, &resp)
return &resp.Response, err
}
type GetMessagesResponse struct {
Messages []*Message `json:"messages"`
Response
}
// GetMessages returns messages for multiple message ids.
func (ch *Channel) GetMessages(ctx context.Context, messageIDs []string) (*GetMessagesResponse, error) {
params := url.Values{}
params.Set("ids", strings.Join(messageIDs, ","))
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "messages")
var resp GetMessagesResponse
err := ch.client.makeRequest(ctx, http.MethodGet, p, params, nil, &resp)
return &resp, err
}
type addMembersOptions struct {
MemberIDs []string `json:"add_members"`
RolesAssignement []*RoleAssignment `json:"assign_roles"`
HideHistory bool `json:"hide_history"`
Message *Message `json:"message,omitempty"`
}
type AddMembersOptions func(*addMembersOptions)
func AddMembersWithMessage(message *Message) func(*addMembersOptions) {
return func(opt *addMembersOptions) {
opt.Message = message
}
}
func AddMembersWithHideHistory() func(*addMembersOptions) {
return func(opt *addMembersOptions) {
opt.HideHistory = true
}
}
func AddMembersWithRolesAssignment(assignements []*RoleAssignment) func(*addMembersOptions) {
return func(opt *addMembersOptions) {
opt.RolesAssignement = assignements
}
}
// AddMembers adds members with given user IDs to the channel.
func (ch *Channel) AddMembers(ctx context.Context, userIDs []string, options ...AddMembersOptions) (*Response, error) {
if len(userIDs) == 0 {
return nil, errors.New("user IDs are empty")
}
opts := &addMembersOptions{
MemberIDs: userIDs,
}
for _, fn := range options {
fn(opts)
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, opts, &resp)
return &resp, err
}
// RemoveMembers deletes members with given IDs from the channel.
func (ch *Channel) RemoveMembers(ctx context.Context, userIDs []string, message *Message) (*Response, error) {
if len(userIDs) == 0 {
return nil, 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(ctx, http.MethodPost, p, nil, data, &resp)
if err != nil {
return nil, err
}
resp.updateChannel(ch)
return &resp.Response, nil
}
type RoleAssignment struct {
// UserID is the ID of the user to assign the role to.
UserID string `json:"user_id"`
// ChannelRole is the role to assign to the user.
ChannelRole string `json:"channel_role"`
}
// AssignRoles assigns roles to members with given IDs.
func (ch *Channel) AssignRole(ctx context.Context, assignments []*RoleAssignment, msg *Message) (*Response, error) {
if len(assignments) == 0 {
return nil, errors.New("assignments are empty")
}
ids := make([]string, 0, len(assignments))
for _, a := range assignments {
ids = append(ids, a.UserID)
}
data := map[string]interface{}{"assign_roles": assignments, "add_members": ids}
if msg != nil {
data["message"] = msg
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
type QueryMembersResponse struct {
Members []*ChannelMember `json:"members"`
Response
}
// QueryMembers queries members of a channel.
func (ch *Channel) QueryMembers(ctx context.Context, q *QueryOption, sorters ...*SortOption) (*QueryMembersResponse, error) {
qp := map[string]interface{}{
"id": ch.ID,
"type": ch.Type,
"filter_conditions": q.Filter,
"limit": q.Limit,
"offset": q.Offset,
"sort": sorters,
}
if ch.ID == "" && len(ch.Members) > 0 {
members := make([]*ChannelMember, 0, len(ch.Members))
for _, m := range ch.Members {
if m.User != nil {
members = append(members, &ChannelMember{UserID: m.User.ID})
} else {
members = append(members, &ChannelMember{UserID: m.UserID})
}
}
qp["members"] = members
}
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
values := url.Values{}
values.Set("payload", string(data))
var resp QueryMembersResponse
err = ch.client.makeRequest(ctx, http.MethodGet, "members", values, nil, &resp)
return &resp, err
}
// AddModerators adds moderators with given IDs to the channel.
func (ch *Channel) AddModerators(ctx context.Context, userIDs ...string) (*Response, error) {
return ch.addModerators(ctx, userIDs, nil)
}
// AddModerators adds moderators with given IDs to the channel and produce system message.
func (ch *Channel) AddModeratorsWithMessage(ctx context.Context, userIDs []string, msg *Message) (*Response, error) {
return ch.addModerators(ctx, userIDs, msg)
}
// AddModerators adds moderators with given IDs to the channel.
func (ch *Channel) addModerators(ctx context.Context, userIDs []string, msg *Message) (*Response, error) {
if len(userIDs) == 0 {
return nil, 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))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
// InviteMembers invites users with given IDs to the channel.
func (ch *Channel) InviteMembers(ctx context.Context, userIDs ...string) (*Response, error) {
return ch.inviteMembers(ctx, userIDs, nil)
}
// InviteMembers invites users with given IDs to the channel and produce system message.
func (ch *Channel) InviteMembersWithMessage(ctx context.Context, userIDs []string, msg *Message) (*Response, error) {
return ch.inviteMembers(ctx, userIDs, msg)
}
// InviteMembers invites users with given IDs to the channel.
func (ch *Channel) inviteMembers(ctx context.Context, userIDs []string, msg *Message) (*Response, error) {
if len(userIDs) == 0 {
return nil, 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))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
// DemoteModerators moderators with given IDs from the channel.
func (ch *Channel) DemoteModerators(ctx context.Context, userIDs ...string) (*Response, error) {
return ch.demoteModerators(ctx, userIDs, nil)
}
// DemoteModerators moderators with given IDs from the channel and produce system message.
func (ch *Channel) DemoteModeratorsWithMessage(ctx context.Context, userIDs []string, msg *Message) (*Response, error) {
return ch.demoteModerators(ctx, userIDs, msg)
}
// DemoteModerators moderators with given IDs from the channel.
func (ch *Channel) demoteModerators(ctx context.Context, userIDs []string, msg *Message) (*Response, error) {
if len(userIDs) == 0 {
return nil, 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))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
type markReadOption struct {
MessageID string `json:"message_id"`
UserID string `json:"user_id"`
}
type MarkReadOption func(*markReadOption)
func MarkReadUntilMessage(id string) func(*markReadOption) {
return func(opt *markReadOption) {
opt.MessageID = id
}
}
// MarkRead sends the mark read event for user with given ID,
// only works if the `read_events` setting is enabled.
func (ch *Channel) MarkRead(ctx context.Context, userID string, options ...MarkReadOption) (*Response, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "read")
opts := &markReadOption{
UserID: userID,
}
for _, fn := range options {
fn(opts)
}
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, opts, &resp)
return &resp, err
}
// RefreshState makes request to channel api and updates channel internal state.
func (ch *Channel) RefreshState(ctx context.Context) (*QueryResponse, error) {
q := &QueryRequest{State: true}
resp, err := ch.Query(ctx, q)
if err != nil {
return nil, err
}
resp.updateChannel(ch)
return resp, nil
}
// Show makes channel visible for userID.
func (ch *Channel) Show(ctx context.Context, userID string) (*Response, error) {
data := map[string]interface{}{
"user_id": userID,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "show")
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
// Hide makes channel hidden for userID.
func (ch *Channel) Hide(ctx context.Context, userID string) (*Response, error) {
return ch.hide(ctx, userID, false)
}
// HideWithHistoryClear clear marks channel as hidden and remove all messages for user.
func (ch *Channel) HideWithHistoryClear(ctx context.Context, userID string) (*Response, error) {
return ch.hide(ctx, userID, true)
}
func (ch *Channel) hide(ctx context.Context, userID string, clearHistory bool) (*Response, error) {
data := map[string]interface{}{
"user_id": userID,
"clear_history": clearHistory,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "hide")
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
type CreateChannelResponse struct {
Channel *Channel
*Response
}
type CreateChannelOptions struct {
HideForCreator bool
}
type CreateChannelOptionFunc func(*CreateChannelOptions)
func HideForCreator(hideForCreator bool) CreateChannelOptionFunc {
return func(options *CreateChannelOptions) {
options.HideForCreator = hideForCreator
}
}
// CreateChannel creates new channel of given type and id or returns already created one.
func (c *Client) CreateChannel(ctx context.Context, chanType, chanID, userID string, data *ChannelRequest, opts ...CreateChannelOptionFunc) (*CreateChannelResponse, error) {
switch {
case chanType == "":
return nil, errors.New("channel type is empty")
case chanID == "" && (data == nil || len(data.Members) == 0):
return nil, errors.New("either channel ID or members must be provided")
case userID == "":
return nil, errors.New("user ID is empty")
}
options := CreateChannelOptions{}
for _, opt := range opts {
opt(&options)
}
ch := &Channel{
Type: chanType,
ID: chanID,
client: c,
CreatedBy: &User{ID: userID},
}
if data == nil {
data = &ChannelRequest{CreatedBy: &User{ID: userID}}
} else {
data.CreatedBy = &User{ID: userID}
}
q := &QueryRequest{
Watch: false,
State: true,
Presence: false,
Data: data,
HideForCreator: options.HideForCreator,
}
resp, err := ch.Query(ctx, q)
if err != nil {
return nil, err
}
return &CreateChannelResponse{Channel: ch, Response: &resp.Response}, nil
}
// CreateChannelWithMembers creates new channel of given type and id or returns already created one.
func (c *Client) CreateChannelWithMembers(ctx context.Context, chanType, chanID, userID string, memberIDs ...string) (*CreateChannelResponse, error) {
return c.CreateChannel(ctx, chanType, chanID, userID, &ChannelRequest{Members: memberIDs})
}
type SendFileRequest struct {
Reader io.Reader `json:"-"`
// name of the file would be stored
FileName string
// User object; required
User *User
}
// SendFile sends file to the channel. Returns file url or error.
func (ch *Channel) SendFile(ctx context.Context, request SendFileRequest) (*SendFileResponse, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "file")
return ch.client.sendFile(ctx, p, request)
}
// SendFile sends image to the channel. Returns file url or error.
func (ch *Channel) SendImage(ctx context.Context, request SendFileRequest) (*SendFileResponse, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "image")
return ch.client.sendFile(ctx, p, request)
}
// DeleteFile removes uploaded file.
func (ch *Channel) DeleteFile(ctx context.Context, location string) (*Response, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "file")
params := url.Values{}
params.Set("url", location)
var resp Response
err := ch.client.makeRequest(ctx, http.MethodDelete, p, params, nil, &resp)
return &resp, err
}
// DeleteImage removes uploaded image.
func (ch *Channel) DeleteImage(ctx context.Context, location string) (*Response, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "image")
params := url.Values{}
params.Set("url", location)
var resp Response
err := ch.client.makeRequest(ctx, http.MethodDelete, p, params, nil, &resp)
return &resp, err
}
// AcceptInvite accepts an invite to the channel.
func (ch *Channel) AcceptInvite(ctx context.Context, userID string, message *Message) (*Response, error) {
if userID == "" {
return nil, 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))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
// RejectInvite rejects an invite to the channel.
func (ch *Channel) RejectInvite(ctx context.Context, userID string, message *Message) (*Response, error) {
if userID == "" {
return nil, 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))
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, &resp)
return &resp, err
}
type ChannelMuteResponse struct {
ChannelMute ChannelMute `json:"channel_mute"`
Response
}
// Mute mutes the channel. The user will stop receiving messages from the channel.
func (ch *Channel) Mute(ctx context.Context, userID string, expiration *time.Duration) (*ChannelMuteResponse, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
}
data := map[string]interface{}{
"user_id": userID,
"channel_cid": ch.cid(),
}
if expiration != nil {
data["expiration"] = int(expiration.Milliseconds())
}
mute := &ChannelMuteResponse{}
err := ch.client.makeRequest(ctx, http.MethodPost, "moderation/mute/channel", nil, data, mute)
return mute, err
}
// Unmute removes a mute from a channel so the user will receive messages again.
func (ch *Channel) Unmute(ctx context.Context, userID string) (*Response, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
}
data := map[string]interface{}{
"user_id": userID,
"channel_cid": ch.cid(),
}
var resp Response
err := ch.client.makeRequest(ctx, http.MethodPost, "moderation/unmute/channel", nil, data, &resp)
return &resp, err
}