1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Check filters before we reuse them

Make sure that we check the content of existing filters before we blindly reuse
them.

Fixes https://github.com/vector-im/vector-web/issues/988
This commit is contained in:
Richard van der Hoff
2016-02-24 15:23:42 +00:00
parent 670d230f2e
commit 43f392955d

View File

@@ -338,7 +338,6 @@ SyncApi.prototype.sync = function() {
self._getOrCreateFilter(
getFilterName(client.credentials.userId), filter
).done(function(filterId) {
debuglog("Using existing filter ID %s", filterId);
self._sync({ filterId: filterId });
}, function(err) {
self._startKeepAlives().done(function() {
@@ -702,16 +701,42 @@ SyncApi.prototype._pokeKeepAlive = function() {
*/
SyncApi.prototype._getOrCreateFilter = function(filterName, filter) {
var client = this.client;
var filterId = client.store.getFilterIdByName(filterName);
var promise = q();
if (filterId) {
// super, just use that.
return q(filterId);
// check that the existing filter matches our expectations
promise = client.getFilter(client.credentials.userId,
filterId, true
).then(function(existingFilter) {
var oldStr = JSON.stringify(existingFilter.getDefinition());
var newStr = JSON.stringify(filter.getDefinition());
if ( oldStr == newStr ) {
// super, just use that.
debuglog("Using existing filter ID %s: %s", filterId, oldStr);
return q(filterId);
}
debuglog("Existing filter ID %s: %s; new filter: %s",
filterId, oldStr, newStr);
return;
});
}
// create a filter
return client.createFilter(filter.getDefinition()).then(function(createdFilter) {
client.store.setFilterIdByName(filterName, createdFilter.filterId);
return createdFilter.filterId;
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;
});
});
};