Stop creating connections for unauthenticated users at startup

This commit is contained in:
Tulir Asokan
2018-06-25 21:30:54 +03:00
parent 0f8a2e7c51
commit 371c6813de
4 changed files with 9 additions and 9 deletions
+2 -2
View File
@@ -95,7 +95,7 @@ class AbstractUser:
async def has_full_access(self, allow_bot=False):
return self.whitelisted and (not self.is_bot or allow_bot) and await self.is_logged_in()
async def start(self):
async def start(self, delete_unless_authenticated=False):
if not self.client:
self._init_client()
await self.client.connect()
@@ -113,7 +113,7 @@ class AbstractUser:
self.session_container.Session.query.filter(
self.session_container.Session.session_id == self.mxid).count() > 0)
if not self.connected and should_connect:
await self.start()
await self.start(delete_unless_authenticated=not even_if_no_session)
return self
def stop(self):
+2 -2
View File
@@ -59,8 +59,8 @@ class Bot(AbstractUser):
if isinstance(id, int):
self.tg_whitelist.append(id)
async def start(self):
await super().start()
async def start(self, delete_unless_authenticated=False):
await super().start(delete_unless_authenticated)
if not await self.is_logged_in():
await self.client.sign_in(bot_token=self.token)
await self.post_login()
+3 -1
View File
@@ -159,12 +159,14 @@ class PublicBridgeWebsite:
if "mxid" not in data:
return self.render_login(error="Please enter your Matrix ID.", status=400)
user = await User.get_by_mxid(data["mxid"]).ensure_started(even_if_no_session=True)
user = await User.get_by_mxid(data["mxid"]).ensure_started()
if not user.whitelisted:
return self.render_login(mxid=user.mxid, error="You are not whitelisted.", status=403)
elif await user.is_logged_in():
return self.render_login(mxid=user.mxid, username=user.username)
await user.ensure_started(even_if_no_session=True)
if "phone" in data:
return await self.post_login_phone(user, data["phone"])
elif "token" in data:
+2 -4
View File
@@ -143,11 +143,9 @@ class User(AbstractUser):
self.log.debug(f"Ensuring post_login() for {self.name}")
asyncio.ensure_future(self.post_login(), loop=self.loop)
elif delete_unless_authenticated:
self.log.debug(f"Unauthenticated user {self.name} start()ed, deleting...")
# User not logged in -> forget user
self.log.debug(f"Unauthenticated user {self.name} start()ed, deleting session...")
self.client.disconnect()
self.client.session.delete()
self.delete()
return self
async def post_login(self, info=None):
@@ -343,4 +341,4 @@ def init(context):
config = context.config
users = [User.from_db(user) for user in DBUser.query.all()]
return [user.start(delete_unless_authenticated=True) for user in users]
return [user.ensure_started() for user in users]