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 =
'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)


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

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)
  @moderation = T.let(Moderation.new(self), Moderation)
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

#moderationObject (readonly)

Returns the value of attribute moderation.



43
44
45
# File 'lib/stream-chat/client.rb', line 43

def moderation
  @moderation
end

Class Method Details

.from_env(**options) ⇒ Object



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

def self.from_env(**options)
  Client.new(ENV.fetch('STREAM_KEY'),
             ENV.fetch('STREAM_SECRET'),
             ENV.fetch('STREAM_CHAT_TIMEOUT', DEFAULT_TIMEOUT),
             base_url: ENV.fetch('STREAM_CHAT_URL', DEFAULT_BASE_URL),
             **options)
end

Instance Method Details

#add_device(device_id, push_provider, user_id, push_provider_name = nil) ⇒ Object



666
667
668
669
670
671
672
673
# File 'lib/stream-chat/client.rb', line 666

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

#ban_user(target_id, **options) ⇒ Object



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

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

#campaign(campaign_id: nil, data: nil) ⇒ Object



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

def campaign(campaign_id: nil, data: nil)
  StreamChat::Campaign.new(self, campaign_id, data)
end

#channel(channel_type, channel_id: nil, data: nil) ⇒ Object



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

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

#channel_batch_updaterObject



1194
1195
1196
# File 'lib/stream-chat/client.rb', line 1194

def channel_batch_updater
  ChannelBatchUpdater.new(self)
end

#check_push(push_data) ⇒ Object



924
925
926
# File 'lib/stream-chat/client.rb', line 924

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

#check_sns(sns_key = nil, sns_secret = nil, sns_topic_arn = nil) ⇒ Object



940
941
942
# File 'lib/stream-chat/client.rb', line 940

def check_sns(sns_key = nil, sns_secret = nil, sns_topic_arn = nil)
  post('check_sns', data: { sns_key: sns_key, sns_secret: sns_secret, sns_topic_arn: sns_topic_arn })
end

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



932
933
934
# File 'lib/stream-chat/client.rb', line 932

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

#commit_message(message_id) ⇒ Object



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

def commit_message(message_id)
  post("messages/#{message_id}/commit")
end

#create_blocklist(name, words) ⇒ Object



771
772
773
# File 'lib/stream-chat/client.rb', line 771

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

#create_campaign(campaign_id: nil, data: nil) ⇒ Object



587
588
589
590
591
592
# File 'lib/stream-chat/client.rb', line 587

def create_campaign(campaign_id: nil, data: nil)
  payload = {}
  payload['id'] = campaign_id if campaign_id
  payload.merge!(data) if data
  post('campaigns', data: payload)
end

#create_channel_type(data) ⇒ Object



527
528
529
530
# File 'lib/stream-chat/client.rb', line 527

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



946
947
948
# File 'lib/stream-chat/client.rb', line 946

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

#create_guest(user) ⇒ Object



735
736
737
# File 'lib/stream-chat/client.rb', line 735

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

#create_import(path, mode) ⇒ Object



1099
1100
1101
# File 'lib/stream-chat/client.rb', line 1099

def create_import(path, mode)
  post('imports', data: { path: path, mode: mode })
end

#create_import_url(filename) ⇒ Object



1089
1090
1091
# File 'lib/stream-chat/client.rb', line 1089

def create_import_url(filename)
  post('import_urls', data: { filename: filename })
end

#create_permission(permission) ⇒ Object



1031
1032
1033
# File 'lib/stream-chat/client.rb', line 1031

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

#create_reminder(message_id, user_id, remind_at = nil) ⇒ Object



1131
1132
1133
1134
1135
# File 'lib/stream-chat/client.rb', line 1131

def create_reminder(message_id, user_id, remind_at = nil)
  data = { user_id: user_id }
  data[:remind_at] = StreamChat.normalize_timestamp(remind_at) if remind_at
  post("messages/#{message_id}/reminders", data: data)
end

#create_role(name) ⇒ Object



