From d4b9fc60a46506659881b180984c6281ff4cb54f Mon Sep 17 00:00:00 2001 From: miruka Date: Sat, 17 Apr 2021 08:07:21 -0400 Subject: [PATCH] Fix invisible presence restoration When we are invisible and restart the client, set ourself to invisible, not offline, and correctly keep the status message that should be set whenever we get back to a visible online. --- src/backend/nio_callbacks.py | 14 +++++++++++--- src/backend/presence.py | 30 +++++++----------------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index 57c90560..7da2ca64 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -884,6 +884,13 @@ class NioCallbacks: # Let's hope they didn't screw up the get_presence API too: ev = await client.get_presence(ev.user_id) + invisible = account and "invisible" in account.presence.value + + if invisible and ev.presence == "offline": + presence.presence = Presence.State.invisible + else: + presence.presence = Presence.State(ev.presence) + presence.currently_active = ev.currently_active or False presence.status_msg = ev.status_msg or "" @@ -894,8 +901,6 @@ class NioCallbacks: else: presence.last_active_at = datetime.fromtimestamp(0) - presence.presence = Presence.State(ev.presence) - # Add all existing members related to this presence for room_id in self.models[self.user_id, "rooms"]: members = self.models[self.user_id, room_id, "members"] @@ -933,7 +938,10 @@ class NioCallbacks: # Restore status msg lost from server due to e.g. getting offline if not ev.status_msg and account.status_msg: - await client.set_presence(ev.presence, account.status_msg) + if invisible: + presence.status_msg = account.status_msg + else: + await client.set_presence(ev.presence, account.status_msg) # Save the presence to be restored next time we restart application if account.save_presence: diff --git a/src/backend/presence.py b/src/backend/presence.py index a8b4351a..5f6344d3 100644 --- a/src/backend/presence.py +++ b/src/backend/presence.py @@ -94,26 +94,10 @@ class Presence: def update_account(self) -> None: """Update presence fields of `Account` related to this `Presence`.""" - # Do not update if account is changing to invisible. - # When setting presence to invisible, the server will give us a - # presence event telling us we are offline, but we do not want to set - # account presence to offline. - if ( - not self.account or - self.presence == self.State.offline and - self.account.presence != self.State.echo_invisible - ): - return - - fields: Dict[str, Any] = {} - - if self.account.presence == self.State.echo_invisible: - fields["presence"] = self.State.invisible - else: - fields["presence"] = self.presence - fields["status_msg"] = self.status_msg - - fields["last_active_at"] = self.last_active_at - fields["currently_active"] = self.currently_active - - self.account.set_fields(**fields) + if self.account: + self.account.set_fields( + presence = self.presence, + status_msg = self.status_msg, + last_active_at = self.last_active_at, + currently_active = self.currently_active, + )