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.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'pp'
4
+
3
5
  unless Object.respond_to? :yield_self
4
6
  class Object
5
7
  def yield_self
@@ -51,21 +53,29 @@ module MatrixSdk
51
53
 
52
54
  def ignore_inspect(*symbols)
53
55
  class_eval %*
54
- def inspect
55
- reentrant = caller_locations.any? { |l| l.absolute_path == __FILE__ && l.label == 'inspect' }
56
- "\\\#<\#{self.class} \#{instance_variables
56
+ include PP::ObjectMixin
57
+
58
+ def pretty_print_instance_variables
59
+ instance_variables
57
60
  .reject { |f| %i[#{symbols.map { |s| "@#{s}" }.join ' '}].include? f }
58
- .map { |f| "\#{f}=\#{reentrant ? instance_variable_get(f) : instance_variable_get(f).inspect}" }.join " " }}>"
61
+ .sort
59
62
  end
60
- *, __FILE__, __LINE__ - 7
63
+
64
+ def pretty_print(pp)
65
+ pp.pp(self)
66
+ end
67
+
68
+ alias inspect pretty_print_inspect
69
+ *, __FILE__, __LINE__ - 14
61
70
  end
62
71
  end
63
72
 
64
73
  module Logging
65
74
  def logger
66
75
  return MatrixSdk.logger if MatrixSdk.global_logger?
76
+ return @logger if instance_variable_defined?(:@logger) && @logger
67
77
 
68
- @logger ||= ::Logging.logger[self]
78
+ ::Logging.logger[self]
69
79
  end
70
80
 