1049
1050
1051
# File 'lib/stream-chat/client.rb', line 1049

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

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



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

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



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

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

#deactivate_users(user_ids, **options) ⇒ Object

Raises:

  • (ArgumentError)


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

def deactivate_users(user_ids, **options)
  raise ArgumentError, 'user_ids should not be empty' if user_ids.empty?

  post('users/deactivate', data: { user_ids: user_ids, **options })
end

#delete(relative_url, params: nil) ⇒ Object



890
891
892
# File 'lib/stream-chat/client.rb', line 890

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

#delete_blocklist(name) ⇒ Object



795
796
797
# File 'lib/stream-chat/client.rb', line 795

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

#delete_campaign(campaign_id, **options) ⇒ Object



619
620
621
# File 'lib/stream-chat/client.rb', line 619

def delete_campaign(campaign_id, **options)
  delete("campaigns/#{campaign_id}", params: options)
end

#delete_channel_type(channel_type) ⇒ Object



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

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

#delete_channels(cids, hard_delete: false) ⇒ Object



840
841
842
# File 'lib/stream-chat/client.rb', line 840

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

#delete_command(name) ⇒ Object



1007
1008
1009
# File 'lib/stream-chat/client.rb', line 1007

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

#delete_device(device_id, user_id) ⇒ Object



677
678
679
# File 'lib/stream-chat/client.rb', line 677

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

#delete_message(message_id, **options) ⇒ Object



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

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

#delete_message_for_me(message_id, user_id) ⇒ Object

Raises:

  • (ArgumentError)


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

def delete_message_for_me(message_id, user_id)
  raise ArgumentError, 'user_id must not be empty for delete_for_me functionality' if user_id.to_s.empty?

  delete_message(message_id, delete_for_me: true, deleted_by: user_id)
end

#delete_message_with_options(message_id, hard: nil, delete_for_me: nil, user_id: nil) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/stream-chat/client.rb', line 452

def delete_message_with_options(message_id, hard: nil, delete_for_me: nil, user_id: nil)
  options = {}
  options[:hard] = true if hard

  if delete_for_me
    raise ArgumentError, 'user_id must not be empty for delete_for_me functionality' if user_id.to_s.empty?

    options[:delete_for_me] = true
    options[:deleted_by] = user_id
  end

  delete_message(message_id, **options)
end

#delete_permission(id) ⇒ Object



1043
1044
1045
# File 'lib/stream-chat/client.rb', line 1043

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

#delete_push_provider(type, name) ⇒ Object



1073
1074
1075
# File 'lib/stream-chat/client.rb', line 1073

def delete_push_provider(type, name)
  delete("push_providers/#{type}/#{name}")
end

#delete_reminder(message_id, user_id) ⇒ Object



1154
1155
1156
# File 'lib/stream-chat/client.rb', line 1154

def delete_reminder(message_id, user_id)
  delete("messages/#{message_id}/reminders", params: { user_id: user_id })
end

#delete_role(name) ⇒ Object



1055
1056
1057
# File 'lib/stream-chat/client.rb', line 1055

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

#delete_user(user_id, **options) ⇒ Object



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

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



833
834
835
# File 'lib/stream-chat/client.rb', line 833

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



805
806
807
# File 'lib/stream-chat/client.rb', line 805

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

#export_user(user_id, **options) ⇒ Object



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

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

#export_users(user_ids) ⇒ Object



821
822
823
# File 'lib/stream-chat/client.rb', line 821

def export_users(user_ids)
  post('export/users', data: { user_ids: user_ids })
end

#flag_message(id, **options) ⇒ Object



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

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

#flag_user(id, **options) ⇒ Object



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

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

#get(relative_url, params: nil) ⇒ Object



885
886
887
# File 'lib/stream-chat/client.rb', line 885

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

#get_active_live_locations(user_id) ⇒ Object



970
971
972
# File 'lib/stream-chat/client.rb', line 970

def get_active_live_locations(user_id)
  get('users/live_locations', params: { user_id: user_id })
