1 | /*
|
2 | Copyright 2016 OpenMarket Ltd
|
3 | Copyright 2019 The Matrix.org Foundation C.I.C.
|
4 |
|
5 | Licensed under the Apache License, Version 2.0 (the "License");
|
6 | you may not use this file except in compliance with the License.
|
7 | You may obtain a copy of the License at
|
8 |
|
9 | http://www.apache.org/licenses/LICENSE-2.0
|
10 |
|
11 | Unless required by applicable law or agreed to in writing, software
|
12 | distributed under the License is distributed on an "AS IS" BASIS,
|
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | See the License for the specific language governing permissions and
|
15 | limitations under the License.
|
16 | */
|
17 |
|
18 | /**
|
19 | * @module filter-component
|
20 | */
|
21 |
|
22 | /**
|
23 | * Checks if a value matches a given field value, which may be a * terminated
|
24 | * wildcard pattern.
|
25 | * @param {String} actual_value The value to be compared
|
26 | * @param {String} filter_value The filter pattern to be compared
|
27 | * @return {bool} true if the actual_value matches the filter_value
|
28 | */
|
29 | function _matches_wildcard(actual_value, filter_value) {
|
30 | if (filter_value.endsWith("*")) {
|
31 | const type_prefix = filter_value.slice(0, -1);
|
32 | return actual_value.substr(0, type_prefix.length) === type_prefix;
|
33 | } else {
|
34 | return actual_value === filter_value;
|
35 | }
|
36 | }
|
37 |
|
38 | /**
|
39 | * FilterComponent is a section of a Filter definition which defines the
|
40 | * types, rooms, senders filters etc to be applied to a particular type of resource.
|
41 | * This is all ported over from synapse's Filter object.
|
42 | *
|
43 | * N.B. that synapse refers to these as 'Filters', and what js-sdk refers to as
|
44 | * 'Filters' are referred to as 'FilterCollections'.
|
45 | *
|
46 | * @constructor
|
47 | * @param {Object} filter_json the definition of this filter JSON, e.g. { 'contains_url': true }
|
48 | */
|
49 | export function FilterComponent(filter_json) {
|
50 | this.filter_json = filter_json;
|
51 |
|
52 | this.types = filter_json.types || null;
|
53 | this.not_types = filter_json.not_types || [];
|
54 |
|
55 | this.rooms = filter_json.rooms || null;
|
56 | this.not_rooms = filter_json.not_rooms || [];
|
57 |
|
58 | this.senders = filter_json.senders || null;
|
59 | this.not_senders = filter_json.not_senders || [];
|
60 |
|
61 | this.contains_url = filter_json.contains_url || null;
|
62 | }
|
63 |
|
64 | /**
|
65 | * Checks with the filter component matches the given event
|
66 | * @param {MatrixEvent} event event to be checked against the filter
|
67 | * @return {bool} true if the event matches the filter
|
68 | */
|
69 | FilterComponent.prototype.check = function(event) {
|
70 | return this._checkFields(
|
71 | event.getRoomId(),
|
72 | event.getSender(),
|
73 | event.getType(),
|
74 | event.getContent() ? event.getContent().url !== undefined : false,
|
75 | );
|
76 | };
|
77 |
|
78 | /**
|
79 | * Checks whether the filter component matches the given event fields.
|
80 | * @param {String} room_id the room_id for the event being checked
|
81 | * @param {String} sender the sender of the event being checked
|
82 | * @param {String} event_type the type of the event being checked
|
83 | * @param {String} contains_url whether the event contains a content.url field
|
84 | * @return {bool} true if the event fields match the filter
|
85 | */
|
86 | FilterComponent.prototype._checkFields =
|
87 | function(room_id, sender, event_type, contains_url) {
|
88 | const literal_keys = {
|
89 | "rooms": function(v) {
|
90 | return room_id === v;
|
91 | },
|
92 | "senders": function(v) {
|
93 | return sender === v;
|
94 | },
|
95 | "types": function(v) {
|
96 | return _matches_wildcard(event_type, v);
|
97 | },
|
98 | };
|
99 |
|
100 | const self = this;
|
101 | for (let n=0; n < Object.keys(literal_keys).length; n++) {
|
102 | const name = Object.keys(literal_keys)[n];
|
103 | const match_func = literal_keys[name];
|
104 | const not_name = "not_" + name;
|
105 | const disallowed_values = self[not_name];
|
106 | if (disallowed_values.filter(match_func).length > 0) {
|
107 | return false;
|
108 | }
|
109 |
|
110 | const allowed_values = self[name];
|
111 | if (allowed_values) {
|
112 | if (!allowed_values.map(match_func)) {
|
113 | return false;
|
114 | }
|
115 | }
|
116 | }
|
117 |
|
118 | const contains_url_filter = this.filter_json.contains_url;
|
119 | if (contains_url_filter !== undefined) {
|
120 | if (contains_url_filter !== contains_url) {
|
121 | return false;
|
122 | }
|
123 | }
|
124 |
|
125 | return true;
|
126 | };
|
127 |
|
128 | /**
|
129 | * Filters a list of events down to those which match this filter component
|
130 | * @param {MatrixEvent[]} events Events to be checked againt the filter component
|
131 | * @return {MatrixEvent[]} events which matched the filter component
|
132 | */
|
133 | FilterComponent.prototype.filter = function(events) {
|
134 | return events.filter(this.check, this);
|
135 | };
|
136 |
|
137 | /**
|
138 | * Returns the limit field for a given filter component, providing a default of
|
139 | * 10 if none is otherwise specified. Cargo-culted from Synapse.
|
140 | * @return {Number} the limit for this filter component.
|
141 | */
|
142 | FilterComponent.prototype.limit = function() {
|
143 | return this.filter_json.limit !== undefined ? this.filter_json.limit : 10;
|
144 | };
|