diff --git a/lib/client.js b/lib/client.js index 84c527201..544fbf9d5 100644 --- a/lib/client.js +++ b/lib/client.js @@ -2277,6 +2277,54 @@ MatrixClient.prototype.getFilter = function(userId, filterId, allowCached) { }); }; +/** + * @param {string} filterName + * @param {Filter} filter + * @return {Promise} Filter ID + */ +MatrixClient.prototype.getOrCreateFilter = function(filterName, filter) { + + var filterId = this.store.getFilterIdByName(filterName); + var promise = q(); + var self = this; + + if (filterId) { + // check that the existing filter matches our expectations + promise = self.getFilter(self.credentials.userId, + filterId, true + ).then(function(existingFilter) { + var oldDef = existingFilter.getDefinition(); + var newDef = filter.getDefinition(); + + if (utils.deepCompare(oldDef, newDef)) { + // super, just use that. + // debuglog("Using existing filter ID %s: %s", filterId, + // JSON.stringify(oldDef)); + return q(filterId); + } + // debuglog("Existing filter ID %s: %s; new filter: %s", + // filterId, JSON.stringify(oldDef), JSON.stringify(newDef)); + return; + }); + } + + return promise.then(function(existingId) { + if (existingId) { + return existingId; + } + + // create a new filter + return self.createFilter(filter.getDefinition() + ).then(function(createdFilter) { + // debuglog("Created new filter ID %s: %s", createdFilter.filterId, + // JSON.stringify(createdFilter.getDefinition())); + self.store.setFilterIdByName(filterName, createdFilter.filterId); + return createdFilter.filterId; + }); + }); +}; + + /** * Gets a bearer token from the Home Server that the user can * present to a third party in order to prove their ownership diff --git a/lib/sync.js b/lib/sync.js index 91bb0cc51..870c83597 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -148,7 +148,7 @@ SyncApi.prototype.syncLeftRooms = function() { timeout: 0 // don't want to block since this is a single isolated req }; - return this._getOrCreateFilter( + return client.getOrCreateFilter( getFilterName(client.credentials.userId, "LEFT_ROOMS"), filter ).then(function(filterId) { qps.filter = filterId; @@ -389,7 +389,7 @@ SyncApi.prototype.sync = function() { var filter = new Filter(client.credentials.userId); filter.setTimelineLimit(self.opts.initialSyncLimit); - self._getOrCreateFilter( + client.getOrCreateFilter( getFilterName(client.credentials.userId), filter ).done(function(filterId) { self._sync({ filterId: filterId }); @@ -837,53 +837,6 @@ SyncApi.prototype._pokeKeepAlive = function() { }); }; -/** - * @param {string} filterName - * @param {Filter} filter - * @return {Promise} Filter ID - */ -SyncApi.prototype._getOrCreateFilter = function(filterName, filter) { - var client = this.client; - - var filterId = client.store.getFilterIdByName(filterName); - var promise = q(); - - if (filterId) { - // check that the existing filter matches our expectations - promise = client.getFilter(client.credentials.userId, - filterId, true - ).then(function(existingFilter) { - var oldDef = existingFilter.getDefinition(); - var newDef = filter.getDefinition(); - - if (utils.deepCompare(oldDef, newDef)) { - // super, just use that. - debuglog("Using existing filter ID %s: %s", filterId, - JSON.stringify(oldDef)); - return q(filterId); - } - debuglog("Existing filter ID %s: %s; new filter: %s", - filterId, JSON.stringify(oldDef), JSON.stringify(newDef)); - return; - }); - } - - return promise.then(function(existingId) { - if (existingId) { - return existingId; - } - - // create a new filter - return client.createFilter(filter.getDefinition() - ).then(function(createdFilter) { - debuglog("Created new filter ID %s: %s", createdFilter.filterId, - JSON.stringify(createdFilter.getDefinition())); - client.store.setFilterIdByName(filterName, createdFilter.filterId); - return createdFilter.filterId; - }); - }); -}; - /** * @param {Object} obj * @return {Object[]}