From 35a8c0aec49675b2b69bbdf4b85a46d858d12db2 Mon Sep 17 00:00:00 2001 From: miruka Date: Tue, 3 Nov 2020 06:36:31 -0400 Subject: [PATCH] Use nio.PushRuleKind instead of own enum --- src/backend/matrix_client.py | 9 ++-- src/backend/models/items.py | 52 ++++++++----------- src/backend/nio_callbacks.py | 10 ++-- .../NotificationRuleDelegate.qml | 8 +-- .../Pages/AccountSettings/Notifications.qml | 10 ++-- 5 files changed, 38 insertions(+), 51 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index d1707146..8c0c70f7 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -41,7 +41,7 @@ from .errors import ( from .html_markdown import HTML_PROCESSOR as HTML from .media_cache import Media, Thumbnail from .models.items import ( - ZERO_DATE, Account, Event, Member, PushRule, PushRuleKind, Room, + ZERO_DATE, Account, Event, Member, PushRule, Room, Transfer, TransferStatus, TypeSpecifier, ) from .models.model_store import ModelStore @@ -1783,7 +1783,7 @@ class MatrixClient(nio.AsyncClient): async def tweak_pushrule( self, - kind: Union[PushRuleKind, str], + kind: Union[nio.PushRuleKind, str], rule_id: str, notify: Optional[bool] = None, highlight: Optional[bool] = None, @@ -1792,8 +1792,7 @@ class MatrixClient(nio.AsyncClient): urgency_hint: Optional[bool] = None, ) -> None: - kind = PushRuleKind[kind] if isinstance(kind, str) else kind - nio_kind = nio.PushRuleKind[kind.value.lower()] + kind = nio.PushRuleKind[kind] if isinstance(kind, str) else kind current: PushRule = \ self.models[self.user_id, "pushrules"][kind.value, rule_id] @@ -1821,7 +1820,7 @@ class MatrixClient(nio.AsyncClient): elif hint is False or (hint is None and not current.urgency_hint): actions.append(nio.PushSetTweak("urgency_hint", False)) - await self.set_pushrule_actions("global", nio_kind, rule_id, actions) + await self.set_pushrule_actions("global", kind, rule_id, actions) async def mass_tweak_pushrules(self, *tweaks_kwargs) -> None: diff --git a/src/backend/models/items.py b/src/backend/models/items.py index 7663f483..f2650765 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -85,48 +85,40 @@ class Account(ModelItem): return (self.order, self.id) < (other.order, other.id) -class PushRuleKind(AutoStrEnum): - Override = auto() - Content = auto() - Room = auto() - Sender = auto() - Underride = auto() - - @dataclass(eq=False) class PushRule(ModelItem): """A push rule configured for one of our account.""" - id: Tuple[str, str] = field() # (kind.value, rule_id) - kind: PushRuleKind = field() - rule_id: str = field() - order: int = field() - default: bool = field() - enabled: bool = True - pattern: str = "" - notify: bool = False - highlight: bool = False - bubble: bool = False - sound: str = "" # usually "default" when set - urgency_hint: bool = False + id: Tuple[str, str] = field() # (kind.value, rule_id) + kind: nio.PushRuleKind = field() + rule_id: str = field() + order: int = field() + default: bool = field() + enabled: bool = True + pattern: str = "" + notify: bool = False + highlight: bool = False + bubble: bool = False + sound: str = "" # usually "default" when set + urgency_hint: bool = False def __lt__(self, other: "PushRule") -> bool: """Sort by `kind`, then `order`.""" return ( - self.kind is PushRuleKind.Underride, - self.kind is PushRuleKind.Sender, - self.kind is PushRuleKind.Room, - self.kind is PushRuleKind.Content, - self.kind is PushRuleKind.Override, + self.kind is nio.PushRuleKind.underride, + self.kind is nio.PushRuleKind.sender, + self.kind is nio.PushRuleKind.room, + self.kind is nio.PushRuleKind.content, + self.kind is nio.PushRuleKind.override, self.order, self.id, ) < ( - other.kind is PushRuleKind.Underride, - other.kind is PushRuleKind.Sender, - other.kind is PushRuleKind.Room, - other.kind is PushRuleKind.Content, - other.kind is PushRuleKind.Override, + other.kind is nio.PushRuleKind.underride, + other.kind is nio.PushRuleKind.sender, + other.kind is nio.PushRuleKind.room, + other.kind is nio.PushRuleKind.content, + other.kind is nio.PushRuleKind.override, other.order, other.id, ) diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index 726f6f44..3995bbdf 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -15,7 +15,7 @@ import nio from .html_markdown import HTML_PROCESSOR from .media_cache import Media -from .models.items import PushRule, PushRuleKind, TypeSpecifier +from .models.items import PushRule, TypeSpecifier from .presence import Presence from .pyotherside_events import DevicesUpdated from .utils import classes_defined_in, plain2html @@ -785,12 +785,8 @@ class NioCallbacks: async def onPushRulesEvent(self, ev: nio.PushRulesEvent) -> None: model = self.models[self.user_id, "pushrules"] - kinds: Dict[PushRuleKind, List[nio.PushRule]] = { - PushRuleKind.Override: ev.global_rules.override, - PushRuleKind.Content: ev.global_rules.content, - PushRuleKind.Room: ev.global_rules.room, - PushRuleKind.Sender: ev.global_rules.sender, - PushRuleKind.Underride: ev.global_rules.underride, + kinds: Dict[nio.PushRuleKind, List[nio.PushRule]] = { + kind: getattr(ev.global_rules, kind.value) for kind in nio.PushRuleKind } # Remove from model rules that are now deleted. diff --git a/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml b/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml index 87cd82c1..b11db0ce 100644 --- a/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml +++ b/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml @@ -13,7 +13,7 @@ HTile { property Item page readonly property QtObject matchingRoom: - model.kind === "Room" ? + model.kind === "room" ? ModelStore.get(page.userId, "rooms").find(model.rule_id) : null @@ -32,7 +32,7 @@ HTile { textFormat: model.rule_id === ".m.rule.contains_user_name" || model.rule_id === ".m.rule.roomnotif" || - model.kind === "Sender" ? + model.kind === "sender" ? HLabel.StyledText : HLabel.PlainText @@ -83,10 +83,10 @@ HTile { model.rule_id === ".m.rule.encrypted" ? qsTr("Encrypted group messages") : - model.kind === "Content" ? + model.kind === "content" ? qsTr('Contains "%1"').arg(model.pattern) : - model.kind === "Sender" ? + model.kind === "sender" ? utils.coloredNameHtml("", model.rule_id) : matchingRoom && matchingRoom.display_name ? diff --git a/src/gui/Pages/AccountSettings/Notifications.qml b/src/gui/Pages/AccountSettings/Notifications.qml index fe6e85bf..4e7f0da3 100644 --- a/src/gui/Pages/AccountSettings/Notifications.qml +++ b/src/gui/Pages/AccountSettings/Notifications.qml @@ -54,14 +54,14 @@ HListView { section.property: "kind" section.delegate: HLabel { width: root.width - topPadding: padding * (section === "Override" ? 1 : 1.5) + topPadding: padding * (section === "override" ? 1 : 1.5) padding: theme.spacing font.pixelSize: theme.fontSize.big text: - section === "Override" ? qsTr("High-priority general rules") : - section === "Content" ? qsTr("Message text rules") : - section === "Room" ? qsTr("Room rules") : - section === "Sender" ? qsTr("Sender rules") : + section === "override" ? qsTr("High-priority general rules") : + section === "content" ? qsTr("Message text rules") : + section === "room" ? qsTr("Room rules") : + section === "sender" ? qsTr("Sender rules") : qsTr("General rules") }