Implement new model, hooks and reconcilation code for new GYU notification settings (#11089)
* Define new notification settings model * Add new hooks * make ts-strict happy * add unit tests * chore: make eslint/prettier happier :) * make ts-strict happier * Update src/notifications/NotificationUtils.ts Co-authored-by: Robin <robin@robin.town> * Add tests for hooks * chore: fixed lint issues * Add comments --------- Co-authored-by: Robin <robin@robin.town>
This commit is contained in:
committed by
GitHub
parent
2972219959
commit
97765613bc
67
src/models/notificationsettings/NotificationSettings.ts
Normal file
67
src/models/notificationsettings/NotificationSettings.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { RoomNotifState } from "../../RoomNotifs";
|
||||
|
||||
export type RoomDefaultNotificationLevel = RoomNotifState.AllMessages | RoomNotifState.MentionsOnly;
|
||||
|
||||
export type NotificationSettings = {
|
||||
globalMute: boolean;
|
||||
defaultLevels: {
|
||||
room: RoomDefaultNotificationLevel;
|
||||
dm: RoomDefaultNotificationLevel;
|
||||
};
|
||||
sound: {
|
||||
people: string | undefined;
|
||||
mentions: string | undefined;
|
||||
calls: string | undefined;
|
||||
};
|
||||
activity: {
|
||||
invite: boolean;
|
||||
status_event: boolean;
|
||||
bot_notices: boolean;
|
||||
};
|
||||
mentions: {
|
||||
user: boolean;
|
||||
keywords: boolean;
|
||||
room: boolean;
|
||||
};
|
||||
keywords: string[];
|
||||
};
|
||||
|
||||
export const DefaultNotificationSettings: NotificationSettings = {
|
||||
globalMute: false,
|
||||
defaultLevels: {
|
||||
room: RoomNotifState.AllMessages,
|
||||
dm: RoomNotifState.AllMessages,
|
||||
},
|
||||
sound: {
|
||||
people: "default",
|
||||
mentions: "default",
|
||||
calls: "ring",
|
||||
},
|
||||
activity: {
|
||||
invite: true,
|
||||
status_event: false,
|
||||
bot_notices: true,
|
||||
},
|
||||
mentions: {
|
||||
user: true,
|
||||
room: true,
|
||||
keywords: true,
|
||||
},
|
||||
keywords: [],
|
||||
};
|
||||
35
src/models/notificationsettings/PushRuleDiff.ts
Normal file
35
src/models/notificationsettings/PushRuleDiff.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { IAnnotatedPushRule, PushRuleAction, PushRuleKind, RuleId } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
export type PushRuleDiff = {
|
||||
updated: PushRuleUpdate[];
|
||||
added: IAnnotatedPushRule[];
|
||||
deleted: PushRuleDeletion[];
|
||||
};
|
||||
|
||||
export type PushRuleDeletion = {
|
||||
rule_id: RuleId | string;
|
||||
kind: PushRuleKind;
|
||||
};
|
||||
|
||||
export type PushRuleUpdate = {
|
||||
rule_id: RuleId | string;
|
||||
kind: PushRuleKind;
|
||||
enabled?: boolean;
|
||||
actions?: PushRuleAction[];
|
||||
};
|
||||
33
src/models/notificationsettings/PushRuleMap.ts
Normal file
33
src/models/notificationsettings/PushRuleMap.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { IAnnotatedPushRule, IPushRules, PushRuleKind, RuleId } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
export type PushRuleMap = Map<RuleId | string, IAnnotatedPushRule>;
|
||||
|
||||
export function buildPushRuleMap(rulesets: IPushRules): PushRuleMap {
|
||||
const rules = new Map<RuleId | string, IAnnotatedPushRule>();
|
||||
|
||||
for (const kind of Object.values(PushRuleKind)) {
|
||||
for (const rule of rulesets.global[kind] ?? []) {
|
||||
if (rule.rule_id.startsWith(".")) {
|
||||
rules.set(rule.rule_id, { ...rule, kind });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rules;
|
||||
}
|
||||
228
src/models/notificationsettings/reconcileNotificationSettings.ts
Normal file
228
src/models/notificationsettings/reconcileNotificationSettings.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { IPushRules, PushRuleKind, RuleId } from "matrix-js-sdk/src/matrix";
|
||||
import { deepCompare } from "matrix-js-sdk/src/utils";
|
||||
|
||||
import { NotificationUtils } from "../../notifications";
|
||||
import { StandardActions } from "../../notifications/StandardActions";
|
||||
import { RoomNotifState } from "../../RoomNotifs";
|
||||
import { NotificationSettings } from "./NotificationSettings";
|
||||
import { PushRuleDiff, PushRuleUpdate } from "./PushRuleDiff";
|
||||
import { buildPushRuleMap } from "./PushRuleMap";
|
||||
|
||||
function toStandardRules(
|
||||
model: NotificationSettings,
|
||||
supportsIntentionalMentions: boolean,
|
||||
): Map<RuleId | string, PushRuleUpdate> {
|
||||
const standardRules = new Map<RuleId | string, PushRuleUpdate>();
|
||||
|
||||
standardRules.set(RuleId.Master, {
|
||||
rule_id: RuleId.Master,
|
||||
kind: PushRuleKind.Override,
|
||||
enabled: model.globalMute,
|
||||
});
|
||||
|
||||
standardRules.set(RuleId.EncryptedMessage, {
|
||||
rule_id: RuleId.EncryptedMessage,
|
||||
kind: PushRuleKind.Underride,
|
||||
enabled: true,
|
||||
actions: NotificationUtils.encodeActions({
|
||||
notify: model.defaultLevels.room === RoomNotifState.AllMessages,
|
||||
highlight: false,
|
||||
}),
|
||||
});
|
||||
standardRules.set(RuleId.Message, {
|
||||
rule_id: RuleId.Message,
|
||||
kind: PushRuleKind.Underride,
|
||||
enabled: true,
|
||||
actions: NotificationUtils.encodeActions({
|
||||
notify: model.defaultLevels.room === RoomNotifState.AllMessages,
|
||||
highlight: false,
|
||||
}),
|
||||
});
|
||||
standardRules.set(RuleId.EncryptedDM, {
|
||||
rule_id: RuleId.EncryptedDM,
|
||||
kind: PushRuleKind.Underride,
|
||||
enabled: true,
|
||||
actions: NotificationUtils.encodeActions({
|
||||
notify: model.defaultLevels.dm === RoomNotifState.AllMessages,
|
||||
highlight: false,
|
||||
sound: model.sound.people,
|
||||
}),
|
||||
});
|
||||
standardRules.set(RuleId.DM, {
|
||||
rule_id: RuleId.DM,
|
||||
kind: PushRuleKind.Underride,
|
||||
enabled: true,
|
||||
actions: NotificationUtils.encodeActions({
|
||||
notify: model.defaultLevels.dm === RoomNotifState.AllMessages,
|
||||
highlight: false,
|
||||
sound: model.sound.people,
|
||||
}),
|
||||
});
|
||||
|
||||
standardRules.set(RuleId.SuppressNotices, {
|
||||
rule_id: RuleId.SuppressNotices,
|
||||
kind: PushRuleKind.Override,
|
||||
enabled: !model.activity.bot_notices,
|
||||
actions: StandardActions.ACTION_DONT_NOTIFY,
|
||||
});
|
||||
standardRules.set(RuleId.InviteToSelf, {
|
||||
rule_id: RuleId.InviteToSelf,
|
||||
kind: PushRuleKind.Override,
|
||||
enabled: model.activity.invite,
|
||||
actions: NotificationUtils.encodeActions({
|
||||
notify: true,
|
||||
highlight: false,
|
||||
sound: model.sound.people,
|
||||
}),
|
||||
});
|
||||
standardRules.set(RuleId.MemberEvent, {
|
||||
rule_id: RuleId.MemberEvent,
|
||||
kind: PushRuleKind.Override,
|
||||
enabled: true,
|
||||
actions: model.activity.status_event ? StandardActions.ACTION_NOTIFY : StandardActions.ACTION_DONT_NOTIFY,
|
||||
});
|
||||
|
||||
const mentionActions = NotificationUtils.encodeActions({
|
||||
notify: true,
|
||||
sound: model.sound.mentions,
|
||||
highlight: true,
|
||||
});
|
||||
const userMentionActions = model.mentions.user ? mentionActions : StandardActions.ACTION_DONT_NOTIFY;
|
||||
if (supportsIntentionalMentions) {
|
||||
standardRules.set(RuleId.IsUserMention, {
|
||||
rule_id: RuleId.IsUserMention,
|
||||
kind: PushRuleKind.ContentSpecific,
|
||||
enabled: true,
|
||||
actions: userMentionActions,
|
||||
});
|
||||
}
|
||||
standardRules.set(RuleId.ContainsDisplayName, {
|
||||
rule_id: RuleId.ContainsDisplayName,
|
||||
kind: PushRuleKind.Override,
|
||||
enabled: true,
|
||||
actions: userMentionActions,
|
||||
});
|
||||
standardRules.set(RuleId.ContainsUserName, {
|
||||
rule_id: RuleId.ContainsUserName,
|
||||
kind: PushRuleKind.ContentSpecific,
|
||||
enabled: true,
|
||||
actions: userMentionActions,
|
||||
});
|
||||
|
||||
const roomMentionActions = model.mentions.room ? StandardActions.ACTION_NOTIFY : StandardActions.ACTION_DONT_NOTIFY;
|
||||
if (supportsIntentionalMentions) {
|
||||
standardRules.set(RuleId.IsRoomMention, {
|
||||
rule_id: RuleId.IsRoomMention,
|
||||
kind: PushRuleKind.ContentSpecific,
|
||||
enabled: true,
|
||||
actions: roomMentionActions,
|
||||
});
|
||||
}
|
||||
standardRules.set(RuleId.AtRoomNotification, {
|
||||
rule_id: RuleId.AtRoomNotification,
|
||||
kind: PushRuleKind.Override,
|
||||
enabled: true,
|
||||
actions: roomMentionActions,
|
||||
});
|
||||
|
||||
standardRules.set(RuleId.Tombstone, {
|
||||
rule_id: RuleId.Tombstone,
|
||||
kind: PushRuleKind.Override,
|
||||
enabled: model.activity.status_event,
|
||||
actions: StandardActions.ACTION_HIGHLIGHT,
|
||||
});
|
||||
|
||||
standardRules.set(RuleId.IncomingCall, {
|
||||
rule_id: RuleId.IncomingCall,
|
||||
kind: PushRuleKind.Underride,
|
||||
enabled: true,
|
||||
actions: NotificationUtils.encodeActions({
|
||||
notify: true,
|
||||
sound: model.sound.calls,
|
||||
}),
|
||||
});
|
||||
|
||||
return standardRules;
|
||||
}
|
||||
|
||||
export function reconcileNotificationSettings(
|
||||
pushRules: IPushRules,
|
||||
model: NotificationSettings,
|
||||
supportsIntentionalMentions: boolean,
|
||||
): PushRuleDiff {
|
||||
const changes: PushRuleDiff = {
|
||||
updated: [],
|
||||
added: [],
|
||||
deleted: [],
|
||||
};
|
||||
|
||||
const oldRules = buildPushRuleMap(pushRules);
|
||||
const newRules = toStandardRules(model, supportsIntentionalMentions);
|
||||
|
||||
for (const rule of newRules.values()) {
|
||||
const original = oldRules.get(rule.rule_id);
|
||||
let changed = false;
|
||||
if (original === undefined) {
|
||||
changed = true;
|
||||
} else if (rule.enabled !== undefined && rule.enabled !== original.enabled) {
|
||||
changed = true;
|
||||
} else if (rule.actions !== undefined) {
|
||||
const originalActions = NotificationUtils.decodeActions(original.actions);
|
||||
const actions = NotificationUtils.decodeActions(rule.actions);
|
||||
if (originalActions === null || actions === null) {
|
||||
changed = true;
|
||||
} else if (!deepCompare(actions, originalActions)) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
changes.updated.push(rule);
|
||||
}
|
||||
}
|
||||
|
||||
const contentRules = pushRules.global.content?.filter((rule) => !rule.rule_id.startsWith(".")) ?? [];
|
||||
const newKeywords = new Set(model.keywords);
|
||||
for (const rule of contentRules) {
|
||||
if (!newKeywords.has(rule.pattern!)) {
|
||||
changes.deleted.push({
|
||||
rule_id: rule.rule_id,
|
||||
kind: PushRuleKind.ContentSpecific,
|
||||
});
|
||||
} else if (rule.enabled !== model.mentions.keywords) {
|
||||
changes.updated.push({
|
||||
rule_id: rule.rule_id,
|
||||
kind: PushRuleKind.ContentSpecific,
|
||||
enabled: model.mentions.keywords,
|
||||
});
|
||||
}
|
||||
newKeywords.delete(rule.pattern!);
|
||||
}
|
||||
for (const keyword of newKeywords) {
|
||||
changes.added.push({
|
||||
rule_id: keyword,
|
||||
kind: PushRuleKind.ContentSpecific,
|
||||
default: false,
|
||||
enabled: model.mentions.keywords,
|
||||
pattern: keyword,
|
||||
actions: StandardActions.ACTION_NOTIFY,
|
||||
});
|
||||
}
|
||||
|
||||
return changes;
|
||||
}
|
||||
95
src/models/notificationsettings/toNotificationSettings.ts
Normal file
95
src/models/notificationsettings/toNotificationSettings.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { IPushRule, IPushRules, RuleId } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { NotificationUtils } from "../../notifications";
|
||||
import { RoomNotifState } from "../../RoomNotifs";
|
||||
import { NotificationSettings } from "./NotificationSettings";
|
||||
import { buildPushRuleMap } from "./PushRuleMap";
|
||||
|
||||
function shouldNotify(rules: (IPushRule | null | undefined | false)[]): boolean {
|
||||
if (rules.length === 0) {
|
||||
return true;
|
||||
}
|
||||
for (const rule of rules) {
|
||||
if (rule === null || rule === undefined || rule === false || !rule.enabled) {
|
||||
continue;
|
||||
}
|
||||
const actions = NotificationUtils.decodeActions(rule.actions);
|
||||
if (actions !== null && actions.notify) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function determineSound(rules: (IPushRule | null | undefined | false)[]): string | undefined {
|
||||
for (const rule of rules) {
|
||||
if (rule === null || rule === undefined || rule === false || !rule.enabled) {
|
||||
continue;
|
||||
}
|
||||
const actions = NotificationUtils.decodeActions(rule.actions);
|
||||
if (actions !== null && actions.notify && actions.sound !== undefined) {
|
||||
return actions.sound;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function toNotificationSettings(
|
||||
pushRules: IPushRules,
|
||||
supportsIntentionalMentions: boolean,
|
||||
): NotificationSettings {
|
||||
const standardRules = buildPushRuleMap(pushRules);
|
||||
const contentRules = pushRules.global.content?.filter((rule) => !rule.rule_id.startsWith(".")) ?? [];
|
||||
const dmRules = [standardRules.get(RuleId.DM), standardRules.get(RuleId.EncryptedDM)];
|
||||
const roomRules = [standardRules.get(RuleId.Message), standardRules.get(RuleId.EncryptedMessage)];
|
||||
return {
|
||||
globalMute: standardRules.get(RuleId.Master)?.enabled ?? false,
|
||||
defaultLevels: {
|
||||
room: shouldNotify(roomRules) ? RoomNotifState.AllMessages : RoomNotifState.MentionsOnly,
|
||||
dm: shouldNotify(dmRules) ? RoomNotifState.AllMessages : RoomNotifState.MentionsOnly,
|
||||
},
|
||||
sound: {
|
||||
calls: determineSound([standardRules.get(RuleId.IncomingCall)]),
|
||||
mentions: determineSound([
|
||||
supportsIntentionalMentions && standardRules.get(RuleId.IsUserMention),
|
||||
standardRules.get(RuleId.ContainsUserName),
|
||||
standardRules.get(RuleId.ContainsDisplayName),
|
||||
]),
|
||||
people: determineSound(dmRules),
|
||||
},
|
||||
activity: {
|
||||
bot_notices: shouldNotify([standardRules.get(RuleId.SuppressNotices)]),
|
||||
invite: shouldNotify([standardRules.get(RuleId.InviteToSelf)]),
|
||||
status_event: shouldNotify([standardRules.get(RuleId.MemberEvent), standardRules.get(RuleId.Tombstone)]),
|
||||
},
|
||||
mentions: {
|
||||
user: shouldNotify([
|
||||
supportsIntentionalMentions && standardRules.get(RuleId.IsUserMention),
|
||||
standardRules.get(RuleId.ContainsUserName),
|
||||
standardRules.get(RuleId.ContainsDisplayName),
|
||||
]),
|
||||
room: shouldNotify([
|
||||
supportsIntentionalMentions && standardRules.get(RuleId.IsRoomMention),
|
||||
standardRules.get(RuleId.AtRoomNotification),
|
||||
]),
|
||||
keywords: shouldNotify(contentRules),
|
||||
},
|
||||
keywords: contentRules.map((it) => it.pattern!),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user