1 | /*
|
2 | Copyright 2015, 2016 OpenMarket Ltd
|
3 |
|
4 | Licensed under the Apache License, Version 2.0 (the "License");
|
5 | you may not use this file except in compliance with the License.
|
6 | You may obtain a copy of the License at
|
7 |
|
8 | http://www.apache.org/licenses/LICENSE-2.0
|
9 |
|
10 | Unless required by applicable law or agreed to in writing, software
|
11 | distributed under the License is distributed on an "AS IS" BASIS,
|
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | See the License for the specific language governing permissions and
|
14 | limitations under the License.
|
15 | */
|
16 | ;
|
17 | /**
|
18 | * @module filter
|
19 | */
|
20 |
|
21 | var FilterComponent = require("./filter-component");
|
22 |
|
23 | /**
|
24 | * @param {Object} obj
|
25 | * @param {string} keyNesting
|
26 | * @param {*} val
|
27 | */
|
28 | function setProp(obj, keyNesting, val) {
|
29 | var nestedKeys = keyNesting.split(".");
|
30 | var currentObj = obj;
|
31 | for (var i = 0; i < nestedKeys.length - 1; i++) {
|
32 | if (!currentObj[nestedKeys[i]]) {
|
33 | currentObj[nestedKeys[i]] = {};
|
34 | }
|
35 | currentObj = currentObj[nestedKeys[i]];
|
36 | }
|
37 | currentObj[nestedKeys[nestedKeys.length - 1]] = val;
|
38 | }
|
39 |
|
40 | /**
|
41 | * Construct a new Filter.
|
42 | * @constructor
|
43 | * @param {string} userId The user ID for this filter.
|
44 | * @param {string=} filterId The filter ID if known.
|
45 | * @prop {string} userId The user ID of the filter
|
46 | * @prop {?string} filterId The filter ID
|
47 | */
|
48 | function Filter(userId, filterId) {
|
49 | this.userId = userId;
|
50 | this.filterId = filterId;
|
51 | this.definition = {};
|
52 | }
|
53 |
|
54 | Filter.LAZY_LOADING_MESSAGES_FILTER = {
|
55 | lazy_load_members: true
|
56 | };
|
57 |
|
58 | Filter.LAZY_LOADING_SYNC_FILTER = {
|
59 | room: {
|
60 | state: Filter.LAZY_LOADING_MESSAGES_FILTER
|
61 | }
|
62 | };
|
63 |
|
64 | /**
|
65 | * Get the ID of this filter on your homeserver (if known)
|
66 | * @return {?Number} The filter ID
|
67 | */
|
68 | Filter.prototype.getFilterId = function () {
|
69 | return this.filterId;
|
70 | };
|
71 |
|
72 | /**
|
73 | * Get the JSON body of the filter.
|
74 | * @return {Object} The filter definition
|
75 | */
|
76 | Filter.prototype.getDefinition = function () {
|
77 | return this.definition;
|
78 | };
|
79 |
|
80 | /**
|
81 | * Set the JSON body of the filter
|
82 | * @param {Object} definition The filter definition
|
83 | */
|
84 | Filter.prototype.setDefinition = function (definition) {
|
85 | this.definition = definition;
|
86 |
|
87 | // This is all ported from synapse's FilterCollection()
|
88 |
|
89 | // definitions look something like:
|
90 | // {
|
91 | // "room": {
|
92 | // "rooms": ["!abcde:example.com"],
|
93 | // "not_rooms": ["!123456:example.com"],
|
94 | // "state": {
|
95 | // "types": ["m.room.*"],
|
96 | // "not_rooms": ["!726s6s6q:example.com"],
|
97 | // },
|
98 | // "timeline": {
|
99 | // "limit": 10,
|
100 | // "types": ["m.room.message"],
|
101 | // "not_rooms": ["!726s6s6q:example.com"],
|
102 | // "not_senders": ["@spam:example.com"]
|
103 | // "contains_url": true
|
104 | // },
|
105 | // "ephemeral": {
|
106 | // "types": ["m.receipt", "m.typing"],
|
107 | // "not_rooms": ["!726s6s6q:example.com"],
|
108 | // "not_senders": ["@spam:example.com"]
|
109 | // }
|
110 | // },
|
111 | // "presence": {
|
112 | // "types": ["m.presence"],
|
113 | // "not_senders": ["@alice:example.com"]
|
114 | // },
|
115 | // "event_format": "client",
|
116 | // "event_fields": ["type", "content", "sender"]
|
117 | // }
|
118 |
|
119 | var room_filter_json = definition.room;
|
120 |
|
121 | // consider the top level rooms/not_rooms filter
|
122 | var room_filter_fields = {};
|
123 | if (room_filter_json) {
|
124 | if (room_filter_json.rooms) {
|
125 | room_filter_fields.rooms = room_filter_json.rooms;
|
126 | }
|
127 | if (room_filter_json.rooms) {
|
128 | room_filter_fields.not_rooms = room_filter_json.not_rooms;
|
129 | }
|
130 |
|
131 | this._include_leave = room_filter_json.include_leave || false;
|
132 | }
|
133 |
|
134 | this._room_filter = new FilterComponent(room_filter_fields);
|
135 | this._room_timeline_filter = new FilterComponent(room_filter_json ? room_filter_json.timeline || {} : {});
|
136 |
|
137 | // don't bother porting this from synapse yet:
|
138 | // this._room_state_filter =
|
139 | // new FilterComponent(room_filter_json.state || {});
|
140 | // this._room_ephemeral_filter =
|
141 | // new FilterComponent(room_filter_json.ephemeral || {});
|
142 | // this._room_account_data_filter =
|
143 | // new FilterComponent(room_filter_json.account_data || {});
|
144 | // this._presence_filter =
|
145 | // new FilterComponent(definition.presence || {});
|
146 | // this._account_data_filter =
|
147 | // new FilterComponent(definition.account_data || {});
|
148 | };
|
149 |
|
150 | /**
|
151 | * Get the room.timeline filter component of the filter
|
152 | * @return {FilterComponent} room timeline filter component
|
153 | */
|
154 | Filter.prototype.getRoomTimelineFilterComponent = function () {
|
155 | return this._room_timeline_filter;
|
156 | };
|
157 |
|
158 | /**
|
159 | * Filter the list of events based on whether they are allowed in a timeline
|
160 | * based on this filter
|
161 | * @param {MatrixEvent[]} events the list of events being filtered
|
162 | * @return {MatrixEvent[]} the list of events which match the filter
|
163 | */
|
164 | Filter.prototype.filterRoomTimeline = function (events) {
|
165 | return this._room_timeline_filter.filter(this._room_filter.filter(events));
|
166 | };
|
167 |
|
168 | /**
|
169 | * Set the max number of events to return for each room's timeline.
|
170 | * @param {Number} limit The max number of events to return for each room.
|
171 | */
|
172 | Filter.prototype.setTimelineLimit = function (limit) {
|
173 | setProp(this.definition, "room.timeline.limit", limit);
|
174 | };
|
175 |
|
176 | /**
|
177 | * Control whether left rooms should be included in responses.
|
178 | * @param {boolean} includeLeave True to make rooms the user has left appear
|
179 | * in responses.
|
180 | */
|
181 | Filter.prototype.setIncludeLeaveRooms = function (includeLeave) {
|
182 | setProp(this.definition, "room.include_leave", includeLeave);
|
183 | };
|
184 |
|
185 | /**
|
186 | * Create a filter from existing data.
|
187 | * @static
|
188 | * @param {string} userId
|
189 | * @param {string} filterId
|
190 | * @param {Object} jsonObj
|
191 | * @return {Filter}
|
192 | */
|
193 | Filter.fromJson = function (userId, filterId, jsonObj) {
|
194 | var filter = new Filter(userId, filterId);
|
195 | filter.setDefinition(jsonObj);
|
196 | return filter;
|
197 | };
|
198 |
|
199 | /** The Filter class */
|
200 | module.exports = Filter;
|
201 | //# sourceMappingURL=filter.js.map |
\ | No newline at end of file |