matrix_sdk 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 444586f27d6c0e30f71f5a9c064c0a3690ac692c819c477642fd143949d06c67
4
- data.tar.gz: 5391c9869e9534f2e3fffd09313423c12228e90931d41d5e3724795b96ea4d20
3
+ metadata.gz: 540f933e3c288eb2cfb590fdc95ba37b44b7e507754885a0a2f02c0227977ab8
4
+ data.tar.gz: 6ebee0cb09293d01ce214cbc718d811954d3e6265c683dce008f4538bfa7d278
5
5
  SHA512:
6
- metadata.gz: 641b169f971c77acc2123f3750f920aff520074972e5bf307706a0bd644eafd9415a4a7d2a9996f9389aed40683da823c26615b6766b0785fdaeec3c9686be71
7
- data.tar.gz: 6c0486fab519db6bc9e33bd1f3826d49128d84974d463105d82370461fba2dba3e6aa44f9b2dc0b4662e8bb4d43106b9b10025833b3be860f68402dc32737bdb
6
+ metadata.gz: '08783bd47d2b55dfeda55bb1301d934f3314f7e642bfa6518d0233e9d29087328658729aa5288c146dbbf0c5c3ddf263bc4c1cb6c85789ff32a95e7397248f3b'
7
+ data.tar.gz: e06303208ca67b7ae2494ae11561c0a59eb1d02d6b9cfa634b0208f1161358cebdecbe44f505019358e928cd424aaa9d5dd6eaaf009192eed138c43f324e17d0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 1.2.0 - 2019-06-28
2
+
3
+ - Adds getters and setters for more specced room state
4
+ - Fixes handling of the timeout parameter for the sync endpoint (#7)
5
+ - Additionally also now allows for running sync with a nil timeout
6
+ - Cleans up the CS protocol implementation slightly, removing a mutation that's not supposed to be there
7
+ - Cleans up the gemspec slightly, no longer uses `git ls-files`
8
+ - Add support for explicitly setting proxy config for API
9
+
1
10
  ## v1.1.1 - 2019-06-05
2
11
 
3
12
  - Fixes a faulty include which broke the single implemented S2S endpoint
@@ -22,7 +22,7 @@ module MatrixSdk
22
22
  }.freeze
23
23
 
24
24
  attr_accessor :access_token, :connection_address, :connection_port, :device_id, :autoretry, :global_headers
25
- attr_reader :homeserver, :validate_certificate, :read_timeout, :protocols, :well_known
25
+ attr_reader :homeserver, :validate_certificate, :read_timeout, :protocols, :well_known, :proxy_uri
26
26
 
27
27
  ignore_inspect :access_token, :logger
28
28
 
@@ -53,6 +53,7 @@ module MatrixSdk
53
53
  @protocols = [@protocols] unless @protocols.is_a? Array
54
54
  @protocols << :CS if @protocols.include?(:AS) && !@protocols.include?(:CS)
55
55
 
56
+ @proxy_uri = params.fetch(:proxy_uri, nil)
56
57
  @connection_address = params.fetch(:address, nil)
57
58
  @connection_port = params.fetch(:port, nil)
58
59
  @access_token = params.fetch(:access_token, nil)
@@ -178,6 +179,18 @@ module MatrixSdk
178
179
  @homeserver = hs_info
179
180
  end
180
181
 
182
+ # @param [URI] proxy_uri The URI for the proxy to use
183
+ # @return [URI]
184
+ def proxy_uri=(proxy_uri)
185
+ proxy_uri = URI(proxy_uri.to_s) unless proxy_uri.is_a? URI
186
+
187
+ if @http && @proxy_uri != proxy_uri
188
+ @http.finish
189
+ @http = nil
190
+ end
191
+ @proxy_uri = proxy_uri
192
+ end
193
+
181
194
  def request(method, api, path, **options)
182
195
  url = homeserver.dup.tap do |u|
183
196
  u.path = api_to_path(api) + path
@@ -262,8 +275,15 @@ module MatrixSdk
262
275
  end
263
276
 
264
277
  def http
