matrix_sdk 2.1.2 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module URI
6
+ # A mxc:// Matrix content URL
7
+ class MXC < Generic
8
+ def full_path
9
+ select(:host, :port, :path, :query, :fragment)
10
+ .reject(&:nil?)
11
+ .join
12
+ end
13
+ end
14
+
15
+ @@schemes['MXC'] = MXC
16
+
17
+ unless @@schemes.key? 'MATRIX'
18
+ # A matrix: URI according to MSC2312
19
+ class MATRIX < Generic
20
+ attr_reader :authority, :action, :mxid, :mxid2, :via
21
+
22
+ def initialize(*args)
23
+ super(*args)
24
+
25
+ @action = nil
26
+ @authority = nil
27
+ @mxid = nil
28
+ @mxid2 = nil
29
+ @via = nil
30
+
31
+ raise InvalidComponentError, 'missing opaque part for matrix URL' if !@opaque && !@path
32
+
33
+ if @path
34
+ @authority = @host
35
+ @authority += ":#{@port}" if @port
36
+ else
37
+ @path, @query = @opaque.split('?')
38
+ @query, @fragment = @query.split('#') if @query&.include? '#'
39
+ @path, @fragment = @path.split('#') if @path&.include? '#'
40
+ @path = "/#{path}"
41
+ @opaque = nil
42
+ end
43
+
44
+ components = @path.delete_prefix('/').split('/', -1)
45
+ raise InvalidComponentError, 'component count must be 2 or 4' if components.size != 2 && components.size != 4
46
+
47
+ sigil = case components.shift
48
+ when 'u', 'user'
49
+ '@'
50
+ when 'r', 'room'
51
+ '#'
52
+ when 'roomid'
53
+ '!'
54
+ else
55
+ raise InvalidComponentError, 'invalid component in path'
56
+ end
57
+
58
+ component = components.shift
59
+ raise InvalidComponentError, "component can't be empty" if component.nil? || component.empty?
60
+
61
+ @mxid = MatrixSdk::MXID.new("#{sigil}#{component}")
62
+
63
+ if components.size == 2
64
+ sigil2 = case components.shift
65
+ when 'e', 'event'
66
+ '$'
67
+ else
68
+ raise InvalidComponentError, 'invalid component in path'
69
+ end
70
+ component = components.shift
71
+ raise InvalidComponentError, "component can't be empty" if component.nil? || component.empty?
72
+
73
+ @mxid2 = MatrixSdk::MXID.new("#{sigil2}#{component}")
74
+ end
75
+
76
+ return unless @query
77
+
78
+ @action = @query.match(/action=([^&]+)/)&.captures&.first&.to_sym
79
+ @via = @query.scan(/via=([^&]+)/)&.flatten&.compact
80
+ end
81
+
82
+ def mxid2?
83
+ !@mxid2.nil?
84
+ end
85
+ end
86
+
87
+ @@schemes['MATRIX'] = MATRIX
88
+ end
89
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MatrixSdk
4
- VERSION = '2.1.2'
4
+ VERSION = '2.4.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.2
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Olofsson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-10 00:00:00.000000000 Z
11
+ date: 2021-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mocha
@@ -95,10 +95,8 @@ files:
95
95
  - README.md
96
96
  - lib/matrix_sdk.rb
97
97
  - lib/matrix_sdk/api.rb
98
- - lib/matrix_sdk/application_service.rb
99
98
  - lib/matrix_sdk/client.rb
100
99
  - lib/matrix_sdk/errors.rb
101
- - lib/matrix_sdk/extensions.rb
102
100
  - lib/matrix_sdk/mxid.rb
103
101
  - lib/matrix_sdk/protocols/as.rb
104
102
  - lib/matrix_sdk/protocols/cs.rb
@@ -107,13 +105,19 @@ files:
107
105
  - lib/matrix_sdk/protocols/ss.rb
108
106
  - lib/matrix_sdk/response.rb
109
107
  - lib/matrix_sdk/room.rb
108
+ - lib/matrix_sdk/rooms/space.rb
110
109
  - lib/matrix_sdk/user.rb
110
+ - lib/matrix_sdk/util/events.rb
111
+ - lib/matrix_sdk/util/extensions.rb
112
+ - lib/matrix_sdk/util/tinycache.rb
113
+ - lib/matrix_sdk/util/tinycache_adapter.rb
114
+ - lib/matrix_sdk/util/uri.rb
111
115
  - lib/matrix_sdk/version.rb
112
116
  homepage: https://github.com/ananace/ruby-matrix-sdk
113
117
  licenses:
114
118
  - MIT
115
119
  metadata: {}
116
- post_install_message:
120
+ post_install_message:
117
121
  rdoc_options: []
118
122
  require_paths:
119
123
  - lib
