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

Returns a new instance of Channel.



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

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.



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

def channel_type
  @channel_type
end

#custom_dataObject (readonly)

Returns the value of attribute custom_data.



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

def custom_data
  @custom_data
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#membersObject (readonly)

Returns the value of attribute members.



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

def members
  @members
end

Instance Method Details

#accept_invite(user_id, **options) ⇒ Object



189
190
191
192
# File 'lib/stream-chat/channel.rb', line 189

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

#add_members(user_ids, **options) ⇒ Object



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

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

#add_moderators(user_ids) ⇒ Object



203
204
205
# File 'lib/stream-chat/channel.rb', line 203

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

#assign_roles(members, message = nil) ⇒ Object



215
216
217
# File 'lib/stream-chat/channel.rb', line 215

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

#ban_user(user_id, **options) ⇒ Object



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

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

#create(user_id) ⇒ Object



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

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

#deleteObject



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

def delete
  @client.delete(url)
end

#delete_file(url) ⇒ Object



292
293
294
# File 'lib/stream-chat/channel.rb', line 292

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

#delete_image(url) ⇒ Object



298
299
300
# File 'lib/stream-chat/channel.rb', line 298

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

#delete_reaction(message_id, reaction_type, user_id) ⇒ Object



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

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



221
222
223
# File 'lib/stream-chat/channel.rb', line 221

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

#get_messages(message_ids) ⇒ Object



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

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

#get_reactions(message_id, **options) ⇒ Object



240
241
242
# File 'lib/stream-chat/channel.rb', line 240

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

#get_replies(parent_id, **options) ⇒ Object



234
235
236
# File 'lib/stream-chat/channel.rb', line 234

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

#hide(user_id) ⇒ Object



259
260
261
# File 'lib/stream-chat/channel.rb', line 259

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

#invite_members(user_ids, **options) ⇒ Object



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

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

#mark_read(user_id, **options) ⇒ Object



227
228
229
230
# File 'lib/stream-chat/channel.rb', line 227

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



161
162
163
164
165
# File 'lib/stream-chat/channel.rb', line 161

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

#query(**options) ⇒ Object



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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/stream-chat/channel.rb', line 108

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

#reject_invite(user_id, **options) ⇒ Object



196
197
198
199
# File 'lib/stream-chat/channel.rb', line 196

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

#remove_members(user_ids) ⇒ Object



209
210
211
# File 'lib/stream-chat/channel.rb', line 209

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

#send_event(event, user_id) ⇒ Object



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

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



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

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



286
287
288
# File 'lib/stream-chat/channel.rb', line 286

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

#send_message(message, user_id) ⇒ Object



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

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

#send_reaction(message_id, reaction, user_id) ⇒ Object



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

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



266
267
268
# File 'lib/stream-chat/channel.rb', line 266

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

#truncate(**options) ⇒ Object



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

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

#unban_user(user_id) ⇒ Object



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

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

#unmute(user_id) ⇒ Object



169
170
171
# File 'lib/stream-chat/channel.rb', line 169

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

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



128
129
130
131
# File 'lib/stream-chat/channel.rb', line 128

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

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



135
136
137
138
139
140
# File 'lib/stream-chat/channel.rb', line 135

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



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

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

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