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)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stream-chat/client.rb', line 61

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.



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

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



45
46
47
# File 'lib/stream-chat/client.rb', line 45

def conn
  @conn
end

#moderationObject (readonly)

Returns the value of attribute moderation.



48
49
50
# File 'lib/stream-chat/client.rb', line 48

def moderation
  @moderation
end

Class Method Details

.from_env(**options) ⇒ Object



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

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



671
672
673
674
675
676
677
678
# File 'lib/stream-chat/client.rb', line 671

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



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

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



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

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

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



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

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

#channel_batch_updaterObject



1245
1246
1247
# File 'lib/stream-chat/client.rb', line 1245

def channel_batch_updater
  ChannelBatchUpdater.new(self)
end

#check_push(push_data) ⇒ Object



975
976
977
# File 'lib/stream-chat/client.rb', line 975

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



991
992
993
# File 'lib/stream-chat/client.rb', line 991

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



983
984
985
# File 'lib/stream-chat/client.rb', line 983

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



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

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

#create_blocklist(name, words) ⇒ Object



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

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

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



592
593
594
595
596
597
# File 'lib/stream-chat/client.rb', line 592

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



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

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



997
998
999
# File 'lib/stream-chat/client.rb', line 997

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

#create_guest(user) ⇒ Object



786
787
788
# File 'lib/stream-chat/client.rb', line 786

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

#create_import(path, mode) ⇒ Object



1150
1151
1152
# File 'lib/stream-chat/client.rb', line 1150

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

#create_import_url(filename) ⇒ Object



1140
1141
1142
# File 'lib/stream-chat/client.rb', line 1140

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

#create_permission(permission) ⇒ Object



1082
1083
1084
# File 'lib/stream-chat/client.rb', line 1082

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

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



1182
1183
1184
1185
1186
# File 'lib/stream-chat/client.rb', line 1182

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



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

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

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



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

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



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

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

#deactivate_users(user_ids, **options) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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

#delete_blocklist(name) ⇒ Object



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

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

#delete_campaign(campaign_id, **options) ⇒ Object



624
625
626
# File 'lib/stream-chat/client.rb', line 624

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

#delete_channel_type(channel_type) ⇒ Object



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

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

#delete_channels(cids, hard_delete: false) ⇒ Object



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

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

#delete_command(name) ⇒ Object



1058
1059
1060
# File 'lib/stream-chat/client.rb', line 1058

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

#delete_device(device_id, user_id) ⇒ Object



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

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

#delete_message(message_id, **options) ⇒ Object



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

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

#delete_message_for_me(message_id, user_id) ⇒ Object

Raises:

  • (ArgumentError)


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

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



457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/stream-chat/client.rb', line 457

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



1094
1095
1096
# File 'lib/stream-chat/client.rb', line 1094

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

#delete_push_provider(type, name) ⇒ Object



1124
1125
1126
# File 'lib/stream-chat/client.rb', line 1124

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

#delete_reminder(message_id, user_id) ⇒ Object



1205
1206
1207
# File 'lib/stream-chat/client.rb', line 1205

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

#delete_role(name) ⇒ Object



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

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

#delete_user(user_id, **options) ⇒ Object



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

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



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

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



856
857
858
# File 'lib/stream-chat/client.rb', line 856

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

#export_user(user_id, **options) ⇒ Object



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

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

#export_users(user_ids) ⇒ Object



872
873
874
# File 'lib/stream-chat/client.rb', line 872

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

#flag_message(id, **options) ⇒ Object



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

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

#flag_user(id, **options) ⇒ Object



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

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

#get(relative_url, params: nil) ⇒ Object



936
937
938
# File 'lib/stream-chat/client.rb', line 936

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

#get_active_live_locations(user_id) ⇒ Object



1021
1022
1023
# File 'lib/stream-chat/client.rb', line 1021

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

#get_app_settingsObject



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

def get_app_settings
  get('app')
end

#get_blocklist(name) ⇒ Object



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

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

#get_campaign(campaign_id) ⇒ Object



604
605
606
# File 'lib/stream-chat/client.rb', line 604

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

#get_channel_type(channel_type) ⇒ Object



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

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

#get_command(name) ⇒ Object



1046
1047
1048
# File 'lib/stream-chat/client.rb', line 1046

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

#get_devices(user_id) ⇒ Object



688
689
690
# File 'lib/stream-chat/client.rb', line 688

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

#get_export_channel_status(task_id) ⇒ Object



862
863
864
# File 'lib/stream-chat/client.rb', line 862

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

#get_import(id) ⇒ Object



1156
1157
1158
# File 'lib/stream-chat/client.rb', line 1156

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

#get_message(id, **options) ⇒ Object



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

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

#get_permission(id) ⇒ Object



1076
1077
1078
# File 'lib/stream-chat/client.rb', line 1076

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

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



695
696
697
698
699
700
701
702
703
704
# File 'lib/stream-chat/client.rb', line 695

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



878
879
880
# File 'lib/stream-chat/client.rb', line 878

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

#hard_delete_message(message_id) ⇒ Object



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

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

#list_blocklistsObject



798
799
800
# File 'lib/stream-chat/client.rb', line 798

