UNPKG

8.01 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/filter-component.js"],"names":["_matches_wildcard","actual_value","filter_value","endsWith","type_prefix","slice","substr","length","FilterComponent","filter_json","types","not_types","rooms","not_rooms","senders","not_senders","contains_url","prototype","check","event","_checkFields","getRoomId","getSender","getType","getContent","url","undefined","room_id","sender","event_type","literal_keys","v","self","n","name","match_func","not_name","disallowed_values","filter","allowed_values","map","contains_url_filter","events","limit","module","exports"],"mappings":"AAAA;;;;;;;;;;;;;;;AAeA;AACA;;;;AAIA;;;;;;;;;;;;;;AAOA,SAASA,iBAAT,CAA2BC,YAA3B,EAAyCC,YAAzC,EAAuD;AACnD,QAAIA,aAAaC,QAAb,CAAsB,GAAtB,CAAJ,EAAgC;AAC5B,YAAMC,cAAcF,aAAaG,KAAb,CAAmB,CAAnB,EAAsB,CAAC,CAAvB,CAApB;AACA,eAAOJ,aAAaK,MAAb,CAAoB,CAApB,EAAuBF,YAAYG,MAAnC,MAA+CH,WAAtD;AACH,KAHD,MAGO;AACH,eAAOH,iBAAiBC,YAAxB;AACH;AACJ;;AAED;;;;;;;;;;;AAWA,SAASM,eAAT,CAAyBC,WAAzB,EAAsC;AAClC,SAAKA,WAAL,GAAmBA,WAAnB;;AAEA,SAAKC,KAAL,GAAaD,YAAYC,KAAZ,IAAqB,IAAlC;AACA,SAAKC,SAAL,GAAiBF,YAAYE,SAAZ,IAAyB,EAA1C;;AAEA,SAAKC,KAAL,GAAaH,YAAYG,KAAZ,IAAqB,IAAlC;AACA,SAAKC,SAAL,GAAiBJ,YAAYI,SAAZ,IAAyB,EAA1C;;AAEA,SAAKC,OAAL,GAAeL,YAAYK,OAAZ,IAAuB,IAAtC;AACA,SAAKC,WAAL,GAAmBN,YAAYM,WAAZ,IAA2B,EAA9C;;AAEA,SAAKC,YAAL,GAAoBP,YAAYO,YAAZ,IAA4B,IAAhD;AACH;;AAED;;;;;AAKAR,gBAAgBS,SAAhB,CAA0BC,KAA1B,GAAkC,UAAS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O,YAA7C;AACA,QAAIyB,wBAAwBf,SAA5B,EAAuC;AACnC,YAAIe,wBAAwBzB,YAA5B,EAA0C;AACtC,mBAAO,KAAP;AACH;AACJ;;AAED,WAAO,IAAP;AACH,CAxCD;;AA0CA;;;;;AAKAR,gBAAgBS,SAAhB,CAA0BqB,MAA1B,GAAmC,UAASI,MAAT,EAAiB;AAChD,WAAOA,OAAOJ,MAAP,CAAc,KAAKpB,KAAnB,EAA0B,IAA1B,CAAP;AACH,CAFD;;AAIA;;;;;AAKAV,gBAAgBS,SAAhB,CAA0B0B,KAA1B,GAAkC,YAAW;AACzC,WAAO,KAAKlC,WAAL,CAAiBkC,KAAjB,KAA2BjB,SAA3B,GAAuC,KAAKjB,WAAL,CAAiBkC,KAAxD,GAAgE,EAAvE;AACH,CAFD;;AAIA;AACAC,OAAOC,OAAP,GAAiBrC,eAAjB","file":"filter-component.js","sourcesContent":["/*\nCopyright 2016 OpenMarket Ltd\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\"use strict\";\n/**\n * @module filter-component\n */\n\n/**\n * Checks if a value matches a given field value, which may be a * terminated\n * wildcard pattern.\n * @param {String} actual_value The value to be compared\n * @param {String} filter_value The filter pattern to be compared\n * @return {bool} true if the actual_value matches the filter_value\n */\nfunction _matches_wildcard(actual_value, filter_value) {\n if (filter_value.endsWith(\"*\")) {\n const type_prefix = filter_value.slice(0, -1);\n return actual_value.substr(0, type_prefix.length) === type_prefix;\n } else {\n return actual_value === filter_value;\n }\n}\n\n/**\n * FilterComponent is a section of a Filter definition which defines the\n * types, rooms, senders filters etc to be applied to a particular type of resource.\n * This is all ported over from synapse's Filter object.\n *\n * N.B. that synapse refers to these as 'Filters', and what js-sdk refers to as\n * 'Filters' are referred to as 'FilterCollections'.\n *\n * @constructor\n * @param {Object} filter_json the definition of this filter JSON, e.g. { 'contains_url': true }\n */\nfunction FilterComponent(filter_json) {\n this.filter_json = filter_json;\n\n this.types = filter_json.types || null;\n this.not_types = filter_json.not_types || [];\n\n this.rooms = filter_json.rooms || null;\n this.not_rooms = filter_json.not_rooms || [];\n\n this.senders = filter_json.senders || null;\n this.not_senders = filter_json.not_senders || [];\n\n this.contains_url = filter_json.contains_url || null;\n}\n\n/**\n * Checks with the filter component matches the given event\n * @param {MatrixEvent} event event to be checked against the filter\n * @return {bool} true if the event matches the filter\n */\nFilterComponent.prototype.check = function(event) {\n return this._checkFields(\n event.getRoomId(),\n event.getSender(),\n event.getType(),\n event.getContent() ? event.getContent().url !== undefined : false,\n );\n};\n\n/**\n * Checks whether the filter component matches the given event fields.\n * @param {String} room_id the room_id for the event being checked\n * @param {String} sender the sender of the event being checked\n * @param {String} event_type the type of the event being checked\n * @param {String} contains_url whether the event contains a content.url field\n * @return {bool} true if the event fields match the filter\n */\nFilterComponent.prototype._checkFields =\n function(room_id, sender, event_type, contains_url) {\n const literal_keys = {\n \"rooms\": function(v) {\n return room_id === v;\n },\n \"senders\": function(v) {\n return sender === v;\n },\n \"types\": function(v) {\n return _matches_wildcard(event_type, v);\n },\n };\n\n const self = this;\n for (let n=0; n < Object.keys(literal_keys).length; n++) {\n const name = Object.keys(literal_keys)[n];\n const match_func = literal_keys[name];\n const not_name = \"not_\" + name;\n const disallowed_values = self[not_name];\n if (disallowed_values.filter(match_func).length > 0) {\n return false;\n }\n\n const allowed_values = self[name];\n if (allowed_values) {\n if (!allowed_values.map(match_func)) {\n return false;\n }\n }\n }\n\n const contains_url_filter = this.filter_json.contains_url;\n if (contains_url_filter !== undefined) {\n if (contains_url_filter !== contains_url) {\n return false;\n }\n }\n\n return true;\n};\n\n/**\n * Filters a list of events down to those which match this filter component\n * @param {MatrixEvent[]} events Events to be checked againt the filter component\n * @return {MatrixEvent[]} events which matched the filter component\n */\nFilterComponent.prototype.filter = function(events) {\n return events.filter(this.check, this);\n};\n\n/**\n * Returns the limit field for a given filter component, providing a default of\n * 10 if none is otherwise specified. Cargo-culted from Synapse.\n * @return {Number} the limit for this filter component.\n */\nFilterComponent.prototype.limit = function() {\n return this.filter_json.limit !== undefined ? this.filter_json.limit : 10;\n};\n\n/** The FilterComponent class */\nmodule.exports = FilterComponent;\n"]}
\No newline at end of file