Class: StreamChat::Client

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

Constant Summary collapse

BASE_URL =
'https://chat.stream-io-api.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = '', api_secret = '', timeout = 6.0, **options) ⇒ Client

initializes a Stream Chat API Client

Examples:

initialized the client with a timeout setting

StreamChat::Client.new('my_key', 'my_secret', 3.0)

Parameters:

  • api_key (string) (defaults to: '')

    your application api_key

  • api_secret (string) (defaults to: '')

    your application secret

  • (string)
  • options (hash)

    extra options



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stream-chat/client.rb', line 34

def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
  @api_key = api_key
  @api_secret = api_secret
  @timeout = timeout
  @options = options
  @auth_token = JWT.encode({ server: true }, @api_secret, 'HS256')
  @base_url = options[:base_url] || BASE_URL
  @conn = Faraday.new(url: @base_url) do |faraday|
    faraday.options[:open_timeout] = @timeout
    faraday.options[:timeout] = @timeout
    faraday.request :multipart
    faraday.adapter :net_http
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



19
20
21
# File 'lib/stream-chat/client.rb', line 19

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/stream-chat/client.rb', line 22

def options
  @options
end

Instance Method Details

#add_device(device_id, push_provider, user_id) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/stream-chat/client.rb', line 236

def add_device(device_id, push_provider, user_id)
  post('devices', data: {
         id: device_id,
         push_provider: push_provider,
         user_id: user_id
       })
end

#ban_user(target_id, **options) ⇒ Object



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

def ban_user(target_id, **options)
  payload = { target_user_id: target_id }.merge(options)
  post('moderation/ban', data: payload)
end

#channel(channel_type, channel_id: nil, data: nil) ⇒ StreamChat::Channel

Creates a channel instance

Parameters:

  • channel_type (string)

    the channel type

  • channel_id (string) (defaults to: nil)

    the channel identifier

  • data (hash) (defaults to: nil)

    additional channel data

Returns:



232
233
234
# File 'lib/stream-chat/client.rb', line 232

def channel(channel_type, channel_id: nil, data: nil)
  StreamChat::Channel.new(self, channel_type, channel_id, data)
end

#check_sqs(sqs_key = nil, sqs_secret = nil, sqs_url = nil) ⇒ Object



359
360
361
# File 'lib/stream-chat/client.rb', line 359

def check_sqs(sqs_key = nil, sqs_secret = nil, sqs_url = nil)
  post('check_sqs', data: { sqs_key: sqs_key, sqs_secret: sqs_secret, sqs_url: sqs_url })
end

#create_blocklist(name, words) ⇒ Object



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

def create_blocklist(name, words)
  post('blocklists', data: { name: name, words: words })
end

#create_channel_type(data) ⇒ Object



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

def create_channel_type(data)
  data['commands'] = ['all'] unless data.key?('commands') || data['commands'].nil? || data['commands'].empty?
  post('channeltypes', data: data)
end

#create_command(command) ⇒ Object



363
364
365
# File 'lib/stream-chat/client.rb', line 363

def create_command(command)
  post('commands', data: command)
end

#create_token(user_id, exp = nil, iat = nil) ⇒ Object



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

def create_token(user_id, exp = nil, iat = nil)
  payload = { user_id: user_id }
  payload['exp'] = exp unless exp.nil?
  payload['iat'] = iat unless iat.nil?
  JWT.encode(payload, @api_secret, 'HS256')
end

#deactivate_user(user_id, **options) ⇒ Object



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

def deactivate_user(user_id, **options)
  post("users/#{user_id}/deactivate", **options)
end

#delete(relative_url, params: nil) ⇒ Object



332
333
334
# File 'lib/stream-chat/client.rb', line 332

def delete(relative_url, params: nil)
  make_http_request(:delete, relative_url, params: params)
end

#delete_blocklist(name) ⇒ Object



284
285
286
# File 'lib/stream-chat/client.rb', line 284

def delete_blocklist(name)
  delete("blocklists/#{name}")
end

#delete_channel_type(channel_type) ⇒ Object



220
221
222
# File 'lib/stream-chat/client.rb', line 220

def delete_channel_type(channel_type)
  delete("channeltypes/#{channel_type}")
end

#delete_command(name) ⇒ Object



375
376
377
# File 'lib/stream-chat/client.rb', line 375

def delete_command(name)
  delete("commands/#{name}")
end

#delete_device(device_id, user_id) ⇒ Object



244
245
246
# File 'lib/stream-chat/client.rb', line 244

def delete_device(device_id, user_id)
  delete('devices', params: { id: device_id, user_id: user_id })
end

#delete_message(message_id) ⇒ Object



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

def delete_message(message_id)
  delete("messages/#{message_id}")
end

#delete_user(user_id, **options) ⇒ Object



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

def delete_user(user_id, **options)
  delete("users/#{user_id}", params: options)
end

#export_channels(*channels) ⇒ Object



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

def export_channels(*channels)
  post('export_channels', data: { channels: channels })
end

#export_user(user_id, **options) ⇒ Object



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

