From af321d8eae597de4de413adda0796db6abfb4a6a Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 28 Feb 2021 09:00:36 -0400 Subject: [PATCH] Add notification options to room context menu --- src/backend/matrix_client.py | 45 +++++++++++++++++++++- src/gui/MainPane/RoomDelegate.qml | 37 ++++++++++++++++++ src/icons/thin/room-menu-notifications.svg | 3 ++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/icons/thin/room-menu-notifications.svg diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 36de56fd..61ef6fd7 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -1819,7 +1819,7 @@ class MatrixClient(nio.AsyncClient): # Now edit the rule old: Optional[PushRule] = None - key = (old_kind.value, old_rule_id) + key = (old_kind.value if old_kind else None, old_rule_id) if None not in key: old = self.models[self.user_id, "pushrules"].get(key) @@ -1914,7 +1914,48 @@ class MatrixClient(nio.AsyncClient): """Remove an existing non-builtin pushrule.""" kind = nio.PushRuleKind[kind] if isinstance(kind, str) else kind - await self.delete_pushrule("global", kind, rule_id) + + if (kind.value, rule_id) in self.models[self.user_id, "pushrules"]: + await self.delete_pushrule("global", kind, rule_id) + + + async def _remove_room_override_rule(self, room_id: str) -> None: + for rule in self.models[self.user_id, "pushrules"].values(): + override_kind = rule.kind is nio.PushRuleKind.override + this_room_cnd = nio.PushEventMatch("room_id", room_id).as_value + + if (override_kind and rule.conditions == [this_room_cnd]): + print(rule) + await self.remove_pushrule(rule.kind, rule.rule_id) + + + async def room_pushrule_use_default(self, room_id: str) -> None: + await self._remove_room_override_rule(room_id) + await self.remove_pushrule(nio.PushRuleKind.room, room_id) + + + async def room_pushrule_all_events(self, room_id: str) -> None: + await self._remove_room_override_rule(room_id) + await self.edit_pushrule( + kind = nio.PushRuleKind.room, + rule_id = room_id, + actions = [nio.PushNotify(), nio.PushSetTweak("sound", "default")], + ) + + + async def room_pushrule_highlights_only(self, room_id: str) -> None: + await self._remove_room_override_rule(room_id) + await self.edit_pushrule(nio.PushRuleKind.room, room_id, actions=[]) + + + async def room_pushrule_ignore_all(self, room_id: str) -> None: + await self._remove_room_override_rule(room_id) + await self.remove_pushrule(nio.PushRuleKind.room, room_id) + + cnd = nio.PushEventMatch("room_id", room_id) + await self.edit_pushrule( + nio.PushRuleKind.override, room_id, conditions=[cnd], actions=[], + ) # Functions to register/modify data into models diff --git a/src/gui/MainPane/RoomDelegate.qml b/src/gui/MainPane/RoomDelegate.qml index 7dd8ed69..d588ce8e 100644 --- a/src/gui/MainPane/RoomDelegate.qml +++ b/src/gui/MainPane/RoomDelegate.qml @@ -136,6 +136,9 @@ HTile { } contextMenu: HMenu { + // This delegate is only used for nested menus + delegate: HMenuItem { icon.name: "room-menu-notifications" } + HMenuItem { icon.name: model.pinned ? "room-unpin": "room-pin" text: model.pinned ? qsTr("Unpin"): qsTr("Pin to top") @@ -144,6 +147,40 @@ HTile { ) } + HMenu { + title: qsTr("Notifications") + + HMenuItem { + text: qsTr("Use default account settings") + onTriggered: py.callClientCoro( + model.for_account, "room_pushrule_use_default", [model.id], + ) + } + + HMenuItem { + text: qsTr("All new messages") + onTriggered: py.callClientCoro( + model.for_account, "room_pushrule_all_events", [model.id], + ) + } + + HMenuItem { + text: qsTr("Highlights only (replies, keywords...)") + onTriggered: py.callClientCoro( + model.for_account, + "room_pushrule_highlights_only", + [model.id], + ) + } + + HMenuItem { + text: qsTr("Ignore new messages") + onTriggered: py.callClientCoro( + model.for_account, "room_pushrule_ignore_all", [model.id], + ) + } + } + HMenuItemPopupSpawner { visible: joined enabled: model.can_invite && accountModel.presence !== "offline" diff --git a/src/icons/thin/room-menu-notifications.svg b/src/icons/thin/room-menu-notifications.svg new file mode 100644 index 00000000..dcebec68 --- /dev/null +++ b/src/icons/thin/room-menu-notifications.svg @@ -0,0 +1,3 @@ + + +