Optimize presence-related model item field changes

This commit is contained in:
miruka 2020-07-18 18:19:56 -04:00
parent d4fb38fd1a
commit 692c78f398
2 changed files with 25 additions and 16 deletions

View File

@ -288,9 +288,10 @@ class MatrixClient(nio.AsyncClient):
account = self.models["accounts"][user_id] account = self.models["accounts"][user_id]
await self.receive_response(response) await self.receive_response(response)
self._presence = "offline" if state == "invisible" else state self._presence = "offline" if state == "invisible" else state
account.presence = Presence.State(state) account.set_fields(
account.status_msg = status_msg presence=Presence.State(state), status_msg=status_msg,
)
if state != "offline": if state != "offline":
account.connecting = True account.connecting = True
@ -1401,8 +1402,10 @@ class MatrixClient(nio.AsyncClient):
# Update manually since we may not receive the presence event back # Update manually since we may not receive the presence event back
# in time # in time
account.presence = Presence.State.offline account.set_fields(
account.currently_active = False presence = Presence.State.offline,
currently_active = False,
)
elif ( elif (
account.presence == Presence.State.offline and account.presence == Presence.State.offline and
presence != "offline" presence != "offline"

View File

@ -97,10 +97,12 @@ class Presence:
""" """
for member in self.members.values(): for member in self.members.values():
member.presence = self.presence member.set_fields(
member.status_msg = self.status_msg presence = self.presence,
member.last_active_at = self.last_active_at status_msg = self.status_msg,
member.currently_active = self.currently_active last_active_at = self.last_active_at,
currently_active = self.currently_active,
)
def update_account(self) -> None: def update_account(self) -> None:
"""Update presence fields of `Account` related to this `Presence`.""" """Update presence fields of `Account` related to this `Presence`."""
@ -116,14 +118,18 @@ class Presence:
): ):
return return
if self.account.presence == self.State.echo_invisible: fields: Dict[str, Any] = {}
self.account.presence = self.State.invisible
else:
self.account.presence = self.presence
self.account.status_msg = self.status_msg
self.account.last_active_at = self.last_active_at if self.account.presence == self.State.echo_invisible:
self.account.currently_active = self.currently_active 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)
@dataclass @dataclass