Skip to content

Commit

Permalink
Fix sync breaking when an invalid filterId is in localStorage
Browse files Browse the repository at this point in the history
 * if getFilter fails for a filterId, null out the localStorage id and
   redirect to the createFilter path
 * add spec
 * fix unit/matrix-client.spec.js http response not matching synapse
  • Loading branch information
pik committed Oct 9, 2016
1 parent 3a17ef9 commit 988e32a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,16 @@ MatrixClient.prototype.getOrCreateFilter = function(filterName, filter) {
// debuglog("Existing filter ID %s: %s; new filter: %s",
// filterId, JSON.stringify(oldDef), JSON.stringify(newDef));
return;
}, function(error) {
// {errcode: "M_UNKNOWN", name: "M_UNKNOWN", message: "No row found", data: Object, httpStatus: 404}
if (error.httpStatus === 404 && (error.errcode === "M_UNKNOWN" || error.errcode === "M_NOT_FOUND")) {
// Clear existing filterId if the localStorage value is out of sync with the server
self.store.setFilterIdByName(filterName, null);
// Return a null value for existingId further down the promise chain
return null;
} else {
throw error;
}
});
}

Expand Down
33 changes: 33 additions & 0 deletions spec/unit/matrix-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ describe("MatrixClient", function() {
if (next.error) {
return q.reject({
errcode: next.error.errcode,
httpStatus: next.error.httpStatus,
name: next.error.errcode,
message: "Expected testing error",
data: next.error
Expand Down Expand Up @@ -204,6 +205,38 @@ describe("MatrixClient", function() {
});
});

describe("getOrCreateFilter", function() {
it("should POST createFilter if no id is present in localStorage", function() {
});
it("should use an existing filter if id is present in localStorage", function() {
});
it("should handle localStorage filterId missing from the server", function(done) {
function getFilterName(userId, suffix) {
// scope this on the user ID because people may login on many accounts
// and they all need to be stored!
return "FILTER_SYNC_" + userId + (suffix ? "_" + suffix : "");
}
var invalidFilterId = 'invalidF1lt3r';
httpLookups = [];
httpLookups.push({
method: "GET",
path: FILTER_PATH + '/' + invalidFilterId,
error: { errcode: "M_UNKNOWN", name: "M_UNKNOWN", message: "No row found", data: Object, httpStatus: 404 }
});
httpLookups.push(FILTER_RESPONSE);
store.getFilterIdByName.andReturn(invalidFilterId);

var filterName = getFilterName(client.credentials.userId);
client.store.setFilterIdByName(filterName, invalidFilterId);
var filter = new sdk.Filter(client.credentials.userId);

client.getOrCreateFilter(filterName, filter).then(function (filterId) {
expect(filterId).toEqual(FILTER_RESPONSE.data.filter_id);
done();
});
});
});

describe("retryImmediately", function() {
it("should return false if there is no request waiting", function() {
client.startClient();
Expand Down

0 comments on commit 988e32a

Please sign in to comment.