Class: StreamChat::Client

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

Constant Summary collapse

DEFAULT_BASE_URL =

For now we disable runtime type checks. We will enable it with a major bump in the future, but for now, let's just run a static type check.

'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

Returns a new instance of Client.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stream-chat/client.rb', line 53

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 = T.let(timeout&.to_f || DEFAULT_TIMEOUT, Float)
  @auth_token = T.let(JWT.encode({ server: true }, @api_secret, 'HS256'), String)
  @base_url = T.let(options[:base_url] || DEFAULT_BASE_URL, String)
  conn = Faraday.new(@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
  @conn = T.let(conn, Faraday::Connection)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



34
35
36
# File 'lib/stream-chat/client.rb', line 34

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



40
41
42
# File 'lib/stream-chat/client.rb', line 40

def conn
  @conn
end

Class Method Details

.from_env(**options) ⇒ Object



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

def self.from_env(**options)
  Client.new(T.must(ENV['STREAM_KEY']),
             T.must(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



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

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



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

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



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

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

#check_push(push_data) ⇒ Object



541
542
543
# File 'lib/stream-chat/client.rb', line 541

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

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



546
547
548
# File 'lib/stream-chat/client.rb', line 546

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



430
431
432
# File 'lib/stream-chat/client.rb', line 430

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

#create_channel_type(data) ⇒ Object



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

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



551
552
553
# File 'lib/stream-chat/client.rb', line 551

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

#create_guest(user) ⇒ Object



415
416
417
# File 'lib/stream-chat/client.rb', line 415

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

#create_permission(permission) ⇒ Object



586
587
588
# File 'lib/stream-chat/client.rb', line 586

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

#create_role(name) ⇒ Object



601
602
603
# File 'lib/stream-chat/client.rb', line 601

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

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



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

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



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

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

#delete(relative_url, params: nil) ⇒ Object



512
513
514
# File 'lib/stream-chat/client.rb', line 512

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

#delete_blocklist(name) ⇒ Object



440
441
442
# File 'lib/stream-chat/client.rb', line 440

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

#delete_channel_type(channel_type) ⇒ Object



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

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

#delete_channels(cids, hard_delete: false) ⇒ Object



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

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

#delete_command(name) ⇒ Object



566
567
568
# File 'lib/stream-chat/client.rb', line 566

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

#delete_device(device_id, user_id) ⇒ Object



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

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

#delete_message(message_id, **options) ⇒ Object



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

def delete_message(message_id, **options)
  delete("messages/#{message_id}", params: options)
end

#delete_permission(id) ⇒ Object



596
597
598
# File 'lib/stream-chat/client.rb', line 596

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

#delete_role(name) ⇒ Object



606
607
608
# File 'lib/stream-chat/client.rb', line 606

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

#delete_user(user_id, **options) ⇒ Object



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

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



460
461
462
# File 'lib/stream-chat/client.rb', line 460

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



445
446
447
# File 'lib/stream-chat/client.rb', line 445

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

#export_user(user_id, **options) ⇒ Object



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

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

#flag_message(id, **options) ⇒ Object



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

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

#flag_user(id, **options) ⇒ Object



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

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

#get(relative_url, params: nil) ⇒ Object



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

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

#get_app_settingsObject



107
108
109
# File 'lib/stream-chat/client.rb', line 107

def get_app_settings
  get('app')
end

#get_blocklist(name) ⇒ Object



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

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

#get_channel_type(channel_type) ⇒ Object



330
331
332
# File 'lib/stream-chat/client.rb', line 330

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

#get_command(name) ⇒ Object



556
557
558
# File 'lib/stream-chat/client.rb', line 556

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

#get_devices(user_id) ⇒ Object



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

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

#get_export_channel_status(task_id) ⇒ Object



450
451
452
# File 'lib/stream-chat/client.rb', line 450

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

#get_message(id) ⇒ Object



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

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

#get_permission(id) ⇒ Object



581
582
583
# File 'lib/stream-chat/client.rb', line 581

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

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



382
383
384
385
386
387
388
389
390
391
# File 'lib/stream-chat/client.rb', line 382

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



455
456
457
# File 'lib/stream-chat/client.rb', line 455

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

#list_blocklistsObject



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

def list_blocklists
  get('blocklists')
end

#list_channel_typesObject



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

def list_channel_types
  get('channeltypes')
end

#list_commandsObject



571
572
573
# File 'lib/stream-chat/client.rb', line 571

def list_commands
  get('commands')
end

#list_permissionsObject



576
577
578
# File 'lib/stream-chat/client.rb', line 576

def list_permissions
  get('permissions')
end

#list_rolesObject



611
612
613
# File 'lib/stream-chat/client.rb', line 611

def list_roles
  get('roles')
end

#mark_all_read(user_id) ⇒ Object



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

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

#mute_user(target_id, user_id) ⇒ Object



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

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



517
518
519
# File 'lib/stream-chat/client.rb', line 517

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



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

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



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

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



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

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



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

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

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



314
315
316
317
318
319
320
321
# File 'lib/stream-chat/client.rb', line 314

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: StreamChat.get_sort_fields(sort)
                                   })
  post('channels', data: data)
end

#query_message_flags(filter_conditions, **options) ⇒ Object



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

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



305
306
307
308
309
310
311
# File 'lib/stream-chat/client.rb', line 305

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

#reactivate_user(user_id, **options) ⇒ Object



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

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

#remove_shadow_ban(target_id, **options) ⇒ Object



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

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



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

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

#revoke_user_token(user_id, before) ⇒ Object



476
477
478
# File 'lib/stream-chat/client.rb', line 476

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

#revoke_users_token(user_ids, before) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/stream-chat/client.rb', line 481

def revoke_users_token(user_ids, before)
  before = T.cast(before, DateTime).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



410
411
412
# File 'lib/stream-chat/client.rb', line 410

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)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/stream-chat/client.rb', line 149

def search(filter_conditions, query, sort: nil, **options)
  offset = T.cast(options[:offset], T.nilable(Integer))
  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: StreamChat.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 = nil) ⇒ Object



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/stream-chat/client.rb', line 522

def send_file(relative_url, file_url, user, content_type = nil)
  url = [@base_url, relative_url].join('/')

  body = { user: user.to_json }

  body[:file] = Faraday::UploadIO.new(file_url, content_type || 'application/octet-stream')

  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



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

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

#set_http_client(client) ⇒ Object



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

def set_http_client(client)
  @conn = client
end

#shadow_ban(target_id, **options) ⇒ Object



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

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



405
406
407
# File 'lib/stream-chat/client.rb', line 405

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

#unban_user(target_id, **options) ⇒ Object



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

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



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

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

#unflag_user(id, **options) ⇒ Object



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

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



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

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



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

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



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

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

#update_blocklist(name, words) ⇒ Object



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

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

#update_channel_type(channel_type, **options) ⇒ Object



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

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

#update_command(name, command) ⇒ Object



561
562
563
# File 'lib/stream-chat/client.rb', line 561

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

#update_message(message) ⇒ Object

Raises:

  • (ArgumentError)


277
278
279
280
281
# File 'lib/stream-chat/client.rb', line 277

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



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

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



591
592
593
# File 'lib/stream-chat/client.rb', line 591

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

#update_user(user) ⇒ Object



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

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

#update_user_partial(update) ⇒ Object



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

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

#update_users(users) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/stream-chat/client.rb', line 167

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



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

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

#verify_webhook(request_body, x_signature) ⇒ Object



394
395
396
397
# File 'lib/stream-chat/client.rb', line 394

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