Class: StreamChat::Channel

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/stream-chat/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, channel_type, channel_id = nil, custom_data = nil) ⇒ Channel



29
30
31
32
33
34
35
36
# File 'lib/stream-chat/channel.rb', line 29

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

Instance Attribute Details

#channel_typeObject (readonly)

Returns the value of attribute channel_type.



17
18
19
# File 'lib/stream-chat/channel.rb', line 17

def channel_type
  @channel_type
end

#cidObject (readonly)

Returns the value of attribute cid.



20
21
22
# File 'lib/stream-chat/channel.rb', line 20

def cid
  @cid
end

#custom_dataObject (readonly)

Returns the value of attribute custom_data.



23
24
25
# File 'lib/stream-chat/channel.rb', line 23

def custom_data
  @custom_data
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/stream-chat/channel.rb', line 14

def id
  @id
end

#membersObject (readonly)

Returns the value of attribute members.



26
27
28
# File 'lib/stream-chat/channel.rb', line 26

def members
  @members
end

Instance Method Details

#accept_invite(user_id, **options) ⇒ Object



268
269
270
271
# File 'lib/stream-chat/channel.rb', line 268

def accept_invite(user_id, **options)
  payload = options.merge({ user_id: user_id, accept_invite: true })
  update(nil, nil, **payload)
end

#add_filter_tags(tags) ⇒ Object



288
289
290
# File 'lib/stream-chat/channel.rb', line 288

def add_filter_tags(tags)
  update(nil, nil, add_filter_tags: tags)
end

#add_members(user_ids, **options) ⇒ Object



252
253
254
255
256
257
# File 'lib/stream-chat/channel.rb', line 252

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

#add_moderators(user_ids) ⇒ Object



282
283
284
# File 'lib/stream-chat/channel.rb', line 282

def add_moderators(user_ids)
  update(nil, nil, add_moderators: user_ids)
end

#archive(user_id) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/stream-chat/channel.rb', line 216

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

#assign_roles(members, message = nil) ⇒ Object



306
307
308
# File 'lib/stream-chat/channel.rb', line 306

def assign_roles(members, message = nil)
  update(nil, message, assign_roles: members)
end

#ban_user(user_id, **options) ⇒ Object



337
338
339
# File 'lib/stream-chat/channel.rb', line 337

def ban_user(user_id, **options)
  @client.ban_user(user_id, type: @channel_type, id: @id, **options)
end

#create(user_id) ⇒ Object



83
84
85
86
# File 'lib/stream-chat/channel.rb', line 83

def create(user_id)
  @custom_data['created_by'] = { id: user_id }
  query(watch: false, state: false, presence: false)
end

#create_draft(message, user_id) ⇒ Object



399
400
401
402
# File 'lib/stream-chat/channel.rb', line 399

def create_draft(message, user_id)
  payload = { message: add_user_id(message, user_id) }
  @client.post("#{url}/draft", data: payload)
end

#deleteObject



159
160
161
# File 'lib/stream-chat/channel.rb', line 159

def delete
  @client.delete(url)
end

#delete_draft(user_id, parent_id: nil) ⇒ Object



410
411
412
413
414
# File 'lib/stream-chat/channel.rb', line 410

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

#delete_file(url) ⇒ Object



383
384
385
# File 'lib/stream-chat/channel.rb', line 383

def delete_file(url)
  @client.delete("#{self.url}/file", params: { url: url })
end

#delete_image(url) ⇒ Object



389
390
391
# File 'lib/stream-chat/channel.rb', line 389

def delete_image(url)
  @client.delete("#{self.url}/image", params: { url: url })
end

#delete_reaction(message_id, reaction_type, user_id) ⇒ Object



74
75
76
77
78
79
# File 'lib/stream-chat/channel.rb', line 74

def delete_reaction(message_id, reaction_type, user_id)
  @client.delete(
    "messages/#{message_id}/reaction/#{reaction_type}",
    params: { user_id: user_id }
  )
end

#demote_moderators(user_ids) ⇒ Object



312
313
314
# File 'lib/stream-chat/channel.rb', line 312

def demote_moderators(user_ids)
  update(nil, nil, demote_moderators: user_ids)
end

#get_draft(user_id, parent_id: nil) ⇒ Object



422
423
424
425
426
# File 'lib/stream-chat/channel.rb', line 422

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

#get_messages(message_ids) ⇒ Object



47
48
49
# File 'lib/stream-chat/channel.rb', line 47

def get_messages(message_ids)
  @client.get("#{url}/messages", params: { 'ids' => message_ids.join(',') })
end

#get_reactions(message_id, **options) ⇒ Object



331
332
333
# File 'lib/stream-chat/channel.rb', line 331

def get_reactions(message_id, **options)
  @client.get("messages/#{message_id}/reactions", params: options)
end

#get_replies(parent_id, **options) ⇒ Object



325
326
327
# File 'lib/stream-chat/channel.rb', line 325

def get_replies(parent_id, **options)
  @client.get("messages/#{parent_id}/replies", params: options)
end

#hide(user_id) ⇒ Object



350
351
352
# File 'lib/stream-chat/channel.rb', line 350

