From 4dacc3a12df18648fc954e32a54de1fd4c6ad054 Mon Sep 17 00:00:00 2001 From: miruka Date: Wed, 16 Sep 2020 10:53:29 -0400 Subject: [PATCH] Make alerts and notification push rules-aware --- src/backend/html_markdown.py | 14 +--------- src/backend/matrix_client.py | 51 ++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/backend/html_markdown.py b/src/backend/html_markdown.py index 69f8bac3..41ce3894 100644 --- a/src/backend/html_markdown.py +++ b/src/backend/html_markdown.py @@ -3,7 +3,7 @@ """HTML and Markdown processing tools.""" import re -from typing import DefaultDict, Dict, List, Optional, Tuple +from typing import Dict, List, Optional, Tuple from urllib.parse import unquote import html_sanitizer.sanitizer as sanitizer @@ -210,18 +210,6 @@ class HTMLProcessor: ] - def user_id_link_in_html(self, html: str, user_id: str) -> bool: - """Return whether html contains a mention link for `user_id`.""" - - regex = re.compile(rf"https?://matrix.to/#/{user_id}", re.IGNORECASE) - - for _, href in self.mentions_in_html(html): - if regex.match(unquote(href.strip())): - return True - - return False - - def from_markdown( self, text: str, diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index c5bd5804..ef43a85a 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -211,6 +211,9 @@ class MatrixClient(nio.AsyncClient): self.unassigned_event_last_read_by: DefaultDict[str, Dict[str, int]] =\ DefaultDict(dict) + self.previous_server_unreads: Dict[str, int] = {} + self.previous_server_highlights: Dict[str, int] = {} + # {room_id: event} self.power_level_events: Dict[str, nio.PowerLevelsEvent] = {} @@ -2101,6 +2104,37 @@ class MatrixClient(nio.AsyncClient): item.id = f"echo-{tx_id}" self.event_to_echo_ids[ev.event_id] = item.id + model[item.id] = item + await self.set_room_last_event(room.room_id, item) + + if from_us or await self.event_is_past(ev): + return item + + # Alerts & notifications + + room_item = self.models[self.user_id, "rooms"][room.room_id] + + unread = \ + room_item.unreads and \ + room_item.unreads != \ + self.previous_server_unreads.get(room.room_id, 0) + + highlight = \ + room_item.highlights and \ + room_item.highlights != \ + self.previous_server_highlights.get(room.room_id, 0) + + self.previous_server_unreads[room.room_id] = room_item.unreads + self.previous_server_highlights[room.room_id] = room_item.highlights + + if highlight: + room_item.set_fields(local_unreads=True, local_highlights=True) + else: + room_item.local_unreads = True + + if unread or highlight: + AlertRequested(high_importance=highlight) + notif_room = room.display_name notif_sender = item.sender_name or item.sender_id body_start = f"{notif_sender}: " @@ -2118,22 +2152,5 @@ class MatrixClient(nio.AsyncClient): # ), ) - 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 item - - mentions_us = HTML.user_id_link_in_html(item.content, self.user_id) - AlertRequested(high_importance=mentions_us) - - room_item = self.models[self.user_id, "rooms"][room.room_id] - room_item.local_unreads = True - - if mentions_us: - room_item.local_highlights = True - await self.update_account_unread_counts() return item