Files
ThreadNet-Web/apps/web/src/settings/controllers/NotificationControllers.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-11-04 21:47:18 -07:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-07-28 15:19:11 -06:00
Copyright 2020 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
Copyright 2017 Travis Ralston
2017-11-04 21:47:18 -07:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2017-11-04 21:47:18 -07:00
*/
2021-10-28 19:41:42 -05:00
import { logger } from "matrix-js-sdk/src/logger";
import { PushRuleActionName } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
2017-11-04 22:28:35 -07:00
import SettingController from "./SettingController";
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../MatrixClientPeg";
2025-02-05 13:25:06 +00:00
import { type SettingLevel } from "../SettingLevel";
2017-11-15 22:09:40 -07:00
// .m.rule.master being enabled means all events match that push rule
// default action on this rule is dont_notify, but it could be something else
export function isPushNotifyDisabled(): boolean {
2017-11-15 22:09:40 -07:00
// Return the value of the master push rule as a default
const masterRule = MatrixClientPeg.get()?.pushProcessor.getPushRuleById(".m.rule.master");
2017-11-15 22:09:40 -07:00
if (!masterRule) {
2021-10-15 16:31:29 +02:00
logger.warn("No master push rule! Notifications are disabled for this user.");
return true;
2017-11-15 22:09:40 -07:00
}
// If the rule is enabled then check it does not notify on everything
return masterRule.enabled && !masterRule.actions.includes(PushRuleActionName.Notify);
2017-11-15 22:09:40 -07:00
}
2017-11-04 21:47:18 -07:00
2017-11-04 22:28:35 -07:00
export class NotificationsEnabledController extends SettingController {
2020-07-30 08:41:51 -06:00
public getValueOverride(
level: SettingLevel,
roomId: string,
calculatedValue: any,
calculatedAtLevel: SettingLevel | null,
2020-07-30 08:41:51 -06:00
): any {
2026-07-06 20:20:51 +01:00
if (!this.sdkContext.notifier.isPossible()) return false;
2017-11-04 22:28:35 -07:00
if (calculatedValue === null || calculatedAtLevel === "default") {
return !isPushNotifyDisabled();
2017-11-15 22:09:40 -07:00
}
return calculatedValue;
2017-11-04 22:28:35 -07:00
}
public onChange(level: SettingLevel, roomId: string, newValue: any): void {
2026-07-06 20:20:51 +01:00
if (this.sdkContext.notifier.supportsDesktopNotifications()) {
this.sdkContext.notifier.setEnabled(newValue);
2017-11-04 22:28:35 -07:00
}
}
}
export class NotificationBodyEnabledController extends SettingController {
2020-07-28 15:19:11 -06:00
public getValueOverride(level: SettingLevel, roomId: string, calculatedValue: any): any {
2026-07-06 20:20:51 +01:00
if (!this.sdkContext.notifier.isPossible()) return false;
2017-11-04 22:28:35 -07:00
2017-11-15 22:09:40 -07:00
if (calculatedValue === null) {
return !isPushNotifyDisabled();
2017-11-15 22:09:40 -07:00
}
return calculatedValue;
2017-11-04 22:28:35 -07:00
}
}