Skip to content

Commit 9776561

Browse files
justjannerobintown
andauthored
Implement new model, hooks and reconcilation code for new GYU notification settings (matrix-org#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 <[email protected]> * Add tests for hooks * chore: fixed lint issues * Add comments --------- Co-authored-by: Robin <[email protected]>
1 parent 2972219 commit 9776561

15 files changed

+2383
-6
lines changed

Diff for: src/hooks/useAsyncRefreshMemo.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { DependencyList, useCallback, useEffect, useState } from "react";
18+
19+
type Fn<T> = () => Promise<T>;
20+
21+
/**
22+
* Works just like useMemo or our own useAsyncMemo, but additionally exposes a method to refresh the cached value
23+
* as if the dependency had changed
24+
* @param fn function to memoize
25+
* @param deps React hooks dependencies for the function
26+
* @param initialValue initial value
27+
* @return tuple of cached value and refresh callback
28+
*/
29+
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue: T): [T, () => void];
30+
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): [T | undefined, () => void];
31+
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): [T | undefined, () => void] {
32+
const [value, setValue] = useState<T | undefined>(initialValue);
33+
const refresh = useCallback(() => {
34+
let discard = false;
35+
fn()
36+
.then((v) => {
37+
if (!discard) {
38+
setValue(v);
39+
}
40+
})
41+
.catch((err) => console.error(err));
42+
return () => {
43+
discard = true;
44+
};
45+
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
46+
useEffect(refresh, [refresh]);
47+
return [value, refresh];
48+
}

Diff for: src/hooks/useNotificationSettings.tsx

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { IPushRules, MatrixClient } from "matrix-js-sdk/src/matrix";
18+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
19+
20+
import { NotificationSettings } from "../models/notificationsettings/NotificationSettings";
21+
import { PushRuleDiff } from "../models/notificationsettings/PushRuleDiff";
22+
import { reconcileNotificationSettings } from "../models/notificationsettings/reconcileNotificationSettings";
23+
import { toNotificationSettings } from "../models/notificationsettings/toNotificationSettings";
24+
25+
async function applyChanges(cli: MatrixClient, changes: PushRuleDiff): Promise<void> {
26+
await Promise.all(changes.deleted.map((change) => cli.deletePushRule("global", change.kind, change.rule_id)));
27+
await Promise.all(changes.added.map((change) => cli.addPushRule("global", change.kind, change.rule_id, change)));
28+
await Promise.all(
29+
changes.updated.map(async (change) => {
30+
if (change.enabled !== undefined) {
31+
await cli.setPushRuleEnabled("global", change.kind, change.rule_id, change.enabled);
32+
}
33+
if (change.actions !== undefined) {
34+
await cli.setPushRuleActions("global", change.kind, change.rule_id, change.actions);
35+
}
36+
}),
37+
);
38+
}
39+
40+
type UseNotificationSettings = {
41+
model: NotificationSettings | null;
42+
hasPendingChanges: boolean;
43+
reconcile: (model: NotificationSettings) => void;
44+
};
45+
46+
export function useNotificationSettings(cli: MatrixClient): UseNotificationSettings {
47+
const supportsIntentionalMentions = useMemo(() => cli.supportsIntentionalMentions(), [cli]);
48+
49+
const pushRules = useRef<IPushRules | null>(null);
50+
const [model, setModel] = useState<NotificationSettings | null>(null);
51+
const [hasPendingChanges, setPendingChanges] = useState<boolean>(false);
52+
const updatePushRules = useCallback(async () => {
53+
const rules = await cli.getPushRules();
54+
const model = toNotificationSettings(rules, supportsIntentionalMentions);
55+
const pendingChanges = reconcileNotificationSettings(rules, model, supportsIntentionalMentions);
56+
pushRules.current = rules;
57+
setPendingChanges(
58+
pendingChanges.updated.length > 0 || pendingChanges.added.length > 0 || pendingChanges.deleted.length > 0,
59+
);
60+
setModel(model);
61+
}, [cli, supportsIntentionalMentions]);
62+
63+
useEffect(() => {
64+
updatePushRules().catch((err) => console.error(err));
65+
}, [cli, updatePushRules]);
66+
67+
const reconcile = useCallback(
68+
(model: NotificationSettings) => {
69+
if (pushRules.current !== null) {
70+
setModel(model);
71+
const changes = reconcileNotificationSettings(pushRules.current, model, supportsIntentionalMentions);
72+
applyChanges(cli, changes)
73+
.then(updatePushRules)
74+
.catch((err) => console.error(err));
75+
}
76+
},
77+
[cli, updatePushRules, supportsIntentionalMentions],
78+
);
79+
80+
return { model, hasPendingChanges, reconcile };
81+
}

Diff for: src/hooks/usePushers.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { IPusher, MatrixClient } from "matrix-js-sdk/src/matrix";
18+
19+
import { useAsyncRefreshMemo } from "./useAsyncRefreshMemo";
20+
21+
export function usePushers(client: MatrixClient): [IPusher[], () => void] {
22+
return useAsyncRefreshMemo<IPusher[]>(() => client.getPushers().then((it) => it.pushers), [client], []);
23+
}

Diff for: src/hooks/useThreepids.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
18+
import { IThreepid } from "matrix-js-sdk/src/@types/threepids";
19+
20+
import { useAsyncRefreshMemo } from "./useAsyncRefreshMemo";
21+
22+
export function useThreepids(client: MatrixClient): [IThreepid[], () => void] {
23+
return useAsyncRefreshMemo<IThreepid[]>(() => client.getThreePids().then((it) => it.threepids), [client], []);
24+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { RoomNotifState } from "../../RoomNotifs";
18+
19+
export type RoomDefaultNotificationLevel = RoomNotifState.AllMessages | RoomNotifState.MentionsOnly;
20+
21+
export type NotificationSettings = {
22+
globalMute: boolean;
23+
defaultLevels: {
24+
room: RoomDefaultNotificationLevel;
25+
dm: RoomDefaultNotificationLevel;
26+
};
27+
sound: {
28+
people: string | undefined;
29+
mentions: string | undefined;
30+
calls: string | undefined;
31+
};
32+
activity: {
33+
invite: boolean;
34+
status_event: boolean;
35+
bot_notices: boolean;
36+
};
37+
mentions: {
38+
user: boolean;
39+
keywords: boolean;
40+
room: boolean;
41+
};
42+
keywords: string[];
43+
};
44+
45+
export const DefaultNotificationSettings: NotificationSettings = {
46+
globalMute: false,
47+
defaultLevels: {
48+
room: RoomNotifState.AllMessages,
49+
dm: RoomNotifState.AllMessages,
50+
},
51+
sound: {
52+
people: "default",
53+
mentions: "default",
54+
calls: "ring",
55+
},
56+
activity: {
57+
invite: true,
58+
status_event: false,
59+
bot_notices: true,
60+
},
61+
mentions: {
62+
user: true,
63+
room: true,
64+
keywords: true,
65+
},
66+
keywords: [],
67+
};

Diff for: src/models/notificationsettings/PushRuleDiff.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { IAnnotatedPushRule, PushRuleAction, PushRuleKind, RuleId } from "matrix-js-sdk/src/matrix";
18+
19+
export type PushRuleDiff = {
20+
updated: PushRuleUpdate[];
21+
added: IAnnotatedPushRule[];
22+
deleted: PushRuleDeletion[];
23+
};
24+
25+
export type PushRuleDeletion = {
26+
rule_id: RuleId | string;
27+
kind: PushRuleKind;
28+
};
29+
30+
export type PushRuleUpdate = {
31+
rule_id: RuleId | string;
32+
kind: PushRuleKind;
33+
enabled?: boolean;
34+
actions?: PushRuleAction[];
35+
};

Diff for: src/models/notificationsettings/PushRuleMap.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { IAnnotatedPushRule, IPushRules, PushRuleKind, RuleId } from "matrix-js-sdk/src/matrix";
18+
19+
export type PushRuleMap = Map<RuleId | string, IAnnotatedPushRule>;
20+
21+
export function buildPushRuleMap(rulesets: IPushRules): PushRuleMap {
22+
const rules = new Map<RuleId | string, IAnnotatedPushRule>();
23+
24+
for (const kind of Object.values(PushRuleKind)) {
25+
for (const rule of rulesets.global[kind] ?? []) {
26+
if (rule.rule_id.startsWith(".")) {
27+
rules.set(rule.rule_id, { ...rule, kind });
28+
}
29+
}
30+
}
31+
32+
return rules;
33+
}

0 commit comments

Comments
 (0)