Class: Chatkit::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chatkit/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chatkit/client.rb', line 22

def initialize(options)
  base_options = {
    locator: options[:instance_locator],
    key: options[:key],
    port: options[:port],
    host: options[:host],
    client: options[:client],
    sdk_info: PusherPlatform::SDKInfo.new({
      product_name: 'chatkit',
      version: '0.7.2'
    })
  }

  @api_v2_instance = PusherPlatform::Instance.new(
    base_options.merge({
      service_name: 'chatkit',
      service_version: 'v2'
    })
  )

  @api_instance = PusherPlatform::Instance.new(
    base_options.merge({
      service_name: 'chatkit',
      service_version: 'v6'
    })
  )

  @authorizer_instance = PusherPlatform::Instance.new(
    base_options.merge({
      service_name: 'chatkit_authorizer',
      service_version: 'v2'
    })
  )

  @cursors_instance = PusherPlatform::Instance.new(
    base_options.merge({
      service_name: 'chatkit_cursors',
      service_version: 'v2'
    })
  )

  @scheduler_instance = PusherPlatform::Instance.new(
    base_options.merge({
      service_name: 'chatkit_scheduler',
      service_version: 'v1'
    })
  )
end

Instance Attribute Details

#api_instanceObject

Returns the value of attribute api_instance.



17
18
19
# File 'lib/chatkit/client.rb', line 17

def api_instance
  @api_instance
end

#api_v2_instanceObject

Returns the value of attribute api_v2_instance.



17
18
19
# File 'lib/chatkit/client.rb', line 17

def api_v2_instance
  @api_v2_instance
end

#authorizer_instanceObject

Returns the value of attribute authorizer_instance.



17
18
19
# File 'lib/chatkit/client.rb', line 17

def authorizer_instance
  @authorizer_instance
end

#cursors_instanceObject

Returns the value of attribute cursors_instance.



17
18
19
# File 'lib/chatkit/client.rb', line 17

def cursors_instance
  @cursors_instance
end

Instance Method Details

#add_users_to_room(options) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/chatkit/client.rb', line 336

def add_users_to_room(options)
  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to add users to")
  end

  if options[:user_ids].nil?
    raise Chatkit::MissingParameterError.new("You must provide a list of IDs of the users you want to add to the room")
  end

  api_request({
    method: "PUT",
    path: "/rooms/#{url_encode_path_segment options[:room_id]}/users/add",
    body: { user_ids: options[:user_ids] },
    jwt: generate_su_token[:token]
  })
end

#api_request(options) ⇒ Object



816
817
818
# File 'lib/chatkit/client.rb', line 816

def api_request(options)
  make_request(@api_instance, options)
end

#api_v2_request(options) ⇒ Object

Service-specific helpers



812
813
814
# File 'lib/chatkit/client.rb', line 812

def api_v2_request(options)
  make_request(@api_v2_instance, options)
end

#assign_global_role_to_user(options) ⇒ Object



683
684
685
# File 'lib/chatkit/client.rb', line 683

def assign_global_role_to_user(options)
  assign_role_to_user(options)
end

#assign_room_role_to_user(options) ⇒ Object



687
688
689
690
691
692
693
# File 'lib/chatkit/client.rb', line 687

def assign_room_role_to_user(options)
  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide a room ID to assign a room role to a user")
  end

  assign_role_to_user(options)
end

#async_delete_room(options) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/chatkit/client.rb', line 283

