/* Copyright 2015, 2016 OpenMarket Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ "use strict"; /** * @module filter */ /** * @param {Object} obj * @param {string} keyNesting * @param {*} val */ function setProp(obj, keyNesting, val) { var nestedKeys = keyNesting.split("."); var currentObj = obj; for (var i = 0; i < (nestedKeys.length - 1); i++) { if (!currentObj[nestedKeys[i]]) { currentObj[nestedKeys[i]] = {}; } currentObj = currentObj[nestedKeys[i]]; } currentObj[nestedKeys[nestedKeys.length - 1]] = val; } /** * Construct a new Filter. * @constructor * @param {string} userId The user ID for this filter. * @param {string=} filterId The filter ID if known. * @prop {string} userId The user ID of the filter * @prop {?string} filterId The filter ID */ function Filter(userId, filterId) { this.userId = userId; this.filterId = filterId; this.definition = {}; } /** * Get the JSON body of the filter. * @return {Object} The filter definition */ Filter.prototype.getDefinition = function() { return this.definition; }; /** * Set the JSON body of the filter * @param {Object} definition The filter definition */ Filter.prototype.setDefinition = function(definition) { this.definition = definition; }; /** * Set the max number of events to return for each room's timeline. * @param {Number} limit The max number of events to return for each room. */ Filter.prototype.setTimelineLimit = function(limit) { setProp(this.definition, "room.timeline.limit", limit); }; /** * Control whether left rooms should be included in responses. * @param {boolean} includeLeave True to make rooms the user has left appear * in responses. */ Filter.prototype.setIncludeLeaveRooms = function(includeLeave) { setProp(this.definition, "room.include_leave", includeLeave); }; /** * Create a filter from existing data. * @static * @param {string} userId * @param {string} filterId * @param {Object} jsonObj * @return {Filter} */ Filter.fromJson = function(userId, filterId, jsonObj) { var filter = new Filter(userId, filterId); filter.setDefinition(jsonObj); return filter; }; /** The Filter class */ module.exports = Filter;