Skip to content

Commit 2d4b498

Browse files
author
Rafael Marinho
committed
[CHA-1609] support batch updates
1 parent 22c3f81 commit 2d4b498

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

channel.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,3 +1280,56 @@ func (ch *Channel) RemoveFilterTags(ctx context.Context, tags []string, message
12801280
resp.updateChannel(ch)
12811281
return &resp.Response, nil
12821282
}
1283+
1284+
// BatchUpdateOperation represents the type of batch update operation.
1285+
type BatchUpdateOperation string
1286+
1287+
const (
1288+
BatchUpdateOperationAddMembers BatchUpdateOperation = "addMembers"
1289+
BatchUpdateOperationRemoveMembers BatchUpdateOperation = "removeMembers"
1290+
BatchUpdateOperationInvites BatchUpdateOperation = "invites"
1291+
BatchUpdateOperationAssignRoles BatchUpdateOperation = "assignRoles"
1292+
BatchUpdateOperationAddModerators BatchUpdateOperation = "addModerators"
1293+
BatchUpdateOperationDemoteModerators BatchUpdateOperation = "demoteModerators"
1294+
BatchUpdateOperationHide BatchUpdateOperation = "hide"
1295+
BatchUpdateOperationShow BatchUpdateOperation = "show"
1296+
BatchUpdateOperationArchive BatchUpdateOperation = "archive"
1297+
BatchUpdateOperationUnarchive BatchUpdateOperation = "unarchive"
1298+
BatchUpdateOperationUpdateData BatchUpdateOperation = "updateData"
1299+
BatchUpdateOperationAddFilterTags BatchUpdateOperation = "addFilterTags"
1300+
BatchUpdateOperationRemoveFilterTags BatchUpdateOperation = "removeFilterTags"
1301+
)
1302+
1303+
// BatchChannelDataUpdate represents data that can be updated on channels in batch.
1304+
type BatchChannelDataUpdate struct {
1305+
Frozen *bool `json:"frozen,omitempty"`
1306+
Disabled *bool `json:"disabled,omitempty"`
1307+
Custom map[string]interface{} `json:"custom,omitempty"`
1308+
Team string `json:"team,omitempty"`
1309+
ConfigOverrides map[string]interface{} `json:"config_overrides,omitempty"`
1310+
AutoTranslationEnabled *bool `json:"auto_translation_enabled,omitempty"`
1311+
AutoTranslationLanguage string `json:"auto_translation_language,omitempty"`
1312+
}
1313+
1314+
// UpdateChannelsBatchFilters represents filters for batch channel updates.
1315+
type UpdateChannelsBatchFilters struct {
1316+
CIDs interface{} `json:"cids,omitempty"`
1317+
Types interface{} `json:"types,omitempty"`
1318+
FilterTags interface{} `json:"filter_tags,omitempty"`
1319+
}
1320+
1321+
// UpdateChannelsBatchOptions represents options for batch channel updates.
1322+
type UpdateChannelsBatchOptions struct {
1323+
Operation BatchUpdateOperation `json:"operation"`
1324+
Filter UpdateChannelsBatchFilters `json:"filter"`
1325+
Members interface{} `json:"members,omitempty"`
1326+
Data *BatchChannelDataUpdate `json:"data,omitempty"`
1327+
FilterTagsUpdate []string `json:"filter_tags_update,omitempty"`
1328+
}
1329+
1330+
// UpdateChannelsBatchResponse represents the response from batch channel update.
1331+
type UpdateChannelsBatchResponse struct {
1332+
Result map[string]string `json:"result"`
1333+
TaskID string `json:"task_id,omitempty"`
1334+
Response
1335+
}

