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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stream-chat/client.rb', line 38

def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
  @api_key = api_key || ENV['STREAM_KEY']
  @api_secret = api_secret || ENV['STREAM_SECRET']
  @timeout = timeout || ENV['STREAM_CHAT_TIMEOUT']
  @options = options
  @auth_token = JWT.encode({ server: true }, @api_secret, 'HS256')
  @base_url = options[:base_url] || ENV['STREAM_CHAT_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.



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

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#add_device(device_id, push_provider, user_id) ⇒ Object



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

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



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

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:



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

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

#check_push(push_data) ⇒ Object



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

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

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



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

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



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

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

#create_channel_type(data) ⇒ Object



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

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



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

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

#create_guest(user) ⇒ Object



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

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

#create_permission(permission) ⇒ Object



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

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

#create_role(name) ⇒ Object



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

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

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



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

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



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

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

#delete(relative_url, params: nil) ⇒ Object



407
408
409
# File 'lib/stream-chat/client.rb', line 407

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

#delete_blocklist(name) ⇒ Object



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

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

#delete_channel_type(channel_type) ⇒ Object



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

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

#delete_channels(cids, hard_delete: false) ⇒ Object



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

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

#delete_command(name) ⇒ Object



453
454
455
# File 'lib/stream-chat/client.rb', line 453

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

#delete_device(device_id, user_id) ⇒ Object



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

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

#delete_message(message_id) ⇒ Object



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

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

#delete_permission(id) ⇒ Object



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

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

#delete_role(name) ⇒ Object



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

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

#delete_user(user_id, **options) ⇒ Object



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

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



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

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



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

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

#export_user(user_id, **options) ⇒ Object



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

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

#flag_message(id, **options) ⇒ Object



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

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

#flag_user(id, **options) ⇒ Object



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

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

#get(relative_url, params: nil) ⇒ Object



403
404
405
# File 'lib/stream-chat/client.rb', line 403

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

#get_app_settingsObject



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

def get_app_settings
  get('app')
end

#get_blocklist(name) ⇒ Object



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

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

#get_channel_type(channel_type) ⇒ Object



255
256
257
# File 'lib/stream-chat/client.rb', line 255

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

#get_command(name) ⇒ Object



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

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

#get_devices(user_id) ⇒ Object



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

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

#get_export_channel_status(task_id) ⇒ Object



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

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

#get_message(id) ⇒ Object



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

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

#get_permission(id) ⇒ Object



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

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

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



299
300
301
302
303
304
305
306
307
308
# File 'lib/stream-chat/client.rb', line 299

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



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

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

#list_blocklistsObject



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

def list_blocklists
  get('blocklists')
end

#list_channel_typesObject



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

def list_channel_types
  get('channeltypes')
end

#list_commandsObject



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

def list_commands
  get('commands')
end

#list_permissionsObject



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

def list_permissions
  get('permissions')
end

#list_rolesObject



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

def list_roles
  get('roles')
end

#mark_all_read(user_id) ⇒ Object



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

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

#mute_user(target_id, user_id) ⇒ Object



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

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



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

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



190
191
192
193
194
195
196
197
198
# File 'lib/stream-chat/client.rb', line 190

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#remove_shadow_ban(target_id, **options) ⇒ Object



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

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



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

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



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

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

#revoke_users_token(user_ids, before) ⇒ Object



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

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



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

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)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stream-chat/client.rb', line 99

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



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/stream-chat/client.rb', line 415

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



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

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

#shadow_ban(target_id, **options) ⇒ Object



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

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



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

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

#unban_user(target_id, **options) ⇒ Object



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

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



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

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

#unflag_user(id, **options) ⇒ Object



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

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



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

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



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

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



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

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

#update_blocklist(name, words) ⇒ Object



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

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

#update_channel_type(channel_type, **options) ⇒ Object



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

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

#update_command(name, command) ⇒ Object



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

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

#update_message(message) ⇒ Object



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

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



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

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



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

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

#update_user(user) ⇒ Object



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

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

#update_user_partial(update) ⇒ Object



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

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

#update_users(users) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/stream-chat/client.rb', line 116

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



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

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

#verify_webhook(request_body, x_signature) ⇒ Object



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

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