def export_user(user_id, **options)
  get("users/#{user_id}/export", params: options)
end

#flag_message(id, **options) ⇒ Object



64
65
66
67
# File 'lib/stream-chat/client.rb', line 64

def flag_message(id, **options)
  payload = { target_message_id: id }.merge(options)
  post('moderation/flag', data: payload)
end

#flag_user(id, **options) ⇒ Object



81
82
83
84
# File 'lib/stream-chat/client.rb', line 81

def flag_user(id, **options)
  payload = { target_user_id: id }.merge(options)
  post('moderation/flag', data: payload)
end

#get(relative_url, params: nil) ⇒ Object



328
329
330
# File 'lib/stream-chat/client.rb', line 328

def get(relative_url, params: nil)
  make_http_request(:get, relative_url, params: params)
end

#get_app_settingsObject



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

def get_app_settings
  get('app')
end

#get_blocklist(name) ⇒ Object



272
273
274
# File 'lib/stream-chat/client.rb', line 272

def get_blocklist(name)
  get("blocklists/#{name}")
end

#get_channel_type(channel_type) ⇒ Object



208
209
210
# File 'lib/stream-chat/client.rb', line 208

def get_channel_type(channel_type)
  get("channeltypes/#{channel_type}")
end

#get_command(name) ⇒ Object



367
368
369
# File 'lib/stream-chat/client.rb', line 367

def get_command(name)
  get("commands/#{name}")
end

#get_devices(user_id) ⇒ Object



248
249
250
# File 'lib/stream-chat/client.rb', line 248

def get_devices(user_id)
  get('devices', params: { user_id: user_id })
end

#get_export_channel_status(task_id) ⇒ Object



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

def get_export_channel_status(task_id)
  get("export_channels/#{task_id}")
end

#get_message(id) ⇒ Object



91
92
93
# File 'lib/stream-chat/client.rb', line 91

def get_message(id)
  get("messages/#{id}")
end

#get_rate_limits(server_side: false, android: false, ios: false, web: false, endpoints: []) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/stream-chat/client.rb', line 252

def get_rate_limits(server_side: false, android: false, ios: false, web: false, endpoints: [])
  params = {}
  params['server_side'] = server_side if server_side
  params['android'] = android if android
  params['ios'] = ios if ios
  params['web'] = web if web
  params['endpoints'] = endpoints.join(',') unless endpoints.empty?

  get('rate_limits', params: params)
end

#list_blocklistsObject



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

def list_blocklists
  get('blocklists')
end

#list_channel_typesObject



212
213
214
# File 'lib/stream-chat/client.rb', line 212

def list_channel_types
  get('channeltypes')
end

#list_commandsObject



379
380
381
# File 'lib/stream-chat/client.rb', line 379

def list_commands
  get('commands')
end

#mark_all_read(user_id) ⇒ Object



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

def mark_all_read(user_id)
  payload = { user: { id: user_id } }
  post('channels/read', data: payload)
end

#mute_user(target_id, user_id) ⇒ Object



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

def mute_user(target_id, user_id)
  payload = { target_id: target_id, user_id: user_id }
  post('moderation/mute', data: payload)
end

#patch(relative_url, params: nil, data: nil) ⇒ Object



336
337
338
# File 'lib/stream-chat/client.rb', line 336

def patch(relative_url, params: nil, data: nil)
  make_http_request(:patch, relative_url, params: params, data: data)
end

#post(relative_url, params: nil, data: nil) ⇒ Object



324
325
326
# File 'lib/stream-chat/client.rb', line 324

def post(relative_url, params: nil, data: nil)
  make_http_request(:post, relative_url, params: params, data: data)
end

#put(relative_url, params: nil, data: nil) ⇒ Object



320
321
322
# File 'lib/stream-chat/client.rb', line 320

def put(relative_url, params: nil, data: nil)
  make_http_request(:put, relative_url, params: params, data: data)
end

#query_channels(filter_conditions, sort: nil, **options) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/stream-chat/client.rb', line 194

def query_channels(filter_conditions, sort: nil, **options)
  data = { state: true, watch: false, presence: false }
  data = data.merge(options).merge({
                                     filter_conditions: filter_conditions,
                                     sort: get_sort_fields(sort)
                                   })
  post('channels', data: data)
end

#query_message_flags(filter_conditions, **options) ⇒ Object



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

def query_message_flags(filter_conditions, **options)
  params = options.merge({
                           filter_conditions: filter_conditions
                         })
  get('moderation/flags/message', params: { payload: params.to_json })
end

#query_users(filter_conditions, sort: nil, **options) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/stream-chat/client.rb', line 186

def query_users(filter_conditions, sort: nil, **options)
  params = options.merge({
                           filter_conditions: filter_conditions,
                           sort: get_sort_fields(sort)
                         })
  get('users', params: { payload: params.to_json })
end

#reactivate_user(user_id, **options) ⇒ Object



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

def reactivate_user(user_id, **options)
  post("users/#{user_id}/reactivate", **options)
end

#revoke_tokens(before) ⇒ Object



