matrix_sdk 2.5.0 → 2.8.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: 24a67aeb6af66f75480104c5c0dc44987e2fe54163ed96047da58bf091476752
4
- data.tar.gz: e5935a94b430a1b195b55d748415cc0ad95ea649e32f5e97045cc82726dd2bd3
3
+ metadata.gz: fd493a721423c1f1a7da7b2ff678ad8e1a949f258fe613616ebedab39d5ae204
4
+ data.tar.gz: 52e6563d1dbb42cd5acddf3809df720fb6ecc5b6f49cbdc14606450e908c32c6
5
5
  SHA512:
6
- metadata.gz: 1ea88ea4c76b573dad7b03aeba49e91b8f7c01040118c38032b49ca00cfbe687bd931c4e17387545e27df529f6ae06fc835ee964b5e79489fb0c5cf84186b35b
7
- data.tar.gz: e2a6a5cd78e7629ffb2cc4af5ec9c9154f2f63db570479ebb2c6fa9aff13b7b231bea877d56694ae799019698af8d4a87394f88fa162dcb172dbbd1ede09fc8f
6
+ metadata.gz: 1fa0a6cbae2b6fa8ab91961df79082017be6a50028cc7b318ad70a5be9c76b8488b4b24cbef5e5a768ac34076236430683d518c18fccb296290599427bd71095
7
+ data.tar.gz: f62ad6791ee5f3921869d2431e55e3a1f44501af1b8c966a957fbcacbcaf809edb2e47b8d8b65d1393c268ff58b19e21fc966b1660c671d4d257121bf832dadf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,30 @@
1
+ ## 2.8.0 - 2022-08-19
2
+
3
+ - Drops support for EoL Ruby 2.6
4
+ - Adds helpers for accessing room state
5
+ - Fixes Client#sync_token usage
6
+ - Improves caching of state events in rooms
7
+
8
+ ## 2.7.0 - 2022-08-12
9
+
10
+ - Adds a bot subsystem + DSL
11
+ - [An example](examples/bot_api.rb) can be found in the examples folder
12
+ - Adds additional useful helpers for Room and User
13
+ - Fixes Client.new_for_domain
14
+ - Improves the handling of MXIDs as strings
15
+ - Improves handling of caches for rooms
16
+ - Improves Client shutdown when using Client#start_listener_thread
17
+ - Improves account data handling, with caches on both Client and Room level
18
+
19
+ ## 2.6.0 - 2022-07-15
20
+
21
+ - Adds some multi-thread usage support to the API (create your API/client with `threadsafe: :multithread/true/false`)
22
+ The API will currently default to running with multi-threaded requests. In case your application is single-threaded - or never performs requests from multiple threads - then you can set `threadsafe: true/false` to support connection reuse.
23
+ - Changes room finding to ignore non-canonical aliases by default
24
+ - Improves room alias handling
25
+ - Improves Ruby 3.0+ support
26
+ - Improves debug output by supporting PP (Ruby pretty print) on all MatrixSdk objects
27
+
1
28
  ## 2.5.0 - 2022-01-14
2
29
 
3
30
  - Adds preliminary support for the Matrix v1.1 `client/v3` API
data/README.md CHANGED
@@ -8,6 +8,8 @@ Live YARD documentation can be found at; https://ruby-sdk.ananace.dev
8
8
 
9
9
  ## Example usage
10
10
 
11
+ For more fully-featured examples, check the [examples](examples/) folder.
12
+
11
13
  ```ruby
12
14
  # Raw API usage
13
15
  require 'matrix_sdk'
@@ -24,7 +26,7 @@ api.request :get, :federation_v1, '/version'
24
26
  ```
25
27
 
26
28
  ```ruby
27
- # Client wrapper
29
+ # Client wrapper with login
28
30
  require 'matrix_sdk'
29
31
 
30
32
  client = MatrixSdk::Client.new 'https://example.com'
@@ -41,7 +43,7 @@ hq.send_text "This is an example message - don't actually do this ;)"
41
43
  ```
42
44
 
43
45
  ```ruby
44
- # Client wrapper
46
+ # Client wrapper with token
45
47
  require 'matrix_sdk'
46
48
 
47
49
  client = MatrixSdk::Client.new 'https://example.com'
@@ -56,6 +58,19 @@ client.rooms.count
56
58
  # => 5
57
59
  ```
58
60
 
61
+ ```ruby
62
+ #!/bin/env ruby
63
+ # Bot DSL
64
+ require 'matrix_sdk/bot'
65
+
66
+ command :plug do
67
+ room.send_text <<~PLUG
68
+ The Ruby SDK is a fine method for writing applications communicating over the Matrix protocol.
69
+ It can easily be integrated with Rails, and it supports most client/bot use-cases.
70
+ PLUG
71
+ end
72
+ ```
73
+
59
74
  ## Contributing
60
75
 
61
76
  Bug reports and pull requests are welcome on GitHub at https://github.com/ananace/ruby-matrix-sdk
@@ -19,7 +19,7 @@ module MatrixSdk
19
19
  }.freeze
20
20
 
21
21
  attr_accessor :access_token, :connection_address, :connection_port, :device_id, :autoretry, :global_headers
22
- attr_reader :homeserver, :validate_certificate, :open_timeout, :read_timeout, :well_known, :proxy_uri
22
+ attr_reader :homeserver, :validate_certificate, :open_timeout, :read_timeout, :well_known, :proxy_uri, :threadsafe
23
23
 
