From 6ee20a2717694d2c6363baab1f6b493662327de5 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Thu, 9 Apr 2020 18:03:12 -0400 Subject: [PATCH] Added limited local read/unread tracking --- src/backend/matrix_client.py | 16 ++++++++++++++++ src/backend/models/items.py | 1 + src/backend/nio_callbacks.py | 9 +++++---- src/gui/MainPane/Room.qml | 28 ++++++++++++++-------------- src/gui/PageLoader.qml | 2 ++ src/themes/Glass.qpl | 3 +++ src/themes/Midnight.qpl | 3 +++ 7 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 2d9e1367..a46c98ab 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -116,6 +116,7 @@ class MatrixClient(nio.AsyncClient): self.backend: "Backend" = backend self.models: ModelStore = self.backend.models + self.open_room: str = None self.profile_task: Optional[asyncio.Future] = None self.server_config_task: Optional[asyncio.Future] = None @@ -831,6 +832,18 @@ class MatrixClient(nio.AsyncClient): await super().room_forget(room_id) + async def room_read(self, room_id: str) -> None: + """Mark all messages in a room as read + + Currently, this doesn't handle sending a read receipt to the server, + only cleaning up any unread indicators. + """ + + self.open_room = room_id + if self.first_sync_done.is_set(): + room = self.models[self.user_id, "rooms"][room_id] + room.mentions = 0 + room.unreads = 0 async def room_mass_invite( self, room_id: str, *user_ids: str, @@ -1153,10 +1166,12 @@ class MatrixClient(nio.AsyncClient): last_event_date = registered.last_event_date typing_members = registered.typing_members mentions = registered.mentions + unreads = registered.unreads except KeyError: last_event_date = datetime.fromtimestamp(0) typing_members = [] mentions = 0 + unreads = 0 self.models[self.user_id, "rooms"][room.room_id] = Room( id = room.room_id, @@ -1191,6 +1206,7 @@ class MatrixClient(nio.AsyncClient): last_event_date = last_event_date, mentions = mentions, + unreads = unreads, ) diff --git a/src/backend/models/items.py b/src/backend/models/items.py index 2c7c1a05..71e0dc0e 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -82,6 +82,7 @@ class Room(ModelItem): last_event_date: datetime = ZeroDate mentions: int = 0 + unreads: int = 0 def __lt__(self, other: "Room") -> bool: """Sort by join state, then descending last event date, then name. diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index 70153f6a..19bcdce7 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -36,7 +36,6 @@ class NioCallbacks: client: "MatrixClient" = field() - def __post_init__(self) -> None: """Register our methods as callbacks.""" @@ -109,9 +108,11 @@ class NioCallbacks: room, ev, content=co, mentions=mention_list, ) - if HTML_PROCESSOR.user_id_link_in_html(co, self.client.user_id): - rooms = self.client.models[self.client.user_id, "rooms"] - rooms[room.room_id].mentions += 1 + if self.client.first_sync_done.is_set() and self.client.open_room != room.room_id: + room = self.client.models[self.client.user_id, "rooms"][room.room_id] + room.unreads += 1 + if HTML_PROCESSOR.user_id_link_in_html(co, self.client.user_id): + room.mentions += 1 diff --git a/src/gui/MainPane/Room.qml b/src/gui/MainPane/Room.qml index 0b14bc73..4cbb508d 100644 --- a/src/gui/MainPane/Room.qml +++ b/src/gui/MainPane/Room.qml @@ -44,23 +44,23 @@ HTileDelegate { color: theme.mainPane.listView.room.name } - // HLabel { - // text: model.mentions - // font.pixelSize: theme.fontSize.small - // verticalAlignment: Qt.AlignVCenter - // leftPadding: theme.spacing / 4 - // rightPadding: leftPadding + HLabel { + text: model.unreads + font.pixelSize: theme.fontSize.small + verticalAlignment: Qt.AlignVCenter + leftPadding: theme.spacing / 4 + rightPadding: leftPadding - // scale: model.mentions === 0 ? 0 : 1 - // visible: scale > 0 + scale: model.unreads === 0 ? 0 : 1 + visible: scale > 0 - // background: Rectangle { - // color: theme.colors.alertBackground - // radius: theme.radius / 4 - // } + background: Rectangle { + color: model.mentions === 0 ? theme.colors.unreadBackground : theme.colors.alertBackground + radius: theme.radius / 4 + } - // Behavior on scale { HNumberAnimation {} } - // }, + Behavior on scale { HNumberAnimation {} } + } HIcon { svgName: "invite-received" diff --git a/src/gui/PageLoader.qml b/src/gui/PageLoader.qml index 54c40e1f..6fafe656 100644 --- a/src/gui/PageLoader.qml +++ b/src/gui/PageLoader.qml @@ -61,6 +61,8 @@ HLoader { function showRoom(userId, roomId) { _show("Pages/Chat/Chat.qml", {userId, roomId}) + py.callClientCoro(userId, "room_read", [roomId], () => {}) + window.uiState.page = "Pages/Chat/Chat.qml" window.uiState.pageProperties = {userId, roomId} window.uiStateChanged() diff --git a/src/themes/Glass.qpl b/src/themes/Glass.qpl index 77803e52..8df5703b 100644 --- a/src/themes/Glass.qpl +++ b/src/themes/Glass.qpl @@ -59,6 +59,9 @@ colors: color negativeBackground: hsluv(0, saturation * 1.5, intensity * 52, opacity) + color unreadBackground: + hsluv(188, saturation * 1.5, intensity * 52, 1) + color alertBackground: negativeBackground color brightText: hsluv(0, 0, intensity * 100) diff --git a/src/themes/Midnight.qpl b/src/themes/Midnight.qpl index dffd41df..217adf2d 100644 --- a/src/themes/Midnight.qpl +++ b/src/themes/Midnight.qpl @@ -62,6 +62,9 @@ colors: color negativeBackground: hsluv(0, saturation * 1.5, intensity * 52, opacity) + color unreadBackground: + hsluv(188, saturation * 1.5, intensity * 52, 1) + color alertBackground: negativeBackground color brightText: hsluv(0, 0, intensity * 100)