296
297
298
299
# File 'lib/stream-chat/client.rb', line 296

def revoke_tokens(before)
  before = before.rfc3339 if before.instance_of?(DateTime)
  update_app_settings({ 'revoke_tokens_issued_before' => before })
end

#revoke_user_token(user_id, before) ⇒ Object



301
302
303
# File 'lib/stream-chat/client.rb', line 301

def revoke_user_token(user_id, before)
  revoke_users_token([user_id], before)
end

#revoke_users_token(user_ids, before) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/stream-chat/client.rb', line 305

def revoke_users_token(user_ids, before)
  before = before.rfc3339 if before.instance_of?(DateTime)

  updates = []
  user_ids.each do |user_id|
    updates.push({
                   'id' => user_id,
                   'set' => {
                     'revoke_tokens_issued_before' => before
                   }
                 })
  end
  update_users_partial(updates)
end

#search(filter_conditions, query, sort: nil, **options) ⇒ Object

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stream-chat/client.rb', line 95

def search(filter_conditions, query, sort: nil, **options)
  offset = options[:offset]
  next_value = options[:next]
  raise ArgumentError, 'cannot use offset with next or sort parameters' if offset&.positive? && (next_value || (!sort.nil? && !sort.empty?))

  to_merge = {
    filter_conditions: filter_conditions,
    sort: get_sort_fields(sort)
  }
  if query.is_a? String
    to_merge[:query] = query
  else
    to_merge[:message_filter_conditions] = query
  end
  get('search', params: { payload: options.merge(to_merge).to_json })
end

#send_file(relative_url, file_url, user, content_type = 'application/octet-stream') ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/stream-chat/client.rb', line 340

def send_file(relative_url, file_url, user, content_type = 'application/octet-stream')
  url = [@base_url, relative_url].join('/')

  file = open(file_url)
  body = { user: user.to_json }

  body[:file] = Faraday::UploadIO.new(file, content_type)

  response = @conn.post url do |req|
    req.headers['X-Stream-Client'] = get_user_agent
    req.headers['Authorization'] = @auth_token
    req.headers['stream-auth-type'] = 'jwt'
    req.params = get_default_params
    req.body = body
  end

  parse_response(response)
end

#unban_user(target_id, **options) ⇒ Object



156
157
158
159
# File 'lib/stream-chat/client.rb', line 156

def unban_user(target_id, **options)
  params = { target_user_id: target_id }.merge(options)
  delete('moderation/ban', params: params)
end

#unflag_message(id, **options) ⇒ Object



69
70
71
72
# File 'lib/stream-chat/client.rb', line 69

def unflag_message(id, **options)
  payload = { target_message_id: id }.merge(options)
  post('moderation/unflag', data: payload)
end

#unflag_user(id, **options) ⇒ Object



86
87
88
89
# File 'lib/stream-chat/client.rb', line 86

def unflag_user(id, **options)
  payload = { target_user_id: id }.merge(options)
  post('moderation/unflag', data: payload)
end

#unmute_user(target_id, user_id) ⇒ Object



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

def unmute_user(target_id, user_id)
  payload = { target_id: target_id, user_id: user_id }
  post('moderation/unmute', data: payload)
end

#update_app_settings(**settings) ⇒ Object



56
57
58
# File 'lib/stream-chat/client.rb', line 56

def update_app_settings(**settings)
  patch('app', **settings)
end

#update_blocklist(name, words) ⇒ Object



280
281
282
# File 'lib/stream-chat/client.rb', line 280

def update_blocklist(name, words)
  put("blocklists/#{name}", data: { words: words })
end

#update_channel_type(channel_type, **options) ⇒ Object



216
217
218
# File 'lib/stream-chat/client.rb', line 216

def update_channel_type(channel_type, **options)
  put("channeltypes/#{channel_type}", data: options)
end

#update_command(name, command) ⇒ Object



371
372
373
# File 'lib/stream-chat/client.rb', line 371

def update_command(name, command)
  put("commands/#{name}", data: command)
end

#update_message(message) ⇒ Object



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

def update_message(message)
  raise ArgumentError 'message must have an id' unless message.key? 'id'

  post("messages/#{message['id']}", data: { message: message })
end

#update_user(user) ⇒ Object



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

def update_user(user)
  update_users([user])
end

#update_user_partial(update) ⇒ Object



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

def update_user_partial(update)
  update_users_partial([update])
end

#update_users(users) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/stream-chat/client.rb', line 112

def update_users(users)
  payload = {}
  users.each do |user|
    id = user[:id] || user['id']
    raise ArgumentError, 'user must have an id' unless id

    payload[id] = user
  end
  post('users', data: { users: payload })
end

#update_users_partial(updates) ⇒ Object



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

def update_users_partial(updates)
  patch('users', data: { users: updates })
end

#verify_webhook(request_body, x_signature) ⇒ Object



263
264
265
266
# File 'lib/stream-chat/client.rb', line 263

def verify_webhook(request_body, x_signature)
  signature = OpenSSL::HMAC.hexdigest('SHA256', @api_secret, request_body)
  signature == x_signature
end