def list_blocklists
  get('blocklists')
end

#list_channel_typesObject



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

def list_channel_types
  get('channeltypes')
end

#list_commandsObject



1064
1065
1066
# File 'lib/stream-chat/client.rb', line 1064

def list_commands
  get('commands')
end

#list_imports(options) ⇒ Object



1162
1163
1164
# File 'lib/stream-chat/client.rb', line 1162

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

#list_permissionsObject



1070
1071
1072
# File 'lib/stream-chat/client.rb', line 1070

def list_permissions
  get('permissions')
end

#list_push_providersObject



1130
1131
1132
# File 'lib/stream-chat/client.rb', line 1130

def list_push_providers
  get('push_providers')
end

#list_rolesObject



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

def list_roles
  get('roles')
end

#mark_all_read(user_id) ⇒ Object



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

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



1230
1231
1232
# File 'lib/stream-chat/client.rb', line 1230

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



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

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

#parse_sns(message) ⇒ Object



755
756
757
# File 'lib/stream-chat/client.rb', line 755

def parse_sns(message)
  StreamChat::Webhook.parse_sns(message)
end

#parse_sqs(message_body) ⇒ Object



745
746
747
# File 'lib/stream-chat/client.rb', line 745

def parse_sqs(message_body)
  StreamChat::Webhook.parse_sqs(message_body)
end

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



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

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



389
390
391
392
393
394
395
396
397
# File 'lib/stream-chat/client.rb', line 389

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



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

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



926
927
928
# File 'lib/stream-chat/client.rb', line 926

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



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

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



661
662
663
664
665
666
667
# File 'lib/stream-chat/client.rb', line 661

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



521
522
523
524
525
526
527
528
# File 'lib/stream-chat/client.rb', line 521

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



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

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



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

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

#query_future_channel_bans(**options) ⇒ Object



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

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

#query_message_flags(filter_conditions, **options) ⇒ Object



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

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



1216
1217
1218
1219
1220
1221
1222
1223
# File 'lib/stream-chat/client.rb', line 1216

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



1266
1267
1268
# File 'lib/stream-chat/client.rb', line 1266

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

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



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

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



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

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



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

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

#remove_shadow_ban(target_id, **options) ⇒ Object



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

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



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

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

#restore_users(user_ids) ⇒ Object



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

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



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

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



897
898
899
900
# File 'lib/stream-chat/client.rb', line 897

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



904
905
906
# File 'lib/stream-chat/client.rb', line 904

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

#revoke_users_token(user_ids, before) ⇒ Object



910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/stream-chat/client.rb', line 910

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



775
776
777
# File 'lib/stream-chat/client.rb', line 775

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)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/stream-chat/client.rb', line 203

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



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'lib/stream-chat/client.rb', line 955

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



761
762
763
# File 'lib/stream-chat/client.rb', line 761

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

#set_http_client(client) ⇒ Object



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

def set_http_client(client)
  @conn = client
end

#shadow_ban(target_id, **options) ⇒ Object



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

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



635
636
637
638
639
640
# File 'lib/stream-chat/client.rb', line 635

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



647
648
649
# File 'lib/stream-chat/client.rb', line 647

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

#translate_message(message_id, language) ⇒ Object



769
770
771
# File 'lib/stream-chat/client.rb', line 769

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

#unban_user(target_id, **options) ⇒ Object



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

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



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

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



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

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

#unflag_user(id, **options) ⇒ Object



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

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



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

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



401
402
403
404
405
406
407
408
# File 'lib/stream-chat/client.rb', line 401

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



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

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

#unread_counts_batch(user_ids) ⇒ Object



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

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

#update_app_settings(**settings) ⇒ Object



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

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

#update_blocklist(name, words) ⇒ Object



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

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

#update_campaign(campaign_id, data) ⇒ Object



614
615
616
# File 'lib/stream-chat/client.rb', line 614

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

#update_channel_type(channel_type, **options) ⇒ Object



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

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

#update_channels_batch(payload) ⇒ Object



1238
1239
1240
# File 'lib/stream-chat/client.rb', line 1238

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

#update_command(name, command) ⇒ Object



1052
1053
1054
# File 'lib/stream-chat/client.rb', line 1052

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

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



1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/stream-chat/client.rb', line 1035

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)


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

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



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

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



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

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

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



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

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



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

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

#update_user_partial(update) ⇒ Object



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

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

#update_users(users) ⇒ Object



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

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

#update_users_partial(updates) ⇒ Object



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

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

#upsert_push_provider(push_provider) ⇒ Object



1118
1119
1120
# File 'lib/stream-chat/client.rb', line 1118

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

#upsert_user(user) ⇒ Object



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

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

#upsert_users(users) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/stream-chat/client.rb', line 236

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_and_parse_webhook(body, signature) ⇒ Object



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

def verify_and_parse_webhook(body, signature)
  StreamChat::Webhook.verify_and_parse_webhook(body, signature, @api_secret)
end

#verify_webhook(request_body, x_signature) ⇒ Object



712
713
714
# File 'lib/stream-chat/client.rb', line 712

def verify_webhook(request_body, x_signature)
  StreamChat::Webhook.verify_signature(request_body, x_signature, @api_secret)
end