-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathchannel.go
More file actions
717 lines (612 loc) · 21.5 KB
/
channel.go
File metadata and controls
717 lines (612 loc) · 21.5 KB
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
package channel
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
stream "github.com/GetStream/stream-chat-go/v5"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/GetStream/stream-cli/pkg/config"
"github.com/GetStream/stream-cli/pkg/utils"
)
func NewCmds() []*cobra.Command {
return []*cobra.Command{
getCmd(),
createCmd(),
deleteCmd(),
updateCmd(),
updatePartialCmd(),
listCmd(),
addMembersCmd(),
removeMemberCmd(),
promoteModeratorCmd(),
demoteModeratorCmd(),
assignRoleCmd(),
hideCmd(),
showCmd(),
listMembersCmd(),
}
}
func getCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-channel --type [channel-type] --id [channel-id]",
Short: "Return a channel",
Example: heredoc.Doc(`
# Returns 'redteam' channel of 'messaging' channel type as JSON
$ stream-cli chat get-channel --type messaging --id redteam
# Returns 'blueteam' channel of 'messaging' channel type as a browsable tree
$ stream-cli chat get-channel --type messaging --id blueteam --output-format tree
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chanType, _ := cmd.Flags().GetString("type")
chanID, _ := cmd.Flags().GetString("id")
r, err := c.Channel(chanType, chanID).Query(cmd.Context(), &stream.QueryRequest{})
if err != nil {
return err
}
return utils.PrintObject(cmd, r.Channel)
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func createCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-channel --type [channel-type] --id [channel-id] --user [user-id] --properties [raw-json]",
Short: "Create a channel",
Long: heredoc.Doc(`
This command allows you to create a new channel. If it
exists already an error will be thrown.
`),
Example: heredoc.Doc(`
# Create a channel with id 'redteam' of type 'messaging' by 'joe'
$ stream-cli chat create-channel --type messaging --id redteam --user joe
# Create a channel with id 'blueteam' of type 'messaging' by 'joe' with extra data
$ stream-cli chat create-channel --type messaging --id blueteam --user joe --properties "{\"age\":\"28\"}"
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chanType, _ := cmd.Flags().GetString("type")
chanID, _ := cmd.Flags().GetString("id")
user, _ := cmd.Flags().GetString("user")
rawProps, _ := cmd.Flags().GetString("properties")
var props stream.ChannelRequest
if rawProps != "" {
err := json.Unmarshal([]byte(rawProps), &props)
if err != nil {
return err
}
}
r, err := c.CreateChannel(cmd.Context(), chanType, chanID, user, &props)
if err != nil {
return err
}
if time.Now().UTC().Unix()-r.Channel.CreatedAt.Unix() > 3 {
return errors.New("channel already exists")
}
cmd.Printf("Successfully created channel [%s]\n", r.Channel.CID)
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.StringP("user", "u", "", "[required] User id who will be considered as the creator of the channel")
fl.StringP("properties", "p", "", "[optional] JSON string of channel properties")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
_ = cmd.MarkFlagRequired("user")
return cmd
}
func deleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-channel --type [channel-type] --id [channel-id]",
Short: "Delete a channel",
Long: heredoc.Doc(`
This command allows you to delete a channel. This operation is asynchronous
in the backend so a task id is returned. You need to use the watch
commnand to poll the results.
`),
Example: heredoc.Doc(`
# Delete a channel with id 'redteam' of type 'messaging'
$ stream-cli chat delete-channel --type messaging --id redteam
> Successfully initiated channel deletion. Task id: 66bbcdcd-b133-43ce-ab63-557c14d2a168
# Wait for the task to complete
$ stream-cli chat watch 66bbcdcd-b133-43ce-ab63-557c14d2a168
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chanType, _ := cmd.Flags().GetString("type")
chanID, _ := cmd.Flags().GetString("id")
hard, _ := cmd.Flags().GetBool("hard")
cids := []string{chanType + ":" + chanID}
resp, err := c.DeleteChannels(cmd.Context(), cids, hard)
if err != nil {
return err
}
if resp.TaskID != "" {
cmd.Printf("Successfully initiated channel deletion. Task id: %s\n", resp.TaskID)
} else {
return errors.New("channel deletion failed")
}
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.Bool("hard", false, "[optional] Channel will be hard deleted. This action is irrevocable.")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func updateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update-channel --type [channel-type] --id [channel-id] --properties [raw-json-properties]",
Short: "Update a channel",
Long: heredoc.Doc(`
Updates an existing channel. The 'properties' are specified as a raw json string. The valid
properties are the 'ChannelRequest' object of the official documentation.
Such as 'team', 'frozen', 'disabled' or any custom property.
https://getstream.io/chat/docs/rest/#channels-updatechannel
`),
Example: heredoc.Doc(`
# Unfreeze a channel
$ stream-cli chat update-channel --type messaging --id redteam --properties "{\"frozen\":false}"
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chanType, _ := cmd.Flags().GetString("type")
chanID, _ := cmd.Flags().GetString("id")
p, _ := cmd.Flags().GetString("properties")
props := make(map[string]interface{})
err = json.Unmarshal([]byte(p), &props)
if err != nil {
return err
}
ch := c.Channel(chanType, chanID)
_, err = ch.Update(cmd.Context(), props, nil)
if err != nil {
return err
}
cmd.Printf("Successfully updated channel [%s]\n", chanID)
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.StringP("properties", "p", "", "[required] Channel properties to update")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
_ = cmd.MarkFlagRequired("properties")
return cmd
}
func updatePartialCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update-channel-partial --type [channel-type] --id [channel-id] --set [raw-json] --unset [property-names]",
Short: "Update a channel partially",
Long: heredoc.Doc(`
Updates an existing channel. The 'set' property is a comma separated list of key value pairs.
The 'unset' property is a comma separated list of property names.
`),
Example: heredoc.Doc(`
# Freeze a channel and set 'age' to 21. At the same time, remove 'haircolor' and 'height'.
$ stream-cli chat update-channel-partial --type messaging --id channel1 --set '{"frozen":true,"age":21}' --unset haircolor,height
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
flags := cmd.Flags()
chanType, _ := flags.GetString("type")
chanID, _ := flags.GetString("id")
update, err := utils.GetPartialUpdateParam(flags)
if err != nil {
return err
}
ch := c.Channel(chanType, chanID)
_, err = ch.PartialUpdate(cmd.Context(), update)
if err != nil {
return err
}
cmd.Printf("Successfully updated channel [%s]\n", chanID)
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.StringP("set", "s", "", "[optional] Raw JSON of key-value pairs to set")
fl.StringP("unset", "u", "", "[optional] Comma separated list of properties to unset")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func listCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-channels --type [channel-type]",
Short: "List channels",
Long: heredoc.Doc(`
List all channels of a given channel type. You can also provide
a limit for paginating the results.
`),
Example: heredoc.Doc(`
# List the top 5 'messaging' channels as a json
$ stream-cli chat list-channels --type messaging --limit 5
# List the top 20 'livestream' channels as a browsable tree
$ stream-cli chat list-channels --type livestream --limit 20 --output-format tree
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chanType, _ := cmd.Flags().GetString("type")
limit, _ := cmd.Flags().GetInt("limit")
resp, err := c.QueryChannels(cmd.Context(), &stream.QueryOption{
Filter: map[string]interface{}{
"type": chanType,
},
Sort: []*stream.SortOption{{Field: "cid", Direction: 1}},
Limit: limit,
})
if err != nil {
return err
}
return utils.PrintObject(cmd, resp)
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.IntP("limit", "l", 10, "[optional] Number of channels to return. Used for pagination")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
_ = cmd.MarkFlagRequired("type")
return cmd
}
func addMembersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-members --type [channel-type] --id [channel-id] [user-id-1] [user-id-2] ...",
Short: "Add members to a channel",
Example: heredoc.Doc(`
# Add members joe, jill and jane to 'red-team' channel
$ stream-cli chat add-members --type messaging --id red-team joe jill jane
`),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chType, _ := cmd.Flags().GetString("type")
chID, _ := cmd.Flags().GetString("id")
_, err = c.Channel(chType, chID).AddMembers(cmd.Context(), args)
if err != nil {
return err
}
cmd.Println("Successfully added user(s) to channel")
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func removeMemberCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "remove-members --type [channel-type] --id [channel-id] [user-id-1] [user-id-2] ...",
Short: "Remove members from a channel",
Example: heredoc.Doc(`
# Remove members joe, jill and jane from 'red-team' channel
$ stream-cli chat remove-members --type messaging --id red-team joe jill jane
`),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chType, _ := cmd.Flags().GetString("type")
chID, _ := cmd.Flags().GetString("id")
_, err = c.Channel(chType, chID).RemoveMembers(cmd.Context(), args, nil)
if err != nil {
return err
}
cmd.Println("Successfully removed user(s) from channel")
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func promoteModeratorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "promote-moderators --type [channel-type] --id [channel-id] [user-id-1] [user-id-2] ...",
Short: "Promote users to channel moderator role",
Args: cobra.MinimumNArgs(1),
Example: heredoc.Doc(`
# Promote 4 users to moderator
$ stream-cli chat promote-moderators --type messaging --id red-team joe mike jane jill
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chType, _ := cmd.Flags().GetString("type")
chID, _ := cmd.Flags().GetString("id")
_, err = c.Channel(chType, chID).AddModerators(cmd.Context(), args...)
if err != nil {
return err
}
cmd.Println("Successfully promoted users to moderators")
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func demoteModeratorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "demote-moderators --type [channel-type] --id [channel-id] [user-id-1] [user-id-2] ...",
Short: "Demote users from moderator role",
Args: cobra.MinimumNArgs(1),
Example: heredoc.Doc(`
# Demote 4 users from moderator role
$ stream-cli chat demote-moderators --type messaging --id red-team joe mike jane jill
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chType, _ := cmd.Flags().GetString("type")
chID, _ := cmd.Flags().GetString("id")
_, err = c.Channel(chType, chID).DemoteModerators(cmd.Context(), args...)
if err != nil {
return err
}
cmd.Println("Successfully demoted users from moderators")
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func assignRoleCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "assign-role --type [channel-type] --id [channel-id] --user-id [user-id] --role [channel-role-name]",
Short: "Assign a role to a user",
Example: heredoc.Doc(`
# Assign 'channel_moderator' role to user 'joe'
$ stream-cli chat assign-role --type messaging --id red-team --user-id joe --role channel_moderator
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chType, _ := cmd.Flags().GetString("type")
chID, _ := cmd.Flags().GetString("id")
userID, _ := cmd.Flags().GetString("user-id")
role, _ := cmd.Flags().GetString("role")
assignments := []*stream.RoleAssignment{{ChannelRole: role, UserID: userID}}
_, err = c.Channel(chType, chID).AssignRole(cmd.Context(), assignments, nil)
if err != nil {
return err
}
cmd.Printf("Successfully assigned role [%s] to [%s].\n", role, userID)
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.StringP("user-id", "u", "", "[required] User id to assign a role to")
fl.StringP("role", "r", "", "[required] Channel role name to assign")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
_ = cmd.MarkFlagRequired("user-id")
_ = cmd.MarkFlagRequired("role")
return cmd
}
func hideCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "hide-channel --type [channel-type] --id [channel-id] --user-id [user-id]",
Short: "Hide a channel",
Long: heredoc.Doc(`
Hiding a channel will remove it from query channel requests for that
user until a new message is added. Please keep in mind that hiding a channel
is only available to members of that channel.
You can still retrieve the list of hidden channels using the { "hidden" : true } query parameter.
`),
Example: heredoc.Doc(`
# Hide a 'red-team' channel for user 'joe'
$ stream-cli chat hide-channel --type messaging --id red-team --user-id joe
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chType, _ := cmd.Flags().GetString("type")
chID, _ := cmd.Flags().GetString("id")
userID, _ := cmd.Flags().GetString("user-id")
_, err = c.Channel(chType, chID).Hide(cmd.Context(), userID)
if err != nil {
return err
}
cmd.Printf("Successfully hid channel for " + userID + "\n")
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.StringP("user-id", "u", "", "[required] User id to hide the channel to")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
_ = cmd.MarkFlagRequired("user-id")
return cmd
}
func showCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show-channel --type [channel-type] --id [channel-id] --user-id [user-id]",
Short: "Show a channel",
Long: heredoc.Doc(`
Hiding a channel will remove it from query channel requests for that
user until a new message is added.
As opposed to this, showing a channel will add it to query channel requests for that user.
`),
Example: heredoc.Doc(`
# Show a 'red-team' channel for user 'joe'
$ stream-cli chat show-channel --type messaging --id red-team --user-id joe
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
chType, _ := cmd.Flags().GetString("type")
chID, _ := cmd.Flags().GetString("id")
userID, _ := cmd.Flags().GetString("user-id")
_, err = c.Channel(chType, chID).Show(cmd.Context(), userID)
if err != nil {
return err
}
cmd.Println("Successfully shown channel for " + userID)
return nil
},
}
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel id")
fl.StringP("user-id", "u", "", "[required] User id to show the channel to")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
_ = cmd.MarkFlagRequired("user-id")
return cmd
}
func listMembersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-members --type [channel-type] --id [channel-id]",
Short: "List members of a channel",
Long: heredoc.Doc(`
List and paginate members of a channel using the Stream Chat API.
This command supports optional filters, offset/limit for pagination,
and sort fields (e.g. "user_id", "created_at").
`),
Example: heredoc.Doc(`
# List first 10 members of 'red-team'
$ stream-cli chat list-members --type messaging --id red-team
# Filter members whose name includes 'tom'
$ stream-cli chat list-members --type messaging --id red-team --filter '{"name":{"$q":"tom"}}'
# Get next page of members (10 offset)
$ stream-cli chat list-members --type messaging --id red-team --offset 10 --limit 10
# Sort members by user_id ascending
$ stream-cli chat list-members --type messaging --id red-team --sort user_id:1
`),
RunE: func(cmd *cobra.Command, args []string) error {
// client
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
// required flags
chanType, _ := cmd.Flags().GetString("type")
chanID, _ := cmd.Flags().GetString("id")
// optional flags
rawFilter, _ := cmd.Flags().GetString("filter")
offset, _ := cmd.Flags().GetInt("offset")
limit, _ := cmd.Flags().GetInt("limit")
rawSort, _ := cmd.Flags().GetString("sort")
// parse filter JSON
filter := map[string]interface{}{}
if rawFilter != "" {
err = json.Unmarshal([]byte(rawFilter), &filter)
if err != nil {
return fmt.Errorf("invalid filter JSON: %w", err)
}
}
// parse sort
var sortOption *stream.SortOption
if rawSort != "" {
parts := strings.Split(rawSort, ":")
if len(parts) != 2 {
return errors.New("sort format must be field:direction (e.g., user_id:1)")
}
direction := 1
if parts[1] == "-1" {
direction = -1
}
sortOption = &stream.SortOption{
Field: parts[0],
Direction: direction,
}
}
// build query
q := &stream.QueryOption{
Filter: filter,
Offset: offset,
Limit: limit,
}
if sortOption == nil {
sortOption = &stream.SortOption{Field: "user_id", Direction: 1}
}
ch := c.Channel(chanType, chanID)
membersResp, err := ch.QueryMembers(cmd.Context(), q, sortOption)
if err != nil {
return err
}
return utils.PrintObject(cmd, membersResp.Members)
},
}
// flags
fl := cmd.Flags()
fl.StringP("type", "t", "", "[required] Channel type such as 'messaging' or 'livestream'")
fl.StringP("id", "i", "", "[required] Channel ID")
fl.String("filter", "", "[optional] JSON string to filter members")
fl.String("sort", "", "[optional] Sorting field and direction (e.g., user_id:1 or created_at:-1)")
fl.Int("offset", 0, "[optional] Pagination offset (default 0)")
fl.Int("limit", 10, "[optional] Pagination limit (default 10)")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}