diff --git a/lib/client.js b/lib/client.js
index bff18efb8..dff82c435 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -1014,6 +1014,7 @@ MatrixClient.prototype.startClient = function(historyLen) {
room.recalculate(self.credentials.userId);
self.store.storeRoom(room);
+ self.emit("Room", room);
}
}
@@ -1088,14 +1089,20 @@ function _pollForEvents(client) {
var roomIds = utils.keys(roomIdToEvents);
for (i = 0; i < roomIds.length; i++) {
var room = self.store.getRoom(roomIds[i]);
+ var shouldStoreRoom = false;
if (!room) {
- // TODO: whine about this. We got an event for a room
- // we don't know about (we should really be doing a
- // roomInitialSync at this point to pull in state).
room = createNewRoom(self, roomIds[i]);
+ shouldStoreRoom = true;
}
room.addEvents(roomIdToEvents[roomIds[i]], "replace");
room.recalculate(self.credentials.userId);
+
+ if (shouldStoreRoom) {
+ self.store.storeRoom(room);
+ // TODO: Should be doing roomInitialSync here to pull in
+ // all state before emitting this(!)
+ self.emit("Room", room);
+ }
}
}
if (data) {
@@ -1152,7 +1159,6 @@ function createNewUser(client, userId) {
function createNewRoom(client, roomId) {
var room = new Room(roomId);
reEmit(client, room, ["Room.name", "Room.timeline"]);
- client.emit("Room", room); // emit created room event
// we need to also re-emit room state and room member events, so hook it up
// to the client now. We need to add a listener for RoomState.members in
@@ -1209,10 +1215,10 @@ module.exports.MatrixClient = MatrixClient;
*/
/**
- * Fires whenever a new Room is created. This Room will not be fully populated
- * yet (e.g. no members). This event is experimental and may change.
+ * Fires whenever a new Room is added. This event is experimental and
+ * may change.
* @event module:client~MatrixClient#"Room"
- * @param {Room} room The newly created room.
+ * @param {Room} room The newly created, fully populated room.
* @example
* matrixClient.on("Room", function(room){
* var roomId = room.roomId;