-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathchannel.rb
More file actions
419 lines (355 loc) · 16.1 KB
/
channel.rb
File metadata and controls
419 lines (355 loc) · 16.1 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
# typed: strict
# frozen_string_literal: true
require 'stream-chat/client'
require 'stream-chat/errors'
require 'stream-chat/util'
require 'stream-chat/types'
module StreamChat
class Channel
extend T::Sig
sig { returns(T.nilable(String)) }
attr_reader :id
sig { returns(String) }
attr_reader :channel_type
sig { returns(String) }
attr_reader :cid
sig { returns(StringKeyHash) }
attr_reader :custom_data
sig { returns(T::Array[StringKeyHash]) }
attr_reader :members
sig { params(client: StreamChat::Client, channel_type: String, channel_id: T.nilable(String), custom_data: T.nilable(StringKeyHash)).void }
def initialize(client, channel_type, channel_id = nil, custom_data = nil)
@channel_type = channel_type
@id = channel_id
@cid = T.let("#{@channel_type}:#{@id}", String)
@client = client
@custom_data = T.let(custom_data || {}, StringKeyHash)
@members = T.let([], T::Array[StringKeyHash])
end
sig { returns(String) }
def url
raise StreamChannelException, 'channel does not have an id' if @id.nil?
"channels/#{@channel_type}/#{@id}"
end
# Gets multiple messages from the channel.
sig { params(message_ids: T::Array[String]).returns(StreamChat::StreamResponse) }
def get_messages(message_ids)
@client.get("#{url}/messages", params: { 'ids' => message_ids.join(',') })
end
# Sends a message to this channel.
sig { params(message: StringKeyHash, user_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def send_message(message, user_id, **options)
payload = options.merge({ message: add_user_id(message, user_id) })
@client.post("#{url}/message", data: payload)
end
# Sends an event on this channel.
sig { params(event: StringKeyHash, user_id: String).returns(StreamChat::StreamResponse) }
def send_event(event, user_id)
payload = { 'event' => add_user_id(event, user_id) }
@client.post("#{url}/event", data: payload)
end
# Sends a new reaction to a given message.
sig { params(message_id: String, reaction: StringKeyHash, user_id: String).returns(StreamChat::StreamResponse) }
def send_reaction(message_id, reaction, user_id)
payload = { reaction: add_user_id(reaction, user_id) }
@client.post("messages/#{message_id}/reaction", data: payload)
end
# Delete a reaction from a message.
sig { params(message_id: String, reaction_type: String, user_id: String).returns(StreamChat::StreamResponse) }
def delete_reaction(message_id, reaction_type, user_id)
@client.delete(
"messages/#{message_id}/reaction/#{reaction_type}",
params: { user_id: user_id }
)
end
# Creates a channel with the given creator user.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def create(user_id)
@custom_data['created_by'] = { id: user_id }
query(watch: false, state: false, presence: false)
end
# Creates or returns a channel.
sig { params(options: T.untyped).returns(StreamChat::StreamResponse) }
def query(**options)
payload = { state: true, data: @custom_data }.merge(options)
url = "channels/#{@channel_type}"
url = "#{url}/#{@id}" unless @id.nil?
state = @client.post("#{url}/query", data: payload)
@id = state['channel']['id'] if @id.nil?
state
end
# Queries members of a channel.
#
# The queryMembers endpoint allows you to list and paginate members from a channel. The
# endpoint supports filtering on numerous criteria to efficiently return members information.
# This endpoint is useful for channels that have large lists of members and
# you want to search members or if you want to display the full list of members for a channel.
sig { params(filter_conditions: StringKeyHash, sort: T.nilable(T::Hash[String, Integer]), options: T.untyped).returns(StreamChat::StreamResponse) }
def query_members(filter_conditions = {}, sort: nil, **options)
params = {}.merge(options).merge({
id: @id,
type: @channel_type,
filter_conditions: filter_conditions,
sort: StreamChat.get_sort_fields(sort)
})
if @id == '' && @members.length.positive?
params['members'] = []
@members.each do |m|
params['members'] << m['user'].nil? ? m['user_id'] : m['user']['id']
end
end
@client.get('members', params: { payload: params.to_json })
end
# Updates a channel.
sig { params(channel_data: T.nilable(StringKeyHash), update_message: T.nilable(StringKeyHash), options: T.untyped).returns(StreamChat::StreamResponse) }
def update(channel_data, update_message = nil, **options)
payload = { data: channel_data, message: update_message }.merge(options)
@client.post(url, data: payload)
end
# Updates a channel partially.
sig { params(set: T.nilable(StringKeyHash), unset: T.nilable(T::Array[String])).returns(StreamChat::StreamResponse) }
def update_partial(set = nil, unset = nil)
raise StreamChannelException, 'set or unset is needed' if set.nil? && unset.nil?
payload = { set: set, unset: unset }
@client.patch(url, data: payload)
end
# Deletes a channel.
sig { returns(StreamChat::StreamResponse) }
def delete
@client.delete(url)
end
# Removes all messages from the channel.
sig { params(options: T.untyped).returns(StreamChat::StreamResponse) }
def truncate(**options)
@client.post("#{url}/truncate", data: options)
end
# Mutes a channel.
#
# Messages added to a muted channel will not trigger push notifications, nor change the
# unread count for the users that muted it. By default, mutes stay in place indefinitely
# until the user removes it; however, you can optionally set an expiration time. The list
# of muted channels and their expiration time is returned when the user connects.
sig { params(user_id: String, expiration: T.nilable(Integer)).returns(StreamChat::StreamResponse) }
def mute(user_id, expiration = nil)
data = { user_id: user_id, channel_cid: @cid }
data['expiration'] = expiration if expiration
@client.post('moderation/mute/channel', data: data)
end
# Unmutes a channel.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def unmute(user_id)
@client.post('moderation/unmute/channel', data: { 'user_id' => user_id, 'channel_cid' => @cid })
end
# Pins a channel for a user.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def pin(user_id)
raise StreamChannelException, 'user ID must not be empty' if user_id.empty?
payload = {
set: {
pinned: true
}
}
@client.patch("#{url}/member/#{CGI.escape(user_id)}", data: payload)
end
# Unins a channel for a user.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def unpin(user_id)
raise StreamChannelException, 'user ID must not be empty' if user_id.empty?
payload = {
set: {
pinned: false
}
}
@client.patch("#{url}/member/#{CGI.escape(user_id)}", data: payload)
end
# Archives a channel for a user.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def archive(user_id)
raise StreamChannelException, 'user ID must not be empty' if user_id.empty?
payload = {
set: {
archived: true
}
}
@client.patch("#{url}/member/#{CGI.escape(user_id)}", data: payload)
end
# Archives a channel for a user.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def unarchive(user_id)
raise StreamChannelException, 'user ID must not be empty' if user_id.empty?
payload = {
set: {
archived: false
}
}
@client.patch("#{url}/member/#{CGI.escape(user_id)}", data: payload)
end
# Updates a member partially in the channel.
sig { params(user_id: String, set: T.nilable(StringKeyHash), unset: T.nilable(T::Array[String])).returns(StreamChat::StreamResponse) }
def update_member_partial(user_id, set: nil, unset: nil)
raise StreamChannelException, 'user ID must not be empty' if user_id.empty?
raise StreamChannelException, 'set or unset is required' if set.nil? && unset.nil?
payload = { set: set, unset: unset }
@client.patch("#{url}/member/#{CGI.escape(user_id)}", data: payload)
end
# Adds members to the channel.
sig { params(user_ids: T::Array[String], options: T.untyped).returns(StreamChat::StreamResponse) }
def add_members(user_ids, **options)
payload = options.dup
payload[:hide_history_before] = StreamChat.normalize_timestamp(payload[:hide_history_before]) if payload[:hide_history_before]
payload = payload.merge({ add_members: user_ids })
update(nil, nil, **payload)
end
# Invites users to the channel.
sig { params(user_ids: T::Array[String], options: T.untyped).returns(StreamChat::StreamResponse) }
def invite_members(user_ids, **options)
payload = options.merge({ invites: user_ids })
update(nil, nil, **payload)
end
# Accepts an invitation to the channel.
sig { params(user_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def accept_invite(user_id, **options)
payload = options.merge({ user_id: user_id, accept_invite: true })
update(nil, nil, **payload)
end
# Rejects an invitation to the channel.
sig { params(user_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def reject_invite(user_id, **options)
payload = options.merge({ user_id: user_id, reject_invite: true })
update(nil, nil, **payload)
end
# Adds moderators to the channel.
sig { params(user_ids: T::Array[String]).returns(StreamChat::StreamResponse) }
def add_moderators(user_ids)
update(nil, nil, add_moderators: user_ids)
end
# Adds filter tags to the channel.
sig { params(tags: T::Array[String]).returns(StreamChat::StreamResponse) }
def add_filter_tags(tags)
update(nil, nil, add_filter_tags: tags)
end
# Removes filter tags from the channel.
sig { params(tags: T::Array[String]).returns(StreamChat::StreamResponse) }
def remove_filter_tags(tags)
update(nil, nil, remove_filter_tags: tags)
end
# Removes members from the channel.
sig { params(user_ids: T::Array[String]).returns(StreamChat::StreamResponse) }
def remove_members(user_ids)
update(nil, nil, remove_members: user_ids)
end
# Assigns roles to members in the channel.
sig { params(members: T::Array[StringKeyHash], message: T.nilable(StringKeyHash)).returns(StreamChat::StreamResponse) }
def assign_roles(members, message = nil)
update(nil, message, assign_roles: members)
end
# Demotes moderators in the channel.
sig { params(user_ids: T::Array[String]).returns(StreamChat::StreamResponse) }
def demote_moderators(user_ids)
update(nil, nil, demote_moderators: user_ids)
end
# Sends the mark read event for this user, only works if the `read_events` setting is enabled.
sig { params(user_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def mark_read(user_id, **options)
payload = add_user_id(options, user_id)
@client.post("#{url}/read", data: payload)
end
# List the message replies for a parent message.
sig { params(parent_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def get_replies(parent_id, **options)
@client.get("messages/#{parent_id}/replies", params: options)
end
# List the reactions, supports pagination.
sig { params(message_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def get_reactions(message_id, **options)
@client.get("messages/#{message_id}/reactions", params: options)
end
# Bans a user from this channel.
sig { params(user_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def ban_user(user_id, **options)
@client.ban_user(user_id, type: @channel_type, id: @id, **options)
end
# Removes the ban for a user on this channel.
sig { params(user_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
def unban_user(user_id, **options)
@client.unban_user(user_id, type: @channel_type, id: @id, **options)
end
# Removes a channel from query channel requests for that user until a new message is added.
# Use `show` to cancel this operation.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def hide(user_id)
@client.post("#{url}/hide", data: { user_id: user_id })
end
# Shows a previously hidden channel.
# Use `hide` to hide a channel.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def show(user_id)
@client.post("#{url}/show", data: { user_id: user_id })
end
# Uploads a file.
#
# This functionality defaults to using the Stream CDN. If you would like, you can
# easily change the logic to upload to your own CDN of choice.
sig { params(url: String, user: StringKeyHash, content_type: T.nilable(String)).returns(StreamChat::StreamResponse) }
def send_file(url, user, content_type = nil)
@client.send_file("#{self.url}/file", url, user, content_type)
end
# Uploads an image.
#
# Stream supported image types are: image/bmp, image/gif, image/jpeg, image/png, image/webp,
# image/heic, image/heic-sequence, image/heif, image/heif-sequence, image/svg+xml.
# You can set a more restrictive list for your application if needed.
# The maximum file size is 100MB.
sig { params(url: String, user: StringKeyHash, content_type: T.nilable(String)).returns(StreamChat::StreamResponse) }
def send_image(url, user, content_type = nil)
@client.send_file("#{self.url}/image", url, user, content_type)
end
# Deletes a file by file url.
sig { params(url: String).returns(StreamChat::StreamResponse) }
def delete_file(url)
@client.delete("#{self.url}/file", params: { url: url })
end
# Deletes an image by image url.
sig { params(url: String).returns(StreamChat::StreamResponse) }
def delete_image(url)
@client.delete("#{self.url}/image", params: { url: url })
end
# Creates or updates a draft message for this channel.
#
# @param [StringKeyHash] message The draft message content
# @param [String] user_id The ID of the user creating/updating the draft
# @return [StreamChat::StreamResponse]
sig { params(message: StringKeyHash, user_id: String).returns(StreamChat::StreamResponse) }
def create_draft(message, user_id)
payload = { message: add_user_id(message, user_id) }
@client.post("#{url}/draft", data: payload)
end
# Deletes a draft message for this channel.
#
# @param [String] user_id The ID of the user deleting the draft
# @param [String] parent_id Optional parent message ID for thread drafts
# @return [StreamChat::StreamResponse]
sig { params(user_id: String, parent_id: T.nilable(String)).returns(StreamChat::StreamResponse) }
def delete_draft(user_id, parent_id: nil)
params = { user_id: user_id }
params[:parent_id] = parent_id if parent_id
@client.delete("#{url}/draft", params: params)
end
# Gets a draft message for this channel.
#
# @param [String] user_id The ID of the user getting the draft
# @param [String] parent_id Optional parent message ID for thread drafts
# @return [StreamChat::StreamResponse]
sig { params(user_id: String, parent_id: T.nilable(String)).returns(StreamChat::StreamResponse) }
def get_draft(user_id, parent_id: nil)
params = { user_id: user_id }
params[:parent_id] = parent_id if parent_id
@client.get("#{url}/draft", params: params)
end
private
sig { params(payload: StringKeyHash, user_id: String).returns(StringKeyHash) }
def add_user_id(payload, user_id)
payload.merge({ user: { id: user_id } })
end
end
end