matrix_sdk 2.1.3 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -0
- data/README.md +1 -1
- data/lib/matrix_sdk/api.rb +49 -32
- data/lib/matrix_sdk/client.rb +71 -54
- data/lib/matrix_sdk/errors.rb +4 -0
- data/lib/matrix_sdk/mxid.rb +50 -15
- data/lib/matrix_sdk/protocols/cs.rb +182 -124
- data/lib/matrix_sdk/protocols/msc.rb +5 -1
- data/lib/matrix_sdk/response.rb +3 -1
- data/lib/matrix_sdk/room.rb +335 -102
- data/lib/matrix_sdk/rooms/space.rb +79 -0
- data/lib/matrix_sdk/user.rb +11 -3
- data/lib/matrix_sdk/util/events.rb +111 -0
- data/lib/matrix_sdk/util/extensions.rb +75 -0
- data/lib/matrix_sdk/util/tinycache.rb +134 -0
- data/lib/matrix_sdk/util/tinycache_adapter.rb +77 -0
- data/lib/matrix_sdk/util/uri.rb +89 -0
- data/lib/matrix_sdk/version.rb +1 -1
- data/lib/matrix_sdk.rb +16 -1
- metadata +12 -8
- data/lib/matrix_sdk/application_service.rb +0 -212
- data/lib/matrix_sdk/extensions.rb +0 -197
@@ -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
|