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



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

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.



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

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#add_device(device_id, push_provider, user_id) ⇒ Object



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

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



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

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:



270
271
272
# File 'lib/stream-chat/client.rb', line 270

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



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

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



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

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

#create_channel_type(data) ⇒ Object



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

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



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

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

#create_permission(permission) ⇒ Object



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

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

#create_role(name) ⇒ Object



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

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

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



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

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



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

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

#delete(relative_url, params: nil) ⇒ Object



382
383
384
# File 'lib/stream-chat/client.rb', line 382

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

#delete_blocklist(name) ⇒ Object



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

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

#delete_channel_type(channel_type) ⇒ Object



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

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

#delete_channels(cids, hard_delete: false) ⇒ Object



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

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

#delete_command(name) ⇒ Object



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

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

#delete_device(device_id, user_id) ⇒ Object



282
283
284
# File 'lib/stream-chat/client.rb', line 282

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

#delete_message(message_id) ⇒ Object



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

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

#delete_permission(id) ⇒ Object



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

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

#delete_role(name) ⇒ Object



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

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

#delete_user(user_id, **options) ⇒ Object



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

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



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

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



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

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

#export_user(user_id, **options) ⇒ Object



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

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

#flag_message(id, **options) ⇒ Object



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

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

#flag_user(id, **options) ⇒ Object



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

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

#get(relative_url, params: nil) ⇒ Object



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

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

#get_app_settingsObject



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

def get_app_settings
  get('app')
end

#get_blocklist(name) ⇒ Object



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

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

#get_channel_type(channel_type) ⇒ Object



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

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

#get_command(name) ⇒ Object



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

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

#get_devices(user_id) ⇒ Object



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

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

#get_export_channel_status(task_id) ⇒ Object



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

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

#get_message(id) ⇒ Object



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

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

#get_permission(id) ⇒ Object



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

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

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



290
291
292
293
294
295
296
297
298
299
# File 'lib/stream-chat/client.rb', line 290

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



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

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

#list_blocklistsObject



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

def list_blocklists
  get('blocklists')
end

#list_channel_typesObject



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

def list_channel_types
  get('channeltypes')
end

#list_commandsObject



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

def list_commands
  get('commands')
end

#list_permissionsObject



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

def list_permissions
  get('permissions')
end

#list_rolesObject



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

def list_roles
  get('roles')
end

#mark_all_read(user_id) ⇒ Object



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

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

#mute_user(target_id, user_id) ⇒ Object



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

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



386
387
388
# File 'lib/stream-chat/client.rb', line 386

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#remove_shadow_ban(target_id, **options) ⇒ Object



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

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



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

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



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

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

#revoke_users_token(user_ids, before) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/stream-chat/client.rb', line 355

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)


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

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



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/stream-chat/client.rb', line 390

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

#shadow_ban(target_id, **options) ⇒ Object



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

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

#unban_user(target_id, **options) ⇒ Object



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

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



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

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

#unflag_user(id, **options) ⇒ Object



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

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



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

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



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

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



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

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

#update_blocklist(name, words) ⇒ Object



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

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

#update_channel_type(channel_type, **options) ⇒ Object



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

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

#update_command(name, command) ⇒ Object



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

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

#update_message(message) ⇒ Object



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

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



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

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



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

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

#update_user(user) ⇒ Object



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

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

#update_user_partial(update) ⇒ Object



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

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

#update_users(users) ⇒ Object



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

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



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

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

#verify_webhook(request_body, x_signature) ⇒ Object



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

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