2018-04-11 23:58:37 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2016-2021 The Matrix.org Foundation C.I.C.
|
2018-04-11 23:58:37 +01: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.
|
2018-04-11 23:58:37 +01:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type IPushRule, type PushRuleAction } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import { StandardActions } from "./StandardActions";
|
|
|
|
|
import { NotificationUtils } from "./NotificationUtils";
|
2020-06-25 09:00:13 +01:00
|
|
|
|
2021-07-11 20:53:12 -06:00
|
|
|
export enum VectorState {
|
2020-06-25 09:00:13 +01:00
|
|
|
/** The push rule is disabled */
|
|
|
|
|
Off = "off",
|
|
|
|
|
/** The user will receive push notification for this rule */
|
|
|
|
|
On = "on",
|
|
|
|
|
/** The user will receive push notification for this rule with sound and
|
|
|
|
|
highlight if this is legitimate */
|
|
|
|
|
Loud = "loud",
|
|
|
|
|
}
|
2018-04-11 23:58:37 +01:00
|
|
|
|
2019-12-19 17:45:24 -07:00
|
|
|
export class PushRuleVectorState {
|
2020-06-25 09:00:13 +01:00
|
|
|
// Backwards compatibility (things should probably be using the enum above instead)
|
2022-12-16 12:29:59 +00:00
|
|
|
public static OFF = VectorState.Off;
|
|
|
|
|
public static ON = VectorState.On;
|
|
|
|
|
public static LOUD = VectorState.Loud;
|
2018-04-11 23:58:37 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enum for state of a push rule as defined by the Vector UI.
|
|
|
|
|
* @readonly
|
|
|
|
|
* @enum {string}
|
|
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public static states = VectorState;
|
2018-04-11 23:58:37 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a PushRuleVectorState to a list of actions
|
|
|
|
|
*
|
|
|
|
|
* @return [object] list of push-rule actions
|
|
|
|
|
*/
|
2023-05-09 18:24:40 +01:00
|
|
|
public static actionsFor(pushRuleVectorState?: VectorState): PushRuleAction[] {
|
2021-07-11 20:53:12 -06:00
|
|
|
if (pushRuleVectorState === VectorState.On) {
|
2018-04-11 23:58:37 +01:00
|
|
|
return StandardActions.ACTION_NOTIFY;
|
2021-07-11 20:53:12 -06:00
|
|
|
} else if (pushRuleVectorState === VectorState.Loud) {
|
2018-04-11 23:58:37 +01:00
|
|
|
return StandardActions.ACTION_HIGHLIGHT_DEFAULT_SOUND;
|
|
|
|
|
}
|
2023-02-16 17:21:44 +00:00
|
|
|
return [];
|
2019-12-19 17:45:24 -07:00
|
|
|
}
|
2018-04-11 23:58:37 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a pushrule's actions to a PushRuleVectorState.
|
|
|
|
|
*
|
|
|
|
|
* Determines whether a content rule is in the PushRuleVectorState.ON
|
|
|
|
|
* category or in PushRuleVectorState.LOUD, regardless of its enabled
|
|
|
|
|
* state. Returns null if it does not match these categories.
|
|
|
|
|
*/
|
2023-02-15 13:36:22 +00:00
|
|
|
public static contentRuleVectorStateKind(rule: IPushRule): VectorState | null {
|
2018-10-11 22:01:10 -05:00
|
|
|
const decoded = NotificationUtils.decodeActions(rule.actions);
|
2018-04-11 23:58:37 +01:00
|
|
|
|
|
|
|
|
if (!decoded) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Count tweaks to determine if it is a ON or LOUD rule
|
2018-10-11 22:01:10 -05:00
|
|
|
let tweaks = 0;
|
2018-04-11 23:58:37 +01:00
|
|
|
if (decoded.sound) {
|
|
|
|
|
tweaks++;
|
|
|
|
|
}
|
|
|
|
|
if (decoded.highlight) {
|
|
|
|
|
tweaks++;
|
|
|
|
|
}
|
2023-02-15 13:36:22 +00:00
|
|
|
let stateKind: VectorState | null = null;
|
2018-04-11 23:58:37 +01:00
|
|
|
switch (tweaks) {
|
|
|
|
|
case 0:
|
2021-07-11 20:53:12 -06:00
|
|
|
stateKind = VectorState.On;
|
2018-04-11 23:58:37 +01:00
|
|
|
break;
|
|
|
|
|
case 2:
|
2021-07-11 20:53:12 -06:00
|
|
|
stateKind = VectorState.Loud;
|
2018-04-11 23:58:37 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return stateKind;
|
2019-12-19 17:45:24 -07:00
|
|
|
}
|
2018-10-11 22:01:10 -05:00
|
|
|
}
|