Skip to content
Snippets Groups Projects
Commit d5f03c1a authored by Tulir Asokan's avatar Tulir Asokan :cat2:
Browse files

Add API for refreshing connection

parent f7690032
No related branches found
No related tags found
No related merge requests found
...@@ -46,6 +46,9 @@ class PublicBridgeWebsite: ...@@ -46,6 +46,9 @@ class PublicBridgeWebsite:
self.app.router.add_options("/api/login", self.login_options) self.app.router.add_options("/api/login", self.login_options)
self.app.router.add_post("/api/login", self.login) self.app.router.add_post("/api/login", self.login)
self.app.router.add_post("/api/logout", self.logout) self.app.router.add_post("/api/logout", self.logout)
self.app.router.add_post("/api/disconnect", self.disconnect)
self.app.router.add_post("/api/reconnect", self.reconnect)
self.app.router.add_post("/api/refresh", self.refresh)
self.app.router.add_static("/", pkg_resources.resource_filename("mautrix_facebook", self.app.router.add_static("/", pkg_resources.resource_filename("mautrix_facebook",
"web/static/")) "web/static/"))
...@@ -106,9 +109,11 @@ class PublicBridgeWebsite: ...@@ -106,9 +109,11 @@ class PublicBridgeWebsite:
"facebook": None, "facebook": None,
} }
if await user.is_logged_in(): if await user.is_logged_in():
info = cast(fbchat.UserData, await user.client.fetch_thread_info([user.fbid]).__anext__()) info = cast(fbchat.UserData,
await user.client.fetch_thread_info([user.fbid]).__anext__())
data["facebook"] = attr.asdict(info) data["facebook"] = attr.asdict(info)
del data["facebook"]["session"] del data["facebook"]["session"]
data["facebook"]["connected"] = user.is_connected
return web.json_response(data, headers=self._acao_headers) return web.json_response(data, headers=self._acao_headers)
async def login(self, request: web.Request) -> web.Response: async def login(self, request: web.Request) -> web.Response:
...@@ -149,3 +154,25 @@ class PublicBridgeWebsite: ...@@ -149,3 +154,25 @@ class PublicBridgeWebsite:
if puppet.is_real_user: if puppet.is_real_user:
await puppet.switch_mxid(None, None) await puppet.switch_mxid(None, None)
return web.json_response({}, headers=self._acao_headers) return web.json_response({}, headers=self._acao_headers)
async def disconnect(self, request: web.Request) -> web.Response:
user = self.check_token(request)
if not user.is_connected:
raise web.HTTPBadRequest(body='{"error": "User is not connected"}',
headers=self._headers)
user.listener.disconnect()
await user.listen_task
return web.json_response({}, headers=self._acao_headers)
async def reconnect(self, request: web.Request) -> web.Response:
user = self.check_token(request)
if user.is_connected:
raise web.HTTPConflict(body='{"error": "User is already connected"}',
headers=self._headers)
user.start_listen()
return web.json_response({}, headers=self._acao_headers)
async def refresh(self, request: web.Request) -> web.Response:
user = self.check_token(request)
await user.try_refresh()
return web.json_response({}, headers=self._acao_headers)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment