Migrate to js-sdk types for push rules

This commit is contained in:
Travis Ralston 2021-07-11 20:03:07 -06:00
parent 9556b61041
commit 5b9fca3b91
4 changed files with 21 additions and 138 deletions

View file

@ -1,6 +1,5 @@
/* /*
Copyright 2016 OpenMarket Ltd Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -16,12 +15,12 @@ limitations under the License.
*/ */
import { PushRuleVectorState, State } from "./PushRuleVectorState"; import { PushRuleVectorState, State } from "./PushRuleVectorState";
import { IExtendedPushRule, IRuleSets } from "./types"; import { IAnnotatedPushRule, IPushRules, PushRuleKind } from "matrix-js-sdk/src/@types/PushRules";
export interface IContentRules { export interface IContentRules {
vectorState: State; vectorState: State;
rules: IExtendedPushRule[]; rules: IAnnotatedPushRule[];
externalRules: IExtendedPushRule[]; externalRules: IAnnotatedPushRule[];
} }
export const SCOPE = "global"; export const SCOPE = "global";
@ -39,9 +38,9 @@ export class ContentRules {
* externalRules: a list of other keyword rules, with states other than * externalRules: a list of other keyword rules, with states other than
* vectorState * vectorState
*/ */
static parseContentRules(rulesets: IRuleSets): IContentRules { public static parseContentRules(rulesets: IPushRules): IContentRules {
// first categorise the keyword rules in terms of their actions // first categorise the keyword rules in terms of their actions
const contentRules = this._categoriseContentRules(rulesets); const contentRules = ContentRules.categoriseContentRules(rulesets);
// Decide which content rules to display in Vector UI. // Decide which content rules to display in Vector UI.
// Vector displays a single global rule for a list of keywords // Vector displays a single global rule for a list of keywords
@ -95,8 +94,8 @@ export class ContentRules {
} }
} }
static _categoriseContentRules(rulesets: IRuleSets) { private static categoriseContentRules(rulesets: IPushRules) {
const contentRules: Record<"on"|"on_but_disabled"|"loud"|"loud_but_disabled"|"other", IExtendedPushRule[]> = { const contentRules: Record<"on"|"on_but_disabled"|"loud"|"loud_but_disabled"|"other", IAnnotatedPushRule[]> = {
on: [], on: [],
on_but_disabled: [], on_but_disabled: [],
loud: [], loud: [],
@ -109,7 +108,7 @@ export class ContentRules {
const r = rulesets.global[kind][i]; const r = rulesets.global[kind][i];
// check it's not a default rule // check it's not a default rule
if (r.rule_id[0] === '.' || kind !== "content") { if (r.rule_id[0] === '.' || kind !== PushRuleKind.ContentSpecific) {
continue; continue;
} }

View file

@ -1,6 +1,5 @@
/* /*
Copyright 2016 OpenMarket Ltd Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -15,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { Action, Actions } from "./types"; import { PushRuleAction, PushRuleActionName, TweakHighlight, TweakSound } from "matrix-js-sdk/src/@types/PushRules";
interface IEncodedActions { interface IEncodedActions {
notify: boolean; notify: boolean;
@ -35,18 +34,18 @@ export class NotificationUtils {
const sound = action.sound; const sound = action.sound;
const highlight = action.highlight; const highlight = action.highlight;
if (notify) { if (notify) {
const actions: Action[] = [Actions.Notify]; const actions: PushRuleAction[] = [PushRuleActionName.Notify];
if (sound) { if (sound) {
actions.push({ "set_tweak": "sound", "value": sound }); actions.push({ "set_tweak": "sound", "value": sound } as TweakSound);
} }
if (highlight) { if (highlight) {
actions.push({ "set_tweak": "highlight" }); actions.push({ "set_tweak": "highlight" } as TweakHighlight);
} else { } else {
actions.push({ "set_tweak": "highlight", "value": false }); actions.push({ "set_tweak": "highlight", "value": false } as TweakHighlight);
} }
return actions; return actions;
} else { } else {
return [Actions.DontNotify]; return [PushRuleActionName.DontNotify];
} }
} }
@ -56,16 +55,16 @@ export class NotificationUtils {
// "highlight: true/false, // "highlight: true/false,
// } // }
// If the actions couldn't be decoded then returns null. // If the actions couldn't be decoded then returns null.
static decodeActions(actions: Action[]): IEncodedActions { static decodeActions(actions: PushRuleAction[]): IEncodedActions {
let notify = false; let notify = false;
let sound = null; let sound = null;
let highlight = false; let highlight = false;
for (let i = 0; i < actions.length; ++i) { for (let i = 0; i < actions.length; ++i) {
const action = actions[i]; const action = actions[i];
if (action === Actions.Notify) { if (action === PushRuleActionName.Notify) {
notify = true; notify = true;
} else if (action === Actions.DontNotify) { } else if (action === PushRuleActionName.DontNotify) {
notify = false; notify = false;
} else if (typeof action === "object") { } else if (typeof action === "object") {
if (action.set_tweak === "sound") { if (action.set_tweak === "sound") {

View file

@ -1,6 +1,5 @@
/* /*
Copyright 2016 OpenMarket Ltd Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -17,7 +16,7 @@ limitations under the License.
import { StandardActions } from "./StandardActions"; import { StandardActions } from "./StandardActions";
import { NotificationUtils } from "./NotificationUtils"; import { NotificationUtils } from "./NotificationUtils";
import { IPushRule } from "./types"; import { IPushRule } from "matrix-js-sdk/src/@types/PushRules";
export enum State { export enum State {
/** The push rule is disabled */ /** The push rule is disabled */

View file

@ -1,114 +0,0 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
export enum NotificationSetting {
AllMessages = "all_messages", // .m.rule.message = notify
DirectMessagesMentionsKeywords = "dm_mentions_keywords", // .m.rule.message = mark_unread. This is the new default.
MentionsKeywordsOnly = "mentions_keywords", // .m.rule.message = mark_unread; .m.rule.room_one_to_one = mark_unread
Never = "never", // .m.rule.master = enabled (dont_notify)
}
export interface ISoundTweak {
// eslint-disable-next-line camelcase
set_tweak: "sound";
value: string;
}
export interface IHighlightTweak {
// eslint-disable-next-line camelcase
set_tweak: "highlight";
value?: boolean;
}
export type Tweak = ISoundTweak | IHighlightTweak;
export enum Actions {
Notify = "notify",
DontNotify = "dont_notify", // no-op
Coalesce = "coalesce", // unused
MarkUnread = "mark_unread", // new
}
export type Action = Actions | Tweak;
// Push rule kinds in descending priority order
export enum Kind {
Override = "override",
ContentSpecific = "content",
RoomSpecific = "room",
SenderSpecific = "sender",
Underride = "underride",
}
export interface IEventMatchCondition {
kind: "event_match";
key: string;
pattern: string;
}
export interface IContainsDisplayNameCondition {
kind: "contains_display_name";
}
export interface IRoomMemberCountCondition {
kind: "room_member_count";
is: string;
}
export interface ISenderNotificationPermissionCondition {
kind: "sender_notification_permission";
key: string;
}
export type Condition =
IEventMatchCondition |
IContainsDisplayNameCondition |
IRoomMemberCountCondition |
ISenderNotificationPermissionCondition;
export enum RuleIds {
MasterRule = ".m.rule.master", // The master rule (all notifications disabling)
MessageRule = ".m.rule.message",
EncryptedMessageRule = ".m.rule.encrypted",
RoomOneToOneRule = ".m.rule.room_one_to_one",
EncryptedRoomOneToOneRule = ".m.rule.room_one_to_one",
}
export interface IPushRule {
enabled: boolean;
// eslint-disable-next-line camelcase
rule_id: RuleIds | string;
actions: Action[];
default: boolean;
conditions?: Condition[]; // only applicable to `underride` and `override` rules
pattern?: string; // only applicable to `content` rules
}
// push rule extended with kind, used by ContentRules and js-sdk's pushprocessor
export interface IExtendedPushRule extends IPushRule {
kind: Kind;
}
export interface IPushRuleSet {
override: IPushRule[];
content: IPushRule[];
room: IPushRule[];
sender: IPushRule[];
underride: IPushRule[];
}
export interface IRuleSets {
global: IPushRuleSet;
}