end

#get_app_settingsObject



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

def get_app_settings
  get('app')
end

#get_blocklist(name) ⇒ Object



759
760
761
# File 'lib/stream-chat/client.rb', line 759

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

#get_campaign(campaign_id) ⇒ Object



599
600
601
# File 'lib/stream-chat/client.rb', line 599

def get_campaign(campaign_id)
  get("campaigns/#{campaign_id}")
end

#get_channel_type(channel_type) ⇒ Object



534
535
536
# File 'lib/stream-chat/client.rb', line 534

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

#get_command(name) ⇒ Object



995
996
997
# File 'lib/stream-chat/client.rb', line 995

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

#get_devices(user_id) ⇒ Object



683
684
685
# File 'lib/stream-chat/client.rb', line 683

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

#get_export_channel_status(task_id) ⇒ Object



811
812
813
# File 'lib/stream-chat/client.rb', line 811

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

#get_import(id) ⇒ Object



1105
1106
1107
# File 'lib/stream-chat/client.rb', line 1105

def get_import(id)
  get("imports/#{id}")
end

#get_message(id, **options) ⇒ Object



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

def get_message(id, **options)
  get("messages/#{id}", params: options)
end

#get_permission(id) ⇒ Object



1025
1026
1027
# File 'lib/stream-chat/client.rb', line 1025

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

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



690
691
692
693
694
695
696
697
698
699
# File 'lib/stream-chat/client.rb', line 690

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



827
828
829
# File 'lib/stream-chat/client.rb', line 827

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

#hard_delete_message(message_id) ⇒ Object



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

def hard_delete_message(message_id)
  delete_message(message_id, hard: true)
end

#list_blocklistsObject



747
748
749
# File 'lib/stream-chat/client.rb', line 747

def list_blocklists
  get('blocklists')
end

#list_channel_typesObject



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

def list_channel_types
  get('channeltypes')
end

#list_commandsObject



1013
1014
1015
# File 'lib/stream-chat/client.rb', line 1013

def list_commands
  get('commands')
end

#list_imports(options) ⇒ Object



1111
1112
1113
# File 'lib/stream-chat/client.rb', line 1111

def list_imports(options)
  get('imports', params: options)
end

#list_permissionsObject



1019
1020
1021
# File 'lib/stream-chat/client.rb', line 1019

def list_permissions
  get('permissions')
end

#list_push_providersObject



1079
1080
1081
# File 'lib/stream-chat/client.rb', line 1079

def list_push_providers
  get('push_providers')
end

#list_rolesObject



1061
1062
1063
# File 'lib/stream-chat/client.rb', line 1061

def list_roles
  get('roles')
end

#mark_all_read(user_id) ⇒ Object



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

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

#mark_delivered(data = nil, user_id: nil) ⇒ Object



1179
1180
1181
# File 'lib/stream-chat/client.rb', line 1179

def mark_delivered(data = nil, user_id: nil)
  post('channels/delivered', data: data || {}, params: { user_id: user_id })
end

#mute_user(target_id, user_id) ⇒ Object



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

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



895
896
897
# File 'lib/stream-chat/client.rb', line 895

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



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

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



880
881
882
# File 'lib/stream-chat/client.rb', line 880

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



875
876
877
# File 'lib/stream-chat/client.rb', line 875

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



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

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_campaigns(filter_conditions, sort: nil, **options) ⇒ Object



656
657
658
659
660
661
662
# File 'lib/stream-chat/client.rb', line 656

def query_campaigns(filter_conditions, sort: nil, **options)
  data = options.merge({
                         filter: filter_conditions,
                         sort: StreamChat.get_sort_fields(sort)
                       })
  post('campaigns/query', data: data)
end

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



516
517
518
519
520
521
522
523
# File 'lib/stream-chat/client.rb', line 516

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_drafts(user_id, filter: nil, sort: nil, **options) ⇒ Object



958
959
960
961
962
963
964
# File 'lib/stream-chat/client.rb', line 958

