matrix_sdk 2.1.3 → 2.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: 550080056a38ee354ff8e633069d71f207e1927a567fcaed4f7ed9792bcbb79a
4
- data.tar.gz: ae8909f5a1092873453c4b7a876315efd29882cee353252d9bd92f069ce3c4ac
3
+ metadata.gz: 4f16ccd1ed10b02a963b9dbd736e577375ca41b26fb45743bc0344f219b1d70f
4
+ data.tar.gz: 3d8eef54f71ccf9e04c0da896e6189fd8d4c2c34c269e2890de37dd4cec023a6
5
5
  SHA512:
6
- metadata.gz: 5838b47c8ea7df7becdb7dadcb07e57c31526c8c3cdd025a18cc2add7db9e9da9f0aff62a16ed4a3a5306566785f941e7cdba90e664f2b872959c7774b4ec4bc
7
- data.tar.gz: 3fcad5ea834b2dd0e6d68e55a8e88e5a88b444d07491f381420c4749ca1ff5375030f3a7c4b1b535ff947422caf7c4b86f81633d425614a05e5577aa9a4963ff
6
+ metadata.gz: 01de0cccbcf0724a101526190ea4e164ec1235daaa0e0c3d42ed445e760246b2b81a054ba4bd7e88229fdb0833e4fe3a8624a4de3dfd032bfa50b4f23946caf1
7
+ data.tar.gz: 1b177862e3b01aef4c10de6a4318b8348a5c6e00885f0b137ce13a269bf0d5579e6a72b93a87a9366b8d75ec2549518a7d21f2da5db4d7f1d60fa464ff11d83b
@@ -1,3 +1,9 @@
1
+ ## 2.2.0 - 2020-11-20
2
+
3
+ - Adds direct message (1:1) room mapping to client abstraction
4
+ - Adds Api#get_room_event_context (#13)
5
+ - Improves support for JRuby
6
+
1
7
  ## 2.1.3 - 2020-09-18
2
8
 
3
9
  - Adds separate state event handler as Client#on_state_event
@@ -268,23 +268,6 @@ module MatrixSdk
268
268
  u.query = [u.query, URI.encode_www_form(options.fetch(:query))].flatten.compact.join('&') if options[:query]
269
269
  u.query = nil if u.query.nil? || u.query.empty?
270
270
  end
271
- request = Net::HTTP.const_get(method.to_s.capitalize.to_sym).new url.request_uri
272
- request.body = options[:body] if options.key? :body
273
- request.body = request.body.to_json if options.key?(:body) && !request.body.is_a?(String)
274
- request.body_stream = options[:body_stream] if options.key? :body_stream
275
-
276
- global_headers.each { |h, v| request[h] = v }
277
- if request.body || request.body_stream
278
- request.content_type = 'application/json'
279
- request.content_length = (request.body || request.body_stream).size
280
- end
281
-
282
- request['authorization'] = "Bearer #{access_token}" if access_token && !options.fetch(:skip_auth, false)
283
- if options.key? :headers
284
- options[:headers].each do |h, v|
285
- request[h.to_s.downcase] = v
286
- end
287
- end
288
271
 
289
272
  failures = 0
290
273
  loop do
@@ -292,10 +275,11 @@ module MatrixSdk
292
275
 
293
276
  req_id = ('A'..'Z').to_a.sample(4).join
294
277
 
295
- print_http(request, id: req_id)
278
+ req_obj = construct_request(url: url, method: method, **options)
279
+ print_http(req_obj, id: req_id)
296
280
  begin
297
281
  dur_start = Time.now
298
- response = http.request request
282
+ response = http.request req_obj
299
283
  dur_end = Time.now
300
284
  duration = dur_end - dur_start
301
285
  rescue EOFError
@@ -344,14 +328,38 @@ module MatrixSdk
344
328
 
345
329
  private
346
330
 
331
+ def construct_request(method:, url:, **options)
332
+ request = Net::HTTP.const_get(method.to_s.capitalize.to_sym).new url.request_uri
333
+
334
+ # FIXME: Handle bodies better, avoid duplicating work
335
+ request.body = options[:body] if options.key? :body
336
+ request.body = request.body.to_json if options.key?(:body) && !request.body.is_a?(String)
337
+ request.body_stream = options[:body_stream] if options.key? :body_stream
338
+
339
+ global_headers.each { |h, v| request[h] = v }
340
+ if request.body || request.body_stream
341
+ request.content_type = 'application/json'
342
+ request.content_length = (request.body || request.body_stream).size
343
+ end
344
+
345
+ request['authorization'] = "Bearer #{access_token}" if access_token && !options.fetch(:skip_auth, false)
346
+ if options.key? :headers
347
+ options[:headers].each do |h, v|
348
+ request[h.to_s.downcase] = v
349
+ end
350
+ end
351
+
352
+ request
353
+ end
354
+
347
355
  def print_http(http, body: true, duration: nil, id: nil)
