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:
parent
2972219959
commit
97765613bc
15 changed files with 2383 additions and 6 deletions
48
src/hooks/useAsyncRefreshMemo.ts
Normal file
48
src/hooks/useAsyncRefreshMemo.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
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 { DependencyList, useCallback, useEffect, useState } from "react";
|
||||
|
||||
type Fn<T> = () => Promise<T>;
|
||||
|
||||
/**
|
||||
* Works just like useMemo or our own useAsyncMemo, but additionally exposes a method to refresh the cached value
|
||||
* as if the dependency had changed
|
||||
* @param fn function to memoize
|
||||
* @param deps React hooks dependencies for the function
|
||||
* @param initialValue initial value
|
||||
* @return tuple of cached value and refresh callback
|
||||
*/
|
||||
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue: T): [T, () => void];
|
||||
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): [T | undefined, () => void];
|
||||
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): [T | undefined, () => void] {
|
||||
const [value, setValue] = useState<T | undefined>(initialValue);
|
||||
const refresh = useCallback(() => {
|
||||
let discard = false;
|
||||
fn()
|
||||
.then((v) => {
|
||||
if (!discard) {
|
||||
setValue(v);
|
||||
}
|
||||
})
|
||||
.catch((err) => console.error(err));
|
||||
return () => {
|
||||
discard = true;
|
||||
};
|
||||
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
useEffect(refresh, [refresh]);
|
||||
return [value, refresh];
|
||||
}
|
81
src/hooks/useNotificationSettings.tsx
Normal file
81
src/hooks/useNotificationSettings.tsx
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
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, MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import { NotificationSettings } from "../models/notificationsettings/NotificationSettings";
|
||||
import { PushRuleDiff } from "../models/notificationsettings/PushRuleDiff";
|
||||
import { reconcileNotificationSettings } from "../models/notificationsettings/reconcileNotificationSettings";
|
||||
import { toNotificationSettings } from "../models/notificationsettings/toNotificationSettings";
|
||||
|
||||
async function applyChanges(cli: MatrixClient, changes: PushRuleDiff): Promise<void> {
|
||||
await Promise.all(changes.deleted.map((change) => cli.deletePushRule("global", change.kind, change.rule_id)));
|
||||
await Promise.all(changes.added.map((change) => cli.addPushRule("global", change.kind, change.rule_id, change)));
|
||||
await Promise.all(
|
||||
changes.updated.map(async (change) => {
|
||||
if (change.enabled !== undefined) {
|
||||
await cli.setPushRuleEnabled("global", change.kind, change.rule_id, change.enabled);
|
||||
}
|
||||
if (change.actions !== undefined) {
|
||||
await cli.setPushRuleActions("global", change.kind, change.rule_id, change.actions);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
type UseNotificationSettings = {
|
||||
model: NotificationSettings | null;
|
||||
hasPendingChanges: boolean;
|
||||
reconcile: (model: NotificationSettings) => void;
|
||||
};
|
||||
|
||||
export function useNotificationSettings(cli: MatrixClient): UseNotificationSettings {
|
||||
const supportsIntentionalMentions = useMemo(() => cli.supportsIntentionalMentions(), [cli]);
|
||||
|
||||
const pushRules = useRef<IPushRules | null>(null);
|
||||
const [model, setModel] = useState<NotificationSettings | null>(null);
|
||||
const [hasPendingChanges, setPendingChanges] = useState<boolean>(false);
|
||||
const updatePushRules = useCallback(async () => {
|
||||
const rules = await cli.getPushRules();
|
||||
const model = toNotificationSettings(rules, supportsIntentionalMentions);
|
||||
const pendingChanges = reconcileNotificationSettings(rules, model, supportsIntentionalMentions);
|
||||
pushRules.current = rules;
|
||||
setPendingChanges(
|
||||
pendingChanges.updated.length > 0 || pendingChanges.added.length > 0 || pendingChanges.deleted.length > 0,
|
||||
);
|
||||
setModel(model);
|
||||
}, [cli, supportsIntentionalMentions]);
|
||||
|
||||
useEffect(() => {
|
||||
updatePushRules().catch((err) => console.error(err));
|
||||
}, [cli, updatePushRules]);
|
||||
|
||||
const reconcile = useCallback(
|
||||
(model: NotificationSettings) => {
|
||||
if (pushRules.current !== null) {
|
||||
setModel(model);
|
||||
const changes = reconcileNotificationSettings(pushRules.current, model, supportsIntentionalMentions);
|
||||
applyChanges(cli, changes)
|
||||
.then(updatePushRules)
|
||||
.catch((err) => console.error(err));
|
||||
}
|
||||
},
|
||||
[cli, updatePushRules, supportsIntentionalMentions],
|
||||
);
|
||||
|
||||
return { model, hasPendingChanges, reconcile };
|
||||
}
|
23
src/hooks/usePushers.ts
Normal file
23
src/hooks/usePushers.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
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 { IPusher, MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { useAsyncRefreshMemo } from "./useAsyncRefreshMemo";
|
||||
|
||||
export function usePushers(client: MatrixClient): [IPusher[], () => void] {
|
||||
return useAsyncRefreshMemo<IPusher[]>(() => client.getPushers().then((it) => it.pushers), [client], []);
|
||||
}
|
24
src/hooks/useThreepids.ts
Normal file
24
src/hooks/useThreepids.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
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 { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { IThreepid } from "matrix-js-sdk/src/@types/threepids";
|
||||
|
||||
import { useAsyncRefreshMemo } from "./useAsyncRefreshMemo";
|
||||
|
||||
export function useThreepids(client: MatrixClient): [IThreepid[], () => void] {
|
||||
return useAsyncRefreshMemo<IThreepid[]>(() => client.getThreePids().then((it) => it.threepids), [client], []);
|
||||
}
|
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!),
|
||||
};
|
||||
}
|
|
@ -16,7 +16,7 @@ limitations under the License.
|
|||
|
||||
import { PushRuleAction, PushRuleActionName, TweakHighlight, TweakSound } from "matrix-js-sdk/src/@types/PushRules";
|
||||
|
||||
interface IEncodedActions {
|
||||
export interface PushRuleActions {
|
||||
notify: boolean;
|
||||
sound?: string;
|
||||
highlight?: boolean;
|
||||
|
@ -29,7 +29,7 @@ export class NotificationUtils {
|
|||
// "highlight: true/false,
|
||||
// }
|
||||
// to a list of push actions.
|
||||
public static encodeActions(action: IEncodedActions): PushRuleAction[] {
|
||||
public static encodeActions(action: PushRuleActions): PushRuleAction[] {
|
||||
const notify = action.notify;
|
||||
const sound = action.sound;
|
||||
const highlight = action.highlight;
|
||||
|
@ -55,13 +55,12 @@ export class NotificationUtils {
|
|||
// "highlight: true/false,
|
||||
// }
|
||||
// If the actions couldn't be decoded then returns null.
|
||||
public static decodeActions(actions: PushRuleAction[]): IEncodedActions | null {
|
||||
public static decodeActions(actions: PushRuleAction[]): PushRuleActions | null {
|
||||
let notify = false;
|
||||
let sound: string | undefined;
|
||||
let highlight: boolean | undefined = false;
|
||||
|
||||
for (let i = 0; i < actions.length; ++i) {
|
||||
const action = actions[i];
|
||||
for (const action of actions) {
|
||||
if (action === PushRuleActionName.Notify) {
|
||||
notify = true;
|
||||
} else if (action === PushRuleActionName.DontNotify) {
|
||||
|
@ -86,7 +85,7 @@ export class NotificationUtils {
|
|||
highlight = true;
|
||||
}
|
||||
|
||||
const result: IEncodedActions = { notify, highlight };
|
||||
const result: PushRuleActions = { notify, highlight };
|
||||
if (sound !== undefined) {
|
||||
result.sound = sound;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue