From 9ea080b7bbd9122d4b17949aca22626f2fa29cdb Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 26 Jun 2015 15:11:27 +0100 Subject: [PATCH] Fetch the user's push rules before any events arrive, so we can annotate events with push data when they arrive. --- lib/client.js | 163 ++++++++++-------- .../integ/matrix-client-event-emitter.spec.js | 1 + spec/integ/matrix-client-opts.spec.js | 5 +- .../integ/matrix-client-room-timeline.spec.js | 16 +- spec/integ/matrix-client-syncing.spec.js | 1 + 5 files changed, 99 insertions(+), 87 deletions(-) diff --git a/lib/client.js b/lib/client.js index 988b1dc71..3b2fbba55 100644 --- a/lib/client.js +++ b/lib/client.js @@ -973,6 +973,91 @@ MatrixClient.prototype.isLoggedIn = function() { // due to ambiguity (or should this be on a chat-specific layer)? // reconnect after connectivity outages + +/** + * This is an internal method. + * @param {MatrixClient} client + * @param {integer} client + */ +function doInitialSync(client, historyLen) { + client._http.authedRequest( + undefined, "GET", "/initialSync", { limit: (historyLen || 12) } + ).done(function(data) { + var i, j; + // intercept the results and put them into our store + if (!(client.store instanceof StubStore)) { + utils.forEach(utils.map(data.presence, _PojoToMatrixEventMapper), + function(e) { + var user = createNewUser(client, e.getContent().user_id); + user.setPresenceEvent(e); + client.store.storeUser(user); + }); + for (i = 0; i < data.rooms.length; i++) { + var room = createNewRoom(client, data.rooms[i].room_id); + if (!data.rooms[i].state) { + data.rooms[i].state = []; + } + if (data.rooms[i].membership === "invite") { + // create fake invite state event (v1 sucks) + data.rooms[i].state.push({ + event_id: "$fake_" + room.roomId, + content: { + membership: "invite" + }, + state_key: client.credentials.userId, + user_id: data.rooms[i].inviter, + room_id: room.roomId, + type: "m.room.member" + }); + } + + _processRoomEvents( + room, data.rooms[i].state, data.rooms[i].messages + ); + + // cache the name/summary/etc prior to storage since we don't + // know how the store will serialise the Room. + room.recalculate(client.credentials.userId); + + client.store.storeRoom(room); + client.emit("Room", room); + } + } + + if (data) { + client.fromToken = data.end; + var events = []; + for (i = 0; i < data.presence.length; i++) { + events.push(new MatrixEvent(data.presence[i])); + } + for (i = 0; i < data.rooms.length; i++) { + if (data.rooms[i].state) { + for (j = 0; j < data.rooms[i].state.length; j++) { + events.push(new MatrixEvent(data.rooms[i].state[j])); + } + } + if (data.rooms[i].messages) { + for (j = 0; j < data.rooms[i].messages.chunk.length; j++) { + events.push( + new MatrixEvent(data.rooms[i].messages.chunk[j]) + ); + } + } + } + utils.forEach(events, function(e) { + client.emit("event", e); + }); + } + + client.clientRunning = true; + client.emit("syncComplete"); + _pollForEvents(client); + }, function(err) { + client.emit("syncError", err); + // TODO: Retries. + }); +}; + /** * High level helper method to call initialSync, emit the resulting events, * and then start polling the eventStream for new events. To listen for these @@ -993,83 +1078,13 @@ MatrixClient.prototype.startClient = function(historyLen) { } var self = this; - this._http.authedRequest( - undefined, "GET", "/initialSync", { limit: (historyLen || 12) } - ).done(function(data) { - var i, j; - // intercept the results and put them into our store - if (!(self.store instanceof StubStore)) { - utils.forEach(utils.map(data.presence, _PojoToMatrixEventMapper), - function(e) { - var user = createNewUser(self, e.getContent().user_id); - user.setPresenceEvent(e); - self.store.storeUser(user); - }); - for (i = 0; i < data.rooms.length; i++) { - var room = createNewRoom(self, data.rooms[i].room_id); - if (!data.rooms[i].state) { - data.rooms[i].state = []; - } - if (data.rooms[i].membership === "invite") { - // create fake invite state event (v1 sucks) - data.rooms[i].state.push({ - event_id: "$fake_" + room.roomId, - content: { - membership: "invite" - }, - state_key: self.credentials.userId, - user_id: data.rooms[i].inviter, - room_id: room.roomId, - type: "m.room.member" - }); - } - - _processRoomEvents( - room, data.rooms[i].state, data.rooms[i].messages - ); - - // cache the name/summary/etc prior to storage since we don't - // know how the store will serialise the Room. - room.recalculate(self.credentials.userId); - - self.store.storeRoom(room); - self.emit("Room", room); - } - } - - if (data) { - self.fromToken = data.end; - var events = []; - for (i = 0; i < data.presence.length; i++) { - events.push(new MatrixEvent(data.presence[i])); - } - for (i = 0; i < data.rooms.length; i++) { - if (data.rooms[i].state) { - for (j = 0; j < data.rooms[i].state.length; j++) { - events.push(new MatrixEvent(data.rooms[i].state[j])); - } - } - if (data.rooms[i].messages) { - for (j = 0; j < data.rooms[i].messages.chunk.length; j++) { - events.push( - new MatrixEvent(data.rooms[i].messages.chunk[j]) - ); - } - } - } - utils.forEach(events, function(e) { - self.emit("event", e); - }); - } - - self.clientRunning = true; - self.emit("syncComplete"); - _pollForEvents(self); + this.pushRules().done(function(result) { + self.pushRules = result; + doInitialSync(self, historyLen); }, function(err) { self.emit("syncError", err); - // TODO: Retries. }); -}; +} /** * This is an internal method. diff --git a/spec/integ/matrix-client-event-emitter.spec.js b/spec/integ/matrix-client-event-emitter.spec.js index 612a3d04e..a6c04f36f 100644 --- a/spec/integ/matrix-client-event-emitter.spec.js +++ b/spec/integ/matrix-client-event-emitter.spec.js @@ -18,6 +18,7 @@ describe("MatrixClient events", function() { userId: selfUserId, accessToken: selfAccessToken }); + httpBackend.when("GET", "/pushrules").respond(200, {}); }); afterEach(function() { diff --git a/spec/integ/matrix-client-opts.spec.js b/spec/integ/matrix-client-opts.spec.js index fdcb7bf60..d8c010c78 100644 --- a/spec/integ/matrix-client-opts.spec.js +++ b/spec/integ/matrix-client-opts.spec.js @@ -100,10 +100,13 @@ describe("MatrixClient opts", function() { expectedEventTypes.indexOf(event.getType()), 1 ); }); + httpBackend.when("GET", "/pushrules").respond(200, {}); httpBackend.when("GET", "/initialSync").respond(200, initialSync); httpBackend.when("GET", "/events").respond(200, eventData); client.startClient(); - httpBackend.flush("/initialSync", 1).then(function() { + httpBackend.flush("/pushrules", 1).then(function() { + return httpBackend.flush("/initialSync", 1); + }).then(function() { return httpBackend.flush("/events", 1); }).done(function() { expect(expectedEventTypes.length).toEqual( diff --git a/spec/integ/matrix-client-room-timeline.spec.js b/spec/integ/matrix-client-room-timeline.spec.js index 0e4c0061f..4c6697658 100644 --- a/spec/integ/matrix-client-room-timeline.spec.js +++ b/spec/integ/matrix-client-room-timeline.spec.js @@ -51,7 +51,7 @@ describe("MatrixClient room timelines", function() { }] }; - beforeEach(function() { + beforeEach(function(done) { utils.beforeEach(this); httpBackend = new HttpBackend(); sdk.request(httpBackend.requestFn); @@ -65,10 +65,13 @@ describe("MatrixClient room timelines", function() { end: "end_", start: "start_" }; + httpBackend.when("GET", "/pushrules").respond(200, {}); httpBackend.when("GET", "/initialSync").respond(200, initialSync); httpBackend.when("GET", "/events").respond(200, function() { return eventData; }); + client.startClient(); + httpBackend.flush("/pushrules").done(done); }); afterEach(function() { @@ -97,7 +100,6 @@ describe("MatrixClient room timelines", function() { done(); }); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -126,7 +128,6 @@ describe("MatrixClient room timelines", function() { }); httpBackend.flush("/txn1", 1); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -158,7 +159,6 @@ describe("MatrixClient room timelines", function() { }); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); }); @@ -193,7 +193,6 @@ describe("MatrixClient room timelines", function() { httpBackend.flush("/messages", 1); httpBackend.flush("/events", 1); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -236,7 +235,6 @@ describe("MatrixClient room timelines", function() { httpBackend.flush("/messages", 1); httpBackend.flush("/events", 1); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -265,7 +263,6 @@ describe("MatrixClient room timelines", function() { httpBackend.flush("/messages", 1); httpBackend.flush("/events", 1); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -289,7 +286,6 @@ describe("MatrixClient room timelines", function() { httpBackend.flush("/messages", 1); httpBackend.flush("/events", 1); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); }); @@ -323,7 +319,6 @@ describe("MatrixClient room timelines", function() { done(); }); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -345,7 +340,6 @@ describe("MatrixClient room timelines", function() { done(); }); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -383,7 +377,6 @@ describe("MatrixClient room timelines", function() { }); }); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); @@ -413,7 +406,6 @@ describe("MatrixClient room timelines", function() { done(); }); }); - client.startClient(); httpBackend.flush("/initialSync", 1); }); }); diff --git a/spec/integ/matrix-client-syncing.spec.js b/spec/integ/matrix-client-syncing.spec.js index 01e4e3866..a97590a49 100644 --- a/spec/integ/matrix-client-syncing.spec.js +++ b/spec/integ/matrix-client-syncing.spec.js @@ -19,6 +19,7 @@ describe("MatrixClient syncing", function() { userId: selfUserId, accessToken: selfAccessToken }); + httpBackend.when("GET", "/pushrules").respond(200, {}); }); afterEach(function() {