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

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

View File

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