Class: StreamChat::Client

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

Constant Summary collapse

DEFAULT_BASE_URL =
'https://chat.stream-io-api.com'
DEFAULT_TIMEOUT =
6.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret, timeout = nil, **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)

    your application api_key

  • api_secret (string)

    your application secret

  • timeout (float) (defaults to: nil)

    the timeout for the http requests

  • options (hash)

    extra options such as base_url

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/stream-chat/client.rb', line 40

def initialize(api_key, api_secret, timeout = nil, **options)
  raise ArgumentError, 'api_key and api_secret are required' if api_key.to_s.empty? || api_secret.to_s.empty?

  @api_key = api_key
  @api_secret = api_secret
  @timeout = timeout || DEFAULT_TIMEOUT
  @options = options
  @auth_token = JWT.encode({ server: true }, @api_secret, 'HS256')
  @base_url = options[:base_url] || DEFAULT_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_persistent, pool_size: 5 do |http|
      # AWS load balancer idle timeout is 60 secs, so let's make it 59
      http.idle_timeout = 59
    end
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



25
26
27
# File 'lib/stream-chat/client.rb', line 25

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.from_env(**options) ⇒ Object

initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET environmental variables. STREAM_CHAT_TIMEOUT and STREAM_CHAT_URL variables are optional.

Parameters:

  • options (hash)

    extra options



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

def self.from_env(**options)
  Client.new(ENV['STREAM_KEY'],
             ENV['STREAM_SECRET'],
             ENV['STREAM_CHAT_TIMEOUT'],
             **{ base_url: ENV['STREAM_CHAT_URL'] }.merge(options))
end

Instance Method Details

#add_device(device_id, push_provider, user_id) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/stream-chat/client.rb', line 308

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



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

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:



304
305
306
# File 'lib/stream-chat/client.rb', line 304

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

#check_push(push_data) ⇒ Object



458
459
460
# File 'lib/stream-chat/client.rb', line 458

def check_push(push_data)
  post('check_push', data: push_data)
end

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



462
463
464
# File 'lib/stream-chat/client.rb', line 462

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



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

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

#create_channel_type(data) ⇒ Object



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

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



466
467
468
# File 'lib/stream-chat/client.rb', line 466

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

#create_guest(user) ⇒ Object



352
353
354
# File 'lib/stream-chat/client.rb', line 352

def create_guest(user)
  post('guests', data: user)
end

#create_permission(permission) ⇒ Object



494
495
496
# File 'lib/stream-chat/client.rb', line 494

def create_permission(permission)
  post('permissions', data: permission)
end

#create_role(name) ⇒ Object



506
507
508
# File 'lib/stream-chat/client.rb', line 506

def create_role(name)
  post('roles', data: { name: name })
end

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



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

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



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

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

#delete(relative_url, params: nil) ⇒ Object



432
433
434
# File 'lib/stream-chat/client.rb', line 432

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

#delete_blocklist(name) ⇒ Object



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

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

#delete_channel_type(channel_type) ⇒ Object



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

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

#delete_channels(cids, hard_delete: false) ⇒ Object



392
393
394
# File 'lib/stream-chat/client.rb', line 392

def delete_channels(cids, hard_delete: false)
  post('channels/delete', data: { cids: cids, hard_delete: hard_delete })
end

#delete_command(name) ⇒ Object



478
479
480
# File 'lib/stream-chat/client.rb', line 478

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

#delete_device(device_id, user_id) ⇒ Object



316
317
318
# File 'lib/stream-chat/client.rb', line 316

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

#delete_message(message_id) ⇒ Object



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

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

#delete_permission(id) ⇒ Object



502
503
504
# File 'lib/stream-chat/client.rb', line 502

def delete_permission(id)
  delete("permissions/#{id}")
end

#delete_role(name) ⇒ Object



510
511
512
# File 'lib/stream-chat/client.rb', line 510

def delete_role(name)
  delete("roles/#{name}")
end

#delete_user(user_id, **options) ⇒ Object



164
165
166
# File 'lib/stream-chat/client.rb', line 164

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

#delete_users(user_ids, user: SOFT_DELETE, messages: nil, conversations: nil) ⇒ Object



388
389
390
# File 'lib/stream-chat/client.rb', line 388

def delete_users(user_ids, user: SOFT_DELETE, messages: nil, conversations: nil)
  post('users/delete', data: { user_ids: user_ids, user: user, messages: messages, conversations: conversations })
end

#export_channels(*channels, **options) ⇒ Object



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

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

#export_user(user_id, **options) ⇒ Object



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

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

#flag_message(id, **options) ⇒ Object



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

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

#flag_user(id, **options) ⇒ Object



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

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

#get(relative_url, params: nil) ⇒ Object



428
429
430
# File 'lib/stream-chat/client.rb', line 428

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

#get_app_settingsObject



89
90
91
# File 'lib/stream-chat/client.rb', line 89

def get_app_settings
  get('app')
end

#get_blocklist(name) ⇒ Object



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

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

#get_channel_type(channel_type) ⇒ Object



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

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

#get_command(name) ⇒ Object



470
471
472
# File 'lib/stream-chat/client.rb', line 470

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

#get_devices(user_id) ⇒ Object



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

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

#get_export_channel_status(task_id) ⇒ Object



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

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

#get_message(id) ⇒ Object



120
121
122
# File 'lib/stream-chat/client.rb', line 120

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

#get_permission(id) ⇒ Object



490
491
492
# File 'lib/stream-chat/client.rb', line 490