channel_batch_updater.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package stream_chat
2+
3+
import (
4+
"context"
5+
)
6+
7+
// ChannelBatchUpdater provides convenience methods for batch channel operations.
8+
type ChannelBatchUpdater struct {
9+
client *Client
10+
}
11+
12+
// AddMembers adds members to channels matching the filter.
13+
func (u *ChannelBatchUpdater) AddMembers(ctx context.Context, filter UpdateChannelsBatchFilters, members interface{}) (*UpdateChannelsBatchResponse, error) {
14+
options := &UpdateChannelsBatchOptions{
15+
Operation: BatchUpdateOperationAddMembers,
16+
Filter: filter,
17+
Members: members,
18+
}
19+
return u.client.UpdateChannelsBatch(ctx, options)
20+
}
21+
22+
// RemoveMembers removes members from channels matching the filter.
23+
func (u *ChannelBatchUpdater) RemoveMembers(ctx context.Context, filter UpdateChannelsBatchFilters, members []string) (*UpdateChannelsBatchResponse, error) {
24+
options := &UpdateChannelsBatchOptions{
25+
Operation: BatchUpdateOperationRemoveMembers,
26+
Filter: filter,
27+
Members: members,
28+
}
29+
return u.client.UpdateChannelsBatch(ctx, options)
30+
}
31+
32+
// InviteMembers invites members to channels matching the filter.
33+
func (u *ChannelBatchUpdater) InviteMembers(ctx context.Context, filter UpdateChannelsBatchFilters, members interface{}) (*UpdateChannelsBatchResponse, error) {
34+
options := &UpdateChannelsBatchOptions{
35+
Operation: BatchUpdateOperationInvites,
36+
Filter: filter,
37+
Members: members,
38+
}
39+
return u.client.UpdateChannelsBatch(ctx, options)
40+
}
41+
42+
// AddModerators adds moderators to channels matching the filter.
43+
func (u *ChannelBatchUpdater) AddModerators(ctx context.Context, filter UpdateChannelsBatchFilters, members []string) (*UpdateChannelsBatchResponse, error) {
44+
options := &UpdateChannelsBatchOptions{
45+
Operation: BatchUpdateOperationAddModerators,
46+
Filter: filter,
47+
Members: members,
48+
}
49+
return u.client.UpdateChannelsBatch(ctx, options)
50+
}
51+
52+
// DemoteModerators removes moderator role from members in channels matching the filter.
53+
func (u *ChannelBatchUpdater) DemoteModerators(ctx context.Context, filter UpdateChannelsBatchFilters, members []string) (*UpdateChannelsBatchResponse, error) {
54+
options := &UpdateChannelsBatchOptions{
55+
Operation: BatchUpdateOperationDemoteModerators,
56+
Filter: filter,
57+
Members: members,
58+
}
59+
return u.client.UpdateChannelsBatch(ctx, options)
60+
}
61+
62+
// AssignRoles assigns roles to members in channels matching the filter.
63+
func (u *ChannelBatchUpdater) AssignRoles(ctx context.Context, filter UpdateChannelsBatchFilters, members []*ChannelMember) (*UpdateChannelsBatchResponse, error) {
64+
options := &UpdateChannelsBatchOptions{
65+
Operation: BatchUpdateOperationAssignRoles,
66+
Filter: filter,
67+
Members: members,
68+
}
69+
return u.client.UpdateChannelsBatch(ctx, options)
70+
}
71+
72+
// Hide hides channels matching the filter.
73+
func (u *ChannelBatchUpdater) Hide(ctx context.Context, filter UpdateChannelsBatchFilters) (*UpdateChannelsBatchResponse, error) {
74+
options := &UpdateChannelsBatchOptions{
75+
Operation: BatchUpdateOperationHide,
76+
Filter: filter,
77+
}
78+
return u.client.UpdateChannelsBatch(ctx, options)
79+
}
80+
81+
// Show shows channels matching the filter.
82+
func (u *ChannelBatchUpdater) Show(ctx context.Context, filter UpdateChannelsBatchFilters) (*UpdateChannelsBatchResponse, error) {
83+
options := &UpdateChannelsBatchOptions{
84+
Operation: BatchUpdateOperationShow,
85+
Filter: filter,
86+
}
87+
return u.client.UpdateChannelsBatch(ctx, options)
88+
}
89+
90+
// Archive archives channels matching the filter.
91+
func (u *ChannelBatchUpdater) Archive(ctx context.Context, filter UpdateChannelsBatchFilters) (*UpdateChannelsBatchResponse, error) {
92+
options := &UpdateChannelsBatchOptions{
93+
Operation: BatchUpdateOperationArchive,
94+
Filter: filter,
95+
}
96+
return u.client.UpdateChannelsBatch(ctx, options)
97+
}
98+
99+
// Unarchive unarchives channels matching the filter.
100+
func (u *ChannelBatchUpdater) Unarchive(ctx context.Context, filter UpdateChannelsBatchFilters) (*UpdateChannelsBatchResponse, error) {
101+
options := &UpdateChannelsBatchOptions{
102+
Operation: BatchUpdateOperationUnarchive,
103+
Filter: filter,
104+
}
105+
return u.client.UpdateChannelsBatch(ctx, options)
106+
}
107+
108+
// UpdateData updates data on channels matching the filter.
109+
func (u *ChannelBatchUpdater) UpdateData(ctx context.Context, filter UpdateChannelsBatchFilters, data *BatchChannelDataUpdate) (*UpdateChannelsBatchResponse, error) {
110+
options := &UpdateChannelsBatchOptions{
111+
Operation: BatchUpdateOperationUpdateData,
112+
Filter: filter,
113+
Data: data,
114+
}
115+
return u.client.UpdateChannelsBatch(ctx, options)
116+
}
117+
118+
// AddFilterTags adds filter tags to channels matching the filter.
119+
func (u *ChannelBatchUpdater) AddFilterTags(ctx context.Context, filter UpdateChannelsBatchFilters, tags []string) (*UpdateChannelsBatchResponse, error) {
120+
options := &UpdateChannelsBatchOptions{
121+
Operation: BatchUpdateOperationAddFilterTags,
122+
Filter: filter,
123+
FilterTagsUpdate: tags,
124+
}
125+
return u.client.UpdateChannelsBatch(ctx, options)
126+
}
127+
128+
// RemoveFilterTags removes filter tags from channels matching the filter.
129+
func (u *ChannelBatchUpdater) RemoveFilterTags(ctx context.Context, filter UpdateChannelsBatchFilters, tags []string) (*UpdateChannelsBatchResponse, error) {
130+
options := &UpdateChannelsBatchOptions{
131+
Operation: BatchUpdateOperationRemoveFilterTags,
132+
Filter: filter,
133+
FilterTagsUpdate: tags,
134+
}
135+
return u.client.UpdateChannelsBatch(ctx, options)
136+
}
137+

client.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,19 @@ func (c *Client) MarkDeliveredSimple(ctx context.Context, userID, messageID, cha
318318

319319
return c.MarkDelivered(ctx, options)
320320
}
321+
322+
// UpdateChannelsBatch updates channels in batch based on the provided options.
323+
func (c *Client) UpdateChannelsBatch(ctx context.Context, options *UpdateChannelsBatchOptions) (*UpdateChannelsBatchResponse, error) {
324+
if options == nil {
325+
return nil, errors.New("options must not be nil")
326+
}
327+
328+
var resp UpdateChannelsBatchResponse
329+
err := c.makeRequest(ctx, http.MethodPut, "channels/batch", nil, options, &resp)
330+
return &resp, err
331+
}
332+
333+
// ChannelBatchUpdater returns a ChannelBatchUpdater instance for batch channel operations.
334+
func (c *Client) ChannelBatchUpdater() *ChannelBatchUpdater {
335+
return &ChannelBatchUpdater{client: c}
336+
}

0 commit comments

Comments
 (0)