You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-23 17:02:25 +03:00
Export push processor & method for converting matrix glob to regexp (#4763)
* Export push processor method for converting matrix glob to regexp Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Export pushProcessor from MatrixClient Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Add capturing group around pattern match Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Improve comment Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
committed by
GitHub
parent
0760c5f64c
commit
8061fa924d
@@ -1234,7 +1234,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
public canSupport = new Map<Feature, ServerSupport>();
|
||||
|
||||
// The pushprocessor caches useful things, so keep one and re-use it
|
||||
protected pushProcessor = new PushProcessor(this);
|
||||
public readonly pushProcessor = new PushProcessor(this);
|
||||
|
||||
// Promise to a response of the server's /versions response
|
||||
// TODO: This should expire: https://github.com/matrix-org/matrix-js-sdk/issues/1020
|
||||
|
||||
@@ -308,6 +308,26 @@ export class PushProcessor {
|
||||
return newRules;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param flags - the flags to pass to the RegExp constructor, defaults to case-insensitive
|
||||
*/
|
||||
public static getPushRuleGlobRegex(pattern: string, alignToWordBoundary = false, flags = "i"): RegExp {
|
||||
const [prefix, suffix] = alignToWordBoundary ? ["(^|\\W)", "(\\W|$)"] : ["^", "$"];
|
||||
|
||||
if (!PushProcessor.cachedGlobToRegex[pattern]) {
|
||||
PushProcessor.cachedGlobToRegex[pattern] = new RegExp(
|
||||
prefix + "(" + globToRegexp(pattern) + ")" + suffix,
|
||||
flags,
|
||||
);
|
||||
}
|
||||
return PushProcessor.cachedGlobToRegex[pattern];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-caches the parsed keys for push rules and cleans out any obsolete cache
|
||||
* entries. Should be called after push rules are updated.
|
||||
@@ -567,11 +587,9 @@ export class PushProcessor {
|
||||
return false;
|
||||
}
|
||||
|
||||
const regex =
|
||||
cond.key === "content.body"
|
||||
? this.createCachedRegex("(^|\\W)", cond.pattern, "(\\W|$)")
|
||||
: this.createCachedRegex("^", cond.pattern, "$");
|
||||
|
||||
// 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");
|
||||
return !!val.match(regex);
|
||||
}
|
||||
|
||||
@@ -621,17 +639,6 @@ export class PushProcessor {
|
||||
);
|
||||
}
|
||||
|
||||
private createCachedRegex(prefix: string, glob: string, suffix: string): RegExp {
|
||||
if (PushProcessor.cachedGlobToRegex[glob]) {
|
||||
return PushProcessor.cachedGlobToRegex[glob];
|
||||
}
|
||||
PushProcessor.cachedGlobToRegex[glob] = new RegExp(
|
||||
prefix + globToRegexp(glob) + suffix,
|
||||
"i", // Case insensitive
|
||||
);
|
||||
return PushProcessor.cachedGlobToRegex[glob];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the key into the separate fields to search by splitting on
|
||||
* unescaped ".", and then removing any escape characters.
|
||||
|
||||
Reference in New Issue
Block a user