1 | /*
|
2 | Copyright 2015, 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
|
20 | */
|
21 |
|
22 | import {FilterComponent} from "./filter-component";
|
23 |
|
24 | /**
|
25 | * @param {Object} obj
|
26 | * @param {string} keyNesting
|
27 | * @param {*} val
|
28 | */
|
29 | function setProp(obj, keyNesting, val) {
|
30 | const nestedKeys = keyNesting.split(".");
|
31 | let currentObj = obj;
|
32 | for (let i = 0; i < (nestedKeys.length - 1); i++) {
|
33 | if (!currentObj[nestedKeys[i]]) {
|
34 | currentObj[nestedKeys[i]] = {};
|
35 | }
|
36 | currentObj = currentObj[nestedKeys[i]];
|
37 | }
|
38 | currentObj[nestedKeys[nestedKeys.length - 1]] = val;
|
39 | }
|
40 |
|
41 | /**
|
42 | * Construct a new Filter.
|
43 | * @constructor
|
44 | * @param {string} userId The user ID for this filter.
|
45 | * @param {string=} filterId The filter ID if known.
|
46 | * @prop {string} userId The user ID of the filter
|
47 | * @prop {?string} filterId The filter ID
|
48 | */
|
49 | export function Filter(userId, filterId) {
|
50 | this.userId = userId;
|
51 | this.filterId = filterId;
|
52 | this.definition = {};
|
53 | }
|
54 |
|
55 | Filter.LAZY_LOADING_MESSAGES_FILTER = {
|
56 | lazy_load_members: true,
|
57 | };
|
58 |
|
59 | Filter.LAZY_LOADING_SYNC_FILTER = {
|
60 | room: {
|
61 | state: Filter.LAZY_LOADING_MESSAGES_FILTER,
|
62 | },
|
63 | };
|
64 |
|
65 |
|
66 | /**
|
67 | * Get the ID of this filter on your homeserver (if known)
|
68 | * @return {?Number} The filter ID
|
69 | */
|
70 | Filter.prototype.getFilterId = function() {
|
71 | return this.filterId;
|
72 | };
|
73 |
|
74 | /**
|
75 | * Get the JSON body of the filter.
|
76 | * @return {Object} The filter definition
|
77 | */
|
78 | Filter.prototype.getDefinition = function() {
|
79 | return this.definition;
|
80 | };
|
81 |
|
82 | /**
|
83 | * Set the JSON body of the filter
|
84 | * @param {Object} definition The filter definition
|
85 | */
|
86 | Filter.prototype.setDefinition = function(definition) {
|
87 | this.definition = definition;
|
88 |
|
89 | // This is all ported from synapse's FilterCollection()
|
90 |
|
91 | // definitions look something like:
|
92 | // {
|
93 | // "room": {
|
94 | // "rooms": ["!abcde:example.com"],
|
95 | // "not_rooms": ["!123456:example.com"],
|
96 | // "state": {
|
97 | // "types": ["m.room.*"],
|
98 | // "not_rooms": ["!726s6s6q:example.com"],
|
99 | // },
|
100 | // "timeline": {
|
101 | // "limit": 10,
|
102 | // "types": ["m.room.message"],
|
103 | // "not_rooms": ["!726s6s6q:example.com"],
|
104 | // "not_senders": ["@spam:example.com"]
|
105 | // "contains_url": true
|
106 | // },
|
107 | // "ephemeral": {
|
108 | // "types": ["m.receipt", "m.typing"],
|
109 | // "not_rooms": ["!726s6s6q:example.com"],
|
110 | // "not_senders": ["@spam:example.com"]
|
111 | // }
|
112 | // },
|
113 | // "presence": {
|
114 | // "types": ["m.presence"],
|
115 | // "not_senders": ["@alice:example.com"]
|
116 | // },
|
117 | // "event_format": "client",
|
118 | // "event_fields": ["type", "content", "sender"]
|
119 | // }
|
120 |
|
121 | const room_filter_json = definition.room;
|
122 |
|
123 | // consider the top level rooms/not_rooms filter
|
124 | const room_filter_fields = {};
|
125 | if (room_filter_json) {
|
126 | if (room_filter_json.rooms) {
|
127 | room_filter_fields.rooms = room_filter_json.rooms;
|
128 | }
|
129 | if (room_filter_json.rooms) {
|
130 | room_filter_fields.not_rooms = room_filter_json.not_rooms;
|
131 | }
|
132 |
|
133 | this._include_leave = room_filter_json.include_leave || false;
|
134 | }
|
135 |
|
136 | this._room_filter = new FilterComponent(room_filter_fields);
|
137 | this._room_timeline_filter = new FilterComponent(
|
138 | room_filter_json ? (room_filter_json.timeline || {}) : {},
|
139 | );
|
140 |
|
141 | // don't bother porting this from synapse yet:
|
142 | // this._room_state_filter =
|
143 | // new FilterComponent(room_filter_json.state || {});
|
144 | // this._room_ephemeral_filter =
|
145 | // new FilterComponent(room_filter_json.ephemeral || {});
|
146 | // this._room_account_data_filter =
|
147 | // new FilterComponent(room_filter_json.account_data || {});
|
148 | // this._presence_filter =
|
149 | // new FilterComponent(definition.presence || {});
|
150 | // this._account_data_filter =
|
151 | // new FilterComponent(definition.account_data || {});
|
152 | };
|
153 |
|
154 | /**
|
155 | * Get the room.timeline filter component of the filter
|
156 | * @return {FilterComponent} room timeline filter component
|
157 | */
|
158 | Filter.prototype.getRoomTimelineFilterComponent = function() {
|
159 | return this._room_timeline_filter;
|
160 | };
|
161 |
|
162 | /**
|
163 | * Filter the list of events based on whether they are allowed in a timeline
|
164 | * based on this filter
|
165 | * @param {MatrixEvent[]} events the list of events being filtered
|
166 | * @return {MatrixEvent[]} the list of events which match the filter
|
167 | */
|
168 | Filter.prototype.filterRoomTimeline = function(events) {
|
169 | return this._room_timeline_filter.filter(this._room_filter.filter(events));
|
170 | };
|
171 |
|
172 | /**
|
173 | * Set the max number of events to return for each room's timeline.
|
174 | * @param {Number} limit The max number of events to return for each room.
|
175 | */
|
176 | Filter.prototype.setTimelineLimit = function(limit) {
|
177 | setProp(this.definition, "room.timeline.limit", limit);
|
178 | };
|
179 |
|
180 | /**
|
181 | * Control whether left rooms should be included in responses.
|
182 | * @param {boolean} includeLeave True to make rooms the user has left appear
|
183 | * in responses.
|
184 | */
|
185 | Filter.prototype.setIncludeLeaveRooms = function(includeLeave) {
|
186 | setProp(this.definition, "room.include_leave", includeLeave);
|
187 | };
|
188 |
|
189 | /**
|
190 | * Create a filter from existing data.
|
191 | * @static
|
192 | * @param {string} userId
|
193 | * @param {string} filterId
|
194 | * @param {Object} jsonObj
|
195 | * @return {Filter}
|
196 | */
|
197 | Filter.fromJson = function(userId, filterId, jsonObj) {
|
198 | const filter = new Filter(userId, filterId);
|
199 | filter.setDefinition(jsonObj);
|
200 | return filter;
|
201 | };
|