@@ 14,6 14,7 @@ from typing import (
from slidge import BaseSession
from slidge.util.types import PseudoPresenceShow, ResourceDict
+from slixmpp.exceptions import XMPPError
from . import events
from .api import MattermostException, get_client_from_registration_form
@@ 105,12 106,21 @@ class Session(BaseSession[str, Recipient]):
else:
return True
- async def renew_token(self):
- self.update_token(
- await self.input(
+ async def renew_token(self) -> bool:
+ await self.logout()
+ try:
+ token = await self.input(
"Your mattermost token has expired, please provide a new one."
)
- )
+ except XMPPError:
+ self.send_gateway_message(
+ "You took too much time to reply. "
+ "Use the re-login command when you're ready to provide a new token."
+ )
+ return False
+ else:
+ self.update_token(token)
+ return True
def update_token(self, token: str):
self.user.registration_form["token"] = token
@@ 125,8 135,11 @@ class Session(BaseSession[str, Recipient]):
except MattermostException as e:
if not e.is_expired_session:
raise
- await self.renew_token()
- return await self.mm_client.login()
+ renewed = await self.renew_token()
+ if renewed:
+ await self.mm_client.login()
+ else:
+ raise
self.contacts.user_legacy_id = (await self.mm_client.me).username
t1 = self._ws_task = asyncio.create_task(self.ws.connect(self.on_mm_event))
t2 = self._update_status_task = asyncio.create_task(
@@ 137,13 150,18 @@ class Session(BaseSession[str, Recipient]):
return f"Connected as '{(await self.mm_client.me).username}'"
def _connection_fail(self, task: asyncio.Task):
- self.log.error("Session tasks ended: %s", task, exc_info=task.exception())
- if task is self._ws_task:
- if self._update_status_task:
- self._update_status_task.cancel()
+ try:
+ exc = task.exception()
+ except asyncio.CancelledError:
+ pass
else:
- if self._ws_task:
- self._ws_task.cancel()
+ self.log.error("Session tasks ended: %s", task, exc_info=exc)
+ if task is self._ws_task:
+ if self._update_status_task:
+ self._update_status_task.cancel()
+ else:
+ if self._ws_task:
+ self._ws_task.cancel()
self.send_gateway_message(
"Oh no! It seems we're not connected anymore. "
"You can try to use the re-login command, but you might need "
@@ 309,9 327,9 @@ class Session(BaseSession[str, Recipient]):
await muc.get_participant_by_mm_user_id(event.user_id)
async def logout(self):
- if self._ws_task is not None:
+ if self._ws_task is not None and not self._ws_task.done():
self._ws_task.cancel()
- if self._update_status_task is not None:
+ if self._update_status_task is not None and not self._update_status_task.done():
self._update_status_task.cancel()
@staticmethod