diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 77d0b816..d70d4a9e 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -1792,8 +1792,11 @@ class MatrixClient(nio.AsyncClient): urgency_hint: Optional[bool] = None, ) -> None: - # XXX: [kind, rule_id] - current: PushRule = self.models[self.user_id, "pushrules"][rule_id] + kind = PushRuleKind[kind] if isinstance(kind, str) else kind + nio_kind = nio.PushRuleKind[kind.value.lower()] + + current: PushRule = \ + self.models[self.user_id, "pushrules"][kind.value, rule_id] actions: List[nio.PushAction] = [] @@ -1819,11 +1822,6 @@ class MatrixClient(nio.AsyncClient): elif hint is False or (hint is None and not current.urgency_hint): actions.append(nio.PushSetTweak("urgency_hint", False)) - nio_kind = nio.PushRuleKind[ - (kind if isinstance(kind, str) else kind.value).lower() - ] - - print(nio_kind, rule_id, actions) await self.set_pushrule_actions("global", nio_kind, rule_id, actions) diff --git a/src/backend/models/items.py b/src/backend/models/items.py index a842680b..7e74519c 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -97,17 +97,18 @@ class PushRuleKind(AutoStrEnum): class PushRule(ModelItem): """A push rule configured for one of our account.""" - id: str = field() - kind: PushRuleKind = field() - order: int = field() - default: bool = field() - enabled: bool = True - pattern: str = "" - notify: bool = False - highlight: bool = False - bubble: bool = False - sound: bool = False - urgency_hint: bool = False + 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: bool = False + urgency_hint: bool = False def __lt__(self, other: "PushRule") -> bool: """Sort by `kind`, then `order`.""" diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index fc66b2f2..b768fcc9 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -813,9 +813,10 @@ class NioCallbacks: sound = tweaks.get("sound", False) is not False hint = tweaks.get("urgency_hint", high) is not False - model[rule.id] = PushRule( - id = rule.id, + model[kind.value, rule.id] = PushRule( + id = (kind.value, rule.id), kind = kind, + rule_id = rule.id, order = order, default = rule.default, enabled = rule.enabled, diff --git a/src/gui/Pages/AccountSettings/NotificationRuleButton.qml b/src/gui/Pages/AccountSettings/NotificationRuleButton.qml index 8ab14661..4d95b887 100644 --- a/src/gui/Pages/AccountSettings/NotificationRuleButton.qml +++ b/src/gui/Pages/AccountSettings/NotificationRuleButton.qml @@ -6,11 +6,10 @@ import "../../Base" HButton { property string toggles: "" - readonly property string key: JSON.stringify([model.kind, model.id]) - readonly property bool on: - toggles && page.pendingEdits[key] && toggles in page.pendingEdits[key]? - page.pendingEdits[key][toggles] : + toggles && page.pendingEdits[model.id] && + toggles in page.pendingEdits[model.id] ? + page.pendingEdits[model.id][toggles] : toggles ? model[toggles] : @@ -25,15 +24,15 @@ HButton { onClicked: { if (! toggles) return - if (! (key in page.pendingEdits)) page.pendingEdits[key] = {} + if (! (model.id in page.pendingEdits)) page.pendingEdits[model.id] = {} if ((! on) === model[toggles]) - delete page.pendingEdits[key][toggles] + delete page.pendingEdits[model.id][toggles] else - page.pendingEdits[key][toggles] = ! on + page.pendingEdits[model.id][toggles] = ! on - if (! Object.keys(page.pendingEdits[key]).length) - delete page.pendingEdits[key] + if (! Object.keys(page.pendingEdits[model.id]).length) + delete page.pendingEdits[model.id] page.pendingEditsChanged() } diff --git a/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml b/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml index 65709bf6..1e1b0121 100644 --- a/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml +++ b/src/gui/Pages/AccountSettings/NotificationRuleDelegate.qml @@ -14,7 +14,7 @@ HTile { readonly property QtObject matchingRoom: model.kind === "Room" ? - ModelStore.get(page.userId, "rooms").find(model.id) : + ModelStore.get(page.userId, "rooms").find(model.rule_id) : null @@ -30,69 +30,69 @@ HTile { wrapMode: HLabel.Wrap textFormat: - model.id === ".m.rule.contains_user_name" || - model.id === ".m.rule.roomnotif" || + model.rule_id === ".m.rule.contains_user_name" || + model.rule_id === ".m.rule.roomnotif" || model.kind === "Sender" ? HLabel.StyledText : HLabel.PlainText text: - model.id === ".m.rule.master" ? + model.rule_id === ".m.rule.master" ? qsTr("Any message") : - model.id === ".m.rule.suppress_notices" ? + model.rule_id === ".m.rule.suppress_notices" ? qsTr("Messages sent by bots") : - model.id === ".m.rule.invite_for_me" ? + model.rule_id === ".m.rule.invite_for_me" ? qsTr("Received room invites") : - model.id === ".m.rule.member_event" ? + model.rule_id === ".m.rule.member_event" ? qsTr("Membership, name & avatar changes") : - model.id === ".m.rule.contains_display_name" ? + model.rule_id === ".m.rule.contains_display_name" ? qsTr("Messages containing my display name") : - model.id === ".m.rule.tombstone" ? + model.rule_id === ".m.rule.tombstone" ? qsTr("Room migration alerts") : - model.id === ".m.rule.reaction" ? + model.rule_id === ".m.rule.reaction" ? qsTr("Emoji reactions") : - model.id === ".m.rule.roomnotif" ? + model.rule_id === ".m.rule.roomnotif" ? qsTr("Messages containing %1").arg( utils.htmlColorize("@room", theme.colors.accentText), ) : - model.id === ".m.rule.contains_user_name" ? + model.rule_id === ".m.rule.contains_user_name" ? qsTr("Contains %1").arg(utils.coloredNameHtml( "", page.userId, page.userId.split(":")[0].substring(1), )): - model.id === ".m.rule.call" ? + model.rule_id === ".m.rule.call" ? qsTr("Incoming audio calls") : - model.id === ".m.rule.encrypted_room_one_to_one" ? + model.rule_id === ".m.rule.encrypted_room_one_to_one" ? qsTr("Encrypted 1-to-1 messages") : - model.id === ".m.rule.room_one_to_one" ? + model.rule_id === ".m.rule.room_one_to_one" ? qsTr("Unencrypted 1-to-1 messages") : - model.id === ".m.rule.message" ? + model.rule_id === ".m.rule.message" ? qsTr("Unencrypted group messages") : - model.id === ".m.rule.encrypted" ? + model.rule_id === ".m.rule.encrypted" ? qsTr("Encrypted group messages") : model.kind === "Content" ? qsTr('Contains "%1"').arg(model.pattern) : model.kind === "Sender" ? - utils.coloredNameHtml("", model.id) : + utils.coloredNameHtml("", model.rule_id) : matchingRoom && matchingRoom.display_name ? matchingRoom.display_name : - model.id + model.rule_id Layout.fillWidth: true } diff --git a/src/gui/Pages/AccountSettings/Notifications.qml b/src/gui/Pages/AccountSettings/Notifications.qml index 88e4fcfa..c99195b1 100644 --- a/src/gui/Pages/AccountSettings/Notifications.qml +++ b/src/gui/Pages/AccountSettings/Notifications.qml @@ -16,8 +16,7 @@ HListView { property bool enableFlickShortcuts: SwipeView ? SwipeView.isCurrentItem : true - // The object's array keys are run through `JSON.stringify(key)`. - // {[kind, rule_id]: {notify, highlight, bubble, sound, urgency_hint}} + // {model.id: {notify, highlight, bubble, sound, urgency_hint}} property var pendingEdits: ({}) property string saveFutureId: "" @@ -28,8 +27,8 @@ HListView { function save() { const args = [] - for (const [kindRuleId, kwargs] of Object.entries(pendingEdits)) { - const [kind, rule_id] = JSON.parse(kindRuleId) + for (const [modelId, kwargs] of Object.entries(pendingEdits)) { + const [kind, rule_id] = JSON.parse(modelId) args.push(Object.assign({}, {kind, rule_id}, kwargs)) }