def hide(user_id)
  @client.post("#{url}/hide", data: { user_id: user_id })
end

#invite_members(user_ids, **options) ⇒ Object



261
262
263
264
# File 'lib/stream-chat/channel.rb', line 261

def invite_members(user_ids, **options)
  payload = options.merge({ invites: user_ids })
  update(nil, nil, **payload)
end

#mark_read(user_id, **options) ⇒ Object



318
319
320
321
# File 'lib/stream-chat/channel.rb', line 318

def mark_read(user_id, **options)
  payload = add_user_id(options, user_id)
  @client.post("#{url}/read", data: payload)
end

#mute(user_id, expiration = nil) ⇒ Object



176
177
178
179
180
# File 'lib/stream-chat/channel.rb', line 176

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

#pin(user_id) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/stream-chat/channel.rb', line 190

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

#query(**options) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/stream-chat/channel.rb', line 90

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

#query_members(filter_conditions = {}, sort: nil, **options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/stream-chat/channel.rb', line 123

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

#refresh_stateObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stream-chat/channel.rb', line 103

def refresh_state
  url = "channels/#{@channel_type}/#{@id}/query"
  state = @client.post(url, data: { state: true })

  # Members can be at top level or inside channel object (like Go's updateChannel)
  if state['members'] && !state['members'].empty?
    @members = state['members']
  elsif state['channel'] && state['channel']['members']
    @members = state['channel']['members']
  end
  state
end

#reject_invite(user_id, **options) ⇒ Object



275
276
277
278
# File 'lib/stream-chat/channel.rb', line 275

def reject_invite(user_id, **options)
  payload = options.merge({ user_id: user_id, reject_invite: true })
  update(nil, nil, **payload)
end

#remove_filter_tags(tags) ⇒ Object



294
295
296
# File 'lib/stream-chat/channel.rb', line 294

def remove_filter_tags(tags)
  update(nil, nil, remove_filter_tags: tags)
end

#remove_members(user_ids) ⇒ Object



300
301
302
# File 'lib/stream-chat/channel.rb', line 300

def remove_members(user_ids)
  update(nil, nil, remove_members: user_ids)
end

#send_event(event, user_id) ⇒ Object



60
61
62
63
# File 'lib/stream-chat/channel.rb', line 60

def send_event(event, user_id)
  payload = { 'event' => add_user_id(event, user_id) }
  @client.post("#{url}/event", data: payload)
end

#send_file(url, user, content_type = nil) ⇒ Object



366
367
368
# File 'lib/stream-chat/channel.rb', line 366

def send_file(url, user, content_type = nil)
  @client.send_file("#{self.url}/file", url, user, content_type)
end

#send_image(url, user, content_type = nil) ⇒ Object



377
378
379
# File 'lib/stream-chat/channel.rb', line 377

def send_image(url, user, content_type = nil)
  @client.send_file("#{self.url}/image", url, user, content_type)
end

#send_message(message, user_id, **options) ⇒ Object



53
54
55
56
# File 'lib/stream-chat/channel.rb', line 53

def send_message(message, user_id, **options)
  payload = options.merge({ message: add_user_id(message, user_id) })
  @client.post("#{url}/message", data: payload)
end

#send_reaction(message_id, reaction, user_id) ⇒ Object



67
68
69
70
# File 'lib/stream-chat/channel.rb', line 67

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

#show(user_id) ⇒ Object



357
358
359
# File 'lib/stream-chat/channel.rb', line 357

def show(user_id)
  @client.post("#{url}/show", data: { user_id: user_id })
end

#truncate(**options) ⇒ Object



165
166
167
# File 'lib/stream-chat/channel.rb', line 165

def truncate(**options)
  @client.post("#{url}/truncate", data: options)
end

#unarchive(user_id) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/stream-chat/channel.rb', line 229

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

#unban_user(user_id, **options) ⇒ Object



343
344
345
# File 'lib/stream-chat/channel.rb', line 343

def unban_user(user_id, **options)
  @client.unban_user(user_id, type: @channel_type, id: @id, **options)
end

#unmute(user_id) ⇒ Object



184
185
186
# File 'lib/stream-chat/channel.rb', line 184

def unmute(user_id)
  @client.post('moderation/unmute/channel', data: { 'user_id' => user_id, 'channel_cid' => @cid })
end

#unpin(user_id) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/stream-chat/channel.rb', line 203

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

#update(channel_data, update_message = nil, **options) ⇒ Object



143
144
145
146
# File 'lib/stream-chat/channel.rb', line 143

def update(channel_data, update_message = nil, **options)
  payload = { data: channel_data, message: update_message }.merge(options)
  @client.post(url, data: payload)
end

#update_member_partial(user_id, set: nil, unset: nil) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/stream-chat/channel.rb', line 242

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

#update_partial(set = nil, unset = nil) ⇒ Object



150
151
152
153
154
155
# File 'lib/stream-chat/channel.rb', line 150

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

#urlObject



39
40
41
42
43
# File 'lib/stream-chat/channel.rb', line 39

def url
  raise StreamChannelException, 'channel does not have an id' if @id.nil?

  "channels/#{@channel_type}/#{@id}"
end