def query_drafts(user_id, filter: nil, sort: nil, **options)
  data = { user_id: user_id }
  data['filter'] = filter if filter
  data['sort'] = sort if sort
  data.merge!(options) if options
  post('drafts/query', data: data)
end

#query_flag_reports(**options) ⇒ Object



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

def query_flag_reports(**options)
  data = { filter_conditions: options }
  post('moderation/reports', data: data)
end

#query_future_channel_bans(**options) ⇒ Object



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

def query_future_channel_bans(**options)
  get('query_future_channel_bans', params: { payload: options.to_json })
end

#query_message_flags(filter_conditions, **options) ⇒ Object



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

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_reminders(user_id, filter_conditions = {}, sort: nil, **options) ⇒ Object



1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/stream-chat/client.rb', line 1165

def query_reminders(user_id, filter_conditions = {}, sort: nil, **options)
  params = options.merge({
                           filter: filter_conditions,
                           sort: sort || [{ field: 'remind_at', direction: 1 }],
                           user_id: user_id
                         })
  post('reminders/query', data: params)
end

#query_team_usage_stats(**options) ⇒ Object



1215
1216
1217
# File 'lib/stream-chat/client.rb', line 1215

def query_team_usage_stats(**options)
  post('stats/team_usage', data: options)
end

#query_threads(filter, sort: nil, **options) ⇒ Object



1116
1117
1118
1119
1120
1121
1122
1123
# File 'lib/stream-chat/client.rb', line 1116

def query_threads(filter, sort: nil, **options)
  params = {}.merge(options).merge({
                                     filter: filter,
                                     sort: StreamChat.get_sort_fields(sort)
                                   })

  post('threads', data: params)
end

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



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

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



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

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

#remove_shadow_ban(target_id, **options) ⇒ Object



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

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

#restore_user(user_id) ⇒ Object



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

def restore_user(user_id)
  post('users/restore', data: { user_ids: [user_id] })
end

#restore_users(user_ids) ⇒ Object



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

def restore_users(user_ids)
  post('users/restore', data: { user_ids: user_ids })
end

#review_flag_report(report_id, review_result, user_id, **details) ⇒ Object



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

def review_flag_report(report_id, review_result, user_id, **details)
  data = {
    review_result: review_result,
    user_id: user_id,
    review_details: details
  }
  patch("moderation/reports/#{report_id}", data: data)
end

#revoke_tokens(before) ⇒ Object



846
847
848
849
# File 'lib/stream-chat/client.rb', line 846

def revoke_tokens(before)
  before = StreamChat.normalize_timestamp(before)
  update_app_settings({ 'revoke_tokens_issued_before' => before })
end

#revoke_user_token(user_id, before) ⇒ Object



853
854
855
# File 'lib/stream-chat/client.rb', line 853

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

#revoke_users_token(user_ids, before) ⇒ Object



859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/stream-chat/client.rb', line 859

def revoke_users_token(user_ids, before)
  before = StreamChat.normalize_timestamp(before)

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

#run_message_action(message_id, data) ⇒ Object



724
725
726
# File 'lib/stream-chat/client.rb', line 724

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)


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/stream-chat/client.rb', line 198

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



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/stream-chat/client.rb', line 904

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



710
711
712
# File 'lib/stream-chat/client.rb', line 710

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

#set_http_client(client) ⇒ Object



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

def set_http_client(client)
  @conn = client
end

#shadow_ban(target_id, **options) ⇒ Object



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

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

#start_campaign(campaign_id, scheduled_for: nil, stop_at: nil) ⇒ Object



630
631
632
633
634
635
# File 'lib/stream-chat/client.rb', line 630

def start_campaign(campaign_id, scheduled_for: nil, stop_at: nil)
  payload = {}
  payload['scheduled_for'] = StreamChat.normalize_timestamp(scheduled_for) if scheduled_for
  payload['stop_at'] = StreamChat.normalize_timestamp(stop_at) if stop_at
  post("campaigns/#{campaign_id}/start", data: payload)
