Class: StreamChat::Client

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

Constant Summary collapse

BASE_URL =
'https://chat-us-east-1.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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stream-chat/client.rb', line 30

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.



15
16
17
# File 'lib/stream-chat/client.rb', line 15

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



16
17
18
# File 'lib/stream-chat/client.rb', line 16

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



17
18
19
# File 'lib/stream-chat/client.rb', line 17

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/stream-chat/client.rb', line 18

def options
  @options
end

Instance Method Details

#add_device(device_id, push_provider, user_id) ⇒ Object



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

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



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

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:



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

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

#create_channel_type(data) ⇒ Object



191
192
193
194
# File 'lib/stream-chat/client.rb', line 191

def create_channel_type(data)
  data['commands'] = ['all'] unless data.key?('commands') || data['commands'].nil? || data['commands'].empty?
  post('channeltypes', data: data)
end

#create_token(user_id, exp = nil) ⇒ Object



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

def create_token(user_id, exp = nil)
  payload = { user_id: user_id }
  payload['exp'] = exp unless exp.nil?
  JWT.encode(payload, @api_secret, 'HS256')
end

#deactivate_user(user_id, **options) ⇒ Object



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

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

#delete(relative_url, params: nil) ⇒ Object



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

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

#delete_channel_type(channel_type) ⇒ Object



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

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

#delete_device(device_id, user_id) ⇒ Object



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

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

#delete_message(message_id) ⇒ Object



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

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

#delete_user(user_id, **options) ⇒ Object



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

def delete_user(user_id, **options)
  delete("users/#{user_id}", params: options)
end

#export_user(user_id, **options) ⇒ Object



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

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

#flag_message(id, **options) ⇒ Object



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

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

#flag_user(id, **options) ⇒ Object



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

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

#get(relative_url, params: nil) ⇒ Object



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

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

#get_app_settingsObject



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

def get_app_settings
  get('app')
end

#get_channel_type(channel_type) ⇒ Object



196
197
198
# File 'lib/stream-chat/client.rb', line 196

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

#get_devices(user_id) ⇒ Object



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

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

#get_message(id) ⇒ Object



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

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

#list_channel_typesObject



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

def list_channel_types
  get('channeltypes')
end

#mark_all_read(user_id) ⇒ Object



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

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

#mute_user(target_id, user_id) ⇒ Object



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

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



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

def patch(relative_url, params: nil, data: nil)
  make_http_request(:patch, relative_url, params: params, data: data)
end

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



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

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



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

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



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

def query_channels(filter_conditions, sort: nil, **options)
  params = { "state": true, "watch": false, "presence": false }
  sort_fields = []
  sort&.each do |k, v|
    sort_fields << { "field": k, "direction": v }
  end
  params = params.merge(options).merge({
                                         "filter_conditions": filter_conditions,
                                         "sort": sort_fields
                                       })
  get('channels', params: { "payload": params.to_json })
end

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



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

def query_users(filter_conditions, sort: nil, **options)
  sort_fields = []
  sort&.each do |k, v|
    sort_fields << { "field": k, "direction": v }
  end
  params = options.merge({
                           "filter_conditions": filter_conditions,
                           "sort": sort_fields
                         })
  get('users', params: { "payload": params.to_json })
end

#reactivate_user(user_id, **options) ⇒ Object



123
124
125
# File 'lib/stream-chat/client.rb', line 123

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

#search(filter_conditions, query, **options) ⇒ Object



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

def search(filter_conditions, query, **options)
  params = options.merge({
                           "filter_conditions": filter_conditions,
                           "query": query
                         })

  get('search', params: { "payload": params.to_json })
end

#send_file(relative_url, file_url, user, content_type = 'application/octet-stream') ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/stream-chat/client.rb', line 265

def send_file(relative_url, file_url, user, content_type = 'application/octet-stream')
  url = [@base_url, relative_url].join('/')

  file = open(file_url)
  body = { user: user.to_json }

  body[:file] = Faraday::UploadIO.new(file, 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

#unban_user(target_id, **options) ⇒ Object



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

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



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

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

#unflag_user(id, **options) ⇒ Object



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

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



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

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

#update_app_settings(**settings) ⇒ Object



51
52
53
# File 'lib/stream-chat/client.rb', line 51

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

#update_channel_type(channel_type, **options) ⇒ Object



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

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

#update_message(message) ⇒ Object



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

def update_message(message)
  raise ArgumentError 'message must have an id' unless message.key? 'id'

  post("messages/#{message['id']}", data: { 'message': message })
end

#update_user(user) ⇒ Object



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

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

#update_user_partial(update) ⇒ Object



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

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

#update_users(users) ⇒ Object



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

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



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

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

#verify_webhook(request_body, x_signature) ⇒ Object



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

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