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
|
|
|
|
2025-01-06 11:18:54 +00: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";
|
2023-08-09 16:10:54 +01:00
|
|
|
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
|
|
|
|
2020-06-17 16:31:42 +01: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
|
2020-09-14 14:35:08 +01:00
|
|
|
export function isPushNotifyDisabled(): boolean {
|
2017-11-15 22:09:40 -07:00
|
|
|
// Return the value of the master push rule as a default
|
2026-01-22 17:04:01 +00:00
|
|
|
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.");
|
2020-06-17 16:31:42 +01:00
|
|
|
return true;
|
2017-11-15 22:09:40 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-17 16:31:42 +01:00
|
|
|
// If the rule is enabled then check it does not notify on everything
|
2021-08-10 11:03:03 +01:00
|
|
|
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,
|
2023-02-16 17:21:44 +00:00
|
|
|
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
|
|
|
|
2017-12-25 13:29:30 -07:00
|
|
|
if (calculatedValue === null || calculatedAtLevel === "default") {
|
2020-06-17 16:31:42 +01:00
|
|
|
return !isPushNotifyDisabled();
|
2017-11-15 22:09:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return calculatedValue;
|
2017-11-04 22:28:35 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00: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) {
|
2020-06-17 16:31:42 +01:00
|
|
|
return !isPushNotifyDisabled();
|
2017-11-15 22:09:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return calculatedValue;
|
2017-11-04 22:28:35 -07:00
|
|
|
}
|
|
|
|
|
}
|