Files
element-web/apps/web/src/notifications/PushRuleVectorState.ts
T

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

85 lines
2.6 KiB
TypeScript
Raw Normal View History

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
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";
2021-07-11 20:53:12 -06:00
export enum VectorState {
/** 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
export class PushRuleVectorState {
// Backwards compatibility (things should probably be using the enum above instead)
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}
*/
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
*/
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;
}
return [];
}
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.
*/
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++;
}
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;
}
2018-10-11 22:01:10 -05:00
}