|
| 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 | + setModel(model); |
| 70 | + const changes = reconcileNotificationSettings(pushRules.current, model, supportsIntentionalMentions); |
| 71 | + applyChanges(cli, changes) |
| 72 | + .then(updatePushRules) |
| 73 | + .catch((err) => console.error(err)); |
| 74 | + }, |
| 75 | + [cli, updatePushRules, supportsIntentionalMentions], |
| 76 | + ); |
| 77 | + |
| 78 | + return { model, hasPendingChanges, reconcile }; |
| 79 | +} |
0 commit comments