diff --git a/lib/client.js b/lib/client.js index aa04497dd..1ec64be36 100644 --- a/lib/client.js +++ b/lib/client.js @@ -2072,11 +2072,10 @@ MatrixClient.prototype.search = function(opts, callback) { /** * Create a new filter. * @param {Object} content The HTTP body for the request - * @param {module:client.callback} callback Optional. * @return {Filter} Resolves to a Filter object. * @return {module:http-api.MatrixError} Rejects: with an error response. */ -MatrixClient.prototype.createFilter = function(content, callback) { +MatrixClient.prototype.createFilter = function(content) { var self = this; var path = utils.encodeUri("/user/$userId/filter", { $userId: this.credentials.userId @@ -2106,7 +2105,7 @@ MatrixClient.prototype.getFilter = function(userId, filterId, allowCached) { if (allowCached) { var filter = this.store.getFilter(userId, filterId); if (filter) { - return filter; + return q(filter); } } diff --git a/lib/store/memory.js b/lib/store/memory.js index 308641a99..ae8257f03 100644 --- a/lib/store/memory.js +++ b/lib/store/memory.js @@ -17,6 +17,11 @@ module.exports.MatrixInMemoryStore = function MatrixInMemoryStore() { // userId: User }; this.syncToken = null; + this.filters = { + // userId: { + // filterId: Filter + // } + }; }; module.exports.MatrixInMemoryStore.prototype = { @@ -116,6 +121,11 @@ module.exports.MatrixInMemoryStore.prototype = { * @param {Filter} filter */ storeFilter: function(filter) { + if (!filter) { return; } + if (!this.filters[filter.userId]) { + this.filters[filter.userId] = {}; + } + this.filters[filter.userId][filter.filterId] = filter; }, /** @@ -125,7 +135,10 @@ module.exports.MatrixInMemoryStore.prototype = { * @return {?Filter} A filter or null. */ getFilter: function(userId, filterId) { - return null; + if (!this.filters[userId] || !this.filters[userId][filterId]) { + return null; + } + return this.filters[userId][filterId]; } // TODO diff --git a/spec/integ/matrix-client-methods.spec.js b/spec/integ/matrix-client-methods.spec.js index d59cfa0d0..d8b51fc4f 100644 --- a/spec/integ/matrix-client-methods.spec.js +++ b/spec/integ/matrix-client-methods.spec.js @@ -4,6 +4,7 @@ var HttpBackend = require("../mock-request"); var publicGlobals = require("../../lib/matrix"); var Room = publicGlobals.Room; var MatrixInMemoryStore = publicGlobals.MatrixInMemoryStore; +var Filter = publicGlobals.Filter; var utils = require("../test-utils"); describe("MatrixClient", function() { @@ -44,6 +45,90 @@ describe("MatrixClient", function() { }); }); + describe("getFilter", function() { + var filterId = "f1lt3r1d"; + + it("should return a filter from the store if allowCached", function(done) { + var filter = Filter.fromJson(userId, filterId, { + event_format: "client" + }); + store.storeFilter(filter); + client.getFilter(userId, filterId, true).done(function(gotFilter) { + expect(gotFilter).toEqual(filter); + done(); + }); + httpBackend.verifyNoOutstandingRequests(); + }); + + it("should do an HTTP request if !allowCached even if one exists", function(done) { + var httpFilterDefinition = { + event_format: "federation" + }; + + httpBackend.when( + "GET", "/user/" + encodeURIComponent(userId) + "/filter/" + filterId + ).respond(200, httpFilterDefinition); + + var storeFilter = Filter.fromJson(userId, filterId, { + event_format: "client" + }); + store.storeFilter(storeFilter); + client.getFilter(userId, filterId, false).done(function(gotFilter) { + expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition); + done(); + }); + + httpBackend.flush(); + }); + + it("should do an HTTP request if nothing is in the cache and then store it", + function(done) { + var httpFilterDefinition = { + event_format: "federation" + }; + expect(store.getFilter(userId, filterId)).toBeNull(); + + httpBackend.when( + "GET", "/user/" + encodeURIComponent(userId) + "/filter/" + filterId + ).respond(200, httpFilterDefinition); + client.getFilter(userId, filterId, true).done(function(gotFilter) { + expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition); + expect(store.getFilter(userId, filterId)).toBeDefined(); + done(); + }); + + httpBackend.flush(); + }); + }); + + describe("createFilter", function() { + var filterId = "f1llllllerid"; + + it("should do an HTTP request and then store the filter", function(done) { + expect(store.getFilter(userId, filterId)).toBeNull(); + + var filterDefinition = { + event_format: "client" + }; + + httpBackend.when( + "POST", "/user/" + encodeURIComponent(userId) + "/filter" + ).check(function(req) { + expect(req.data).toEqual(filterDefinition); + }).respond(200, { + filter_id: filterId + }); + + client.createFilter(filterDefinition).done(function(gotFilter) { + expect(gotFilter.getDefinition()).toEqual(filterDefinition); + expect(store.getFilter(userId, filterId)).toEqual(gotFilter); + done(); + }); + + httpBackend.flush(); + }); + }); + describe("searching", function() { var response = {