Store pushrules in model with (kind, rule_id) keys

Two rules of different kinds can have the same rule ID
This commit is contained in:
miruka 2020-11-01 01:10:49 -04:00
parent d5bcaca874
commit 99c5346dba
6 changed files with 50 additions and 52 deletions

View File

@ -1792,8 +1792,11 @@ class MatrixClient(nio.AsyncClient):
urgency_hint: Optional[bool] = None, urgency_hint: Optional[bool] = None,
) -> None: ) -> None:
# XXX: [kind, rule_id] kind = PushRuleKind[kind] if isinstance(kind, str) else kind
current: PushRule = self.models[self.user_id, "pushrules"][rule_id] nio_kind = nio.PushRuleKind[kind.value.lower()]
current: PushRule = \
self.models[self.user_id, "pushrules"][kind.value, rule_id]
actions: List[nio.PushAction] = [] 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): elif hint is False or (hint is None and not current.urgency_hint):
actions.append(nio.PushSetTweak("urgency_hint", False)) 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) await self.set_pushrule_actions("global", nio_kind, rule_id, actions)

View File

@ -97,17 +97,18 @@ class PushRuleKind(AutoStrEnum):
class PushRule(ModelItem): class PushRule(ModelItem):
"""A push rule configured for one of our account.""" """A push rule configured for one of our account."""
id: str = field() id: Tuple[str, str] = field() # (kind.value, rule_id)
kind: PushRuleKind = field() kind: PushRuleKind = field()
order: int = field() rule_id: str = field()
default: bool = field() order: int = field()
enabled: bool = True default: bool = field()
pattern: str = "" enabled: bool = True
notify: bool = False pattern: str = ""
highlight: bool = False notify: bool = False
bubble: bool = False highlight: bool = False
sound: bool = False bubble: bool = False
urgency_hint: bool = False sound: bool = False
urgency_hint: bool = False
def __lt__(self, other: "PushRule") -> bool: def __lt__(self, other: "PushRule") -> bool:
"""Sort by `kind`, then `order`.""" """Sort by `kind`, then `order`."""

View File

