Fetch the user's push rules before any events arrive, so we can annotate events with push data when they arrive.

This commit is contained in:
David Baker
2015-06-26 15:11:27 +01:00
parent 20b5553e48
commit 9ea080b7bb
5 changed files with 99 additions and 87 deletions
+89 -74
View File
@@ -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.
@@ -18,6 +18,7 @@ describe("MatrixClient events", function() {
userId: selfUserId,
accessToken: selfAccessToken
});
httpBackend.when("GET", "/pushrules").respond(200, {});
});
afterEach(function() {
+4 -1
View File
@@ -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(
+4 -12
View File
@@ -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);
});
});
+1
View File
@@ -19,6 +19,7 @@ describe("MatrixClient syncing", function() {
userId: selfUserId,
accessToken: selfAccessToken
});
httpBackend.when("GET", "/pushrules").respond(200, {});
});
afterEach(function() {