From 0ba8c6eecf9edafdb67b6898d0bf40cb0e7dc945 Mon Sep 17 00:00:00 2001 From: miruka Date: Thu, 16 Apr 2020 14:08:24 -0400 Subject: [PATCH] Make all events able to increment unread/mentions --- src/backend/matrix_client.py | 29 +++++++++++++++++++++-------- src/backend/models/items.py | 2 +- src/backend/nio_callbacks.py | 11 ----------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index cadb9831..7a43ff69 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -114,9 +114,9 @@ class MatrixClient(nio.AsyncClient): ), ) - self.backend: "Backend" = backend - self.models: ModelStore = self.backend.models - self.open_room: str = None + self.backend: "Backend" = backend + self.models: ModelStore = self.backend.models + self.open_room: Optional[str] = None self.profile_task: Optional[asyncio.Future] = None self.server_config_task: Optional[asyncio.Future] = None @@ -1304,13 +1304,26 @@ class MatrixClient(nio.AsyncClient): tx_id = ev.source.get("content", {}).get( f"{__app_name__}.transaction_id", ) - local_sender = ev.sender in self.backend.clients + from_us = ev.sender in self.backend.clients - if local_sender and tx_id and f"echo-{tx_id}" in model: + if from_us and tx_id and f"echo-{tx_id}" in model: item.id = f"echo-{tx_id}" - if not local_sender and not await self.event_is_past(ev): - AlertRequested() - model[item.id] = item await self.set_room_last_event(room.room_id, item) + + # Alerts + + if from_us or await self.event_is_past(ev): + return + + AlertRequested() + + if self.open_room != room.room_id: + room = self.models[self.user_id, "rooms"][room.room_id] + room.unreads += 1 + + content = fields.get("content", "") + + if HTML.user_id_link_in_html(content, self.user_id): + room.mentions += 1 diff --git a/src/backend/models/items.py b/src/backend/models/items.py index 3063cf56..b29e05fb 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -82,7 +82,7 @@ class Room(ModelItem): last_event_date: datetime = ZeroDate mentions: int = 0 - unreads: 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 f1cefe8e..e99891fb 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -108,17 +108,6 @@ class NioCallbacks: room, ev, content=co, mentions=mention_list, ) - past = await self.client.event_is_past(ev) - from_us = ev.sender in self.client.backend.clients - - if not past and not from_us and self.client.open_room != room.room_id: - model = self.client.models[self.client.user_id, "rooms"] - room = model[room.room_id] - room.unreads += 1 - - if HTML_PROCESSOR.user_id_link_in_html(co, self.client.user_id): - room.mentions += 1 - async def onRoomMessageNotice(self, room, ev) -> None: await self.onRoomMessageText(room, ev)