348
356
  return unless logger.debug?
349
357
 
350
358
  if http.is_a? Net::HTTPRequest
351
- dir = "#{id ? id + ' : ' : nil}>"
359
+ dir = "#{id ? "#{id} : " : nil}>"
352
360
  logger.debug "#{dir} Sending a #{http.method} request to `#{http.path}`:"
353
361
  else
354
- dir = "#{id ? id + ' : ' : nil}<"
362
+ dir = "#{id ? "#{id} : " : nil}<"
355
363
  logger.debug "#{dir} Received a #{http.code} #{http.message} response:#{duration ? " [#{(duration * 1000).to_i}ms]" : nil}"
356
364
  end
357
365
  http.to_hash.map { |k, v| "#{k}: #{k == 'authorization' ? '[ REDACTED ]' : v.join(', ')}" }.each do |h|
@@ -360,7 +368,7 @@ module MatrixSdk
360
368
  logger.debug dir
361
369
  if body
362
370
  clean_body = JSON.parse(http.body) rescue nil if http.body
363
- clean_body.keys.each { |k| clean_body[k] = '[ REDACTED ]' if %w[password access_token].include?(k) }.to_json if clean_body.is_a? Hash
371
+ clean_body.each_key { |k| clean_body[k] = '[ REDACTED ]' if %w[password access_token].include?(k) }.to_json if clean_body.is_a? Hash
364
372
  clean_body = clean_body.to_s if clean_body
365
373
  logger.debug "#{dir} #{clean_body.length < 200 ? clean_body : clean_body.slice(0..200) + "... [truncated, #{clean_body.length} Bytes]"}" if clean_body
366
374
  end
@@ -156,6 +156,27 @@ module MatrixSdk
156
156
  rooms
157
157
  end
158
158
 
159
+ # Gets a list of all direct chat rooms (1:1 chats / direct message chats) for the currenct user
160
+ #
161
+ # @return [Hash[String,Array[String]]] A mapping of MXIDs to a list of direct rooms with that user
162
+ def direct_rooms
163
+ Hash[api.get_account_data(mxid, 'm.direct').map do |mxid, rooms|
164
+ [mxid.to_s, rooms]
165
+ end]
166
+ end
167
+
168
+ # Gets a direct message room for the given user if one exists
169
+ #
170
+ # @note Will return the oldest room if multiple exist
171
+ # @return [Room,nil] A direct message room if one exists
172
+ def direct_room(mxid)
173
+ mxid = MatrixSdk::MXID.new mxid.to_s unless mxid.is_a? MatrixSdk::MXID
174
+ raise ArgumentError, 'Must be a valid user ID' unless mxid.user?
175
+
176
+ room_id = direct_rooms[mxid.to_s]&.first
177
+ ensure_room room_id if room_id
178
+ end
179
+
159
180
  # Gets a list of all relevant rooms, either the ones currently handled by
160
181
  # the client, or the list of currently joined ones if no rooms are handled
161
182
  #
@@ -433,10 +454,11 @@ module MatrixSdk
433
454
  errors = 0
434
455
  thread, cancel_token = api.msc2108_sync_sse(params) do |data, event:, id:|
435
456
  @next_batch = id if id
436
- if event.to_sym == :sync
457
+ case event.to_sym
458
+ when :sync
437
459
  handle_sync_response(data)
438
460
  errors = 0
439
- elsif event.to_sym == :sync_error
461
+ when :sync_error
440
462
  logger.error "SSE Sync error received; #{data.type}: #{data.message}"
441
463
  errors += 1
442
464
 
@@ -191,7 +191,9 @@ module MatrixSdk
191
191
  end
192
192
 
193
193
  def respond_to_missing?(method, *)
194
- event.key? method
194
+ return true if event.key? method
195
+
196
+ super
195
197
  end
196
198
  end
197
199
  end
@@ -32,13 +32,13 @@ module MatrixSdk
32
32
  #
33
33
  # @return [String]
34
34
  def homeserver
35
- port_s = port ? ':' + port.to_s : ''
35
+ port_s = port ? ":#{port}" : ''
36
36
  domain ? domain + port_s : ''
37
37
  end
38
38
 
39
39
  # Gets the homserver part of the ID as a suffix (':homeserver')
40
40
  def homeserver_suffix
41
- ':' + homeserver if domain
41
+ ":#{homeserver}" if domain
42
42
  end
43
43
 
44
44
  def to_s
@@ -49,18 +49,13 @@ module MatrixSdk
49
49
  #
50
50
  # @return [Symbol] The MXID type, one of (:user_id, :room_id, :event_id, :group_id, or :room_alias)
51
51
  def type
52
- case sigil
53
- when '@'
54
- :user_id
55
- when '!'
56
- :room_id
57
- when '$'
58
- :event_id
59
- when '+'
60
- :group_id
61
- when '#'
62
- :room_alias
63
- end
52
+ {
53
+ '@' => :user_id,
54
+ '!' => :room_id,
55
+ '$' => :event_id,
56
+ '+' => :group_id,
57
+ '#' => :room_alias
58
+ }[sigil]
64
59
  end
65
60
 
66
61
  # Checks if the ID is valid
@@ -767,6 +767,30 @@ module MatrixSdk::Protocols::CS
767
767
  request(:get, :client_r0, "/rooms/#{room_id}/state", query: query)
768
768
  end
769
769
 
770
+ # Retrieves number of events that happened just before and after the specified event
771
+ #
772
+ # @param room_id [MXID,String] The room to get events from.
773
+ # @param event_id [MXID,String] The event to get context around.
774
+ # @option params [Integer] :limit (10) The limit of messages to retrieve
775
+ # @option params [String] :filter A filter to limit the retrieval to
776
+ # @return [Response] A response hash with contextual event information
777
+ # @see https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-rooms-roomid-context-eventid
778
+ # The Matrix Spec, for more information about the call and response
779
+ # @example Find event context with filter and limit specified
780
+ # api.get_room_event_context('#room:example.com', '$event_id:example.com', filter: { types: ['m.room.message'] }.to_json, limit: 20)
781
+ def get_room_event_context(room_id, event_id, **params)
782
+ query = {}
783
+ query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
784
+
785
+ query[:limit] = params.fetch(:limit) if params.key? :limit
786
+ query[:filter] = params.fetch(:filter) if params.key? :filter
787
+
788
+ room_id = ERB::Util.url_encode room_id.to_s
789
+ event_id = ERB::Util.url_encode event_id.to_s
790
+
791
+ request(:get, :client_r0, "/rooms/#{room_id}/context/#{event_id}", query: query)
792
+ end
793
+
770
794
  ## Specialized getters for specced state
771
795
  #
772
796
 
@@ -1085,7 +1109,7 @@ module MatrixSdk::Protocols::CS
1085
1109
  # @return [Response] The resulting state event
1086
1110
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-guest-server-acl
1087
1111
  # The Matrix Spec, for more information about the event and data
1088
- def set_room_server_acl(room_id, allow_ip_literals: false, allow:, deny:, **params)
1112
+ def set_room_server_acl(room_id, allow:, deny:, allow_ip_literals: false, **params)
1089
1113
  content = {
1090
1114
  allow_ip_literals: allow_ip_literals,
1091
1115
  allow: allow,
@@ -1712,7 +1736,7 @@ module MatrixSdk::Protocols::CS
1712
1736
  # # => { :device_keys => { :'@alice:example.com' => { :ABCDEFGHIJ => { ...
1713
1737
  # @see https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-keys-query
1714
1738
  # The Matrix Spec, for more information about the parameters and data
1715
- def keys_query(timeout: nil, device_keys:, token: nil, **params)
1739
+ def keys_query(device_keys:, timeout: nil, token: nil, **params)
1716
1740
  body = {
1717
1741
  timeout: (timeout || 10) * 1000,
1718
1742
  device_keys: device_keys
@@ -1834,7 +1858,7 @@ module MatrixSdk::Protocols::CS
1834
1858
  # @return [Response] A response hash containing the full data of the requested push rule
1835
1859
  # @see https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushrules-scope-kind-ruleid
1836
1860
  # The Matrix Spec, for more information about the parameters and data
1837
- def get_pushrule(scope: 'global', kind:, id:)
1861
+ def get_pushrule(kind:, id:, scope: 'global')
1838
1862
  scope = ERB::Util.url_encode scope.to_s
1839
1863
  kind = ERB::Util.url_encode kind.to_s
1840
1864
  id = ERB::Util.url_encode id.to_s
@@ -1850,7 +1874,7 @@ module MatrixSdk::Protocols::CS
1850
1874
  # @return [Response] A response hash containing an :enabled key for if the rule is enabled or not
1851
1875
  # @see https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushrules-scope-kind-ruleid-enabled
1852
1876
  # The Matrix Spec, for more information about the parameters and data
1853
- def get_pushrule_enabled(scope: 'global', kind:, id:)
1877
+ def get_pushrule_enabled(kind:, id:, scope: 'global')
1854
1878
  scope = ERB::Util.url_encode scope.to_s
1855
1879
  kind = ERB::Util.url_encode kind.to_s
1856
1880
  id = ERB::Util.url_encode id.to_s
@@ -1867,7 +1891,7 @@ module MatrixSdk::Protocols::CS
1867
1891
  # @return [Response] An empty response hash if the push rule was enabled/disabled successfully
1868
1892
  # @see https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-pushrules-scope-kind-ruleid-enabled
1869
1893
  # The Matrix Spec, for more information about the parameters and data
1870
- def set_pushrule_enabled(enabled, scope: 'global', kind:, id:)
1894
+ def set_pushrule_enabled(enabled, kind:, id:, scope: 'global')
1871
1895
  scope = ERB::Util.url_encode scope.to_s
1872
1896
  kind = ERB::Util.url_encode kind.to_s
1873
1897
  id = ERB::Util.url_encode id.to_s
@@ -1887,7 +1911,7 @@ module MatrixSdk::Protocols::CS
1887
1911
  # @return [Response] A response hash containing an :enabled key for if the rule is enabled or not
1888
1912
  # @see https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushrules-scope-kind-ruleid-actions
1889
1913
  # The Matrix Spec, for more information about the parameters and data
1890
- def get_pushrule_actions(scope: 'global', kind:, id:)
1914
+ def get_pushrule_actions(kind:, id:, scope: 'global')
1891
1915
  scope = ERB::Util.url_encode scope.to_s
1892
1916
  kind = ERB::Util.url_encode kind.to_s
1893
1917
  id = ERB::Util.url_encode id.to_s
@@ -1904,7 +1928,7 @@ module MatrixSdk::Protocols::CS
1904
1928
  # @return [Response] An empty response hash if the push rule actions were modified successfully
1905
1929
  # @see https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-pushrules-scope-kind-ruleid-actions
1906
1930
  # The Matrix Spec, for more information about the parameters and data
1907
- def set_pushrule_actions(actions, scope: 'global', kind:, id:)
1931
+ def set_pushrule_actions(actions, kind:, id:, scope: 'global')
1908
1932
  scope = ERB::Util.url_encode scope.to_s
1909
1933
  kind = ERB::Util.url_encode kind.to_s
1910
1934
  id = ERB::Util.url_encode id.to_s
@@ -47,7 +47,7 @@ module MatrixSdk::Protocols::MSC
47
47
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
48
48
 
49
49
  req = Net::HTTP::Get.new(homeserver.dup.tap do |u|
50
- u.path = api_to_path(:client_r0) + '/sync/sse'
50
+ u.path = "#{api_to_path :client_r0}/sync/sse"
51
51
  u.query = URI.encode_www_form(query)
52
52
  end)
53
53
  req['accept'] = 'text/event-stream'
@@ -46,7 +46,9 @@ module MatrixSdk
46
46
  attr_reader :api
47
47
 
48
48
  def respond_to_missing?(name, *_args)
49
- key? name
49
+ return true if key? name
50
+
51
+ super
50
52
  end
51
53
 
52
54
  def method_missing(name, *args)
@@ -104,7 +104,6 @@ module MatrixSdk
104
104
  logger.debug "Created room #{room_id}"
105
105
  end
106
106
 
107
-
108
107
  #
109
108
  # Event handlers
110
109
  #
@@ -127,7 +126,6 @@ module MatrixSdk
127
126
  ensure_room_handlers[:ephemeral_event]
128
127
  end
129
128
 
130
-
131
129
  #
132
130
  # State readers
133
131
  #
@@ -365,7 +363,7 @@ module MatrixSdk
365
363
  # @param reverse [Boolean] whether to fill messages in reverse or not
366
364
  # @param limit [Integer] the maximum number of messages to backfill
367
365
  # @note This will trigger the `on_event` events as messages are added
368
- def backfill_messages(reverse = false, limit = 10)
366
+ def backfill_messages(reverse = false, limit = 10) # rubocop:disable Style/OptionalBooleanParameter
369
367
  data = client.api.get_room_messages(id, @prev_batch, direction: :b, limit: limit)
370
368
 
371
369
  events = data[:chunk]
@@ -122,6 +122,14 @@ module MatrixSdk
122
122
  Time.now - (since / 1000)
123
123
  end
124
124
 
125
+ # Gets a direct message room with the user if one exists
126
+ #
127
+ # @return [Room,nil] A direct message room if one exists
128
+ # @see MatrixSdk::Client#direct_room
129
+ def direct_room
130
+ client.direct_room(id)
131
+ end
132
+
125
133
  # Returns all the current device keys for the user, retrieving them if necessary
126
134
  def device_keys
127
135
  @device_keys ||= client.api.keys_query(device_keys: { id => [] }).yield_self do |resp|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MatrixSdk
4
- VERSION = '2.1.3'
4
+ VERSION = '2.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matrix_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.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: 2020-09-18 00:00:00.000000000 Z
11
+ date: 2020-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mocha