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
# 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
  @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) ⇒ Object



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

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

#add_moderators(user_ids) ⇒ Object



110
111
112
# File 'lib/stream-chat/channel.rb', line 110

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

#ban_user(user_id, **options) ⇒ Object



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

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

#create(user_id) ⇒ Object



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

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

#deleteObject



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

def delete
  @client.delete(url)
end

#delete_file(url) ⇒ Object



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

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

#delete_image(url) ⇒ Object



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

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

#delete_reaction(message_id, reaction_type, user_id) ⇒ Object



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

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



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

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

#get_reactions(message_id, **options) ⇒ Object



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

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

#get_replies(parent_id, **options) ⇒ Object



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

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

#hide(user_id) ⇒ Object



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

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

#invite_members(user_ids) ⇒ Object



106
107
108
# File 'lib/stream-chat/channel.rb', line 106

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

#mark_read(user_id, **options) ⇒ Object



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

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

#query(**options) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/stream-chat/channel.rb', line 54

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stream-chat/channel.rb', line 64

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



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

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

#send_event(event, user_id) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#truncateObject



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

def truncate
  @client.post("#{url}/truncate")
end

#unban_user(user_id) ⇒ Object



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

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

#update(channel_data, update_message = nil) ⇒ Object



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

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



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

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



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

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

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