diff --git a/src/backend/models/items.py b/src/backend/models/items.py index 7e74519c..30343f2b 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -120,6 +120,7 @@ class PushRule(ModelItem): self.kind is PushRuleKind.Content, self.kind is PushRuleKind.Override, self.order, + self.id, ) < ( other.kind is PushRuleKind.Underride, other.kind is PushRuleKind.Sender, @@ -127,6 +128,7 @@ class PushRule(ModelItem): other.kind is PushRuleKind.Content, other.kind is PushRuleKind.Override, other.order, + other.id, ) diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index b768fcc9..f4204ce8 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -8,7 +8,7 @@ from dataclasses import dataclass, field from datetime import datetime, timedelta from html import escape from pathlib import Path -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, Union from urllib.parse import quote import nio @@ -784,7 +784,6 @@ class NioCallbacks: async def onPushRulesEvent(self, ev: nio.PushRulesEvent) -> None: model = self.models[self.user_id, "pushrules"] - model.clear() # XXX kinds: Dict[PushRuleKind, List[nio.PushRule]] = { PushRuleKind.Override: ev.global_rules.override, @@ -794,6 +793,22 @@ class NioCallbacks: PushRuleKind.Underride: ev.global_rules.underride, } + # Remove from model rules that are now deleted. + # MUST be done first to avoid having rules sharing the same kind+order. + + new_keys: Set[Tuple[str, str]] = set() + + for kind, rules in kinds.items(): + for rule in rules: + new_keys.add((kind.value, rule.id)) + + with model.batch_remove(): + for key in tuple(model): + if key not in new_keys: + del model[key] + + # Then, add new rules/modify changed existing ones + for kind, rules in kinds.items(): for order, rule in enumerate(rules): tweaks = {