Skip to content

Commit 8061fa9

Browse files
authored
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>
1 parent 0760c5f commit 8061fa9

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
12341234
public canSupport = new Map<Feature, ServerSupport>();
12351235

12361236
// The pushprocessor caches useful things, so keep one and re-use it
1237-
protected pushProcessor = new PushProcessor(this);
1237+
public readonly pushProcessor = new PushProcessor(this);
12381238

12391239
// Promise to a response of the server's /versions response
12401240
// TODO: This should expire: https://github.com/matrix-org/matrix-js-sdk/issues/1020

src/pushprocessor.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,26 @@ export class PushProcessor {
308308
return newRules;
309309
}
310310

311+
/**
312+
* Create a RegExp object for the given glob pattern with a single capture group around the pattern itself, caching the result.
313+
* No cache invalidation is present currently,
314+
* as this will be inherently bounded to the size of the user's own push rules.
315+
* @param pattern - the glob pattern to convert to a RegExp
316+
* @param alignToWordBoundary - whether to align the pattern to word boundaries, as specified for `content.body` matches
317+
* @param flags - the flags to pass to the RegExp constructor, defaults to case-insensitive
318+
*/
319+
public static getPushRuleGlobRegex(pattern: string, alignToWordBoundary = false, flags = "i"): RegExp {
320+
const [prefix, suffix] = alignToWordBoundary ? ["(^|\\W)", "(\\W|$)"] : ["^", "$"];
321+
322+
if (!PushProcessor.cachedGlobToRegex[pattern]) {
323+
PushProcessor.cachedGlobToRegex[pattern] = new RegExp(
324+
prefix + "(" + globToRegexp(pattern) + ")" + suffix,
325+
flags,
326+
);
327+
}
328+
return PushProcessor.cachedGlobToRegex[pattern];
329+
}
330+
311331
/**
312332
* Pre-caches the parsed keys for push rules and cleans out any obsolete cache
313333
* entries. Should be called after push rules are updated.
@@ -567,11 +587,9 @@ export class PushProcessor {
567587
return false;
568588
}
569589

570-
const regex =
571-
cond.key === "content.body"
572-
? this.createCachedRegex("(^|\\W)", cond.pattern, "(\\W|$)")
573-
: this.createCachedRegex("^", cond.pattern, "$");
574-
590+
// Align to word boundary on `content.body` matches, whole string otherwise
591+
// https://spec.matrix.org/v1.13/client-server-api/#conditions-1
592+
const regex = PushProcessor.getPushRuleGlobRegex(cond.pattern, cond.key === "content.body");
575593
return !!val.match(regex);
576594
}
577595

@@ -621,17 +639,6 @@ export class PushProcessor {
621639
);
622640
}
623641

624-
private createCachedRegex(prefix: string, glob: string, suffix: string): RegExp {
625-
if (PushProcessor.cachedGlobToRegex[glob]) {
626-
return PushProcessor.cachedGlobToRegex[glob];
627-
}
628-
PushProcessor.cachedGlobToRegex[glob] = new RegExp(
629-
prefix + globToRegexp(glob) + suffix,
630-
"i", // Case insensitive
631-
);
632-
return PushProcessor.cachedGlobToRegex[glob];
633-
}
634-
635642
/**
636643
* Parse the key into the separate fields to search by splitting on
637644
* unescaped ".", and then removing any escape characters.

0 commit comments

Comments
 (0)