UNPKG

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