matrix_sdk 2.1.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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