Don't reset model on account data pushrules event

This commit is contained in:
miruka 2020-11-01 01:23:33 -04:00
parent 99c5346dba
commit 9473e97c61
2 changed files with 19 additions and 2 deletions

View File

@ -120,6 +120,7 @@ class PushRule(ModelItem):
self.kind is PushRuleKind.Content, self.kind is PushRuleKind.Content,
self.kind is PushRuleKind.Override, self.kind is PushRuleKind.Override,
self.order, self.order,
self.id,
) < ( ) < (
other.kind is PushRuleKind.Underride, other.kind is PushRuleKind.Underride,
other.kind is PushRuleKind.Sender, other.kind is PushRuleKind.Sender,
@ -127,6 +128,7 @@ class PushRule(ModelItem):
other.kind is PushRuleKind.Content, other.kind is PushRuleKind.Content,
other.kind is PushRuleKind.Override, other.kind is PushRuleKind.Override,
other.order, other.order,
other.id,
) )

View File

@ -8,7 +8,7 @@ from dataclasses import dataclass, field
from datetime import datetime, timedelta from datetime import datetime, timedelta
from html import escape from html import escape
from pathlib import Path 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 from urllib.parse import quote
import nio import nio
@ -784,7 +784,6 @@ class NioCallbacks:
async def onPushRulesEvent(self, ev: nio.PushRulesEvent) -> None: async def onPushRulesEvent(self, ev: nio.PushRulesEvent) -> None:
model = self.models[self.user_id, "pushrules"] model = self.models[self.user_id, "pushrules"]
model.clear() # XXX
kinds: Dict[PushRuleKind, List[nio.PushRule]] = { kinds: Dict[PushRuleKind, List[nio.PushRule]] = {
PushRuleKind.Override: ev.global_rules.override, PushRuleKind.Override: ev.global_rules.override,
@ -794,6 +793,22 @@ class NioCallbacks:
PushRuleKind.Underride: ev.global_rules.underride, 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 kind, rules in kinds.items():
for order, rule in enumerate(rules): for order, rule in enumerate(rules):
tweaks = { tweaks = {