1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

move getOrCreateClient from sync.js to client.js

This commit is contained in:
Matthew Hodgson
2016-09-05 02:44:24 +01:00
parent 2daa1b6007
commit ed5c061566
2 changed files with 50 additions and 49 deletions

View File

@@ -2277,6 +2277,54 @@ MatrixClient.prototype.getFilter = function(userId, filterId, allowCached) {
});
};
/**
* @param {string} filterName
* @param {Filter} filter
* @return {Promise<String>} 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