feat: complete Ancestor quote bot with production-ready Docker support
This commit is contained in:
393
node_modules/matrix-js-sdk/lib/pushprocessor.js
generated
vendored
393
node_modules/matrix-js-sdk/lib/pushprocessor.js
generated
vendored
@@ -1,25 +1,32 @@
|
||||
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
||||
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
||||
/*
|
||||
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
||||
"use strict";
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
import { deepCompare, escapeRegExp, globToRegexp, isNullOrUndefined } from "./utils.js";
|
||||
import { ConditionKind, PushRuleActionName, PushRuleKind, RuleId, TweakName } from "./@types/PushRules.js";
|
||||
import { EventType } from "./@types/event.js";
|
||||
const RULEKINDS_IN_ORDER = [PushRuleKind.Override, PushRuleKind.ContentSpecific, PushRuleKind.RoomSpecific, PushRuleKind.SenderSpecific, PushRuleKind.Underride];
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.PushProcessor = void 0;
|
||||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
||||
var _utils = require("./utils");
|
||||
var _logger = require("./logger");
|
||||
var _PushRules = require("./@types/PushRules");
|
||||
var _event = require("./@types/event");
|
||||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
||||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } /*
|
||||
Copyright 2015 - 2021 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.
|
||||
*/
|
||||
const RULEKINDS_IN_ORDER = [_PushRules.PushRuleKind.Override, _PushRules.PushRuleKind.ContentSpecific, _PushRules.PushRuleKind.RoomSpecific, _PushRules.PushRuleKind.SenderSpecific, _PushRules.PushRuleKind.Underride];
|
||||
|
||||
// The default override rules to apply to the push rules that arrive from the server.
|
||||
// We do this for two reasons:
|
||||
@@ -28,143 +35,90 @@ const RULEKINDS_IN_ORDER = [PushRuleKind.Override, PushRuleKind.ContentSpecific,
|
||||
// more details.
|
||||
// 2. We often want to start using push rules ahead of the server supporting them,
|
||||
// and so we can put them here.
|
||||
const DEFAULT_OVERRIDE_RULES = {
|
||||
".m.rule.is_room_mention": {
|
||||
// Matrix v1.7
|
||||
rule_id: ".m.rule.is_room_mention",
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: ConditionKind.EventPropertyIs,
|
||||
key: "content.m\\.mentions.room",
|
||||
value: true
|
||||
}, {
|
||||
kind: ConditionKind.SenderNotificationPermission,
|
||||
key: "room"
|
||||
}],
|
||||
actions: [PushRuleActionName.Notify, {
|
||||
set_tweak: TweakName.Highlight
|
||||
}]
|
||||
},
|
||||
".m.rule.reaction": {
|
||||
// For homeservers which don't support MSC2153 yet
|
||||
rule_id: ".m.rule.reaction",
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: ConditionKind.EventMatch,
|
||||
key: "type",
|
||||
pattern: "m.reaction"
|
||||
}],
|
||||
actions: [PushRuleActionName.DontNotify]
|
||||
},
|
||||
".org.matrix.msc3786.rule.room.server_acl": {
|
||||
// For homeservers which don't support MSC3786 yet
|
||||
rule_id: ".org.matrix.msc3786.rule.room.server_acl",
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: ConditionKind.EventMatch,
|
||||
key: "type",
|
||||
pattern: EventType.RoomServerAcl
|
||||
}, {
|
||||
kind: ConditionKind.EventMatch,
|
||||
key: "state_key",
|
||||
pattern: ""
|
||||
}],
|
||||
actions: []
|
||||
}
|
||||
};
|
||||
const DEFAULT_OVERRIDE_RULES = [{
|
||||
// For homeservers which don't support MSC2153 yet
|
||||
rule_id: ".m.rule.reaction",
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: _PushRules.ConditionKind.EventMatch,
|
||||
key: "type",
|
||||
pattern: "m.reaction"
|
||||
}],
|
||||
actions: [_PushRules.PushRuleActionName.DontNotify]
|
||||
}, {
|
||||
rule_id: _PushRules.RuleId.IsUserMention,
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: _PushRules.ConditionKind.EventPropertyContains,
|
||||
key: "content.m\\.mentions.user_ids",
|
||||
value: "" // The user ID is dynamically added in rewriteDefaultRules.
|
||||
}],
|
||||
|
||||
// A special rule id for `EXPECTED_DEFAULT_OVERRIDE_RULE_IDS` and friends which denotes where user-defined rules live in the order.
|
||||
const UserDefinedRules = Symbol("UserDefinedRules");
|
||||
const EXPECTED_DEFAULT_OVERRIDE_RULE_IDS = [RuleId.Master, UserDefinedRules, RuleId.SuppressNotices, RuleId.InviteToSelf, RuleId.MemberEvent, RuleId.IsUserMention, RuleId.ContainsDisplayName, RuleId.IsRoomMention, RuleId.AtRoomNotification, RuleId.Tombstone, ".m.rule.reaction", ".m.rule.room.server_acl", ".org.matrix.msc3786.rule.room.server_acl", ".m.rule.suppress_edits"];
|
||||
const DEFAULT_UNDERRIDE_RULES = {
|
||||
".org.matrix.msc3914.rule.room.call": {
|
||||
// For homeservers which don't support MSC3914 yet
|
||||
rule_id: ".org.matrix.msc3914.rule.room.call",
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: ConditionKind.EventMatch,
|
||||
key: "type",
|
||||
pattern: "org.matrix.msc3401.call"
|
||||
}, {
|
||||
kind: ConditionKind.CallStarted
|
||||
}],
|
||||
actions: [PushRuleActionName.Notify, {
|
||||
set_tweak: TweakName.Sound,
|
||||
value: "default"
|
||||
}]
|
||||
}
|
||||
};
|
||||
const EXPECTED_DEFAULT_UNDERRIDE_RULE_IDS = [UserDefinedRules, RuleId.IncomingCall, ".org.matrix.msc3914.rule.room.call", RuleId.EncryptedDM, RuleId.DM, RuleId.Message, RuleId.EncryptedMessage];
|
||||
|
||||
/**
|
||||
* Make sure that each of the rules listed in `defaultRuleIds` is listed in the given set of push rules.
|
||||
*
|
||||
* @param logger - A `Logger` to write log messages to.
|
||||
* @param kind - the kind of push rule set being merged.
|
||||
* @param incomingRules - the existing set of known push rules for the user.
|
||||
* @param defaultRules - a lookup table for the default definitions of push rules.
|
||||
* @param orderedRuleIds - the IDs of the expected push rules, in order.
|
||||
*
|
||||
* @returns A copy of `incomingRules`, with any missing default rules inserted in the right place.
|
||||
*/
|
||||
function mergeRulesWithDefaults(logger, kind, incomingRules, defaultRules, orderedRuleIds) {
|
||||
// Split the incomingRules into defaults and custom
|
||||
const incomingDefaultRules = incomingRules.filter(rule => rule.default);
|
||||
const incomingCustomRules = incomingRules.filter(rule => !rule.default);
|
||||
function insertDefaultPushRule(ruleId) {
|
||||
if (ruleId === UserDefinedRules) {
|
||||
// Re-insert any user-defined rules that were in `incomingRules`
|
||||
newRules.push(...incomingCustomRules);
|
||||
} else if (ruleId in defaultRules) {
|
||||
logger.warn(`Adding default global ${kind} push rule ${ruleId}`);
|
||||
newRules.push(defaultRules[ruleId]);
|
||||
} else {
|
||||
logger.warn(`Missing default global ${kind} push rule ${ruleId}`);
|
||||
}
|
||||
}
|
||||
let nextExpectedRuleIdIndex = 0;
|
||||
const newRules = [];
|
||||
// Merge our expected rules (including the incoming custom rules) into the incoming default rules.
|
||||
for (const rule of incomingDefaultRules) {
|
||||
const ruleIndex = orderedRuleIds.indexOf(rule.rule_id);
|
||||
if (ruleIndex === -1) {
|
||||
// an unrecognised rule; copy it over
|
||||
newRules.push(rule);
|
||||
continue;
|
||||
}
|
||||
while (ruleIndex > nextExpectedRuleIdIndex) {
|
||||
// insert new rules
|
||||
const defaultRuleId = orderedRuleIds[nextExpectedRuleIdIndex];
|
||||
insertDefaultPushRule(defaultRuleId);
|
||||
nextExpectedRuleIdIndex += 1;
|
||||
}
|
||||
// copy over the existing rule
|
||||
newRules.push(rule);
|
||||
nextExpectedRuleIdIndex += 1;
|
||||
}
|
||||
|
||||
// Now copy over any remaining default rules
|
||||
for (const ruleId of orderedRuleIds.slice(nextExpectedRuleIdIndex)) {
|
||||
insertDefaultPushRule(ruleId);
|
||||
}
|
||||
return newRules;
|
||||
}
|
||||
export class PushProcessor {
|
||||
actions: [_PushRules.PushRuleActionName.Notify, {
|
||||
set_tweak: _PushRules.TweakName.Highlight
|
||||
}]
|
||||
}, {
|
||||
rule_id: _PushRules.RuleId.IsRoomMention,
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: _PushRules.ConditionKind.EventPropertyIs,
|
||||
key: "content.m\\.mentions.room",
|
||||
value: true
|
||||
}, {
|
||||
kind: _PushRules.ConditionKind.SenderNotificationPermission,
|
||||
key: "room"
|
||||
}],
|
||||
actions: [_PushRules.PushRuleActionName.Notify, {
|
||||
set_tweak: _PushRules.TweakName.Highlight
|
||||
}]
|
||||
}, {
|
||||
// For homeservers which don't support MSC3786 yet
|
||||
rule_id: ".org.matrix.msc3786.rule.room.server_acl",
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: _PushRules.ConditionKind.EventMatch,
|
||||
key: "type",
|
||||
pattern: _event.EventType.RoomServerAcl
|
||||
}, {
|
||||
kind: _PushRules.ConditionKind.EventMatch,
|
||||
key: "state_key",
|
||||
pattern: ""
|
||||
}],
|
||||
actions: []
|
||||
}];
|
||||
const DEFAULT_UNDERRIDE_RULES = [{
|
||||
// For homeservers which don't support MSC3914 yet
|
||||
rule_id: ".org.matrix.msc3914.rule.room.call",
|
||||
default: true,
|
||||
enabled: true,
|
||||
conditions: [{
|
||||
kind: _PushRules.ConditionKind.EventMatch,
|
||||
key: "type",
|
||||
pattern: "org.matrix.msc3401.call"
|
||||
}, {
|
||||
kind: _PushRules.ConditionKind.CallStarted
|
||||
}],
|
||||
actions: [_PushRules.PushRuleActionName.Notify, {
|
||||
set_tweak: _PushRules.TweakName.Sound,
|
||||
value: "default"
|
||||
}]
|
||||
}];
|
||||
class PushProcessor {
|
||||
/**
|
||||
* Construct a Push Processor.
|
||||
* @param client - The Matrix client object to use
|
||||
*/
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
/**
|
||||
* Maps the original key from the push rules to a list of property names
|
||||
* after unescaping.
|
||||
*/
|
||||
_defineProperty(this, "parsedKeys", new Map());
|
||||
this.client = client;
|
||||
(0, _defineProperty2.default)(this, "parsedKeys", new Map());
|
||||
}
|
||||
/**
|
||||
* Convert a list of actions into a object with the actions as keys and their values
|
||||
@@ -181,7 +135,7 @@ export class PushProcessor {
|
||||
tweaks: {}
|
||||
};
|
||||
for (const action of actionList) {
|
||||
if (action === PushRuleActionName.Notify) {
|
||||
if (action === _PushRules.PushRuleActionName.Notify) {
|
||||
actionObj.notify = true;
|
||||
} else if (typeof action === "object") {
|
||||
if (action.value === undefined) {
|
||||
@@ -197,13 +151,12 @@ export class PushProcessor {
|
||||
* Rewrites conditions on a client's push rules to match the defaults
|
||||
* where applicable. Useful for upgrading push rules to more strict
|
||||
* conditions when the server is falling behind on defaults.
|
||||
*
|
||||
* @param logger - A `Logger` to write log messages to.
|
||||
* @param incomingRules - The client's existing push rules
|
||||
* @param userId - The Matrix ID of the client.
|
||||
* @returns The rewritten rules
|
||||
*/
|
||||
static rewriteDefaultRules(logger, incomingRules, userId = undefined) {
|
||||
static rewriteDefaultRules(incomingRules, userId = undefined) {
|
||||
var _newRules$global$unde;
|
||||
let newRules = JSON.parse(JSON.stringify(incomingRules)); // deep clone
|
||||
|
||||
// These lines are mostly to make the tests happy. We shouldn't run into these
|
||||
@@ -214,27 +167,50 @@ export class PushProcessor {
|
||||
if (!newRules.global.underride) newRules.global.underride = [];
|
||||
|
||||
// Merge the client-level defaults with the ones from the server
|
||||
newRules.global.override = mergeRulesWithDefaults(logger, PushRuleKind.Override, newRules.global.override, DEFAULT_OVERRIDE_RULES, EXPECTED_DEFAULT_OVERRIDE_RULE_IDS);
|
||||
newRules.global.underride = mergeRulesWithDefaults(logger, PushRuleKind.Underride, newRules.global.underride, DEFAULT_UNDERRIDE_RULES, EXPECTED_DEFAULT_UNDERRIDE_RULE_IDS);
|
||||
return newRules;
|
||||
}
|
||||
const globalOverrides = newRules.global.override;
|
||||
for (const originalOverride of DEFAULT_OVERRIDE_RULES) {
|
||||
const existingRule = globalOverrides.find(r => r.rule_id === originalOverride.rule_id);
|
||||
|
||||
/**
|
||||
* Create a RegExp object for the given glob pattern with a single capture group around the pattern itself, caching the result.
|
||||
* No cache invalidation is present currently,
|
||||
* as this will be inherently bounded to the size of the user's own push rules.
|
||||
* @param pattern - the glob pattern to convert to a RegExp
|
||||
* @param alignToWordBoundary - whether to align the pattern to word boundaries,
|
||||
* as specified for `content.body` matches, will use lookaround assertions to ensure the match only includes the pattern
|
||||
* @param flags - the flags to pass to the RegExp constructor, defaults to case-insensitive
|
||||
*/
|
||||
static getPushRuleGlobRegex(pattern, alignToWordBoundary = false, flags = "i") {
|
||||
const [prefix, suffix] = alignToWordBoundary ? ["(?<=^|\\W)", "(?=\\W|$)"] : ["^", "$"];
|
||||
const cacheKey = `${alignToWordBoundary}-${flags}-${pattern}`;
|
||||
if (!PushProcessor.cachedGlobToRegex[cacheKey]) {
|
||||
PushProcessor.cachedGlobToRegex[cacheKey] = new RegExp(prefix + "(" + globToRegexp(pattern) + ")" + suffix, flags);
|
||||
// Dynamically add the user ID as the value for the is_user_mention rule.
|
||||
let override;
|
||||
if (originalOverride.rule_id === _PushRules.RuleId.IsUserMention) {
|
||||
// If the user ID wasn't provided, skip the rule.
|
||||
if (!userId) {
|
||||
continue;
|
||||
}
|
||||
override = JSON.parse(JSON.stringify(originalOverride)); // deep clone
|
||||
override.conditions[0].value = userId;
|
||||
} else {
|
||||
override = originalOverride;
|
||||
}
|
||||
if (existingRule) {
|
||||
// Copy over the actions, default, and conditions. Don't touch the user's preference.
|
||||
existingRule.default = override.default;
|
||||
existingRule.conditions = override.conditions;
|
||||
existingRule.actions = override.actions;
|
||||
} else {
|
||||
// Add the rule
|
||||
const ruleId = override.rule_id;
|
||||
_logger.logger.warn(`Adding default global override for ${ruleId}`);
|
||||
globalOverrides.push(override);
|
||||
}
|
||||
}
|
||||
return PushProcessor.cachedGlobToRegex[cacheKey];
|
||||
const globalUnderrides = (_newRules$global$unde = newRules.global.underride) !== null && _newRules$global$unde !== void 0 ? _newRules$global$unde : [];
|
||||
for (const underride of DEFAULT_UNDERRIDE_RULES) {
|
||||
const existingRule = globalUnderrides.find(r => r.rule_id === underride.rule_id);
|
||||
if (existingRule) {
|
||||
// Copy over the actions, default, and conditions. Don't touch the user's preference.
|
||||
existingRule.default = underride.default;
|
||||
existingRule.conditions = underride.conditions;
|
||||
existingRule.actions = underride.actions;
|
||||
} else {
|
||||
// Add the rule
|
||||
const ruleId = underride.rule_id;
|
||||
_logger.logger.warn(`Adding default global underride for ${ruleId}`);
|
||||
globalUnderrides.push(underride);
|
||||
}
|
||||
}
|
||||
return newRules;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,7 +237,7 @@ export class PushProcessor {
|
||||
continue;
|
||||
}
|
||||
for (const condition of rule.conditions) {
|
||||
if (condition.kind !== ConditionKind.EventMatch) {
|
||||
if (condition.kind !== _PushRules.ConditionKind.EventMatch) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -309,36 +285,36 @@ export class PushProcessor {
|
||||
conditions: []
|
||||
};
|
||||
switch (kind) {
|
||||
case PushRuleKind.Underride:
|
||||
case PushRuleKind.Override:
|
||||
case _PushRules.PushRuleKind.Underride:
|
||||
case _PushRules.PushRuleKind.Override:
|
||||
rawrule.conditions = tprule.conditions;
|
||||
break;
|
||||
case PushRuleKind.RoomSpecific:
|
||||
case _PushRules.PushRuleKind.RoomSpecific:
|
||||
if (!tprule.rule_id) {
|
||||
return null;
|
||||
}
|
||||
rawrule.conditions.push({
|
||||
kind: ConditionKind.EventMatch,
|
||||
kind: _PushRules.ConditionKind.EventMatch,
|
||||
key: "room_id",
|
||||
value: tprule.rule_id
|
||||
});
|
||||
break;
|
||||
case PushRuleKind.SenderSpecific:
|
||||
case _PushRules.PushRuleKind.SenderSpecific:
|
||||
if (!tprule.rule_id) {
|
||||
return null;
|
||||
}
|
||||
rawrule.conditions.push({
|
||||
kind: ConditionKind.EventMatch,
|
||||
kind: _PushRules.ConditionKind.EventMatch,
|
||||
key: "user_id",
|
||||
value: tprule.rule_id
|
||||
});
|
||||
break;
|
||||
case PushRuleKind.ContentSpecific:
|
||||
case _PushRules.PushRuleKind.ContentSpecific:
|
||||
if (!tprule.pattern) {
|
||||
return null;
|
||||
}
|
||||
rawrule.conditions.push({
|
||||
kind: ConditionKind.EventMatch,
|
||||
kind: _PushRules.ConditionKind.EventMatch,
|
||||
key: "content.body",
|
||||
pattern: tprule.pattern
|
||||
});
|
||||
@@ -348,20 +324,20 @@ export class PushProcessor {
|
||||
}
|
||||
eventFulfillsCondition(cond, ev) {
|
||||
switch (cond.kind) {
|
||||
case ConditionKind.EventMatch:
|
||||
case _PushRules.ConditionKind.EventMatch:
|
||||
return this.eventFulfillsEventMatchCondition(cond, ev);
|
||||
case ConditionKind.EventPropertyIs:
|
||||
case _PushRules.ConditionKind.EventPropertyIs:
|
||||
return this.eventFulfillsEventPropertyIsCondition(cond, ev);
|
||||
case ConditionKind.EventPropertyContains:
|
||||
case _PushRules.ConditionKind.EventPropertyContains:
|
||||
return this.eventFulfillsEventPropertyContains(cond, ev);
|
||||
case ConditionKind.ContainsDisplayName:
|
||||
case _PushRules.ConditionKind.ContainsDisplayName:
|
||||
return this.eventFulfillsDisplayNameCondition(cond, ev);
|
||||
case ConditionKind.RoomMemberCount:
|
||||
case _PushRules.ConditionKind.RoomMemberCount:
|
||||
return this.eventFulfillsRoomMemberCountCondition(cond, ev);
|
||||
case ConditionKind.SenderNotificationPermission:
|
||||
case _PushRules.ConditionKind.SenderNotificationPermission:
|
||||
return this.eventFulfillsSenderNotifPermCondition(cond, ev);
|
||||
case ConditionKind.CallStarted:
|
||||
case ConditionKind.CallStartedPrefix:
|
||||
case _PushRules.ConditionKind.CallStarted:
|
||||
case _PushRules.ConditionKind.CallStartedPrefix:
|
||||
return this.eventFulfillsCallStartedCondition(cond, ev);
|
||||
}
|
||||
|
||||
@@ -376,7 +352,7 @@ export class PushProcessor {
|
||||
return false;
|
||||
}
|
||||
const room = this.client.getRoom(ev.getRoomId());
|
||||
if (!room?.currentState) {
|
||||
if (!(room !== null && room !== void 0 && room.currentState)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -420,6 +396,7 @@ export class PushProcessor {
|
||||
}
|
||||
}
|
||||
eventFulfillsDisplayNameCondition(cond, ev) {
|
||||
var _room$currentState;
|
||||
let content = ev.getContent();
|
||||
if (ev.isEncrypted() && ev.getClearContent()) {
|
||||
content = ev.getClearContent();
|
||||
@@ -428,7 +405,7 @@ export class PushProcessor {
|
||||
return false;
|
||||
}
|
||||
const room = this.client.getRoom(ev.getRoomId());
|
||||
const member = room?.currentState?.getMember(this.client.credentials.userId);
|
||||
const member = room === null || room === void 0 || (_room$currentState = room.currentState) === null || _room$currentState === void 0 ? void 0 : _room$currentState.getMember(this.client.credentials.userId);
|
||||
if (!member) {
|
||||
return false;
|
||||
}
|
||||
@@ -436,7 +413,7 @@ export class PushProcessor {
|
||||
|
||||
// N.B. we can't use \b as it chokes on unicode. however \W seems to be okay
|
||||
// as shorthand for [^0-9A-Za-z_].
|
||||
const pat = new RegExp("(^|\\W)" + escapeRegExp(displayName) + "(\\W|$)", "i");
|
||||
const pat = new RegExp("(^|\\W)" + (0, _utils.escapeRegExp)(displayName) + "(\\W|$)", "i");
|
||||
return content.body.search(pat) > -1;
|
||||
}
|
||||
|
||||
@@ -465,10 +442,7 @@ export class PushProcessor {
|
||||
if (typeof cond.pattern !== "string") {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Align to word boundary on `content.body` matches, whole string otherwise
|
||||
// https://spec.matrix.org/v1.13/client-server-api/#conditions-1
|
||||
const regex = PushProcessor.getPushRuleGlobRegex(cond.pattern, cond.key === "content.body");
|
||||
const regex = cond.key === "content.body" ? this.createCachedRegex("(^|\\W)", cond.pattern, "(\\W|$)") : this.createCachedRegex("^", cond.pattern, "$");
|
||||
return !!val.match(regex);
|
||||
}
|
||||
|
||||
@@ -506,7 +480,16 @@ export class PushProcessor {
|
||||
eventFulfillsCallStartedCondition(_cond, ev) {
|
||||
// Since servers don't support properly sending push notification
|
||||
// about MSC3401 call events, we do the handling ourselves
|
||||
return ["m.ring", "m.prompt"].includes(ev.getContent()["m.intent"]) && !("m.terminated" in ev.getContent()) && (ev.getPrevContent()["m.terminated"] !== ev.getContent()["m.terminated"] || deepCompare(ev.getPrevContent(), {}));
|
||||
return ["m.ring", "m.prompt"].includes(ev.getContent()["m.intent"]) && !("m.terminated" in ev.getContent()) && (ev.getPrevContent()["m.terminated"] !== ev.getContent()["m.terminated"] || (0, _utils.deepCompare)(ev.getPrevContent(), {}));
|
||||
}
|
||||
createCachedRegex(prefix, glob, suffix) {
|
||||
if (PushProcessor.cachedGlobToRegex[glob]) {
|
||||
return PushProcessor.cachedGlobToRegex[glob];
|
||||
}
|
||||
PushProcessor.cachedGlobToRegex[glob] = new RegExp(prefix + (0, _utils.globToRegexp)(glob) + suffix, "i") // Case insensitive
|
||||
;
|
||||
|
||||
return PushProcessor.cachedGlobToRegex[glob];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -600,7 +583,7 @@ export class PushProcessor {
|
||||
for (; currentIndex < parts.length; ++currentIndex) {
|
||||
// The previous iteration resulted in null or undefined, bail (and
|
||||
// avoid the type error of attempting to retrieve a property).
|
||||
if (isNullOrUndefined(val)) {
|
||||
if ((0, _utils.isNullOrUndefined)(val)) {
|
||||
return undefined;
|
||||
}
|
||||
const thisPart = parts[currentIndex];
|
||||
@@ -628,7 +611,7 @@ export class PushProcessor {
|
||||
if (actionObj.tweaks.highlight === undefined) {
|
||||
// if it isn't specified, highlight if it's a content
|
||||
// rule but otherwise not
|
||||
actionObj.tweaks.highlight = rule.kind == PushRuleKind.ContentSpecific;
|
||||
actionObj.tweaks.highlight = rule.kind == _PushRules.PushRuleKind.ContentSpecific;
|
||||
}
|
||||
return {
|
||||
actions: actionObj,
|
||||
@@ -636,11 +619,12 @@ export class PushProcessor {
|
||||
};
|
||||
}
|
||||
ruleMatchesEvent(rule, ev) {
|
||||
var _rule$conditions;
|
||||
// Disable the deprecated mentions push rules if the new mentions property exists.
|
||||
if (this.client.supportsIntentionalMentions() && ev.getContent()["m.mentions"] !== undefined && (rule.rule_id === RuleId.ContainsUserName || rule.rule_id === RuleId.ContainsDisplayName || rule.rule_id === RuleId.AtRoomNotification)) {
|
||||
if (this.client.supportsIntentionalMentions() && ev.getContent()["m.mentions"] !== undefined && (rule.rule_id === _PushRules.RuleId.ContainsUserName || rule.rule_id === _PushRules.RuleId.ContainsDisplayName || rule.rule_id === _PushRules.RuleId.AtRoomNotification)) {
|
||||
return false;
|
||||
}
|
||||
return !rule.conditions?.some(cond => !this.eventFulfillsCondition(cond, ev));
|
||||
return !((_rule$conditions = rule.conditions) !== null && _rule$conditions !== void 0 && _rule$conditions.some(cond => !this.eventFulfillsCondition(cond, ev)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -663,8 +647,9 @@ export class PushProcessor {
|
||||
* @returns The push rule, or null if no such rule was found
|
||||
*/
|
||||
getPushRuleById(ruleId) {
|
||||
var _result$rule;
|
||||
const result = this.getPushRuleAndKindById(ruleId);
|
||||
return result?.rule ?? null;
|
||||
return (_result$rule = result === null || result === void 0 ? void 0 : result.rule) !== null && _result$rule !== void 0 ? _result$rule : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -676,7 +661,8 @@ export class PushProcessor {
|
||||
*/
|
||||
getPushRuleAndKindById(ruleId) {
|
||||
for (const scope of ["global"]) {
|
||||
if (this.client.pushRules?.[scope] === undefined) continue;
|
||||
var _this$client$pushRule;
|
||||
if (((_this$client$pushRule = this.client.pushRules) === null || _this$client$pushRule === void 0 ? void 0 : _this$client$pushRule[scope]) === undefined) continue;
|
||||
for (const kind of RULEKINDS_IN_ORDER) {
|
||||
if (this.client.pushRules[scope][kind] === undefined) continue;
|
||||
for (const rule of this.client.pushRules[scope][kind]) {
|
||||
@@ -690,5 +676,6 @@ export class PushProcessor {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
_defineProperty(PushProcessor, "cachedGlobToRegex", {});
|
||||
exports.PushProcessor = PushProcessor;
|
||||
(0, _defineProperty2.default)(PushProcessor, "cachedGlobToRegex", {});
|
||||
//# sourceMappingURL=pushprocessor.js.map
|
||||
Reference in New Issue
Block a user