265
- @http ||= Net::HTTP.new (@connection_address || homeserver.host), (@connection_port || homeserver.port)
266
- return @http if @http.active?
278
+ return @http if @http&.active?
279
+
280
+ host = (@connection_address || homeserver.host)
281
+ port = (@connection_port || homeserver.port)
282
+ @http ||= if proxy_uri
283
+ Net::HTTP.new(host, port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
284
+ else
285
+ Net::HTTP.new(host, port)
286
+ end
267
287
 
268
288
  @http.read_timeout = read_timeout
269
289
  @http.use_ssl = homeserver.scheme == 'https'
@@ -205,8 +205,9 @@ module MatrixSdk
205
205
  ensure_room(data.room_id)
206
206
  end
207
207
 
208
- def join_room(room_id_or_alias)
209
- data = api.join_room(room_id_or_alias)
208
+ def join_room(room_id_or_alias, server_name: [])
209
+ server_name = [server_name] unless server_name.is_a? Array
210
+ data = api.join_room(room_id_or_alias, server_name: server_name)
210
211
  ensure_room(data.fetch(:room_id, room_id_or_alias))
211
212
  end
212
213
 
@@ -266,7 +267,7 @@ module MatrixSdk
266
267
 
267
268
  attempts = 0
268
269
  data = loop do
269
- begin # rubocop:disable Style/RedundantBegin
270
+ begin
270
271
  break api.sync extra_params
271
272
  rescue MatrixSdk::MatrixTimeoutError => e
272
273
  raise e if (attempts += 1) >= params.fetch(:allow_sync_retry, 0)
@@ -7,7 +7,7 @@ module MatrixSdk
7
7
 
8
8
  # An error specialized and raised for failed requests
9
9
  class MatrixRequestError < MatrixError
10
- attr_reader :code, :httpstatus, :message
10
+ attr_reader :code, :data, :httpstatus, :message
11
11
  alias error message
12
12
 
13
13
  def self.class_by_code(code)
@@ -30,6 +30,7 @@ module MatrixSdk
30
30
  @code = error[:errcode]
31
31
  @httpstatus = status
32
32
  @message = error[:error]
33
+ @data = error.reject { |k, _v| %i[errcode error].include? k }
33
34
 
34
35
  super error[:error]
35
36
  end
@@ -98,7 +98,7 @@ module MatrixSdk
98
98
 
99
99
  def fire(event, filter = nil)
100
100
  reverse_each do |_k, h|
101
- begin # rubocop:disable Style/RedundantBegin
101
+ begin
102
102
  h[:block].call(event) if event.matches?(h[:filter], filter)
103
103
  rescue StandardError => e
104
104
  logger.error "#{e.class.name} occurred when firing event (#{event})\n#{e}"
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # rubocop:disable Metrics/ModuleLength
3
4
  module MatrixSdk::Protocols::CS
4
5
  # Gets the available client API versions
5
6
  # @return [Array]
6
7
  def client_api_versions
7
- @client_api_versions ||= request(:get, :client, '/versions').versions.tap do |vers|
8
+ (@client_api_versions ||= request(:get, :client, '/versions')).versions.tap do |vers|
8
9
  vers.instance_eval <<-'CODE', __FILE__, __LINE__ + 1
9
10
  def latest
10
11
  last
@@ -14,9 +15,9 @@ module MatrixSdk::Protocols::CS
14
15
  end
15
16
 
16
17
  # Gets the list of available unstable client API features
17
- # @return [Array]
18
+ # @return [Hash]
18
19
  def client_api_unstable_features
19
- @client_api_unstable_features ||= request(:get, :client, '/versions').unstable_features.tap do |vers|
20
+ (@client_api_versions ||= request(:get, :client, '/versions')).unstable_features.tap do |vers|
20
21
  vers.instance_eval <<-'CODE', __FILE__, __LINE__ + 1
21
22
  def has?(feature)
22
23
  fetch(feature, nil)
@@ -25,8 +26,10 @@ module MatrixSdk::Protocols::CS
25
26
  end
26
27
  end
27
28
 
29
+ # Gets the list of available methods for logging in
30
+ # @return [Response]
28
31
  def allowed_login_methods
29
- request(:get, :client_r0, '/login').flows
32
+ request(:get, :client_r0, '/login')
30
33
  end
31
34
 
32
35
  # Runs the client API /sync method
@@ -37,16 +40,14 @@ module MatrixSdk::Protocols::CS
37
40
  # @option params [Boolean] :full_state Should the sync include the full state
38
41
  # @option params [Boolean] :set_presence Should the sync set the user status to online
39
42
  # @return [Response]
40
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#get-matrix-client-r0-sync
43
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#get-matrix-client-r0-sync
41
44
  # For more information on the parameters and what they mean
42
45
  def sync(timeout: 30.0, **params)
43
- query = {
44
- timeout: timeout
45
- }.merge(params).select do |k, _v|
46
+ query = params.select do |k, _v|
46
47
  %i[since filter full_state set_presence].include? k
47
48
  end
48
49
 
49
- query[:timeout] = (query.fetch(:timeout, 30) * 1000).to_i
50
+ query[:timeout] = (timeout * 1000).to_i if timeout
50
51
  query[:timeout] = params.delete(:timeout_ms).to_i if params.key? :timeout_ms
51
52
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
52
53
 
@@ -66,7 +67,7 @@ module MatrixSdk::Protocols::CS
66
67
  # @option params [Boolean] :store_token (true) Should the resulting access token be stored for the API
67
68
  # @option params [Boolean] :store_device_id (store_token value) Should the resulting device ID be stored for the API
68
69
  # @return [Response]
69
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#post-matrix-client-r0-register
70
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#post-matrix-client-r0-register
70
71
  # For options that are permitted in this call
71
72
  def register(kind: 'user', **params)
72
73
  query = {}
@@ -118,7 +119,7 @@ module MatrixSdk::Protocols::CS
118
119
  # @option params [String] :initial_device_display_name (USER_AGENT) The device display name to specify for this login attempt
119
120
  # @option params [String] :device_id The device ID to set on the login
120
121
  # @return [Response] A response hash with the parameters :user_id, :access_token, :home_server, and :device_id.
121
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#post-matrix-client-r0-login
122
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#post-matrix-client-r0-login
122
123
  # The Matrix Spec, for more information about the call and response
123
124
  def login(login_type: 'm.login.password', **params)
124
125
  query = {}
@@ -142,7 +143,7 @@ module MatrixSdk::Protocols::CS
142
143
 
143
144
  # Logs out the currently logged in user
144
145
  # @return [Response] An empty response if the logout was successful
145
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#post-matrix-client-r0-logout
146
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#post-matrix-client-r0-logout
146
147
  # The Matrix Spec, for more information about the call and response
147
148
  def logout(**params)
148
149
  query = {}
@@ -201,7 +202,7 @@ module MatrixSdk::Protocols::CS
201
202
  # @option params [String] :room_alias A room alias to apply on creation
202
203
  # @option params [Boolean] :invite Should the room be created invite-only
203
204
  # @return [Response] A response hash with ...
204
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#post-matrix-client-r0-createroom
205
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#post-matrix-client-r0-createroom
205
206
  # The Matrix Spec, for more information about the call and response
206
207
  def create_room(visibility: :public, **params)
207
208
  query = {}
@@ -222,7 +223,7 @@ module MatrixSdk::Protocols::CS
222
223
  # @param params [Hash] Extra room join options
223
224
  # @option params [String[]] :server_name A list of servers to perform the join through
224
225
  # @return [Response] A response hash with the parameter :room_id
225
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#post-matrix-client-r0-join-roomidoralias
226
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#post-matrix-client-r0-join-roomidoralias
226
227
  # The Matrix Spec, for more information about the call and response
227
228
  # @todo Add support for 3rd-party signed objects
228
229
  def join_room(id_or_alias, **params)
@@ -245,8 +246,8 @@ module MatrixSdk::Protocols::CS
245
246
  # @param params [Hash] Options for the request
246
247
  # @option params [String] :state_key The state key of the event, if there is one
247
248
  # @return [Response] A response hash with the parameter :event_id
248
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey
249
- # https://matrix.org/docs/spec/client_server/r0.3.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype
249
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey
250
+ # https://matrix.org/docs/spec/client_server/r0.5.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype
250
251
  # The Matrix Spec, for more information about the call and response
251
252
  def send_state_event(room_id, event_type, content, **params)
252
253
  query = {}
@@ -266,7 +267,7 @@ module MatrixSdk::Protocols::CS
266
267
  # @param params [Hash] Options for the request
267
268
  # @option params [Integer] :txn_id The ID of the transaction, or automatically generated
268
269
  # @return [Response] A response hash with the parameter :event_id
269
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
270
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
270
271
  # The Matrix Spec, for more information about the call and response
271
272
  def send_message_event(room_id, event_type, content, **params)
272
273
  query = {}
@@ -289,7 +290,7 @@ module MatrixSdk::Protocols::CS
289
290
  # @option params [String] :reason The reason for the redaction
290
291
  # @option params [Integer] :txn_id The ID of the transaction, or automatically generated
291
292
  # @return [Response] A response hash with the parameter :event_id
292
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
293
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
293
294
  # The Matrix Spec, for more information about the call and response
294
295
  def redact_event(room_id, event_id, **params)
295
296
  query = {}
@@ -344,10 +345,10 @@ module MatrixSdk::Protocols::CS
344
345
  # @option params [Hash] :extra_content Extra data to insert into the content hash
345
346
  # @return [Response] A response hash with the parameter :event_id
346
347
  # @see send_message_event For more information on the underlying call
347
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#m-image
348
- # https://matrix.org/docs/spec/client_server/r0.3.0.html#m-file
349
- # https://matrix.org/docs/spec/client_server/r0.3.0.html#m-video
350
- # https://matrix.org/docs/spec/client_server/r0.3.0.html#m-audio
348
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-image
349
+ # https://matrix.org/docs/spec/client_server/r0.5.0.html#m-file
350
+ # https://matrix.org/docs/spec/client_server/r0.5.0.html#m-video
351
+ # https://matrix.org/docs/spec/client_server/r0.5.0.html#m-audio
351
352
  # The Matrix Spec, for more information about the call and response
352
353
  def send_content(room_id, url, name, msg_type, **params)
353
354
  content = {
@@ -372,7 +373,7 @@ module MatrixSdk::Protocols::CS
372
373
  # @option params [Hash] :thumbnail_info Image information about the location thumbnail
373
374
  # @return [Response] A response hash with the parameter :event_id
374
375
  # @see send_message_event For more information on the underlying call
375
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#m-location
376
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-location
376
377
  # The Matrix Spec, for more information about the call and response
377
378
  def send_location(room_id, geo_uri, name, **params)
378
379
  content = {
@@ -395,7 +396,7 @@ module MatrixSdk::Protocols::CS
395
396
  # @option params [String] :msg_type ('m.text') The message type to send
396
397
  # @return [Response] A response hash with the parameter :event_id
397
398
  # @see send_message_event For more information on the underlying call
398
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#m-text
399
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-text
399
400
  # The Matrix Spec, for more information about the call and response
400
401
  def send_message(room_id, message, **params)
401
402
  content = {
@@ -413,7 +414,7 @@ module MatrixSdk::Protocols::CS
413
414
  # @option params [String] :msg_type ('m.emote') The message type to send
414
415
  # @return [Response] A response hash with the parameter :event_id
415
416
  # @see send_message_event For more information on the underlying call
416
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#m-emote
417
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-emote
417
418
  # The Matrix Spec, for more information about the call and response
418
419
  def send_emote(room_id, emote, **params)
419
420
  content = {
@@ -431,7 +432,7 @@ module MatrixSdk::Protocols::CS
431
432
  # @option params [String] :msg_type ('m.notice') The message type to send
432
433
  # @return [Response] A response hash with the parameter :event_id
433
434
  # @see send_message_event For more information on the underlying call
434
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#m-notice
435
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-notice
435
436
  # The Matrix Spec, for more information about the call and response
436
437
  def send_notice(room_id, notice, **params)
437
438
  content = {
@@ -451,7 +452,7 @@ module MatrixSdk::Protocols::CS
451
452
  # @option params [String] :to A token to limit retrieval to
452
453
  # @option params [String] :filter A filter to limit the retrieval to
453
454
  # @return [Response] A response hash with the message information containing :start, :end, and :chunk fields
454
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#get-matrix-client-r0-rooms-roomid-messages
455
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#get-matrix-client-r0-rooms-roomid-messages
455
456
  # The Matrix Spec, for more information about the call and response
456
457
  def get_room_messages(room_id, token, direction, limit: 10, **params)
457
458
  query = {
@@ -473,7 +474,7 @@ module MatrixSdk::Protocols::CS
473
474
  # @param room_id [MXID,String] The room ID to read from
474
475
  # @param state_type [String] The state type to read
475
476
  # @return [Response] A response hash with the contents of the state event
476
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype
477
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype
477
478
  # The Matrix Spec, for more information about the call and response
478
479
  def get_room_state(room_id, state_type = nil, key: nil, **params)
479
480
  query = {}
@@ -486,17 +487,30 @@ module MatrixSdk::Protocols::CS
486
487
  request(:get, :client_r0, "/rooms/#{room_id}/state#{state_type.empty? ? nil : "/#{state_type}"}#{key.empty? ? nil : "/#{key}"}", query: query)
487
488
  end
488
489
 
489
- # Gets the display name of a room
490
+ ## Specialized getters for specced state
491
+ #
492
+
493
+ # Gets the current display name of a room
490
494
  #
491
495
  # @param room_id [MXID,String] The room ID to look up
496
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
492
497
  # @return [Response] A response hash with the parameter :name
498
+ # @raise [MatrixNotFoundError] Raised if no name has been set on the room
493
499
  # @see get_room_state
494
- # @see https://matrix.org/docs/spec/client_server/r0.3.0.html#m-room-name
500
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-name
495
501
  # The Matrix Spec, for more information about the event and data
496
502
  def get_room_name(room_id, **params)
497
503
  get_room_state(room_id, 'm.room.name', params)
498
504
  end
499
505
 
506
+ # Sets the display name of a room
507
+ #
508
+ # @param [MXID,String] room_id The room ID to work on
509
+ # @param [String] name The new name of the room
510
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
511
+ # @return [Response] The resulting state event
512
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-name
513
+ # The Matrix Spec, for more information about the event and data
500
514
  def set_room_name(room_id, name, **params)
501
515
  content = {
502
516
  name: name
@@ -504,10 +518,27 @@ module MatrixSdk::Protocols::CS
504
518
  send_state_event(room_id, 'm.room.name', content, params)
505
519
  end
506
520
 
521
+ # Gets the current topic of a room
522
+ #
523
+ # @param [MXID,String] room_id The room ID to look up
524
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
525
+ # @return [Response] A response hash with the parameter :topic
526
+ # @raise [MatrixNotFoundError] Raised if no topic has been set on the room
527
+ # @see get_room_state
528
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-topic
529
+ # The Matrix Spec, for more information about the event and data
507
530
  def get_room_topic(room_id, **params)
508
531
  get_room_state(room_id, 'm.room.topic', params)
509
532
  end
510
533
 
534
+ # Sets the topic of a room
535
+ #
536
+ # @param [MXID,String] room_id The room ID to work on
537
+ # @param [String] topic The new topic of the room
538
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
539
+ # @return [Response] The resulting state event
540
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-topic
541
+ # The Matrix Spec, for more information about the event and data
511
542
  def set_room_topic(room_id, topic, **params)
512
543
  content = {
513
544
  topic: topic
@@ -515,14 +546,274 @@ module MatrixSdk::Protocols::CS
515
546
  send_state_event(room_id, 'm.room.topic', content, params)
516
547
  end
517
548
 
518
- def get_power_levels(room_id, **params)
549
+ # Gets the current avatar URL of a room
550
+ #
551
+ # @param [MXID,String] room_id The room ID to look up
552
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
553
+ # @return [Response] A response hash with the parameters :url and (optionally) :info
554
+ # @raise [MatrixNotFoundError] Raised if no avatar has been set on the room
555
+ # @see get_room_state
556
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-avatar
557
+ # The Matrix Spec, for more information about the event and data
558
+ def get_room_avatar(room_id, **params)
559
+ get_room_state(room_id, 'm.room.avatar', params)
560
+ end
561
+
562
+ # Sets the avatar URL for a room
563
+ #
564
+ # @param [MXID,String] room_id The room ID to work on
565
+ # @param [String,URI] url The new avatar URL for the room
566
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
567
+ # @return [Response] The resulting state event
568
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-avatar
569
+ # The Matrix Spec, for more information about the event and data
570
+ def set_room_avatar(room_id, url, **params)
571
+ content = {
572
+ url: url
573
+ }
574
+ send_state_event(room_id, 'm.room.avatar', content, params)
575
+ end
576
+
577
+ # Gets a list of current aliases of a room
578
+ #
579
+ # @param [MXID,String] room_id The room ID to look up
580
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
581
+ # @return [Response] A response hash with the array :aliases
582
+ # @raise [MatrixNotFoundError] Raised if no aliases has been set on the room by the specified HS
583
+ # @see get_room_state
584
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-avatar
585
+ # The Matrix Spec, for more information about the event and data
586
+ # @example Looking up aliases for a room
587
+ # api.get_room_aliases('!QtykxKocfZaZOUrTwp:matrix.org')
588
+ # # MatrixSdk::MatrixNotFoundError: HTTP 404 (M_NOT_FOUND): Event not found.
589
+ # api.get_room_aliases('!QtykxKocfZaZOUrTwp:matrix.org', key: 'matrix.org')
590
+ # # => {:aliases=>["#matrix:matrix.org"]}
591
+ # api.get_room_aliases('!QtykxKocfZaZOUrTwp:matrix.org', key: 'kittenface.studio')
592
+ # # => {:aliases=>["#worlddominationhq:kittenface.studio"]}
593
+ # @example A way to find all aliases for a room
594
+ # api.get_room_state('!mjbDjyNsRXndKLkHIe:matrix.org')
595
+ # .select { |ch| ch[:type] == 'm.room.aliases' }
596
+ # .map { |ch| ch[:content][:aliases] }
597
+ # .flatten
598
+ # .compact
599
+ # # => ["#synapse:im.kabi.tk", "#synapse:matrix.org", "#synapse-community:matrix.org", "#synapse-ops:matrix.org", "#synops:matrix.org", ...
600
+ def get_room_aliases(room_id, **params)
601
+ get_room_state(room_id, 'm.room.aliases', params)
602
+ end
603
+
604
+ # Gets a list of pinned events in a room
605
+ #
606
+ # @param [MXID,String] room_id The room ID to look up
607
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
608
+ # @return [Response] A response hash with the array :pinned
609
+ # @raise [MatrixNotFoundError] Raised if no aliases has been set on the room by the specified HS
610
+ # @see get_room_state
611
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-pinned-events
612
+ # The Matrix Spec, for more information about the event and data
613
+ def get_room_pinned_events(room_id, **params)
614
+ get_room_state(room_id, 'm.room.pinned_events', params)
615
+ end
616
+
617
+ # Sets the list of pinned events in a room
618
+ #
619
+ # @param [MXID,String] room_id The room ID to work on
620
+ # @param [Array[String]] events The new list of events to set as pinned
621
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
622
+ # @return [Response] The resulting state event
623
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-pinned-events
624
+ # The Matrix Spec, for more information about the event and data
625
+ def set_room_pinned_events(room_id, events, **params)
626
+ content = {
627
+ pinned: events
628
+ }
629
+ send_state_event(room_id, 'm.room.pinned_events', content, params)
630
+ end
631
+
632
+ # Gets the configured power levels for a room
633
+ #
634
+ # @param [MXID,String] room_id The room ID to look up
635
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
636
+ # @return [Response] A response hash with power level information
637
+ # @see get_room_state
638
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-power-levels
639
+ # The Matrix Spec, for more information about the event and data
640
+ def get_room_power_levels(room_id, **params)
519
641
  get_room_state(room_id, 'm.room.power_levels', params)
520
642
  end
643
+ alias get_power_levels get_room_power_levels
521
644
 
522
- def set_power_levels(room_id, content, **params)
645
+ # Sets the configuration for power levels in a room
646
+ #
647
+ # @param [MXID,String] room_id The room ID to work on
648
+ # @param [Hash] content The new power level configuration
649
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
650
+ # @return [Response] The resulting state event
651
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-power-levels
652
+ # The Matrix Spec, for more information about the event and data
653
+ def set_room_power_levels(room_id, content, **params)
523
654
  content[:events] = {} unless content.key? :events
524
655
  send_state_event(room_id, 'm.room.power_levels', content, params)
525
656
  end
657
+ alias set_power_levels set_room_power_levels
658
+
659
+ # Gets the join rules for a room
660
+ #
661
+ # @param [MXID,String] room_id The room ID to look up
662
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
663
+ # @return [Response] A response hash with the key :join_rule
664
+ # @see get_room_state
665
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-join-rules
666
+ # The Matrix Spec, for more information about the event and data
667
+ def get_room_join_rules(room_id, **params)
668
+ get_room_state(room_id, 'm.room.join_rules', params)
669
+ end
670
+
671
+ # Sets the join rules for a room
672
+ #
673
+ # @param [MXID,String] room_id The room ID to work on
674
+ # @param [String,Symbol] join_rule The new join rule setting (Currently only public and invite are implemented)
675
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
676
+ # @return [Response] The resulting state event
677
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-join-rules
678
+ # The Matrix Spec, for more information about the event and data
679
+ def set_room_join_rules(room_id, join_rule, **params)
680
+ content = {
681
+ join_rule: join_rule
682
+ }
683
+
684
+ send_state_event(room_id, 'm.room.join_rules', content, params)
685
+ end
686
+
687
+ # Gets the guest access settings for a room
688
+ #
689
+ # @param [MXID,String] room_id The room ID to look up
690
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
691
+ # @return [Response] A response hash with the key :guest_acces, either :can_join or :forbidden
692
+ # @see get_room_state
693
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-guest-access
694
+ # The Matrix Spec, for more information about the event and data
695
+ def get_room_guest_access(room_id, **params)
696
+ get_room_state(room_id, 'm.room.guest_access', params)
697
+ end
698
+
699
+ # Sets the guest access settings for a room
700
+ #
701
+ # @param [MXID,String] room_id The room ID to work on
702
+ # @param [:can_join, :forbidden] guest_access The new guest access setting for the room
703
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
704
+ # @return [Response] The resulting state event
705
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-guest-access
706
+ # The Matrix Spec, for more information about the event and data
707
+ def set_room_guest_access(room_id, guest_access, **params)
708
+ content = {
709
+ guest_access: guest_access
710
+ }
711
+
712
+ send_state_event(room_id, 'm.room.guest_access', content, params)
713
+ end
714
+
715
+ # Gets the creation configuration object for a room
716
+ #
717
+ # @param [MXID,String] room_id The room ID to look up
718
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
719
+ # @return [Response] A response hash with the configuration the room was created for
720
+ # @see get_room_state
721
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-create
722
+ # The Matrix Spec, for more information about the event and data
723
+ def get_room_creation_info(room_id, **params)
724
+ get_room_state(room_id, 'm.room.create', params)
725
+ end
726
+
727
+ # Gets the encryption configuration for a room
728
+ #
729
+ # @param [MXID,String] room_id The room ID to look up
730
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
731
+ # @return [Response] A response hash with the configuration the room was created for
732
+ # @see get_room_state
733
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-encryption
734
+ # The Matrix Spec, for more information about the event and data
735
+ def get_room_encryption_settings(room_id, **params)
736
+ get_room_state(room_id, 'm.room.encryption', params)
737
+ end
738
+
739
+ # Sets the encryption configuration for a room
740
+ #
741
+ # @param [MXID,String] room_id The room ID to work on
742
+ # @param ['m.megolm.v1.aes-sha2'] algorithm The encryption algorithm to use
743
+ # @param [Integer] rotation_period_ms The interval between key rotation in milliseconds
744
+ # @param [Integer] rotation_period_msgs The interval between key rotation in messages
745
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
746
+ # @return [Response] The resulting state event
747
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-guest-encryption
748
+ # The Matrix Spec, for more information about the event and data
749
+ def set_room_encryption_settings(room_id, algorithm: 'm.megolm.v1.aes-sha2', rotation_period_ms: 1 * 7 * 24 * 60 * 60 * 1000, rotation_period_msgs: 100, **params)
750
+ content = {
751
+ algorithm: algorithm,
752
+ rotation_period_ms: rotation_period_ms,
753
+ rotation_period_msgs: rotation_period_msgs
754
+ }
755
+ send_state_event(room_id, 'm.room.encryption', content, params)
756
+ end
757
+
758
+ # Gets the history availabiilty for a room
759
+ #
760
+ # @param [MXID,String] room_id The room ID to look up
761
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
762
+ # @return [Response] A response hash with the key :history_visibility
763
+ # @see get_room_state
764
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-history-visibility
765
+ # The Matrix Spec, for more information about the event and data
766
+ def get_room_history_visibility(room_id, **params)
767
+ get_room_state(room_id, 'm.room.history_visibility', params)
768
+ end
769
+
770
+ # Sets the history availability for a room
771
+ #
772
+ # @param [MXID,String] room_id The room ID to work on
773
+ # @param [:invited, :joined, :shared, :world_readable] visibility The new history visibility level
774
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
775
+ # @return [Response] The resulting state event
776
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-guest-history-visibility
777
+ # The Matrix Spec, for more information about the event and data
778
+ def set_room_history_visibility(room_id, visibility, **params)
779
+ content = {
780
+ history_visibility: visibility
781
+ }
782
+
783
+ send_state_event(room_id, 'm.room.history_visibility', content, params)
784
+ end
785
+
786
+ # Gets the server ACLs for a room
787
+ #
788
+ # @param [MXID,String] room_id The room ID to look up
789
+ # @param [Hash] params Extra options to provide to the request, see #get_room_state
790
+ # @return [Response] A response hash with server ACL information
791
+ # @see get_room_state
792
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-server-acl
793
+ # The Matrix Spec, for more information about the event and data
794
+ def get_room_server_acl(room_id, **params)
795
+ get_room_state(room_id, 'm.room.server_acl', params)
796
+ end
797
+
798
+ # Sets the server ACL configuration for a room
799
+ #
800
+ # @param [MXID,String] room_id The room ID to work on
801
+ # @param [Boolean] allow_ip_literals If HSes with literal IP domains should be allowed
802
+ # @param [Array[String]] allow A list of HS wildcards that are allowed to communicate with the room
803
+ # @param [Array[String]] deny A list of HS wildcards that are denied from communicating with the room
804
+ # @param [Hash] params Extra options to set on the request, see #send_state_event
805
+ # @return [Response] The resulting state event
806
+ # @see https://matrix.org/docs/spec/client_server/r0.5.0.html#m-room-guest-server-acl
807
+ # The Matrix Spec, for more information about the event and data
808
+ def set_room_server_acl(room_id, allow_ip_literals: false, allow:, deny:, **params)
809
+ content = {
810
+ allow_ip_literals: allow_ip_literals,
811
+ allow: allow,
812
+ deny: deny
813
+ }
814
+
815
+ send_state_event(room_id, 'm.room.server_acl', content, params)
816
+ end
526
817
 
527
818
  def leave_room(room_id, **params)
528
819
  query = {}
@@ -774,16 +1065,34 @@ module MatrixSdk::Protocols::CS
774
1065
  request(:get, :client_r0, "/profile/#{user_id}", query: query)
775
1066
  end
776
1067
 
777
- def get_download_url(mxcurl, **_params)
1068
+ # Converts a Matrix content URL (mxc://) to a media download URL
1069
+ # @param [String,URI] mxcurl The Matrix content URL to convert
1070
+ # @param [String,URI] source A source HS to use for the convertion, defaults to the connected HS
1071
+ # @return [URI] The full download URL for the requested piece of media
1072
+ #
1073
+ # @example Converting a MXC URL
1074
+ # url = 'mxc://example.com/media_hash'
1075
+ #
1076
+ # api.get_download_url(url)
1077
+ # # => #<URI::HTTPS https://example.com/_matrix/media/r0/download/example.com/media_hash>
1078
+ # api.get_download_url(url, source: 'matrix.org')
1079
+ # # => #<URI::HTTPS https://matrix.org/_matrix/media/r0/download/example.com/media_hash>
1080
+ def get_download_url(mxcurl, source: nil, **_params)
778
1081
  mxcurl = URI.parse(mxcurl.to_s) unless mxcurl.is_a? URI
779
1082
  raise 'Not a mxc:// URL' unless mxcurl.is_a? URI::MATRIX
780
1083
 
781
- homeserver.dup.tap do |u|
1084
+ source ||= homeserver.dup
1085
+ source = URI(hs.to_s) unless hs.is_a? URI
1086
+ source.tap do |u|
782
1087
  full_path = ERB::Util.url_encode mxcurl.full_path.to_s
783
1088
  u.path = "/_matrix/media/r0/download/#{full_path}"
784
1089
  end
785
1090
  end
786
1091
 
1092
+ # Gets the room ID for an alias
1093
+ # @param [String,MXID] room_alias The room alias to look up
1094
+ # @return [Response] An object containing the :room_id key and a key of :servers that know of the room
1095
+ # @raise [MatrixNotFoundError] No room with the requested alias exists
787
1096
  def get_room_id(room_alias, **params)
788
1097
  query = {}
789
1098
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
@@ -793,6 +1102,11 @@ module MatrixSdk::Protocols::CS
793
1102
  request(:get, :client_r0, "/directory/room/#{room_alias}", query: query)
794
1103
  end
795
1104
 
1105
+ # Sets the room ID for an alias
1106
+ # @param [String,MXID] room_id The room to set an alias for
1107
+ # @param [String,MXID] room_alias The alias to configure for the room
1108
+ # @return [Response] An empty object denoting success
1109
+ # @raise [MatrixConflictError] The alias is already in use
796
1110
  def set_room_alias(room_id, room_alias, **params)
797
1111
  query = {}
798
1112
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
@@ -805,6 +1119,9 @@ module MatrixSdk::Protocols::CS
805
1119
  request(:put, :client_r0, "/directory/room/#{room_alias}", body: content, query: query)
806
1120
  end
807
1121
 
1122
+ # Remove an alias from its room
1123
+ # @param [String,MXID] room_alias The alias to remove
1124
+ # @return [Response] An empty object denoting success
808
1125
  def remove_room_alias(room_alias, **params)
809
1126
  query = {}
810
1127
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
@@ -823,45 +1140,63 @@ module MatrixSdk::Protocols::CS
823
1140
  request(:get, :client_r0, "/rooms/#{room_id}/members", query: query)
824
1141
  end
825
1142
 
826
- def set_join_rule(room_id, join_rule, **params)
827
- content = {
828
- join_rule: join_rule
829
- }
830
-
831
- send_state_event(room_id, 'm.room.join_rules', params.merge(content))
832
- end
833
-
834
- def set_guest_access(room_id, guest_access, **params)
835
- # raise ArgumentError, '`guest_access` must be one of [:can_join, :forbidden]' unless %i[can_join forbidden].include? guest_access
836
- content = {
837
- guest_access: guest_access
838
- }
839
-
840
- send_state_event(room_id, 'm.room.guest_access', params.merge(content))
841
- end
842
-
1143
+ # Gets a list of the current users registered devices
1144
+ # @return [Response] An object including all information about the users devices.
1145
+ # @see https://matrix.org/docs/spec/client_server/r0.4.0#get-matrix-client-r0-devices
1146
+ # The Matrix Spec, for more information about the data
843
1147
  def get_devices
844
1148
  request(:get, :client_r0, '/devices')
845
1149
  end
846
1150
 
1151
+ # Gets the information about a certain client device
1152
+ # @param [String] device_id The ID of the device to look up
1153
+ # @return [Response] An object containing all available device information
1154
+ # @see https://matrix.org/docs/spec/client_server/r0.4.0#get-matrix-client-r0-devices-deviceid
1155
+ # The Matrix Spec, for more information about the data
847
1156
  def get_device(device_id)
848
1157
  device_id = ERB::Util.url_encode device_id.to_s
849
1158
 
850
1159
  request(:get, :client_r0, "/devices/#{device_id}")
851
1160
  end
852
1161
 
1162
+ # Sets the metadata for a device
1163
+ # @param [String] device_id The ID of the device to modify
1164
+ # @param [String] display_name The new display name to set for the device
1165
+ # @see https://matrix.org/docs/spec/client_server/r0.4.0#put-matrix-client-r0-devices-deviceid
1166
+ # The Matrix Spec, for more information about the data
853
1167
  def set_device(device_id, display_name:)
854
1168
  device_id = ERB::Util.url_encode device_id.to_s
855
1169
 
856
1170
  request(:put, :client_r0, "/devices/#{device_id}", body: { display_name: display_name })
857
1171
  end
858
1172
 
1173
+ # Removes a device from the current user
1174
+ # @param [String] device_id The device to remove
1175
+ # @param [Hash] auth Authentication data for the removal request
1176
+ # @raise [MatrixNotAuthorizeedError] The request did not contain enough authentication information,
1177
+ # the data in this error will include the necessary information to perform interactive auth
1178
+ # @see https://matrix.org/docs/spec/client_server/r0.4.0#delete-matrix-client-r0-devices-deviceid
1179
+ # The Matrix Spec, for more information about the data
859
1180
  def delete_device(device_id, auth:)
860
1181
  device_id = ERB::Util.url_encode device_id.to_s
861
1182
 
862
1183
  request(:delete, :client_r0, "/devices/#{device_id}", body: { auth: auth })
863
1184
  end
864
1185
 
1186
+ # Run a query for device keys
1187
+ # @param [Numeric] timeout The timeout - in seconds - for the query
1188
+ # @param [Array] device_keys The list of devices to query
1189
+ # @param [String] token The sync token that led to this query - if any
1190
+ # @param [Hash] params Additional parameters
1191
+ # @option params [Integer] timeout_ms The timeout in milliseconds for the query, overrides _timeout_
1192
+ # @example Looking up all the device keys for a user
1193
+ # api.keys_query(device_keys: { '@alice:example.com': [] })
1194
+ # # => { :device_keys => { :'@alice:example.com' => { :JLAFKJWSCS => { ...
1195
+ # @example Looking up a specific device for a user
1196
+ # api.keys_query(device_keys: { '@alice:example.com': ['ABCDEFGHIJ'] })
1197
+ # # => { :device_keys => { :'@alice:example.com' => { :ABCDEFGHIJ => { ...
1198
+ # @see https://matrix.org/docs/spec/client_server/r0.4.0#post-matrix-client-r0-keys-query
1199
+ # The Matrix Spec, for more information about the parameters and data
865
1200
  def keys_query(timeout: nil, device_keys:, token: nil, **params)
866
1201
  body = {
867
1202
  timeout: (timeout || 10) * 1000,
@@ -873,6 +1208,8 @@ module MatrixSdk::Protocols::CS
873
1208
  request(:post, :client_r0, '/keys/query', body: body)
874
1209
  end
875
1210
 
1211
+ # Gets the MXID of the currently logged-in user
1212
+ # @return [Response] An object containing the key :user_id
876
1213
  def whoami?(**params)
877
1214
  query = {}
878
1215
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
@@ -880,3 +1217,4 @@ module MatrixSdk::Protocols::CS
880
1217
  request(:get, :client_r0, '/account/whoami', query: query)
881
1218
  end
882
1219
  end
1220
+ # rubocop:enable Metrics/ModuleLength
@@ -139,18 +139,18 @@ module MatrixSdk
139
139
 
140
140
  # Gets the avatar url of the room - if any
141
141
  def avatar_url
142
- @avatar_url ||= client.api.get_room_state(id, 'm.room.avatar').url
142
+ @avatar_url ||= client.api.get_room_avatar(id).url
143
143
  rescue MatrixNotFoundError
144
144
  # No avatar has been set
145
145
  nil
146
146
  end
147
147
 
148
148
  def guest_access
149
- @guest_access ||= client.api.get_room_state(id, 'm.room.guest_access').guest_access.to_sym
149
+ @guest_access ||= client.api.get_room_guest_access(id).guest_access.to_sym
150
150
  end
151
151
 
152
152
  def join_rule
153
- @join_rule ||= client.api.get_room_state(id, 'm.room.join_rules').join_rule.to_sym
153
+ @join_rule ||= client.api.get_room_join_rules(id).join_rule.to_sym
154
154
  end
155
155
 
156
156
  # Checks if +guest_access+ is set to +:can_join+
@@ -374,15 +374,24 @@ module MatrixSdk
374
374
  true
375
375
  end
376
376
 
377
+ # Returns a list of the room tags
378
+ # @return [Response] A list of the tags and their data, with add and remove methods implemented
379
+ # @example Managing tags
380
+ # room.tags
381
+ # # => { :room_tag => { data: false } }
382
+ # room.tags.add('some_tag', data: true)
383
+ # # => { :some_tag => { data: true }, :room_tag => { data: false} }
384
+ # room.tags.remove('room_tag')
385
+ # # => { :some_tag => { data: true} }
377
386
  def tags
378
387
  client.api.get_user_tags(client.mxid, id)[:tags].tap do |tag_obj|
379
388
  tag_obj.instance_variable_set(:@room, self)
380
389
  tag_obj.define_singleton_method(:room) do
381
390
  @room
382
391
  end
383
- tag_obj.define_singleton_method(:add) do |tag, params = {}|
384
- @room.add_tag(tag.to_s.to_sym, params)
385
- self[tag.to_s.to_sym] = params
392
+ tag_obj.define_singleton_method(:add) do |tag, **data|
393
+ @room.add_tag(tag.to_s.to_sym, data)
394
+ self[tag.to_s.to_sym] = data
386
395
  self
387
396
  end
388
397
  tag_obj.define_singleton_method(:remove) do |tag|
@@ -392,13 +401,18 @@ module MatrixSdk
392
401
  end
393
402
  end
394
403
 
404
+ # Remove a tag from the room
405
+ # @param [String] tag The tag to remove
395
406
  def remove_tag(tag)
396
407
  client.api.remove_user_tag(client.mxid, id, tag)
397
408
  true
398
409
  end
399
410
 
400
- def add_tag(tag, params = {})
401
- client.api.add_user_tag(client.mxid, id, tag, params)
411
+ # Add a tag to the room
412
+ # @param [String] tag The tag to add
413
+ # @param [Hash] data The data to assign to the tag
414
+ def add_tag(tag, **data)
415
+ client.api.add_user_tag(client.mxid, id, tag, data)
402
416
  true
403
417
  end
404
418
 
@@ -466,7 +480,7 @@ module MatrixSdk
466
480
  # alias list updates.
467
481
  def reload_aliases!
468
482
  begin
469
- new_aliases = client.api.get_room_state(id, 'm.room.aliases').aliases
483
+ new_aliases = client.api.get_room_aliases(id).aliases
470
484
  rescue MatrixNotFoundError
471
485
  data = client.api.get_room_state(id)
472
486
  new_aliases = data.select { |chunk| chunk[:type] == 'm.room.aliases' && chunk.key?(:content) && chunk[:content].key?(:aliases) }
@@ -507,10 +521,7 @@ module MatrixSdk
507
521
  avatar_url = URI(avatar_url) unless avatar_url.is_a? URI
508
522
  raise ArgumentError, 'Must be a valid MXC URL' unless avatar_url.is_a? URI::MATRIX
509
523
 
510
- content = {
511
- url: avatar_url
512
- }
513
- client.api.send_state_event(id, 'm.room.avatar', content)
524
+ client.api.set_room_avatar(id, avatar_url)
514
525
  @avatar_url = avatar_url
515
526
  end
516
527
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MatrixSdk
4
- VERSION = '1.1.1'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matrix_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Olofsson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-05 00:00:00.000000000 Z
11
+ date: 2019-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: logging
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: mocha
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -81,36 +67,32 @@ dependencies:
81
67
  - !ruby/object:Gem::Version
82
68
  version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
- name: ci_reporter_test_unit
70
+ name: logging
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
- - - ">="
73
+ - - "~>"
88
74
  - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
75
+ version: '2'
76
+ type: :runtime
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
- - - ">="
80
+ - - "~>"
95
81
  - !ruby/object:Gem::Version
96
- version: '0'
82
+ version: '2'
97
83
  description: SDK for applications using the Matrix protocol
98
84
  email:
99
85
  - ace@haxalot.com
100
86
  executables: []
101
87
  extensions: []
102
- extra_rdoc_files: []
88
+ extra_rdoc_files:
89
+ - CHANGELOG.md
90
+ - LICENSE.txt
91
+ - README.md
103
92
  files:
104
- - ".gitignore"
105
- - ".gitlab-ci.yml"
106
- - ".rubocop.yml"
107
- - ".travis.yml"
108
93
  - CHANGELOG.md
109
- - Gemfile
110
94
  - LICENSE.txt
111
95
  - README.md
112
- - Rakefile
113
- - examples/simple_client.rb
114
96
  - lib/matrix_sdk.rb
115
97
  - lib/matrix_sdk/api.rb
116
98
  - lib/matrix_sdk/application_service.rb
@@ -126,7 +108,6 @@ files:
126
108
  - lib/matrix_sdk/room.rb
127
109
  - lib/matrix_sdk/user.rb
128
110
  - lib/matrix_sdk/version.rb
129
- - matrix-sdk.gemspec
130
111
  homepage: https://github.com/ananace/ruby-matrix-sdk
131
112
  licenses:
132
113
  - MIT
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/.gitlab-ci.yml DELETED
@@ -1,41 +0,0 @@
1
- ---
2
- image: "ruby:2.6"
3
-
4
- # Cache gems in between builds
5
- cache:
6
- paths:
7
- - vendor/ruby
8
-
9
- before_script:
10
- - gem install bundler -N
11
- - bundle install -j $(nproc) --path vendor
12
-
13
- rubocop:
14
- before_script: []
15
- script:
16
- - gem install rubocop -N
17
- - rubocop lib
18
-
19
- test:
20
- coverage: '/\((\d+.\d+\%)\) covered/'
21
- script:
22
- - GENERATE_REPORTS=true CI_REPORTS=reports bundle exec rake test
23
- artifacts:
24
- expire_in: 1 week
25
- paths:
26
- - coverage/
27
- reports:
28
- junit: "reports/TEST-*.xml"
29
-
30
- pages:
31
- stage: deploy
32
- before_script: []
33
- script:
34
- - gem install yard -N
35
- - yard doc -o public/
36
- artifacts:
37
- paths:
38
- - public/
39
- only:
40
- - master
41
- when: always
data/.rubocop.yml DELETED
@@ -1,53 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 2.6
3
- Exclude:
4
- - '*.spec'
5
- - 'Rakefile'
6
-
7
- # Broken in CI
8
- Lint/Void:
9
- Enabled: false
10
-
11
- Style/ClassAndModuleChildren:
12
- Enabled: false
13
-
14
- # Don't enforce documentation
15
- Style/Documentation:
16
- Enabled: false
17
-
18
- Metrics/ClassLength:
19
- Enabled: false
20
-
21
- Metrics/MethodLength:
22
- Max: 50
23
-
24
- # Matrix has a lot of methods in the CS API
25
- Metrics/ModuleLength:
26
- Max: 500
27
-
28
- Metrics/LineLength:
29
- Max: 190
30
-
31
- Style/RescueModifier:
32
- Enabled: false
33
-
34
- Style/RegexpLiteral:
35
- Enabled: false
36
-
37
- Style/MultilineBlockChain:
38
- Enabled: false
39
-
40
- Metrics/AbcSize:
41
- Enabled: false
42
-
43
- Metrics/CyclomaticComplexity:
44
- Enabled: false
45
-
46
- Metrics/PerceivedComplexity:
47
- Enabled: false
48
-
49
- Style/FormatStringToken:
50
- Enabled: false
51
-
52
- Naming/AccessorMethodName:
53
- Enabled: false
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.7
5
- before_install: gem install bundler -v 1.14.6
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in matrix-sdk.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,15 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList['test/**/*_test.rb']
8
- end
9
-
10
- if ENV['GENERATE_REPORTS'] == 'true'
11
- require 'ci/reporter/rake/test_unit'
12
- task :test => 'ci:setup:testunit'
13
- end
14
-
15
- task :default => :test
@@ -1,148 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'io/console'
4
- require 'matrix_sdk'
5
-
6
- # A filter to only discover joined rooms
7
- ROOM_DISCOVERY_FILTER = {
8
- event_fields: %w[sender membership],
9
- presence: { senders: [], types: [] },
10
- account_data: { senders: [], types: [] },
11
- room: {
12
- ephemeral: { senders: [], types: [] },
13
- state: {
14
- senders: [],
15
- types: [
16
- 'm.room.aliases',
17
- 'm.room.canonical_alias',
18
- 'm.room.member'
19
- ],
20
- lazy_load_members: true
21
- },
22
- timeline: { senders: [], types: [] },
23
- account_data: { senders: [], types: [] }
24
- }
25
- }.freeze
26
-
27
- # A filter to only retrieve messages from rooms
28
- ROOM_STATE_FILTER = {
29
- presence: { senders: [], types: [] },
30
- account_data: { senders: [], types: [] },
31
- room: {
32
- ephemeral: { senders: [], types: [] },
33
- state: {
34
- types: ['m.room.member'],
35
- lazy_load_members: true
36
- },
37
- timeline: {
38
- types: ['m.room.message']
39
- },
40
- account_data: { senders: [], types: [] }
41
- }
42
- }.freeze
43
-
44
-
45
- class SimpleClient < MatrixSdk::Client
46
- def initialize(hs_url)
47
- super hs_url, sync_filter_limit: 10
48
-
49
- @pls = {}
50
- @tracked_rooms = []
51
- @filter = ROOM_STATE_FILTER.dup
52
- end
53
-
54
- def add_listener(room)
55
- room.on_event.add_handler { |ev| on_message(room, ev) }
56
- @tracked_rooms << room.id
57
- end
58
-
59
- def run
60
- # Only track messages from the listened rooms
61
- @filter[:room][:rooms] = @tracked_rooms
62
- start_listener_thread(filter: @filter.to_json)
63
- end
64
-
65
- private
66
-
67
- def get_user_level(room, mxid)
68
- levels = @pls[room.id] ||= api.get_power_levels(room.id)[:users]
69
- levels[mxid.to_sym]
70
- end
71
-
72
- def on_message(room, event)
73
- case event.type
74
- when 'm.room.member'
75
- puts "[#{Time.now.strftime '%H:%M'}] #{event[:content][:displayname]} joined." if event.membership == 'join'
76
- when 'm.room.message'
77
- user = get_user event.sender
78
- admin_level = get_user_level(room, user.id) || 0
79
- prefix = ' '
80
- prefix = '+' if admin_level >= 50
81
- prefix = '@' if admin_level >= 100
82
- if %w[m.text m.notice].include? event.content[:msgtype]
83
- notice = event.content[:msgtype] == 'm.notice'
84
- puts "[#{Time.now.strftime '%H:%M'}] <#{prefix}#{user.display_name}> #{"\033[1;30m" if notice}#{event.content[:body]}#{"\033[0m" if notice}"
85
- elsif event[:content][:msgtype] == 'm.emote'
86
- puts "[#{Time.now.strftime '%H:%M'}] *#{prefix}#{user.display_name} #{event.content[:body]}"
87
- else
88
- puts "[#{Time.now.strftime '%H:%M'}] <#{prefix}#{user.display_name}> (#{event.content[:msgtype]}) #{event.content[:body]} - #{api.get_download_url event.content[:url]}"
89
- end
90
- end
91
- end
92
- end
93
-
94
- if $PROGRAM_NAME == __FILE__
95
- raise "Usage: #{$PROGRAM_NAME} [-d] homeserver_url room_id_or_alias" unless ARGV.length >= 2
96
- begin
97
- if ARGV.first == '-d'
98
- Thread.abort_on_exception = true
99
- MatrixSdk.debug!
100
- ARGV.shift
101
- end
102
-
103
- client = SimpleClient.new ARGV.first
104
- ARGV.shift
105
-
106
- print 'Username: '
107
- user = STDIN.gets.strip
108
- puts 'Password: '
109
- password = STDIN.noecho(&:gets).strip
110
-
111
- puts 'Logging in...'
112
- client.login(user, password, no_sync: true)
113
-
114
- # Only retrieve list of joined room in first sync
115
- sync_filter = client.sync_filter.merge(ROOM_DISCOVERY_FILTER)
116
- sync_filter[:room][:state][:senders] << client.mxid
117
- client.listen_for_events(timeout: 5, filter: sync_filter.to_json)
118
-
119
- puts 'Finding room...'
120
- room = client.find_room(ARGV.last)
121
- room ||= begin
122
- puts 'Joining room...'
123
- client.join_room(ARGV.last)
124
- end
125
-
126
- client.add_listener(room)
127
-
128
- puts 'Starting listener'
129
- client.run
130
-
131
- puts 'Entering main loop'
132
- loop do
133
- print '> '
134
- msg = STDIN.gets.strip
135
- break if msg.start_with? '/quit'
136
-
137
- if msg.start_with? '/me'
138
- room.send_emote msg.gsub(/\/me\s*/, '')
139
- else
140
- room.send_text msg
141
- end
142
- end
143
- rescue Interrupt
144
- puts 'Interrupted, exiting...'
145
- ensure
146
- client.logout if client && client.logged_in?
147
- end
148
- end
data/matrix-sdk.gemspec DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.join File.expand_path('lib', __dir__), 'matrix_sdk/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'matrix_sdk'
7
- spec.version = MatrixSdk::VERSION
8
- spec.authors = ['Alexander Olofsson']
9
- spec.email = ['ace@haxalot.com']
10
-
11
- spec.summary = 'SDK for applications using the Matrix protocol'
12
- spec.description = spec.summary
13
- spec.homepage = 'https://github.com/ananace/ruby-matrix-sdk'
14
- spec.license = 'MIT'
15
-
16
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
- f.match(%r{^(test|spec|features)/})
18
- end
19
- spec.require_paths = ['lib']
20
-
21
- spec.add_dependency 'logging', '~> 2'
22
-
23
- spec.add_development_dependency 'mocha'
24
- spec.add_development_dependency 'rake'
25
- spec.add_development_dependency 'simplecov'
26
- spec.add_development_dependency 'test-unit'
27
-
28
- # TODO: Put this in a better location
29
- spec.add_development_dependency 'ci_reporter_test_unit'
30
- end