@@ -128,8 +132,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
132
  - !ruby/object:Gem::Version
129
133
  version: '0'
130
134
  requirements: []
131
- rubygems_version: 3.1.2
132
- signing_key:
135
+ rubygems_version: 3.2.14
136
+ signing_key:
133
137
  specification_version: 4
134
138
  summary: SDK for applications using the Matrix protocol
135
139
  test_files: []
@@ -1,212 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'matrix_sdk'
4
-
5
- module MatrixSdk
6
- class ApplicationService
7
- include MatrixSdk::Logging
8
- attr_reader :api, :port
9
-
10
- def_delegators :@api,
11
- :access_token, :access_token=, :device_id, :device_id=, :homeserver, :homeserver=,
12
- :validate_certificate, :validate_certificate=
13
-
14
- def initialize(hs_url, as_token:, hs_token:, default_routes: true, **params)
15
- logger.warning 'This abstraction is still under HEAVY development, expect errors'
16
-
17
- params = { protocols: %i[AS CS] }.merge(params).merge(access_token: as_token)
18
- if hs_url.is_a? Api
19
- @api = hs_url
20
- params.each do |k, v|
21
- api.instance_variable_set("@#{k}", v) if api.instance_variable_defined? "@#{k}"
22
- end
23
- else
24
- @api = Api.new hs_url, params
25
- end
26
-
27
- @id = params.fetch(:id, MatrixSdk::Api::USER_AGENT)
28
- @port = params.fetch(:port, 8888)
29
- @url = params.fetch(:url, URI("http://localhost:#{@port}"))
30
- @as_token = as_token
31
- @hs_token = hs_token
32
-
33
- @method_map = {}
34
-
35
- if default_routes
36
- add_method(:GET, '/_matrix/app/v1/users/', %r{^/_matrix/app/v1/users/(?<user>[^/]+)$}, :do_get_user)
37
- add_method(:GET, '/_matrix/app/v1/rooms/', %r{^/_matrix/app/v1/rooms/(?<room>[^/]+)$}, :do_get_room)
38
-
39
- add_method(:GET, '/_matrix/app/v1/thirdparty/protocol/', %r{^/_matrix/app/v1/thirdparty/protocol/(?<protocol>[^/]+)$}, :do_get_3p_protocol_p)
40
- add_method(:GET, '/_matrix/app/v1/thirdparty/user/', %r{^/_matrix/app/v1/thirdparty/user/(?<protocol>[^/]+)$}, :do_get_3p_user_p)
41
- add_method(:GET, '/_matrix/app/v1/thirdparty/location/', %r{^/_matrix/app/v1/thirdparty/location/(?<protocol>[^/]+)$}, :do_get_3p_location_p)
42
- add_method(:GET, '/_matrix/app/v1/thirdparty/user', %r{^/_matrix/app/v1/thirdparty/user$}, :do_get_3p_user)
43
- add_method(:GET, '/_matrix/app/v1/thirdparty/location', %r{^/_matrix/app/v1/thirdparty/location$}, :do_get_3p_location)
44
-
45
- add_method(:PUT, '/_matrix/app/v1/transactions/', %r{^/_matrix/app/v1/transactions/(?<txn_id>[^/]+)$}, :do_put_transaction)
46
-
47
- if params.fetch(:legacy_routes, false)
48
- add_method(:GET, '/users/', %r{^/users/(?<user>[^/]+)$}, :do_get_user)
49
- add_method(:GET, '/rooms/', %r{^/rooms/(?<room>[^/]+)$}, :do_get_room)
50
-
51
- add_method(:GET, '/_matrix/app/unstable/thirdparty/protocol/', %r{^/_matrix/app/unstable/thirdparty/protocol/(?<protocol>[^/]+)$}, :do_get_3p_protocol_p)
52
- add_method(:GET, '/_matrix/app/unstable/thirdparty/user/', %r{^/_matrix/app/unstable/thirdparty/user/(?<protocol>[^/]+)$}, :do_get_3p_user_p)
53
- add_method(:GET, '/_matrix/app/unstable/thirdparty/location/', %r{^/_matrix/app/unstable/thirdparty/location/(?<protocol>[^/]+)$}, :do_get_3p_location_p)
54
- add_method(:GET, '/_matrix/app/unstable/thirdparty/user', %r{^/_matrix/app/unstable/thirdparty/user$}, :do_get_3p_user)
55
- add_method(:GET, '/_matrix/app/unstable/thirdparty/location', %r{^/_matrix/app/unstable/thirdparty/location$}, :do_get_3p_location)
56
-
57
- add_method(:PUT, '/transactions/', %r{^/transactions/(?<txn_id>[^/]+)$}, :do_put_transaction)
58
- end
59
- end
60
-
61
- start_server
62
- end
63
-
64
- def registration
65
- {
66
- id: @id,
67
- url: @url,
68
- as_token: @as_token,
69
- hs_token: @hs_token,
70
- sender_localpart: '',
71
- namespaces: {
72
- users: [],
73
- aliases: [],
74
- rooms: []
75
- },
76
- rate_limited: false,
77
- protocols: []
78
- }
79
- end
80
-
81
- def port=(port)
82
- raise ArgumentError, 'Port must be a number' unless port.is_a? Numeric
83
-
84
- raise NotImplementedError, "Can't change port of a running server" if server.status != :Stop
85
-
86
- @port = port
87
- end
88
-
89
- protected
90
-
91
- def add_method(verb, prefix, regex, proc = nil, &block)
92
- proc ||= block
93
- raise ArgumentError, 'No method specified' if proc.nil?
94
-
95
- method_entry = (@method_map[verb] ||= {})[regex] = {
96
- verb: verb,
97
- prefix: prefix,
98
- proc: proc
99
- }
100
- return true unless @server
101
-
102
- server.mount_proc(method.prefix) { |req, res| _handle_proc(verb, method_entry, req, res) }
103
- end
104
-
105
- def do_get_user(user:, **params)
106
- [user, params]
107
- raise NotImplementedError
108
- end
109
-
110
- def do_get_room(room:, **params)
111
- [room, params]
112
- raise NotImplementedError
113
- end
114
-
115
- def do_get_3p_protocol_p(protocol:, **params)
116
- [protocol, params]
117
- raise NotImplementedError
118
- end
119
-
120
- def do_get_3p_user_p(protocol:, **params)
121
- [protocol, params]
122
- raise NotImplementedError
123
- end
124
-
125
- def do_get_3p_location_p(protocol:, **params)
126
- [protocol, params]
127
- raise NotImplementedError
128
- end
129
-
130
- def do_get_3p_location(**params)
131
- [protocol, params]
132
- raise NotImplementedError
133
- end
134
-
135
- def do_get_3p_user(**params)
136
- [protocol, params]
137
- raise NotImplementedError
138
- end
139
-
140
- def do_put_transaction(txn_id:, **params)
141
- [txn_id, params]
142
- raise NotImplementedError
143
- end
144
-
145
- def start_server
146
- server.start
147
-
148
- @method_map.each do |verb, method_entry|
149
- # break if verb != method_entry[:verb]
150
-
151
- method = method_entry[:proc]
152
- server.mount_proc(method.prefix) { |req, res| _handle_proc(verb, method_entry, req, res) }
153
- end
154
-
155
- logger.info "Application Service is now running on port #{port}"
156
- end
157
-
158
- def stop_server
159
- @server&.shutdown
160
- @server = nil
161
- end
162
-
163
- private
164
-
165
- def _handle_proc(verb, method_entry, req, res)
166
- logger.debug "Received request for #{verb} #{method_entry}"
167
- match = regex.match(req.request_uri.path)
168
- match_hash = Hash[match.names.zip(match.captures)].merge(
169
- request: req,
170
- response: res
171
- )
172
-
173
- if method.is_a? Symbol
174
- send method, match_hash
175
- else
176
- method.call match_hash
177
- end
178
- end
179
-
180
- def server
181
- @server ||= WEBrick::HTTPServer.new(Port: port, ServerSoftware: "#{MatrixSdk::Api::USER_AGENT} (Ruby #{RUBY_VERSION})").tap do |server|
182
- server.mount_proc '/', &:handle_request
183
- end
184
- end
185
-
186
- def handle_request(request, response)
187
- logger.debug "Received request #{request.inspect}"
188
-
189
- req_method = request.request_method.to_s.to_sym
190
- req_uri = request.request_uri
191
-
192
- map = @method_map[req_method]
193
- raise WEBrick::HTTPStatus[405], { message: 'Unsupported verb' }.to_json if map.nil?
194
-
195
- discovered = map.find { |k, _v| k =~ req_uri.path }
196
- raise WEBrick::HTTPStatus[404], { message: 'Unknown request' }.to_json if discovered.nil?
197
-
198
- method = discovered.last
199
- match = Regexp.last_match
200
- match_hash = Hash[match.names.zip(match.captures)].merge(
201
- request: request,
202
- response: response
203
- )
204
-
205
- if method.is_a? Symbol
206
- send method, match_hash
207
- else
208
- method.call match_hash
209
- end
210
- end
211
- end
212
- end
@@ -1,197 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'uri'
4
-
5
- module URI
6
- # A mxc:// Matrix content URL
7
- class MATRIX < Generic
8
- def full_path
9
- select(:host, :port, :path, :query, :fragment)
10
- .reject(&:nil?)
11
- .join
12
- end
13
- end
14
-
15
- @@schemes['MXC'] = MATRIX
16
- end
17
-
18
- unless Object.respond_to? :yield_self
19
- class Object
20
- def yield_self
21
- yield(self)
22
- end
23
- end
24
- end
25
-
26
- module MatrixSdk
27
- module Extensions
28
- def events(*symbols)
29
- module_name = "#{name}Events"
30
-
31
- initializers = []
32
- readers = []
33
- methods = []
34
-
35
- symbols.each do |sym|
36
- name = sym.to_s
37
-
38
- initializers << "
39
- @on_#{name} = MatrixSdk::EventHandlerArray.new
40
- "
41
- readers << ":on_#{name}"
42
- methods << "
43
- def fire_#{name}(ev, filter = nil)
44
- @on_#{name}.fire(ev, filter)
45
- when_#{name}(ev) if !ev.handled?
46
- end
47
-
48
- def when_#{name}(ev); end
49
- "
50
- end
51
-
52
- class_eval "
53
- module #{module_name}
54
- attr_reader #{readers.join ', '}
55
-
56
- def event_initialize
57
- #{initializers.join}
58
- end
59
-
60
- #{methods.join}
61
- end
62
-
63
- include #{module_name}
64
- ", __FILE__, __LINE__ - 12
65
- end
66
-
67
- def ignore_inspect(*symbols)
68
- class_eval %*
69
- def inspect
70
- reentrant = caller_locations.any? { |l| l.absolute_path == __FILE__ && l.label == 'inspect' }
71
- "\#{to_s[0..-2]} \#{instance_variables
72
- .reject { |f| %i[#{symbols.map { |s| "@#{s}" }.join ' '}].include? f }
73
- .map { |f| "\#{f}=\#{reentrant ? instance_variable_get(f) : instance_variable_get(f).inspect}" }.join " " }}>"
74
- end
75
- *, __FILE__, __LINE__ - 7
76
- end
77
- end
78
-
79
- module Logging
80
- def logger
81
- return MatrixSdk.logger if MatrixSdk.global_logger?
82
-
83
- @logger ||= ::Logging.logger[self]
84
- end
85
-
86
- def logger=(logger)
87
- @logger = logger
88
- end
89
- end
90
-
91
- class EventHandlerArray < Hash
92
- include MatrixSdk::Logging
93
- attr_accessor :reraise_exceptions
94
-
95
- def initialize(*args)
96
- @reraise_exceptions = false
97
-
98
- super(*args)
99
- end
100
-
101
- def add_handler(filter = nil, id = nil, &block)
102
- id ||= block.hash
103
- self[id] = { filter: filter, id: id, block: block }
104
- end
105
-
106
- def remove_handler(id)
107
- delete id
108
- end
109
-
110
- def fire(event, filter = nil)
111
- reverse_each do |_k, h|
112
- begin
113
- h[:block].call(event) if !h[:filter] || event.matches?(h[:filter], filter)
114
- rescue StandardError => e
115
- logger.error "#{e.class.name} occurred when firing event (#{event})\n#{e}"
116
-
117
- raise e if @reraise_exceptions
118
- end
119
- end
120
- end
121
- end
122
-
123
- class Event
124
- extend MatrixSdk::Extensions
125
-
126
- attr_writer :handled
127
-
128
- ignore_inspect :sender
129
-
130
- def initialize(sender)
131
- @sender = sender
132
- @handled = false
133
- end
134
-
135
- def handled?
136
- @handled
137
- end
138
-
139
- def matches?(_filter)
140
- true
141
- end
142
- end
143
-
144
- class ErrorEvent < Event
145
- attr_accessor :error
146
-
147
- def initialize(error, source)
148
- @error = error
149
- super source
150
- end
151
-
152
- def source
153
- @sender
154
- end
155
- end
156
-
157
- class MatrixEvent < Event
158
- attr_accessor :event, :filter
159
-
160
- ignore_inspect :sender
161
-
162
- def initialize(sender, event = nil, filter = nil)
163
- @event = event
164
- @filter = filter || @event[:type]
165
- super sender
166
- end
167
-
168
- def matches?(filter, filter_override = nil)
169
- return true if filter_override.nil? && (@filter.nil? || filter.nil?)
170
-
171
- to_match = filter_override || @filter
172
- if filter.is_a? Regexp
173
- filter.match(to_match) { true } || false
174
- else
175
- to_match == filter
176
- end
177
- end
178
-
179
- def [](key)
180
- event[key]
181
- end
182
-
183
- def to_s
184
- "#{event[:type]}: #{event.reject { |k, _v| k == :type }.to_json}"
185
- end
186
-
187
- def method_missing(method, *args)
188
- return event[method] if event.key? method
189
-
190
- super
191
- end
192
-
193
- def respond_to_missing?(method, *)
194
- event.key? method
195
- end
196
- end
197
- end