def get_permission(id)
  get("permissions/#{id}")
end

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



324
325
326
327
328
329
330
331
332
333
# File 'lib/stream-chat/client.rb', line 324

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

#get_task(task_id) ⇒ Object



384
385
386
# File 'lib/stream-chat/client.rb', line 384

def get_task(task_id)
  get("tasks/#{task_id}")
end

#list_blocklistsObject



356
357
358
# File 'lib/stream-chat/client.rb', line 356

def list_blocklists
  get('blocklists')
end

#list_channel_typesObject



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

def list_channel_types
  get('channeltypes')
end

#list_commandsObject



482
483
484
# File 'lib/stream-chat/client.rb', line 482

def list_commands
  get('commands')
end

#list_permissionsObject



486
487
488
# File 'lib/stream-chat/client.rb', line 486

def list_permissions
  get('permissions')
end

#list_rolesObject



514
515
516
# File 'lib/stream-chat/client.rb', line 514

def list_roles
  get('roles')
end

#mark_all_read(user_id) ⇒ Object



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

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

#mute_user(target_id, user_id) ⇒ Object



200
201
202
203
# File 'lib/stream-chat/client.rb', line 200

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



436
437
438
# File 'lib/stream-chat/client.rb', line 436

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

#pin_message(message_id, user_id, expiration: nil) ⇒ Object



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

def pin_message(message_id, user_id, expiration: nil)
  updates = {
    set: {
      pinned: true,
      pin_expires: expiration
    }
  }
  update_message_partial(message_id, updates, user_id: user_id)
end

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



424
425
426
# File 'lib/stream-chat/client.rb', line 424

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



420
421
422
# File 'lib/stream-chat/client.rb', line 420

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

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



250
251
252
253
254
255
256
# File 'lib/stream-chat/client.rb', line 250

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

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



266
267
268
269
270
271
272
273
# File 'lib/stream-chat/client.rb', line 266

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



103
104
105
106
107
108
# File 'lib/stream-chat/client.rb', line 103

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



258
259
260
261
262
263
264
# File 'lib/stream-chat/client.rb', line 258

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



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

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

#remove_shadow_ban(target_id, **options) ⇒ Object



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

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

#revoke_tokens(before) ⇒ Object



396
397
398
399
# File 'lib/stream-chat/client.rb', line 396

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



401
402
403
# File 'lib/stream-chat/client.rb', line 401

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

#revoke_users_token(user_ids, before) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/stream-chat/client.rb', line 405

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

#run_message_action(message_id, data) ⇒ Object



348
349
350
# File 'lib/stream-chat/client.rb', line 348

def run_message_action(message_id, data)
  post("messages/#{message_id}/action", data: data)
end

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

Raises:

  • (ArgumentError)


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

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



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/stream-chat/client.rb', line 440

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

  body = { user: user.to_json }

  body[:file] = Faraday::UploadIO.new(file_url, 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

#send_user_event(user_id, event) ⇒ Object



340
341
342
# File 'lib/stream-chat/client.rb', line 340

def send_user_event(user_id, event)
  post("users/#{user_id}/event", data: event)
end

#set_http_client(client) ⇒ Object

Sets the underlying Faraday http client.

Parameters:

  • an (client)

    instance of Faraday::Connection



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

def set_http_client(client)
  @conn = client
end

#shadow_ban(target_id, **options) ⇒ Object



190
191
192
193
# File 'lib/stream-chat/client.rb', line 190

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

#translate_message(message_id, language) ⇒ Object



344
345
346
# File 'lib/stream-chat/client.rb', line 344

def translate_message(message_id, language)
  post("messages/#{message_id}/translate", data: { language: language })
end

#unban_user(target_id, **options) ⇒ Object



185
186
187
188
# File 'lib/stream-chat/client.rb', line 185

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



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

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

#unflag_user(id, **options) ⇒ Object



115
116
117
118
# File 'lib/stream-chat/client.rb', line 115

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



205
206
207
208
# File 'lib/stream-chat/client.rb', line 205

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

#unpin_message(message_id, user_id) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/stream-chat/client.rb', line 225

def unpin_message(message_id, user_id)
  updates = {
    set: {
      pinned: false
    }
  }
  update_message_partial(message_id, updates, user_id: user_id)
end

#update_app_settings(**settings) ⇒ Object



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

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

#update_blocklist(name, words) ⇒ Object



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

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

#update_channel_type(channel_type, **options) ⇒ Object



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

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

#update_command(name, command) ⇒ Object



474
475
476
# File 'lib/stream-chat/client.rb', line 474

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

#update_message(message) ⇒ Object



234
235
236
237
238
# File 'lib/stream-chat/client.rb', line 234

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

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

#update_message_partial(message_id, updates, user_id: nil, **options) ⇒ Object



240
241
242
243
244
# File 'lib/stream-chat/client.rb', line 240

def update_message_partial(message_id, updates, user_id: nil, **options)
  params = updates.merge(options)
  params['user'] = { id: user_id } if user_id
  put("messages/#{message_id}", data: params)
end

#update_permission(id, permission) ⇒ Object



498
499
500
# File 'lib/stream-chat/client.rb', line 498

def update_permission(id, permission)
  put("permissions/#{id}", data: permission)
end

#update_user(user) ⇒ Object



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

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

#update_user_partial(update) ⇒ Object



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

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

#update_users(users) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/stream-chat/client.rb', line 141

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



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

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

#verify_webhook(request_body, x_signature) ⇒ Object



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

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