diff --git a/lib/filter.js b/lib/filter.js index 7eeb78147..bb104d5e4 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -17,7 +17,7 @@ function setProp(obj, keyNesting, val) { } currentObj = currentObj[nestedKeys[i]]; } - currentObj[nestedKeys.length - 1] = val; + currentObj[nestedKeys[nestedKeys.length - 1]] = val; } /** @@ -25,29 +25,29 @@ function setProp(obj, keyNesting, val) { * @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.body = { - event_format: "client" - }; + this.definition = {}; } /** * Get the JSON body of the filter. - * @return {Object} The JSON body + * @return {Object} The filter definition */ -Filter.prototype.getBody = function() { - return this.body; +Filter.prototype.getDefinition = function() { + return this.definition; }; /** * Set the JSON body of the filter - * @param {Object} body + * @param {Object} body The filter definition */ -Filter.prototype.setBody = function(body) { - this.body = body; +Filter.prototype.setDefinition = function(definition) { + this.definition = definition; }; /** @@ -55,7 +55,7 @@ Filter.prototype.setBody = function(body) { * @param {Number} limit The max number of events to return for each room. */ Filter.prototype.setTimelineLimit = function(limit) { - setProp(this.body, "room.timeline.limit", limit); + setProp(this.definition, "room.timeline.limit", limit); }; /** @@ -68,7 +68,7 @@ Filter.prototype.setTimelineLimit = function(limit) { */ Filter.fromJson = function(userId, filterId, jsonObj) { var filter = new Filter(userId, filterId); - filter.setBody(jsonObj); + filter.setDefinition(jsonObj); return filter; }; diff --git a/lib/matrix.js b/lib/matrix.js index 6f9b9bfe7..f150f7e76 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -34,6 +34,7 @@ module.exports.CRYPTO_ENABLED = require("./client").CRYPTO_ENABLED; module.exports.ContentRepo = require("./content-repo"); /** The {@link module:filter~Filter|Filter} class. */ module.exports.Filter = require("./filter"); + /** * Create a new Matrix Call. * @function diff --git a/spec/unit/filter.spec.js b/spec/unit/filter.spec.js new file mode 100644 index 000000000..e9c62f588 --- /dev/null +++ b/spec/unit/filter.spec.js @@ -0,0 +1,50 @@ +"use strict"; +var sdk = require("../.."); +var Filter = sdk.Filter; +var utils = require("../test-utils"); + +describe("Filter", function() { + var filterId = "f1lt3ring15g00d4ursoul"; + var userId = "@sir_arthur_david:humming.tiger"; + var filter; + + beforeEach(function() { + utils.beforeEach(this); + filter = new Filter(userId); + }); + + describe("fromJson", function() { + it("create a new Filter from the provided values", function() { + var definition = { + event_fields: ["type", "content"] + }; + var f = Filter.fromJson(userId, filterId, definition); + expect(f.getDefinition()).toEqual(definition); + expect(f.userId).toEqual(userId); + expect(f.filterId).toEqual(filterId); + }); + }); + + describe("setTimelineLimit", function() { + it("should set room.timeline.limit of the filter definition", function() { + filter.setTimelineLimit(10); + expect(filter.getDefinition()).toEqual({ + room: { + timeline: { + limit: 10 + } + } + }); + }); + }); + + describe("setDefinition/getDefinition", function() { + it("should set and get the filter body", function() { + var definition = { + event_format: "client" + }; + filter.setDefinition(definition); + expect(filter.getDefinition()).toEqual(definition); + }); + }); +});