end

#stop_campaign(campaign_id) ⇒ Object



642
643
644
# File 'lib/stream-chat/client.rb', line 642

def stop_campaign(campaign_id)
  post("campaigns/#{campaign_id}/stop")
end

#translate_message(message_id, language) ⇒ Object



718
719
720
# File 'lib/stream-chat/client.rb', line 718

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

#unban_user(target_id, **options) ⇒ Object



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

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

#undelete_message(message_id, undeleted_by, **options) ⇒ Object



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

def undelete_message(message_id, undeleted_by, **options)
  payload = { undeleted_by: undeleted_by }.merge(options)
  post("messages/#{message_id}/undelete", data: payload)
end

#unflag_message(id, **options) ⇒ Object



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

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

#unflag_user(id, **options) ⇒ Object



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

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



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

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



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

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

#unread_counts(user_id) ⇒ Object



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

def unread_counts(user_id)
  get('/unread', params: { user_id: user_id })
end

#unread_counts_batch(user_ids) ⇒ Object



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

def unread_counts_batch(user_ids)
  post('/unread_batch', data: { user_ids: user_ids })
end

#update_app_settings(**settings) ⇒ Object



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

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

#update_blocklist(name, words) ⇒ Object



783
784
785
# File 'lib/stream-chat/client.rb', line 783

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

#update_campaign(campaign_id, data) ⇒ Object



609
610
611
# File 'lib/stream-chat/client.rb', line 609

def update_campaign(campaign_id, data)
  put("campaigns/#{campaign_id}", data: data)
end

#update_channel_type(channel_type, **options) ⇒ Object



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

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

#update_channels_batch(payload) ⇒ Object



1187
1188
1189
# File 'lib/stream-chat/client.rb', line 1187

def update_channels_batch(payload)
  put('channels/batch', data: payload)
end

#update_command(name, command) ⇒ Object



1001
1002
1003
# File 'lib/stream-chat/client.rb', line 1001

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

#update_location(user_id, created_by_device_id:, message_id:, **options) ⇒ Object



984
985
986
987
988
989
990
991
# File 'lib/stream-chat/client.rb', line 984

def update_location(user_id, created_by_device_id:, message_id:, **options)
  data = {
    created_by_device_id: created_by_device_id,
    message_id: message_id
  }
  data.merge!(options) if options
  put('users/live_locations', data: data, params: { user_id: user_id })
end

#update_message(message) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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



1037
1038
1039
# File 'lib/stream-chat/client.rb', line 1037

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

#update_reminder(message_id, user_id, remind_at = nil) ⇒ Object



1143
1144
1145
1146
1147
# File 'lib/stream-chat/client.rb', line 1143

def update_reminder(message_id, user_id, remind_at = nil)
  data = { user_id: user_id }
  data[:remind_at] = StreamChat.normalize_timestamp(remind_at) if remind_at
  patch("messages/#{message_id}/reminders", data: data)
end

#update_user(user) ⇒ Object



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

def update_user(user)
  warn '[DEPRECATION] `update_user` is deprecated.  Please use `upsert_user` instead.'
  upsert_user(user)
end

#update_user_partial(update) ⇒ Object



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

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

#update_users(users) ⇒ Object



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

def update_users(users)
  warn '[DEPRECATION] `update_users` is deprecated.  Please use `upsert_users` instead.'
  upsert_users(users)
end

#update_users_partial(updates) ⇒ Object



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

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

#upsert_push_provider(push_provider) ⇒ Object



1067
1068
1069
# File 'lib/stream-chat/client.rb', line 1067

def upsert_push_provider(push_provider)
  post('push_providers', data: { push_provider: push_provider })
end

#upsert_user(user) ⇒ Object



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

def upsert_user(user)
  upsert_users([user])
end

#upsert_users(users) ⇒ Object



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

def upsert_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

#verify_webhook(request_body, x_signature) ⇒ Object



703
704
705
706
# File 'lib/stream-chat/client.rb', line 703

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