From 7ee128f1858c747813ce912732f074e9ecdfe464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 26 Mar 2019 13:45:25 +0100 Subject: [PATCH] daemon: Add initial room sending support. Note: Sending a message for now works only if the other clients have started Olm sessions for us already. --- pantalaimon/daemon.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/pantalaimon/daemon.py b/pantalaimon/daemon.py index a5e8e81..300f414 100755 --- a/pantalaimon/daemon.py +++ b/pantalaimon/daemon.py @@ -282,6 +282,37 @@ class ProxyDaemon: text=json.dumps(json_response) ) + async def send_message(self, request): + access_token = self.get_access_token(request) + + if not access_token: + return self._missing_token + + try: + client = self.client_sessions[access_token] + except KeyError: + return self._uknown_token + + msgtype = request.match_info["event_type"] + room_id = request.match_info["room_id"] + txnid = request.match_info["txnid"] + + try: + content = await request.json() + except JSONDecodeError: + return self._not_json + + try: + response = await client.room_send(room_id, msgtype, content, txnid) + except GroupEncryptionError: + await client.share_group_session(room_id) + response = await client.room_send(room_id, msgtype, content, txnid) + + return web.Response( + status=response.transport_response.status, + text=await response.transport_response.text() + ) + async def shutdown(self, app): """Shut the daemon down closing all the client sessions it has. @@ -303,8 +334,12 @@ async def init(): app.add_routes([ web.post("/_matrix/client/r0/login", proxy.login), web.get("/_matrix/client/r0/sync", proxy.sync), + web.put( + r"/_matrix/client/r0/rooms/{room_id}/send/{event_type}/{txnid}", + proxy.send_message + ), ]) - app.router.add_route('*', "/" + '{proxyPath:.*}', proxy.router) + app.router.add_route("*", "/" + "{proxyPath:.*}", proxy.router) app.on_shutdown.append(proxy.shutdown) return proxy, app