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
Implement MSC3758: a push rule condition to match event properties exactly (#3179)
* Add some comments. * Support MSC3758 to exactly match values in push rule conditions. * Update to stable prefix.
This commit is contained in:
@@ -25,6 +25,7 @@ import {
|
||||
ICallStartedPrefixCondition,
|
||||
IContainsDisplayNameCondition,
|
||||
IEventMatchCondition,
|
||||
IEventPropertyIsCondition,
|
||||
IPushRule,
|
||||
IPushRules,
|
||||
IRoomMemberCountCondition,
|
||||
@@ -337,6 +338,8 @@ export class PushProcessor {
|
||||
switch (cond.kind) {
|
||||
case ConditionKind.EventMatch:
|
||||
return this.eventFulfillsEventMatchCondition(cond, ev);
|
||||
case ConditionKind.EventPropertyIs:
|
||||
return this.eventFulfillsEventPropertyIsCondition(cond, ev);
|
||||
case ConditionKind.ContainsDisplayName:
|
||||
return this.eventFulfillsDisplayNameCondition(cond, ev);
|
||||
case ConditionKind.RoomMemberCount:
|
||||
@@ -435,6 +438,13 @@ export class PushProcessor {
|
||||
return content.body.search(pat) > -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given event matches the push rule condition by fetching
|
||||
* the property from the event and comparing against the condition's glob-based
|
||||
* pattern.
|
||||
* @param cond - The push rule condition to check for a match.
|
||||
* @param ev - The event to check for a match.
|
||||
*/
|
||||
private eventFulfillsEventMatchCondition(cond: IEventMatchCondition, ev: MatrixEvent): boolean {
|
||||
if (!cond.key) {
|
||||
return false;
|
||||
@@ -445,6 +455,9 @@ export class PushProcessor {
|
||||
return false;
|
||||
}
|
||||
|
||||
// XXX This does not match in a case-insensitive manner.
|
||||
//
|
||||
// See https://spec.matrix.org/v1.5/client-server-api/#conditions-1
|
||||
if (cond.value) {
|
||||
return cond.value === val;
|
||||
}
|
||||
@@ -461,6 +474,20 @@ export class PushProcessor {
|
||||
return !!val.match(regex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given event matches the push rule condition by fetching
|
||||
* the property from the event and comparing exactly against the condition's
|
||||
* value.
|
||||
* @param cond - The push rule condition to check for a match.
|
||||
* @param ev - The event to check for a match.
|
||||
*/
|
||||
private eventFulfillsEventPropertyIsCondition(cond: IEventPropertyIsCondition, ev: MatrixEvent): boolean {
|
||||
if (!cond.key || cond.value === undefined) {
|
||||
return false;
|
||||
}
|
||||
return cond.value === this.valueForDottedKey(cond.key, ev);
|
||||
}
|
||||
|
||||
private eventFulfillsCallStartedCondition(
|
||||
_cond: ICallStartedCondition | ICallStartedPrefixCondition,
|
||||
ev: MatrixEvent,
|
||||
@@ -578,10 +605,13 @@ export class PushProcessor {
|
||||
}
|
||||
|
||||
for (; currentIndex < parts.length; ++currentIndex) {
|
||||
const thisPart = parts[currentIndex];
|
||||
if (isNullOrUndefined(val[thisPart])) {
|
||||
return null;
|
||||
// The previous iteration resulted in null or undefined, bail (and
|
||||
// avoid the type error of attempting to retrieve a property).
|
||||
if (isNullOrUndefined(val)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const thisPart = parts[currentIndex];
|
||||
val = val[thisPart];
|
||||
}
|
||||
return val;
|
||||
|
||||
Reference in New Issue
Block a user