From d5e0a3340d6f33771f521bd21bd5a3ba8163ebf7 Mon Sep 17 00:00:00 2001 From: vslg Date: Thu, 2 Jul 2020 18:28:41 -0300 Subject: [PATCH] Use presence events to update Account presence --- src/backend/backend.py | 26 ++++++++++++++++---------- src/backend/matrix_client.py | 9 +++++---- src/backend/models/items.py | 16 ++++++++++++++-- src/backend/nio_callbacks.py | 4 ++++ 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/backend/backend.py b/src/backend/backend.py index fb24bd49..28d46ee4 100644 --- a/src/backend/backend.py +++ b/src/backend/backend.py @@ -150,12 +150,13 @@ class Backend: for i, account in enumerate(self.models["accounts"].values()) ) + 1 + account = Account(client.user_id, order) + self.clients[client.user_id] = client - self.models["accounts"][client.user_id] = Account( - client.user_id, - order, - presence = Presence.State.online, - ) + self.models["accounts"][client.user_id] = account + + presence = self.presences.setdefault(client.user_id, Presence()) + presence.members["account", client.user_id] = account return client.user_id @@ -167,7 +168,7 @@ class Backend: device_id: str, homeserver: str = "https://matrix.org", order: int = -1, - presence: str = "online", + state: str = "online", ) -> None: """Create and register a `MatrixClient` with known account details.""" @@ -176,10 +177,15 @@ class Backend: user=user_id, homeserver=homeserver, device_id=device_id, ) - self.clients[user_id] = client - self.models["accounts"][user_id] = Account(user_id, order) + account = Account(user_id, order) - await client.resume(user_id, token, device_id, presence) + self.clients[user_id] = client + self.models["accounts"][user_id] = account + + presence = self.presences.setdefault(user_id, Presence()) + presence.members["account", user_id] = account + + await client.resume(user_id, token, device_id, state) async def load_saved_accounts(self) -> List[str]: @@ -192,7 +198,7 @@ class Backend: device_id = info["device_id"], homeserver = info["homeserver"], order = info.get("order", -1), - presence = info.get("presence", "online"), + state = info.get("presence", "online"), ) return user_id diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 615543ca..9308f883 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -246,14 +246,14 @@ class MatrixClient(nio.AsyncClient): user_id: str, token: str, device_id: str, - presence: str = "online", + state: str = "online", ) -> None: """Login to the server using an existing access token.""" response = nio.LoginResponse(user_id, device_id, token) await self.receive_response(response) - asyncio.ensure_future(self.set_presence(presence)) + await self.set_presence(state) self.start_task = asyncio.ensure_future(self._start()) @@ -1222,9 +1222,10 @@ class MatrixClient(nio.AsyncClient): await super().set_presence("offline" if presence == "invisible" else presence) - self.models["accounts"][self.user].presence = Presence.State(presence) - await self.backend.saved_accounts.add(self.user) + if presence == "invisible": + self.models["accounts"][self.user_id].presence = \ + Presence.State.invisible async def import_keys(self, infile: str, passphrase: str) -> None: diff --git a/src/backend/models/items.py b/src/backend/models/items.py index d893a9f0..41f231f1 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -56,15 +56,24 @@ class Presence(): last_active_ago: int = -1 status_msg: str = "" - members: Dict[Tuple[str, str], "Member"] = field(default_factory=dict) + members: Dict[Tuple[str, str], Union["Member", "Account"]] = \ + field(default_factory=dict) def update_members(self): for member in self.members.values(): + if ( + member.presence == self.State.invisible + ) and ( + self.presence == self.State.offline + ): + continue + member.presence = self.presence member.status_msg = self.status_msg member.last_active_ago = self.last_active_ago member.currently_active = self.currently_active + @dataclass class Account(ModelItem): """A logged in matrix account.""" @@ -81,7 +90,10 @@ class Account(ModelItem): local_unreads: bool = False local_highlights: bool = False - presence: Presence.State = Presence.State.offline + presence: Presence.State = Presence.State.offline + currently_active: bool = False + last_active_ago: int = -1 + status_msg: str = "" def __lt__(self, other: "Account") -> bool: """Sort by order, then by user ID.""" diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index ae7a84c4..840dd2a0 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -609,4 +609,8 @@ class NioCallbacks: presence.members[room_id, ev.user_id] = member presence.update_members() + + if ev.user_id in self.models["accounts"]: + await self.client.backend.saved_accounts.add(ev.user_id) + self.client.backend.presences[ev.user_id] = presence