2018-04-12 01:58:37 +03:00
|
|
|
/*
|
2021-07-12 05:03:07 +03:00
|
|
|
Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
|
2018-04-12 01:58:37 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-07-12 05:03:07 +03:00
|
|
|
import { IAnnotatedPushRule, IPushRules, PushRuleKind } from "matrix-js-sdk/src/@types/PushRules";
|
2018-04-12 01:58:37 +03:00
|
|
|
|
2021-10-23 01:23:32 +03:00
|
|
|
import { PushRuleVectorState, VectorState } from "./PushRuleVectorState";
|
|
|
|
|
2020-06-25 11:00:13 +03:00
|
|
|
export interface IContentRules {
|
2021-07-12 05:53:12 +03:00
|
|
|
vectorState: VectorState;
|
2021-07-12 05:03:07 +03:00
|
|
|
rules: IAnnotatedPushRule[];
|
|
|
|
externalRules: IAnnotatedPushRule[];
|
2020-06-25 11:00:13 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
export class ContentRules {
|
2018-04-12 01:58:37 +03:00
|
|
|
/**
|
|
|
|
* Extract the keyword rules from a list of rules, and parse them
|
|
|
|
* into a form which is useful for Vector's UI.
|
|
|
|
*
|
|
|
|
* Returns an object containing:
|
|
|
|
* rules: the primary list of keyword rules
|
|
|
|
* vectorState: a PushRuleVectorState indicating whether those rules are
|
|
|
|
* OFF/ON/LOUD
|
|
|
|
* externalRules: a list of other keyword rules, with states other than
|
|
|
|
* vectorState
|
|
|
|
*/
|
2021-07-12 05:03:07 +03:00
|
|
|
public static parseContentRules(rulesets: IPushRules): IContentRules {
|
2018-04-12 01:58:37 +03:00
|
|
|
// first categorise the keyword rules in terms of their actions
|
2021-07-12 05:03:07 +03:00
|
|
|
const contentRules = ContentRules.categoriseContentRules(rulesets);
|
2018-04-12 01:58:37 +03:00
|
|
|
|
|
|
|
// Decide which content rules to display in Vector UI.
|
|
|
|
// Vector displays a single global rule for a list of keywords
|
|
|
|
// whereas Matrix has a push rule per keyword.
|
|
|
|
// Vector can set the unique rule in ON, LOUD or OFF state.
|
|
|
|
// Matrix has enabled/disabled plus a combination of (highlight, sound) tweaks.
|
|
|
|
|
|
|
|
// The code below determines which set of user's content push rules can be
|
|
|
|
// displayed by the vector UI.
|
|
|
|
// Push rules that does not fit, ie defined by another Matrix client, ends
|
|
|
|
// in externalRules.
|
|
|
|
// There is priority in the determination of which set will be the displayed one.
|
|
|
|
// The set with rules that have LOUD tweaks is the first choice. Then, the ones
|
|
|
|
// with ON tweaks (no tweaks).
|
|
|
|
|
|
|
|
if (contentRules.loud.length) {
|
|
|
|
return {
|
2021-07-12 05:53:12 +03:00
|
|
|
vectorState: VectorState.Loud,
|
2018-04-12 01:58:37 +03:00
|
|
|
rules: contentRules.loud,
|
2020-06-25 11:00:13 +03:00
|
|
|
externalRules: [
|
|
|
|
...contentRules.loud_but_disabled,
|
|
|
|
...contentRules.on,
|
|
|
|
...contentRules.on_but_disabled,
|
|
|
|
...contentRules.other,
|
|
|
|
],
|
2018-04-12 01:58:37 +03:00
|
|
|
};
|
2018-10-12 06:04:53 +03:00
|
|
|
} else if (contentRules.loud_but_disabled.length) {
|
2018-04-12 01:58:37 +03:00
|
|
|
return {
|
2021-07-12 05:53:12 +03:00
|
|
|
vectorState: VectorState.Off,
|
2018-04-12 01:58:37 +03:00
|
|
|
rules: contentRules.loud_but_disabled,
|
2020-06-25 11:00:13 +03:00
|
|
|
externalRules: [...contentRules.on, ...contentRules.on_but_disabled, ...contentRules.other],
|
2018-04-12 01:58:37 +03:00
|
|
|
};
|
2018-10-12 06:04:53 +03:00
|
|
|
} else if (contentRules.on.length) {
|
2018-04-12 01:58:37 +03:00
|
|
|
return {
|
2021-07-12 05:53:12 +03:00
|
|
|
vectorState: VectorState.On,
|
2018-04-12 01:58:37 +03:00
|
|
|
rules: contentRules.on,
|
2020-06-25 11:00:13 +03:00
|
|
|
externalRules: [...contentRules.on_but_disabled, ...contentRules.other],
|
2018-04-12 01:58:37 +03:00
|
|
|
};
|
2018-10-12 06:04:53 +03:00
|
|
|
} else if (contentRules.on_but_disabled.length) {
|
2018-04-12 01:58:37 +03:00
|
|
|
return {
|
2021-07-12 05:53:12 +03:00
|
|
|
vectorState: VectorState.Off,
|
2018-04-12 01:58:37 +03:00
|
|
|
rules: contentRules.on_but_disabled,
|
|
|
|
externalRules: contentRules.other,
|
2018-10-12 06:04:53 +03:00
|
|
|
};
|
|
|
|
} else {
|
2018-04-12 01:58:37 +03:00
|
|
|
return {
|
2021-07-12 05:53:12 +03:00
|
|
|
vectorState: VectorState.On,
|
2018-04-12 01:58:37 +03:00
|
|
|
rules: [],
|
|
|
|
externalRules: contentRules.other,
|
2018-10-12 06:04:53 +03:00
|
|
|
};
|
2018-04-12 01:58:37 +03:00
|
|
|
}
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
2018-04-12 01:58:37 +03:00
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
private static categoriseContentRules(
|
|
|
|
rulesets: IPushRules,
|
|
|
|
): Record<"on" | "on_but_disabled" | "loud" | "loud_but_disabled" | "other", IAnnotatedPushRule[]> {
|
2021-07-12 05:03:07 +03:00
|
|
|
const contentRules: Record<
|
|
|
|
"on" | "on_but_disabled" | "loud" | "loud_but_disabled" | "other",
|
|
|
|
IAnnotatedPushRule[]
|
|
|
|
> = {
|
2020-06-25 11:00:13 +03:00
|
|
|
on: [],
|
|
|
|
on_but_disabled: [],
|
|
|
|
loud: [],
|
|
|
|
loud_but_disabled: [],
|
|
|
|
other: [],
|
|
|
|
};
|
|
|
|
|
2018-10-12 06:04:53 +03:00
|
|
|
for (const kind in rulesets.global) {
|
|
|
|
for (let i = 0; i < Object.keys(rulesets.global[kind]).length; ++i) {
|
|
|
|
const r = rulesets.global[kind][i];
|
2018-04-12 01:58:37 +03:00
|
|
|
|
|
|
|
// check it's not a default rule
|
2021-07-12 05:03:07 +03:00
|
|
|
if (r.rule_id[0] === "." || kind !== PushRuleKind.ContentSpecific) {
|
2018-04-12 01:58:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-25 11:00:13 +03:00
|
|
|
// this is needed as we are flattening an object of arrays into a single array
|
|
|
|
r.kind = kind;
|
2018-04-12 01:58:37 +03:00
|
|
|
|
|
|
|
switch (PushRuleVectorState.contentRuleVectorStateKind(r)) {
|
2021-07-12 05:53:12 +03:00
|
|
|
case VectorState.On:
|
2018-04-12 01:58:37 +03:00
|
|
|
if (r.enabled) {
|
|
|
|
contentRules.on.push(r);
|
2018-10-12 06:04:53 +03:00
|
|
|
} else {
|
2018-04-12 01:58:37 +03:00
|
|
|
contentRules.on_but_disabled.push(r);
|
|
|
|
}
|
|
|
|
break;
|
2021-07-12 05:53:12 +03:00
|
|
|
case VectorState.Loud:
|
2018-04-12 01:58:37 +03:00
|
|
|
if (r.enabled) {
|
|
|
|
contentRules.loud.push(r);
|
2018-10-12 06:04:53 +03:00
|
|
|
} else {
|
2018-04-12 01:58:37 +03:00
|
|
|
contentRules.loud_but_disabled.push(r);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
contentRules.other.push(r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contentRules;
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
|
|
|
}
|