Update account total notif/highlight calculation

This commit is contained in:
miruka 2020-05-31 19:41:05 -04:00
parent 3791a08eea
commit d2300bf663

View File

@ -1212,6 +1212,21 @@ class MatrixClient(nio.AsyncClient):
self.models[self.user_id, "rooms"][room_id].last_event_date = \ self.models[self.user_id, "rooms"][room_id].last_event_date = \
ZeroDate ZeroDate
async def update_account_unread_counts(self) -> None:
"""Recalculate total unread notifications/highlights for our account"""
unreads = highlights = 0
for room in self.models[self.user_id, "rooms"].values():
unreads += room.unreads
highlights += room.highlights
account = self.models["accounts"][self.user_id]
account.total_unread = unreads
account.total_highlights = highlights
# Functions to register data into models # Functions to register data into models
async def event_is_past(self, ev: Union[nio.Event, Event]) -> bool: async def event_is_past(self, ev: Union[nio.Event, Event]) -> bool:
@ -1256,12 +1271,17 @@ class MatrixClient(nio.AsyncClient):
try: try:
registered = self.models[self.user_id, "rooms"][room.room_id] registered = self.models[self.user_id, "rooms"][room.room_id]
except KeyError: except KeyError:
registered = None registered = None
last_event_date = datetime.fromtimestamp(0) last_event_date = datetime.fromtimestamp(0)
typing_members = [] typing_members = []
update_account_unread_counts = True
else: else:
last_event_date = registered.last_event_date last_event_date = registered.last_event_date
typing_members = registered.typing_members typing_members = registered.typing_members
update_account_unread_counts = (
registered.unreads != room.unread_notifications or
registered.highlights != room.unread_highlights
)
room_item = Room( room_item = Room(
id = room.room_id, id = room.room_id,
@ -1308,6 +1328,9 @@ class MatrixClient(nio.AsyncClient):
for user_id in room.users: for user_id in room.users:
await self.add_member(room, user_id) await self.add_member(room, user_id)
if update_account_unread_counts:
await self.update_account_unread_counts()
async def add_member(self, room: nio.MatrixRoom, user_id: str) -> None: async def add_member(self, room: nio.MatrixRoom, user_id: str) -> None:
"""Register/update a room member into our models.""" """Register/update a room member into our models."""