def async_delete_room(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room to delete")
  end

  scheduler_request({
    method: "PUT",
    path: "/rooms/#{url_encode_path_segment options[:id]}",
    jwt: generate_su_token[:token]
  })
end

#async_delete_user(options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/chatkit/client.rb', line 161

def async_delete_user(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to delete")
  end

  scheduler_request({
    method: "PUT",
    path: "/users/#{url_encode_path_segment options[:id]}",
    jwt: generate_su_token[:token]
  })
end

#authenticate(options) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/chatkit/client.rb', line 71

def authenticate(options)
  user_id = options['user_id'] || options[:user_id]
  auth_payload = options['auth_payload'] || options[:auth_payload] || {
    grant_type: 'client_credentials'
  }
  @api_instance.authenticate(auth_payload, { user_id: user_id })
end

#authorizer_request(options) ⇒ Object



820
821
822
# File 'lib/chatkit/client.rb', line 820

def authorizer_request(options)
  make_request(@authorizer_instance, options)
end

#create_global_role(options) ⇒ Object

Roles and permissions API



663
664
665
666
# File 'lib/chatkit/client.rb', line 663

def create_global_role(options)
  options[:scope] = GLOBAL_SCOPE
  create_role(options)
end

#create_room(options) ⇒ Object

Room API



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/chatkit/client.rb', line 234

def create_room(options)
  if options[:creator_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user creating the room")
  end

  if options[:name].nil?
    raise Chatkit::MissingParameterError.new("You must provide a name for the room")
  end

  body = {
    name: options[:name],
    private: options[:private] || false
  }

  body[:id] = options[:id] unless options[:id].nil?
  body[:push_notification_title_override] = options[:push_notification_title_override] unless options[:push_notification_title_override].nil?
  body[:custom_data] = options[:custom_data] unless options[:custom_data].nil?

  unless options[:user_ids].nil?
    body[:user_ids] = options[:user_ids]
  end

  api_request({
    method: "POST",
    path: "/rooms",
    body: body,
    jwt: generate_su_token({ user_id: options[:creator_id] })[:token]
  })
end

#create_room_role(options) ⇒ Object



668
669
670
671
# File 'lib/chatkit/client.rb', line 668

def create_room_role(options)
  options[:scope] = ROOM_SCOPE
  create_role(options)
end

#create_user(options) ⇒ Object

User API



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chatkit/client.rb', line 100

def create_user(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide an ID for the user you want to create")
  end

  if options[:name].nil?
    raise Chatkit::MissingParameterError.new("You must provide a name for the user you want to create")
  end

  body = {
    id: options[:id],
    name: options[:name]
  }

  unless options[:avatar_url].nil?
    body[:avatar_url] = options[:avatar_url]
  end

  unless options[:custom_data].nil?
    body[:custom_data] = options[:custom_data]
  end

  api_request({
    method: "POST",
    path: "/users",
    body: body,
    jwt: generate_su_token[:token]
  })
end

#create_users(options) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chatkit/client.rb', line 130

def create_users(options)
  if options[:users].nil?
    raise Chatkit::MissingParameterError.new("You must provide a list of users that you want to create")
  end

  api_request({
    method: "POST",
    path: "/batch_users",
    body: options,
    jwt: generate_su_token[:token]
  })
end

#cursors_request(options) ⇒ Object



824
825
826
# File 'lib/chatkit/client.rb', line 824

def cursors_request(options)
  make_request(@cursors_instance, options)
end

#delete_global_role(options) ⇒ Object



673
674
675
676
# File 'lib/chatkit/client.rb', line 673

def delete_global_role(options)
  options[:scope] = GLOBAL_SCOPE
  delete_role(options)
end

#delete_message(options) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/chatkit/client.rb', line 525

def delete_message(options)
  if options[:message_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the message you want to delete")
  end

  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room to which the message belongs")
  end

  api_request({
    method: "DELETE",
    path: "/rooms/#{options[:room_id]}/messages/#{options[:message_id]}",
    jwt: generate_su_token[:token]
  })
end

#delete_room_role(options) ⇒ Object



678
679
680
681
# File 'lib/chatkit/client.rb', line 678

def delete_room_role(options)
  options[:scope] = ROOM_SCOPE
  delete_role(options)
end

#edit_message(room_id, message_id, options) ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/chatkit/client.rb', line 607

def edit_message(room_id, message_id, options)
  if room_id.nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room in which the message to edit belongs")
  end

  if message_id.nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the message to edit")
  end

  if options[:sender_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user editing the message")
  end

  if options[:text].nil?
    raise Chatkit::MissingParameterError.new("You must provide some text for the message")
  end

  if !options['room_id'].nil?
    raise Chatkit::UnexpectedParameterError.new("a messages room id cannot be edited")
  end
  if !options['message_id'].nil?
    raise Chatkit::UnexpectedParameterError.new("a messages id cannot be edited")
  end


  attachment = options[:attachment]

  unless attachment.nil?
    if attachment[:resource_link].nil?
      raise Chatkit::MissingParameterError.new("You must provide a resource_link for the message attachment")
    end

    valid_file_types = ['image', 'video', 'audio', 'file']

    if attachment[:type].nil? || !valid_file_types.include?(attachment[:type])
      raise Chatkit::MissingParameterError.new(
        "You must provide a valid type for the message attachment, i.e. one of: #{valid_file_types.join(', ')}"
      )
    end
  end

  payload = {
    text: options[:text],
    attachment: options[:attachment]
  }

  api_v2_request({
    method: "PUT",
    path: "/rooms/#{url_encode_path_segment room_id}/messages/#{message_id}",
    body: payload,
    jwt: generate_su_token({ user_id: options[:sender_id] })[:token]
  })
end

#edit_multipart_message(room_id, message_id, options) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/chatkit/client.rb', line 552

def edit_multipart_message(room_id, message_id, options)
  verify({
    sender_id: "You must provide the ID of the user editing the message",
    parts: "You must provide a parts array",
  }, options)
  verify({
    room_id: "You must provide the ID of the room in which the message to edit belongs",
    message_id: "You must provide the ID of the message to edit",
  }, {room_id: room_id, message_id: message_id})
  if !options['room_id'].nil?
    raise Chatkit::UnexpectedParameterError.new("a messages room id cannot be edited")
  end
  if !options['message_id'].nil?
    raise Chatkit::UnexpectedParameterError.new("a messages id cannot be edited")
  end

  if not options[:parts].length > 0
    raise Chatkit::MissingParameterError.new("parts array must have at least one item")
  end

  # this assumes the token lives long enough to finish all S3 uploads
  token = generate_su_token({ user_id: options[:sender_id] })[:token]

  request_parts = options[:parts].map { |part|
    verify({type: "Each part must define a type"}, part)

    if !part[:content].nil?
      {
        type: part[:type],
        content: part[:content]
      }
    elsif !part[:url].nil?
      {
        type: part[:type],
        url: part[:url]
      }
    elsif !part[:file].nil?
      attachment_id = upload_attachment(token, room_id, part)
      {
        type: part[:type],
        attachment: {id: attachment_id},
      }.reject{ |_,v| v.nil? }
    else
      raise Chatkit::MissingParameterError.new("Each part must have one of :file, :content or :url")
    end
  }

  api_request({
    method: "PUT",
    path: "/rooms/#{url_encode_path_segment room_id}/messages/#{message_id}",
    body: {parts: request_parts},
    jwt: token
  })
end

#edit_simple_message(room_id, message_id, options) ⇒ Object



541
542
543
544
545
546
547
548
549
550
# File 'lib/chatkit/client.rb', line 541

def edit_simple_message(room_id, message_id, options)
  verify({text: "You must provide some text for the message",
         }, options)

  options[:parts] = [{type: "text/plain",
                      content: options[:text]
                     }]

  edit_multipart_message(room_id, message_id, options)
end

#fetch_multipart_message(options) ⇒ Object

Messages API



372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/chatkit/client.rb', line 372

def fetch_multipart_message(options)
  verify({
    room_id: "You must provide the ID of the room to fetch the message from",
    message_id: "You must provide the message ID"
  }, options)

  api_request({
    method: "GET",
    path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages/#{options[:message_id]}",
    jwt: generate_su_token[:token]
  })
end

#fetch_multipart_messages(options) ⇒ Object



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

def fetch_multipart_messages(options)
  verify({
    room_id: "You must provide the ID of the room to fetch the messages from",
  }, options)

  if !options[:limit].nil? and options[:limit] <= 0
    raise Chatkit::MissingParameterError.new("Limit must be greater than 0")
  end

  optional_params = [:initial_id, :direction, :limit]
  query_params = options.select { |key,_| optional_params.include? key }

  api_request({
    method: "GET",
    path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages",
    query: query_params,
    jwt: generate_su_token[:token]
  })
end

#generate_access_token(options) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/chatkit/client.rb', line 79

def generate_access_token(options)
  if options.empty?
    raise Chatkit::Error.new("You must provide a either a user_id or `su: true`")
  end

  @api_instance.generate_access_token(options)
end

#generate_su_token(options = {}) ⇒ Object



87
88
89
# File 'lib/chatkit/client.rb', line 87

def generate_su_token(options = {})
  generate_access_token({ su: true }.merge(options))
end

#get_delete_status(options) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/chatkit/client.rb', line 173

def get_delete_status(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the job you want to query status of")
  end

  scheduler_request({
    method: "GET",
    path: "/status/#{url_encode_path_segment options[:id]}",
    jwt: generate_su_token[:token]
  })
end

#get_permissions_for_global_role(options) ⇒ Object



727
728
729
730
# File 'lib/chatkit/client.rb', line 727

def get_permissions_for_global_role(options)
  options[:scope] = GLOBAL_SCOPE
  get_permissions_for_role(options)
end

#get_permissions_for_room_role(options) ⇒ Object



732
733
734
735
# File 'lib/chatkit/client.rb', line 732

def get_permissions_for_room_role(options)
  options[:scope] = ROOM_SCOPE
  get_permissions_for_role(options)
end

#get_read_cursor(options) ⇒ Object

Cursors API



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/chatkit/client.rb', line 749

def get_read_cursor(options)
  if options[:user_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to fetch")
  end

  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room that you want the read cursor for")
  end

  cursors_request({
    method: "GET",
    path: "/cursors/0/rooms/#{url_encode_path_segment options[:room_id]}/users/#{url_encode_path_segment options[:user_id]}",
    jwt: generate_su_token[:token]
  })
end

#get_rolesObject



695
696
697
698
699
700
701
# File 'lib/chatkit/client.rb', line 695

def get_roles
  authorizer_request({
    method: "GET",
    path: "/roles",
    jwt: generate_su_token[:token]
  })
end

#get_room(options) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/chatkit/client.rb', line 295

def get_room(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch")
  end

  api_request({
    method: "GET",
    path: "/rooms/#{url_encode_path_segment options[:id]}",
    jwt: generate_su_token[:token]
  })
end

#get_room_messages(options) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/chatkit/client.rb', line 408

def get_room_messages(options)
  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch messages from")
  end

  query_params = {}
  query_params[:initial_id] = options[:initial_id] unless options[:initial_id].nil?
  query_params[:direction] = options[:direction] unless options[:direction].nil?
  query_params[:limit] = options[:limit] unless options[:limit].nil?

  api_v2_request({
    method: "GET",
    path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages",
    query: query_params,
    jwt: generate_su_token[:token]
  })
end

#get_room_read_cursors(options) ⇒ Object



798
799
800
801
802
803
804
805
806
807
808
# File 'lib/chatkit/client.rb', line 798

def get_room_read_cursors(options)
  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room that you want the read cursors for")
  end

  cursors_request({
    method: "GET",
    path: "/cursors/0/rooms/#{url_encode_path_segment options[:room_id]}",
    jwt: generate_su_token[:token]
  })
end

#get_rooms(options = nil) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/chatkit/client.rb', line 307

def get_rooms(options = nil)
  request_options = {
    method: "GET",
    path: "/rooms",
    jwt: generate_su_token[:token]
  }

  unless options.nil?
    query = {}
    query[:include_private] = !options[:include_private].nil? ? options[:include_private] : false
    query[:from_id] = options[:from_id] unless options[:from_id].nil?

    request_options.merge!({
      query: query
    })
  end

  api_request(request_options)
end

#get_user(options) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/chatkit/client.rb', line 185

def get_user(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to fetch")
  end

  api_request({
    method: "GET",
    path: "/users/#{url_encode_path_segment options[:id]}",
    jwt: generate_su_token[:token]
  })
end

#get_user_joinable_rooms(options) ⇒ Object



331
332
333
334
# File 'lib/chatkit/client.rb', line 331

def get_user_joinable_rooms(options)
  options[:joinable] = true
  get_rooms_for_user(options)
end

#get_user_read_cursors(options) ⇒ Object



786
787
788
789
790
791
792
793
794
795
796
# File 'lib/chatkit/client.rb', line 786

def get_user_read_cursors(options)
  if options[:user_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursors you want to fetch")
  end

  cursors_request({
    method: "GET",
    path: "/cursors/0/users/#{url_encode_path_segment options[:user_id]}",
    jwt: generate_su_token[:token]
  })
end

#get_user_roles(options) ⇒ Object



703
704
705
706
707
708
709
710
711
712
713
# File 'lib/chatkit/client.rb', line 703

def get_user_roles(options)
  if options[:user_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose roles you want to fetch")
  end

  authorizer_request({
    method: "GET",
    path: "/users/#{url_encode_path_segment options[:user_id]}/roles",
    jwt: generate_su_token[:token]
  })
end

#get_user_rooms(options) ⇒ Object



327
328
329
# File 'lib/chatkit/client.rb', line 327

def get_user_rooms(options)
  get_rooms_for_user(options)
end

#get_users(options = nil) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/chatkit/client.rb', line 197

def get_users(options = nil)
  request_options = {
    method: "GET",
    path: "/users",
    jwt: generate_su_token[:token]
  }

  unless options.nil?
    query = {}
    query[:from_ts] = options[:from_timestamp] unless options[:from_timestamp].nil?
    query[:limit] = options[:limit] unless options[:limit].nil?

    request_options.merge!({
      query: query
    })
  end

  api_request(request_options)
end

#get_users_by_id(options) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/chatkit/client.rb', line 217

def get_users_by_id(options)
  if options[:user_ids].nil?
    raise Chatkit::MissingParameterError.new("You must provide the IDs of the users you want to fetch")
  end

  api_request({
    method: "GET",
    path: "/users_by_ids",
    query: {
      id: options[:user_ids],
    },
    jwt: generate_su_token[:token]
  })
end

#remove_global_role_for_user(options) ⇒ Object



715
716
717
# File 'lib/chatkit/client.rb', line 715

def remove_global_role_for_user(options)
  remove_role_for_user(options)
end

#remove_room_role_for_user(options) ⇒ Object



719
720
721
722
723
724
725
# File 'lib/chatkit/client.rb', line 719

def remove_room_role_for_user(options)
  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide a room ID")
  end

  remove_role_for_user(options)
end

#remove_users_from_room(options) ⇒ Object



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

def remove_users_from_room(options)
  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to remove users from")
  end

  if options[:user_ids].nil?
    raise Chatkit::MissingParameterError.new("You must provide a list of IDs of the users you want to remove from the room")
  end

  api_request({
    method: "PUT",
    path: "/rooms/#{url_encode_path_segment options[:room_id]}/users/remove",
    body: { user_ids: options[:user_ids] },
    jwt: generate_su_token[:token]
  })
end

#scheduler_request(options) ⇒ Object



828
829
830
# File 'lib/chatkit/client.rb', line 828

def scheduler_request(options)
  make_request(@scheduler_instance, options)
end

#send_message(options) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/chatkit/client.rb', line 483

def send_message(options)
  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room to send the message to")
  end

  if options[:sender_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user sending the message")
  end

  if options[:text].nil?
    raise Chatkit::MissingParameterError.new("You must provide some text for the message")
  end

  attachment = options[:attachment]

  unless attachment.nil?
    if attachment[:resource_link].nil?
      raise Chatkit::MissingParameterError.new("You must provide a resource_link for the message attachment")
    end

    valid_file_types = ['image', 'video', 'audio', 'file']

    if attachment[:type].nil? || !valid_file_types.include?(attachment[:type])
      raise Chatkit::MissingParameterError.new(
        "You must provide a valid type for the message attachment, i.e. one of: #{valid_file_types.join(', ')}"
      )
    end
  end

  payload = {
    text: options[:text],
    attachment: options[:attachment]
  }

  api_v2_request({
    method: "POST",
    path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages",
    body: payload,
    jwt: generate_su_token({ user_id: options[:sender_id] })[:token]
  })
end

#send_multipart_message(options) ⇒ Object



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/chatkit/client.rb', line 437

def send_multipart_message(options)
  verify({
    room_id: "You must provide the ID of the room to send the message to",
    sender_id: "You must provide the ID of the user sending the message",
    parts: "You must provide a parts array",
  }, options)

  if not options[:parts].length > 0
    raise Chatkit::MissingParameterError.new("parts array must have at least one item")
  end

  # this assumes the token lives long enough to finish all S3 uploads
  token = generate_su_token({ user_id: options[:sender_id] })[:token]

  request_parts = options[:parts].map { |part|
    verify({type: "Each part must define a type"}, part)

    if !part[:content].nil?
      {
        type: part[:type],
        content: part[:content]
      }
    elsif !part[:url].nil?
      {
        type: part[:type],
        url: part[:url]
      }
    elsif !part[:file].nil?
      attachment_id = upload_attachment(token, options[:room_id], part)
      {
        type: part[:type],
        attachment: {id: attachment_id}
      }.reject{ |_,v| v.nil? }
    else
      raise Chatkit::MissingParameterError.new("Each part must have one of :file, :content or :url")
    end
  }

  api_request({
    method: "POST",
    path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages",
    body: {parts: request_parts},
    jwt: token
  })
end

#send_simple_message(options) ⇒ Object



426
427
428
429
430
431
432
433
434
435
# File 'lib/chatkit/client.rb', line 426

def send_simple_message(options)
  verify({text: "You must provide some text for the message",
         }, options)

  options[:parts] = [{type: "text/plain",
                      content: options[:text]
                     }]

  send_multipart_message(options)
end

#set_read_cursor(options) ⇒ Object



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/chatkit/client.rb', line 765

def set_read_cursor(options)
  if options[:user_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to set")
  end

  if options[:room_id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to set the user's cursor in")
  end

  if options[:position].nil?
    raise Chatkit::MissingParameterError.new("You must provide position of the read cursor")
  end

  cursors_request({
    method: "PUT",
    path: "/cursors/0/rooms/#{url_encode_path_segment options[:room_id]}/users/#{url_encode_path_segment options[:user_id]}",
    body: { position: options[:position] },
    jwt: generate_su_token[:token]
  })
end

#update_permissions_for_global_role(options) ⇒ Object



737
738
739
740
# File 'lib/chatkit/client.rb', line 737

def update_permissions_for_global_role(options)
  options[:scope] = GLOBAL_SCOPE
  update_permissions_for_role(options)
end

#update_permissions_for_room_role(options) ⇒ Object



742
743
744
745
# File 'lib/chatkit/client.rb', line 742

def update_permissions_for_room_role(options)
  options[:scope] = ROOM_SCOPE
  update_permissions_for_role(options)
end

#update_room(options) ⇒ Object



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

def update_room(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the room to update")
  end

  payload = {}
  payload[:name] = options[:name] unless options[:name].nil?
  payload[:private] = options[:private] unless options[:private].nil?
  payload[:push_notification_title_override] = options[:push_notification_title_override] if options.key?(:push_notification_title_override) # We want to accept nil
  payload[:custom_data] = options[:custom_data] unless options[:custom_data].nil?

  api_request({
    method: "PUT",
    path: "/rooms/#{url_encode_path_segment options[:id]}",
    body: payload,
    jwt: generate_su_token[:token]
  })
end

#update_user(options) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chatkit/client.rb', line 143

def update_user(options)
  if options[:id].nil?
    raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to update")
  end

  payload = {}
  payload[:name] = options[:name] unless options[:name].nil?
  payload[:avatar_url] = options[:avatar_url] unless options[:avatar_url].nil?
  payload[:custom_data] = options[:custom_data] unless options[:custom_data].nil?

  api_request({
    method: "PUT",
    path: "/users/#{url_encode_path_segment options[:id]}",
    body: payload,
    jwt: generate_su_token({ user_id: options[:id] })[:token]
  })
end

#url_encode_path_segment(s) ⇒ Object

This helper should be used to encode parameters that appear in path segments. CGI::escape should NOT be used as it treats the string as if it appears in a query string. E.G. We want “user name” to be encoded as “user%20name” rather than “user+name”



94
95
96
# File 'lib/chatkit/client.rb', line 94

def url_encode_path_segment(s)
  ERB::Util.url_encode(s)
end