@ -813,9 +813,10 @@ class NioCallbacks:
sound = tweaks.get("sound", False) is not False sound = tweaks.get("sound", False) is not False
hint = tweaks.get("urgency_hint", high) is not False hint = tweaks.get("urgency_hint", high) is not False
model[rule.id] = PushRule( model[kind.value, rule.id] = PushRule(
id = rule.id, id = (kind.value, rule.id),
kind = kind, kind = kind,
rule_id = rule.id,
order = order, order = order,
default = rule.default, default = rule.default,
enabled = rule.enabled, enabled = rule.enabled,

View File

@ -6,11 +6,10 @@ import "../../Base"
HButton { HButton {
property string toggles: "" property string toggles: ""
readonly property string key: JSON.stringify([model.kind, model.id])
readonly property bool on: readonly property bool on:
toggles && page.pendingEdits[key] && toggles in page.pendingEdits[key]? toggles && page.pendingEdits[model.id] &&
page.pendingEdits[key][toggles] : toggles in page.pendingEdits[model.id] ?
page.pendingEdits[model.id][toggles] :
toggles ? toggles ?
model[toggles] : model[toggles] :
@ -25,15 +24,15 @@ HButton {
onClicked: { onClicked: {
if (! toggles) return 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]) if ((! on) === model[toggles])
delete page.pendingEdits[key][toggles] delete page.pendingEdits[model.id][toggles]
else else
page.pendingEdits[key][toggles] = ! on page.pendingEdits[model.id][toggles] = ! on
if (! Object.keys(page.pendingEdits[key]).length) if (! Object.keys(page.pendingEdits[model.id]).length)
delete page.pendingEdits[key] delete page.pendingEdits[model.id]
page.pendingEditsChanged() page.pendingEditsChanged()
} }

View File

@ -14,7 +14,7 @@ HTile {
readonly property QtObject matchingRoom: readonly property QtObject matchingRoom:
model.kind === "Room" ? model.kind === "Room" ?
ModelStore.get(page.userId, "rooms").find(model.id) : ModelStore.get(page.userId, "rooms").find(model.rule_id) :
null null
@ -30,69 +30,69 @@ HTile {
wrapMode: HLabel.Wrap wrapMode: HLabel.Wrap
textFormat: textFormat:
model.id === ".m.rule.contains_user_name" || model.rule_id === ".m.rule.contains_user_name" ||
model.id === ".m.rule.roomnotif" || model.rule_id === ".m.rule.roomnotif" ||
model.kind === "Sender" ? model.kind === "Sender" ?
HLabel.StyledText : HLabel.StyledText :
HLabel.PlainText HLabel.PlainText
text: text:
model.id === ".m.rule.master" ? model.rule_id === ".m.rule.master" ?
qsTr("Any message") : qsTr("Any message") :
model.id === ".m.rule.suppress_notices" ? model.rule_id === ".m.rule.suppress_notices" ?
qsTr("Messages sent by bots") : qsTr("Messages sent by bots") :
model.id === ".m.rule.invite_for_me" ? model.rule_id === ".m.rule.invite_for_me" ?
qsTr("Received room invites") : qsTr("Received room invites") :
model.id === ".m.rule.member_event" ? model.rule_id === ".m.rule.member_event" ?
qsTr("Membership, name & avatar changes") : 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") : qsTr("Messages containing my display name") :
model.id === ".m.rule.tombstone" ? model.rule_id === ".m.rule.tombstone" ?
qsTr("Room migration alerts") : qsTr("Room migration alerts") :
model.id === ".m.rule.reaction" ? model.rule_id === ".m.rule.reaction" ?
qsTr("Emoji reactions") : qsTr("Emoji reactions") :
model.id === ".m.rule.roomnotif" ? model.rule_id === ".m.rule.roomnotif" ?
qsTr("Messages containing %1").arg( qsTr("Messages containing %1").arg(
utils.htmlColorize("@room", theme.colors.accentText), 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( qsTr("Contains %1").arg(utils.coloredNameHtml(
"", page.userId, page.userId.split(":")[0].substring(1), "", page.userId, page.userId.split(":")[0].substring(1),
)): )):
model.id === ".m.rule.call" ? model.rule_id === ".m.rule.call" ?
qsTr("Incoming audio calls") : 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") : 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") : qsTr("Unencrypted 1-to-1 messages") :
model.id === ".m.rule.message" ? model.rule_id === ".m.rule.message" ?
qsTr("Unencrypted group messages") : qsTr("Unencrypted group messages") :
model.id === ".m.rule.encrypted" ? model.rule_id === ".m.rule.encrypted" ?
qsTr("Encrypted group messages") : qsTr("Encrypted group messages") :
model.kind === "Content" ? model.kind === "Content" ?
qsTr('Contains "%1"').arg(model.pattern) : qsTr('Contains "%1"').arg(model.pattern) :
model.kind === "Sender" ? model.kind === "Sender" ?
utils.coloredNameHtml("", model.id) : utils.coloredNameHtml("", model.rule_id) :
matchingRoom && matchingRoom.display_name ? matchingRoom && matchingRoom.display_name ?
matchingRoom.display_name : matchingRoom.display_name :
model.id model.rule_id
Layout.fillWidth: true Layout.fillWidth: true
} }

View File

@ -16,8 +16,7 @@ HListView {
property bool enableFlickShortcuts: property bool enableFlickShortcuts:
SwipeView ? SwipeView.isCurrentItem : true SwipeView ? SwipeView.isCurrentItem : true
// The object's array keys are run through `JSON.stringify(key)`. // {model.id: {notify, highlight, bubble, sound, urgency_hint}}
// {[kind, rule_id]: {notify, highlight, bubble, sound, urgency_hint}}
property var pendingEdits: ({}) property var pendingEdits: ({})
property string saveFutureId: "" property string saveFutureId: ""
@ -28,8 +27,8 @@ HListView {
function save() { function save() {
const args = [] const args = []
for (const [kindRuleId, kwargs] of Object.entries(pendingEdits)) { for (const [modelId, kwargs] of Object.entries(pendingEdits)) {
const [kind, rule_id] = JSON.parse(kindRuleId) const [kind, rule_id] = JSON.parse(modelId)
args.push(Object.assign({}, {kind, rule_id}, kwargs)) args.push(Object.assign({}, {kind, rule_id}, kwargs))
} }