Class: StreamChat::Channel

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

Overview

rubocop:todo Metrics/ClassLength # rubocop:todo Style/Documentation

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.



13
14
15
16
17
18
19
20
# File 'lib/stream-chat/channel.rb', line 13

def initialize(client, channel_type, channel_id = nil, custom_data = nil)
  @channel_type = channel_type
  @id = channel_id
  @cid = "#{@channel_type}:#{@id}"
  @client = client
  @custom_data = custom_data
  @custom_data = {} if @custom_data.nil?
end

Instance Attribute Details

#channel_typeObject (readonly)

Returns the value of attribute channel_type.



9
10
11
# File 'lib/stream-chat/channel.rb', line 9

def channel_type
  @channel_type
end

#custom_dataObject (readonly)

Returns the value of attribute custom_data.



10
11
12
# File 'lib/stream-chat/channel.rb', line 10

def custom_data
  @custom_data
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/stream-chat/channel.rb', line 8

def id
  @id
end

#membersObject (readonly)

Returns the value of attribute members.



11
12
13
# File 'lib/stream-chat/channel.rb', line 11

def members
  @members
end

Instance Method Details

#add_members(user_ids, **options) ⇒ Object



117
118
119
120
# File 'lib/stream-chat/channel.rb', line 117

def add_members(user_ids, **options)
  payload = options.merge({ add_members: user_ids })
  @client.post(url, data: payload)
end

#add_moderators(user_ids) ⇒ Object



126
127
128
# File 'lib/stream-chat/channel.rb', line 126

def add_moderators(user_ids)
  @client.post(url, data: { add_moderators: user_ids })
end

#assign_roles(members, message = nil) ⇒ Object



134
135
136
# File 'lib/stream-chat/channel.rb', line 134

def assign_roles(members, message = nil)
  @client.post(url, data: { assign_roles: members, message: message })
end

#ban_user(user_id, **options) ⇒ Object



155
156
157
# File 'lib/stream-chat/channel.rb', line 155

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

#create(user_id) ⇒ Object



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

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

#deleteObject



99
100
101
# File 'lib/stream-chat/channel.rb', line 99

def delete
  @client.delete(url)
end

#delete_file(url) ⇒ Object



179
180
181
# File 'lib/stream-chat/channel.rb', line 179

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

#delete_image(url) ⇒ Object



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

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

#delete_reaction(message_id, reaction_type, user_id) ⇒ Object



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

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



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

def demote_moderators(user_ids)
  @client.post(url, data: { demote_moderators: user_ids })
end

#get_messages(message_ids) ⇒ Object



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

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

#get_reactions(message_id, **options) ⇒ Object



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

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

#get_replies(parent_id, **options) ⇒ Object



147
148
149
# File 'lib/stream-chat/channel.rb', line 147

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

#hide(user_id) ⇒ Object



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

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

#invite_members(user_ids) ⇒ Object



122
123
124
# File 'lib/stream-chat/channel.rb', line 122

def invite_members(user_ids)
  @client.post(url, data: { invites: user_ids })
end

#mark_read(user_id, **options) ⇒ Object



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

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



107
108
109
110
111
# File 'lib/stream-chat/channel.rb', line 107

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



59
60
61
62
63
64
65
66
67
# File 'lib/stream-chat/channel.rb', line 59

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/stream-chat/channel.rb', line 69

def query_members(filter_conditions = {}, sort: nil, **options)
  params = {}.merge(options).merge({
                                     id: @id,
                                     type: @channel_type,
                                     filter_conditions: filter_conditions,
                                     sort: 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

#remove_members(user_ids) ⇒ Object



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

def remove_members(user_ids)
  @client.post(url, data: { remove_members: user_ids })
end

#send_event(event, user_id) ⇒ Object



37
38
39
40
# File 'lib/stream-chat/channel.rb', line 37

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



171
172
173
# File 'lib/stream-chat/channel.rb', line 171

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



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

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



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

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



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

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



167
168
169
# File 'lib/stream-chat/channel.rb', line 167

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

#truncate(**options) ⇒ Object



103
104
105
# File 'lib/stream-chat/channel.rb', line 103

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

#unban_user(user_id) ⇒ Object



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

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

#unmute(user_id) ⇒ Object



113
114
115
# File 'lib/stream-chat/channel.rb', line 113

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

#update(channel_data, update_message = nil) ⇒ Object



87
88
89
90
# File 'lib/stream-chat/channel.rb', line 87

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

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



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

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



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

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

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