71
81
  def logger=(logger)
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MatrixSdk::Util
4
+ class StateEventCache
5
+ extend MatrixSdk::Extensions
6
+ extend MatrixSdk::Util::Tinycache
7
+ include Enumerable
8
+
9
+ attr_reader :room
10
+
11
+ attr_accessor :cache_time
12
+
13
+ ignore_inspect :client, :room, :tinycache_adapter
14
+
15
+ def initialize(room, cache_time: 30 * 60, **_params)
16
+ raise ArgumentError, 'Must be given a Room instance' unless room.is_a? MatrixSdk::Room
17
+
18
+ @room = room
19
+ @cache_time = cache_time
20
+ end
21
+
22
+ def client
23
+ @room.client
24
+ end
25
+
26
+ def reload!
27
+ tinycache_adapter.clear
28
+ end
29
+
30
+ def keys
31
+ tinycache_adapter.send(:cache).keys.map do |type|
32
+ real_type = type.split('|').first
33
+ state_key = type.split('|').last
34
+ state_key = nil if state_key == real_type
35
+
36
+ [real_type, state_key]
37
+ end
38
+ end
39
+
40
+ def values
41
+ keys.map { |key| tinycache_adapter.read(key) }
42
+ end
43
+
44
+ def size
45
+ keys.count
46
+ end
47
+
48
+ def key?(type, key = nil)
49
+ keys.key?("#{type}#{key ? "|#{key}" : ''}")
50
+ end
51
+
52
+ def expire(type, key = nil)
53
+ tinycache_adapter.expire("#{type}#{key ? "|#{key}" : ''}")
54
+ end
55
+
56
+ def each(live: false)
57
+ return to_enum(__method__, live: live) { keys.count } unless block_given?
58
+
59
+ keys.each do |type|
60
+ real_type = type.split('|').first
61
+ state_key = type.split('|').last
62
+ state_key = nil if state_key == real_type
63
+
64
+ v = live ? self[real_type, key: state_key] : tinycache_adapter.read(type)
65
+ # hash = v.hash
66
+ yield [real_type, state_key], v
67
+ # self[key] = v if hash != v.hash
68
+ end
69
+ end
70
+
71
+ def delete(type, key = nil)
72
+ type = type.to_s unless type.is_a? String
73
+ client.api.set_room_state(room.id, type, {}, **{ state_key: key }.compact)
74
+ tinycache_adapter.delete("#{type}#{key ? "|#{key}" : ''}")
75
+ end
76
+
77
+ def [](type, key = nil)
78
+ type = type.to_s unless type.is_a? String
79
+ tinycache_adapter.fetch("#{type}#{key ? "|#{key}" : ''}", expires_in: @cache_time) do
80
+ client.api.get_room_state(room.id, type, **{ key: key }.compact)
81
+ rescue MatrixSdk::MatrixNotFoundError
82
+ {}
83
+ end
84
+ end
85
+
86
+ def []=(type, key = nil, value) # rubocop:disable Style/OptionalArguments Not possible to put optional last
87
+ type = type.to_s unless type.is_a? String
88
+ client.api.set_room_state(room.id, type, value, **{ state_key: key }.compact)
89
+ tinycache_adapter.write("#{type}#{key ? "|#{key}" : ''}", value)
90
+ end
91
+ end
92
+ end
@@ -75,13 +75,19 @@ module MatrixSdk::Util
75
75
 
76
76
  define_method(method_names[:with_cache]) do |*args|
77
77
  tinycache_adapter.fetch(__send__(method_names[:cache_key], *args), expires_in: expires_in) do
78
- __send__(method_names[:without_cache], *args)
78
+ named = args.delete_at(-1) if args.last.is_a? Hash
79
+ named ||= {}
80
+
81
+ __send__(method_names[:without_cache], *args, **named)
79
82
  end
80
83
  end
81
84
 
82
85
  define_method(method_names[:without_cache]) do |*args|
83
86
  orig = method(method_name).super_method
84
- orig.call(*args)
87
+ named = args.delete_at(-1) if args.last.is_a? Hash
88
+ named ||= {}
89
+
90
+ orig.call(*args, **named)
85
91
  end
86
92
 
87
93
  define_method(method_names[:clear_cache]) do |*args|
@@ -2,8 +2,12 @@
2
2
 
3
3
  module MatrixSdk::Util
4
4
  class TinycacheAdapter
5
+ extend MatrixSdk::Extensions
6
+
5
7
  attr_accessor :config, :client
6
8
 
9
+ ignore_inspect :client
10
+
7
11
  def initialize
8
12
  @config = {}
9
13
 
@@ -54,12 +58,18 @@ module MatrixSdk::Util
54
58
  true
55
59
  end
56
60
 
61
+ def expire(key)
62
+ return unless exist? key
63
+
64
+ cache[key].expires_at = Time.at(0)
65
+ end
66
+
57
67
  def clear
58
68
  @cache = {}
59
69
  end
60
70
 
61
71
  def cleanup
62
- @cache.delete_if { |_, v| v.expired? }
72
+ @cache.select { |_, v| v.expired? }.each { |_, v| v.value = nil }
63
73
  end
64
74
 
65
75
  private
@@ -7,14 +7,20 @@ module URI
7
7
  class MXC < Generic
8
8
  def full_path
9
9
  select(:host, :port, :path, :query, :fragment)
10
- .reject(&:nil?)
10
+ .compact
11
11
  .join
12
12
  end
13
13
  end
14
14
 
15
- @@schemes['MXC'] = MXC
15
+ # TODO: Use +register_scheme+ on Ruby >=3.1 and fall back to old behavior
16
+ # for older Rubies. May be removed at EOL of Ruby 3.0.
17
+ if respond_to? :register_scheme
18
+ register_scheme 'MXC', MXC
19
+ else
20
+ @@schemes['MXC'] = MXC
21
+ end
16
22
 
17
- unless @@schemes.key? 'MATRIX'
23
+ unless scheme_list.key? 'MATRIX'
18
24
  # A matrix: URI according to MSC2312
19
25
  class MATRIX < Generic
20
26
  attr_reader :authority, :action, :mxid, :mxid2, :via
@@ -84,6 +90,12 @@ module URI
84
90
  end
85
91
  end
86
92
 
87
- @@schemes['MATRIX'] = MATRIX
93
+ # TODO: Use +register_scheme+ on Ruby >=3.1 and fall back to old behavior
94
+ # for older Rubies. May be removed at EOL of Ruby 3.0.
95
+ if respond_to? :register_scheme
96
+ register_scheme 'MATRIX', MATRIX
97
+ else
98
+ @@schemes['MATRIX'] = MATRIX
99
+ end
88
100
  end
89
101
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MatrixSdk
4
- VERSION = '2.5.0'
4
+ VERSION = '2.8.0'
5
5
  end
data/lib/matrix_sdk.rb CHANGED
@@ -10,7 +10,7 @@ autoload :Logging, 'logging'
10
10
 
11
11
  module MatrixSdk
12
12
  autoload :Api, 'matrix_sdk/api'
13
- autoload :ApplicationService, 'matrix_sdk/application_service'
13
+ # autoload :ApplicationService, 'matrix_sdk/application_service'
14
14
  autoload :Client, 'matrix_sdk/client'
15
15
  autoload :MXID, 'matrix_sdk/mxid'
16
16
  autoload :Response, 'matrix_sdk/response'
@@ -28,11 +28,17 @@ module MatrixSdk
28
28
  autoload :MatrixTimeoutError, 'matrix_sdk/errors'
29
29
  autoload :MatrixUnexpectedResponseError, 'matrix_sdk/errors'
30
30
 
31
+ module Bot
32
+ autoload :Base, 'matrix_sdk/bot/base'
33
+ end
34
+
31
35
  module Rooms
32
36
  autoload :Space, 'matrix_sdk/rooms/space'
33
37
  end
34
38
 
35
39
  module Util
40
+ autoload :AccountDataCache, 'matrix_sdk/util/account_data_cache'
41
+ autoload :StateEventCache, 'matrix_sdk/util/state_event_cache'
36
42
  autoload :Tinycache, 'matrix_sdk/util/tinycache'
37
43
  autoload :TinycacheAdapter, 'matrix_sdk/util/tinycache_adapter'
38
44
  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.5.0
4
+ version: 2.8.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: 2022-01-14 00:00:00.000000000 Z
11
+ date: 2022-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mocha
@@ -95,6 +95,9 @@ files:
95
95
  - README.md
96
96
  - lib/matrix_sdk.rb
97
97
  - lib/matrix_sdk/api.rb
98
+ - lib/matrix_sdk/bot.rb
99
+ - lib/matrix_sdk/bot/base.rb
100
+ - lib/matrix_sdk/bot/main.rb
98
101
  - lib/matrix_sdk/client.rb
99
102
  - lib/matrix_sdk/errors.rb
100
103
  - lib/matrix_sdk/mxid.rb
@@ -107,8 +110,10 @@ files:
107
110
  - lib/matrix_sdk/room.rb
108
111
  - lib/matrix_sdk/rooms/space.rb
109
112
  - lib/matrix_sdk/user.rb
113
+ - lib/matrix_sdk/util/account_data_cache.rb
110
114
  - lib/matrix_sdk/util/events.rb
111
115
  - lib/matrix_sdk/util/extensions.rb
116
+ - lib/matrix_sdk/util/state_event_cache.rb
112
117
  - lib/matrix_sdk/util/tinycache.rb
113
118
  - lib/matrix_sdk/util/tinycache_adapter.rb
114
119
  - lib/matrix_sdk/util/uri.rb
@@ -132,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
137
  - !ruby/object:Gem::Version
133
138
  version: '0'
134
139
  requirements: []
135
- rubygems_version: 3.2.22
140
+ rubygems_version: 3.3.8
136
141
  signing_key:
137
142
  specification_version: 4
138
143
  summary: SDK for applications using the Matrix protocol