24
24
  ignore_inspect :access_token, :logger
25
25
 
@@ -39,7 +39,10 @@ module MatrixSdk
39
39
  # @option params [Hash] :global_headers Additional headers to set for all requests
40
40
  # @option params [Boolean] :skip_login Should the API skip logging in if the HS URL contains user information
41
41
  # @option params [Boolean] :synapse (true) Is the API connecting to a Synapse instance
42
- # @option params [Hash] :well_known The .well-known object that the server was discovered through, should not be set manually
42
+ # @option params [Boolean,:multithread] :threadsafe (:multithread) Should the connection be threadsafe/mutexed - or
43
+ # safe for simultaneous multi-thread usage. Will default to +:multithread+ - a.k.a. per-thread HTTP connections
44
+ # and requests
45
+ # @note Using threadsafe +:multithread+ currently doesn't support connection re-use
43
46
  def initialize(homeserver, **params)
44
47
  @homeserver = homeserver
45
48
  raise ArgumentError, 'Homeserver URL must be String or URI' unless @homeserver.is_a?(String) || @homeserver.is_a?(URI)
@@ -64,7 +67,9 @@ module MatrixSdk
64
67
  @global_headers.merge!(params.fetch(:global_headers)) if params.key? :global_headers
65
68
  @synapse = params.fetch(:synapse, true)
66
69
  @http = nil
67
- @http_lock = Mutex.new
70
+ @inflight = []
71
+
72
+ self.threadsafe = params.fetch(:threadsafe, :multithread)
68
73
 
69
74
  ([params.fetch(:protocols, [:CS])].flatten - protocols).each do |proto|
70
75
  self.class.include MatrixSdk::Protocols.const_get(proto)
@@ -244,6 +249,17 @@ module MatrixSdk
244
249
  @proxy_uri = proxy_uri
245
250
  end
246
251
 
252
+ # @param [Boolean,:multithread] threadsafe What level of thread-safety the API should use
253
+ # @return [Boolean,:multithread]
254
+ def threadsafe=(threadsafe)
255
+ raise ArgumentError, 'Threadsafe must be either a boolean or :multithread' unless [true, false, :multithread].include? threadsafe
256
+ raise ArugmentError, 'JRuby only support :multithread/false for threadsafe' if RUBY_ENGINE == 'jruby' && threadsafe == true
257
+
258
+ @threadsafe = threadsafe
259
+ @http_lock = nil unless threadsafe == true
260
+ @threadsafe
261
+ end
262
+
247
263
  # Perform a raw Matrix API request
248
264
  #
249
265
  # @example Simple API query
@@ -284,14 +300,25 @@ module MatrixSdk
284
300
  print_http(req_obj, id: req_id)
285
301
  response = duration = nil
286
302
 
287
- @http_lock.synchronize do
303
+ loc_http = http
304
+ perform_request = proc do
305
+ @inflight << loc_http
288
306
  dur_start = Time.now
289
- response = http.request req_obj
307
+ response = loc_http.request req_obj
290
308
  dur_end = Time.now
291
309
  duration = dur_end - dur_start
292
310
  rescue EOFError
293
311
  logger.error 'Socket closed unexpectedly'
294
312
  raise
313
+ ensure
314
+ @inflight.delete loc_http
315
+ end
316
+
317
+ if @threadsafe == true
318
+ http_lock.synchronize { perform_request.call }
319
+ else
320
+ perform_request.call
321
+ loc_http.finish if @threadsafe == :multithread
295
322
  end
296
323
  print_http(response, duration: duration, id: req_id)
297
324
 
@@ -333,6 +360,10 @@ module MatrixSdk
333
360
  ret
334
361
  end
335
362
 
363
+ def stop_inflight
364
+ @inflight.each(&:finish)
365
+ end
366
+
336
367
  private
337
368
 
338
369
  def construct_request(method:, url:, **options)
@@ -395,18 +426,26 @@ module MatrixSdk
395
426
 
396
427
  host = (@connection_address || homeserver.host)
397
428
  port = (@connection_port || homeserver.port)
398
- @http ||= if proxy_uri
399
- Net::HTTP.new(host, port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
400
- else
401
- Net::HTTP.new(host, port)
402
- end
403
-
404
- @http.open_timeout = open_timeout if open_timeout
405
- @http.read_timeout = read_timeout if read_timeout
406
- @http.use_ssl = homeserver.scheme == 'https'
407
- @http.verify_mode = validate_certificate ? ::OpenSSL::SSL::VERIFY_PEER : ::OpenSSL::SSL::VERIFY_NONE
408
- @http.start
409
- @http
429
+
430
+ connection = @http unless @threadsafe == :multithread
431
+ connection ||= if proxy_uri
432
+ Net::HTTP.new(host, port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
433
+ else
434
+ Net::HTTP.new(host, port)
435
+ end
436
+
437
+ connection.open_timeout = open_timeout if open_timeout
438
+ connection.read_timeout = read_timeout if read_timeout
439
+ connection.use_ssl = homeserver.scheme == 'https'
440
+ connection.verify_mode = validate_certificate ? ::OpenSSL::SSL::VERIFY_PEER : ::OpenSSL::SSL::VERIFY_NONE
441
+ connection.start
442
+ @http = connection unless @threadsafe == :multithread
443
+
444
+ connection
445
+ end
446
+
447
+ def http_lock
448
+ @http_lock ||= Mutex.new if @threadsafe == true
410
449
  end
411
450
  end
412
451
  end