diff --git a/lib/pushprocessor.js b/lib/pushprocessor.js index 8ee34c449..75217e72a 100644 --- a/lib/pushprocessor.js +++ b/lib/pushprocessor.js @@ -1,15 +1,20 @@ var micromatch = require("micromatch"); +/** + * Construct a Push Processor. + * @constructor + * @param {Object} client The Matrix client object to use + */ module.exports = function(client) { - - - var escapeRegExp = function(string){ + var escapeRegExp = function(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }; var matchingRuleFromKindSet = function(ev, kindset, device) { var rulekinds_in_order = ['override', 'content', 'room', 'sender', 'underride']; - for (var ruleKindIndex = 0; ruleKindIndex < rulekinds_in_order.length; ++ruleKindIndex) { + for (var ruleKindIndex = 0; + ruleKindIndex < rulekinds_in_order.length; + ++ruleKindIndex) { var kind = rulekinds_in_order[ruleKindIndex]; var ruleset = kindset[kind]; @@ -68,7 +73,7 @@ module.exports = function(client) { if (device) { rawrule.conditions.push({ 'kind': 'device', - 'profile_tag': device, + 'profile_tag': device }); } return rawrule; @@ -91,7 +96,9 @@ module.exports = function(client) { "contains_display_name": eventFulfillsDisplayNameCondition, "room_member_count": eventFulfillsRoomMemberCountCondition }; - if (condition_functions[cond.kind]) { return condition_functions[cond.kind](cond, ev); } + if (condition_functions[cond.kind]) { + return condition_functions[cond.kind](cond, ev); + } return true; }; @@ -126,12 +133,14 @@ module.exports = function(client) { }; var eventFulfillsDisplayNameCondition = function(cond, ev) { - if (!ev.content || ! ev.content.body || typeof ev.content.body != 'string') { return false; } + if (!ev.content || ! ev.content.body || typeof ev.content.body != 'string') { + return false; + } var room = client.getRoom(ev.room_id); var displayName = room.currentState.getMember(client.credentials.userId).name; - var pat = new RegExp("\\b"+escapeRegExp(displayName)+"\\b", 'i'); + var pat = new RegExp("\\b" + escapeRegExp(displayName) + "\\b", 'i'); return ev.content.body.search(pat) > -1; }; @@ -143,9 +152,10 @@ module.exports = function(client) { var val = valueForDottedKey(cond.key, ev); if (!val || typeof val != 'string') { return false; } - // Supportting ! in globs would mean figuring out when we don't want to use things as a regex, like room IDs + // Supportting ! in globs would mean figuring out when we + // don't want to use things as a regex, like room IDs var pat = cond.pattern.replace("!", "\\!"); - + if (cond.key == 'content.body') { return micromatch.contains(val, pat); } else { @@ -196,7 +206,7 @@ module.exports = function(client) { var pushActionsForEventAndRulesets = function(ev, rulesets) { var rule = matchingRuleForEventWithRulesets(ev, rulesets); if (!rule) { return {}; } - + var actionObj = actionListToActionsObject(rule.actions); // Some actions are implicit in some situations: we add those here