1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Expose more functionality of pushprocessor

This exposes a function to get rules by their ID and
ruleMatchesEvent to test whether a rule matches a given event.
This commit is contained in:
David Baker
2017-11-01 19:40:50 +00:00
parent ff5f95227a
commit e34fd89bb6

View File

@@ -18,6 +18,8 @@ limitations under the License.
* @module pushprocessor * @module pushprocessor
*/ */
const RULEKINDS_IN_ORDER = ['override', 'content', 'room', 'sender', 'underride'];
/** /**
* Construct a Push Processor. * Construct a Push Processor.
* @constructor * @constructor
@@ -28,12 +30,11 @@ function PushProcessor(client) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}; };
const matchingRuleFromKindSet = function(ev, kindset, device) { const matchingRuleFromKindSet = (ev, kindset, device) => {
const rulekinds_in_order = ['override', 'content', 'room', 'sender', 'underride'];
for (let ruleKindIndex = 0; for (let ruleKindIndex = 0;
ruleKindIndex < rulekinds_in_order.length; ruleKindIndex < RULEKINDS_IN_ORDER.length;
++ruleKindIndex) { ++ruleKindIndex) {
const kind = rulekinds_in_order[ruleKindIndex]; const kind = RULEKINDS_IN_ORDER[ruleKindIndex];
const ruleset = kindset[kind]; const ruleset = kindset[kind];
for (let ruleIndex = 0; ruleIndex < ruleset.length; ++ruleIndex) { for (let ruleIndex = 0; ruleIndex < ruleset.length; ++ruleIndex) {
@@ -47,7 +48,7 @@ function PushProcessor(client) {
continue; continue;
} }
if (ruleMatchesEvent(rawrule, ev)) { if (this.ruleMatchesEvent(rawrule, ev)) {
rule.kind = kind; rule.kind = kind;
return rule; return rule;
} }
@@ -107,16 +108,6 @@ function PushProcessor(client) {
return rawrule; return rawrule;
}; };
const ruleMatchesEvent = function(rule, ev) {
let ret = true;
for (let i = 0; i < rule.conditions.length; ++i) {
const cond = rule.conditions[i];
ret &= eventFulfillsCondition(cond, ev);
}
//console.log("Rule "+rule.rule_id+(ret ? " matches" : " doesn't match"));
return ret;
};
const eventFulfillsCondition = function(cond, ev) { const eventFulfillsCondition = function(cond, ev) {
const condition_functions = { const condition_functions = {
"event_match": eventFulfillsEventMatchCondition, "event_match": eventFulfillsEventMatchCondition,
@@ -331,6 +322,17 @@ function PushProcessor(client) {
return actionObj; return actionObj;
}; };
this.ruleMatchesEvent = function(rule, ev) {
let ret = true;
for (let i = 0; i < rule.conditions.length; ++i) {
const cond = rule.conditions[i];
ret &= eventFulfillsCondition(cond, ev);
}
//console.log("Rule "+rule.rule_id+(ret ? " matches" : " doesn't match"));
return ret;
};
/** /**
* Get the user's push actions for the given event * Get the user's push actions for the given event
* *
@@ -341,6 +343,27 @@ function PushProcessor(client) {
this.actionsForEvent = function(ev) { this.actionsForEvent = function(ev) {
return pushActionsForEventAndRulesets(ev, client.pushRules); return pushActionsForEventAndRulesets(ev, client.pushRules);
}; };
/**
* Get one of the users push rules by its ID
*
* @param {string} ruleId The ID of the rule to search for
* @return {object} The push rule, or null if no such rule was found
*/
this.getPushRuleById = function(ruleId) {
for (const scope of ['device', 'global']) {
if (client.pushRules[scope] === undefined) continue;
for (const kind of RULEKINDS_IN_ORDER) {
if (client.pushRules[scope][kind] === undefined) continue;
for (const rule of client.pushRules[scope][kind]) {
if (rule.rule_id === ruleId) return rule;
}
}
}
return null;
}
} }
/** /**