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.
This commit is contained in:
miruka 2021-04-17 08:07:21 -04:00
parent 22bce261d3
commit d4b9fc60a4
2 changed files with 18 additions and 26 deletions

View File

@ -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:

View File

@ -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,
)