From a1e5a1981d4e1b1abb25e8da6bed9ba270676b86 Mon Sep 17 00:00:00 2001 From: vslg Date: Thu, 2 Jul 2020 11:44:54 -0300 Subject: [PATCH] Fix presence not being updated after loaded once --- src/backend/backend.py | 2 +- src/backend/matrix_client.py | 22 ++++++++-------- src/backend/models/items.py | 12 +++++++-- src/backend/nio_callbacks.py | 25 +++++++++++++------ src/backend/user_files.py | 7 +++--- .../RoomPane/MemberView/MemberDelegate.qml | 2 +- 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/backend/backend.py b/src/backend/backend.py index 6d929e2e..fb24bd49 100644 --- a/src/backend/backend.py +++ b/src/backend/backend.py @@ -6,7 +6,7 @@ import os import sys import traceback from pathlib import Path -from typing import TYPE_CHECKING, Any, DefaultDict, Dict, List, Optional +from typing import Any, DefaultDict, Dict, List, Optional from appdirs import AppDirs diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 2c622e60..615543ca 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -253,7 +253,7 @@ class MatrixClient(nio.AsyncClient): response = nio.LoginResponse(user_id, device_id, token) await self.receive_response(response) - await self.set_presence(presence) + asyncio.ensure_future(self.set_presence(presence)) self.start_task = asyncio.ensure_future(self._start()) @@ -1581,10 +1581,9 @@ class MatrixClient(nio.AsyncClient): async def add_member(self, room: nio.MatrixRoom, user_id: str) -> None: """Register/update a room member into our models.""" - member = room.users[user_id] - presence = self.backend.presences.get(user_id, Presence()) - - self.models[self.user_id, room.room_id, "members"][user_id] = Member( + member = room.users[user_id] + presence = self.backend.presences.get(user_id, Presence()) + member_item = Member( id = user_id, display_name = room.user_name(user_id) # disambiguated if member.display_name else "", @@ -1592,13 +1591,16 @@ class MatrixClient(nio.AsyncClient): typing = user_id in room.typing_users, power_level = member.power_level, invited = member.invited, - - presence = presence.presence, - last_active_ago = presence.last_active_ago, - status_msg = presence.status_msg, - currently_active = presence.currently_active, ) + if user_id in self.backend.presences: + presence.members[room.room_id, user_id] = member_item + + presence.update_members() + + self.models[self.user_id, room.room_id, "members"][user_id] = \ + member_item + if member.display_name: HTML.rooms_user_id_names[room.room_id][user_id] = \ member.display_name diff --git a/src/backend/models/items.py b/src/backend/models/items.py index e0e8a03c..d893a9f0 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -51,11 +51,19 @@ class Presence(): order.index(other) # type: ignore ) - last_active_ago: int = -1 - status_msg: str = "" presence: State = State.offline currently_active: bool = False + last_active_ago: int = -1 + status_msg: str = "" + members: Dict[Tuple[str, str], "Member"] = field(default_factory=dict) + + def update_members(self): + for member in self.members.values(): + 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): diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index 53595798..ae7a84c4 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -592,10 +592,21 @@ class NioCallbacks: # Presence event callbacks async def onPresenceEvent(self, ev: nio.PresenceEvent) -> None: - self.client.backend.presences[ev.user_id] = Presence( - status_msg = ev.status_msg or "", - presence = Presence.State(ev.presence) if ev.presence - else Presence.State.offline, - last_active_ago = ev.last_active_ago or -1, - currently_active = ev.currently_active, - ) + presence = self.client.backend.presences.get(ev.user_id, Presence()) + + presence.status_msg = ev.status_msg or "" + presence.presence = Presence.State(ev.presence) if ev.presence\ + else Presence.State.offline + presence.last_active_ago = ev.last_active_ago or -1 + presence.currently_active = ev.currently_active or False + + for room_id in self.models[self.user_id, "rooms"]: + member = self.models[self.user_id, room_id, "members"].get( + ev.user_id, + ) + + if member: + presence.members[room_id, ev.user_id] = member + + presence.update_members() + self.client.backend.presences[ev.user_id] = presence diff --git a/src/backend/user_files.py b/src/backend/user_files.py index f2322bfc..a1641af5 100644 --- a/src/backend/user_files.py +++ b/src/backend/user_files.py @@ -182,8 +182,9 @@ class Accounts(JSONDataFile): the corresponding `MatrixClient` in `backend.clients`. """ - client = self.backend.clients[user_id] - saved = await self.read() + client = self.backend.clients[user_id] + saved = await self.read() + presence = self.backend.models["accounts"][user_id].presence.value await self.write({ **saved, @@ -192,7 +193,7 @@ class Accounts(JSONDataFile): "token": client.access_token, "device_id": client.device_id, "enabled": True, - "presence": client._presence or "online", + "presence": presence or "online", "order": max([ account.get("order", i) for i, account in enumerate(saved.values()) diff --git a/src/gui/Pages/Chat/RoomPane/MemberView/MemberDelegate.qml b/src/gui/Pages/Chat/RoomPane/MemberView/MemberDelegate.qml index dc271132..864c9937 100644 --- a/src/gui/Pages/Chat/RoomPane/MemberView/MemberDelegate.qml +++ b/src/gui/Pages/Chat/RoomPane/MemberView/MemberDelegate.qml @@ -44,7 +44,7 @@ HTile { TitleRightInfoLabel { tile: member text: - model.presence === "offline" && + (model.presence !== "online") && model.last_active_ago !== -1 ? "for " + utils.formatRelativeTime(