1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-30 04:23:07 +03:00

Sync room state when another device joins a room.

This commit is contained in:
Kegan Dougal
2015-06-23 10:18:13 +01:00
parent bc0e2ad504
commit ef77c6f989
2 changed files with 17 additions and 7 deletions

View File

@ -23,7 +23,7 @@ var rl = readline.createInterface({
terminal: false terminal: false
}); });
rl.on('line', function(line) { rl.on('line', function(line) {
if (line.indexOf("/enter ") === 0 && !viewingRoom) { if (line.indexOf("/join ") === 0 && !viewingRoom) {
var roomIndex = line.split(" ")[1]; var roomIndex = line.split(" ")[1];
viewingRoom = roomList[roomIndex]; viewingRoom = roomList[roomIndex];
if (viewingRoom.getMember(myUserId).membership === "invite") { if (viewingRoom.getMember(myUserId).membership === "invite") {
@ -103,7 +103,7 @@ function printHelp() {
console.log("Global commands:"); console.log("Global commands:");
console.log(" '/help' : Show this help."); console.log(" '/help' : Show this help.");
console.log("Room list index commands:"); console.log("Room list index commands:");
console.log(" '/enter <index>' Enter a room, e.g. '/enter 5'"); console.log(" '/join <index>' Join a room, e.g. '/join 5'");
console.log("Room commands:"); console.log("Room commands:");
console.log(" '/exit' Return to the room list index."); console.log(" '/exit' Return to the room list index.");
console.log(" '/members' Show the room member list."); console.log(" '/members' Show the room member list.");

View File

@ -67,6 +67,9 @@ function MatrixClient(opts) {
userId: (opts.userId || null) userId: (opts.userId || null)
}; };
this._http = new httpApi.MatrixHttpApi(httpOpts); this._http = new httpApi.MatrixHttpApi(httpOpts);
this._syncingRooms = {
// room_id: Promise
};
} }
utils.inherits(MatrixClient, EventEmitter); utils.inherits(MatrixClient, EventEmitter);
@ -1084,9 +1087,9 @@ function _pollForEvents(client) {
isBrandNewRoom = true; isBrandNewRoom = true;
} }
//var wasJoined = room.hasMembershipState( var wasJoined = room.hasMembershipState(
// self.credentials.userId, "join" self.credentials.userId, "join"
//); );
room.addEvents(roomIdToEvents[roomId], "replace"); room.addEvents(roomIdToEvents[roomId], "replace");
room.recalculate(self.credentials.userId); room.recalculate(self.credentials.userId);
@ -1097,7 +1100,7 @@ function _pollForEvents(client) {
self.store.storeRoom(room); self.store.storeRoom(room);
self.emit("Room", room); self.emit("Room", room);
} }
/* TODO invite->join trigger roominitialsync
var justJoined = room.hasMembershipState( var justJoined = room.hasMembershipState(
self.credentials.userId, "join" self.credentials.userId, "join"
); );
@ -1106,7 +1109,7 @@ function _pollForEvents(client) {
// we've just transitioned into a join state for this room, // we've just transitioned into a join state for this room,
// so sync state. // so sync state.
_syncRoom(self, room); _syncRoom(self, room);
} */ }
}); });
} }
if (data) { if (data) {
@ -1127,15 +1130,22 @@ function _pollForEvents(client) {
} }
function _syncRoom(client, room) { function _syncRoom(client, room) {
if (client._syncingRooms[room.roomId]) {
return client._syncingRooms[room.roomId];
}
var defer = q.defer(); var defer = q.defer();
client._syncingRooms[room.roomId] = defer.promise;
client.roomInitialSync(room.roomId, 8).done(function(res) { client.roomInitialSync(room.roomId, 8).done(function(res) {
room.timeline = []; // blow away any previous messages.
_processRoomEvents(room, res.state, res.messages); _processRoomEvents(room, res.state, res.messages);
room.recalculate(client.credentials.userId); room.recalculate(client.credentials.userId);
client.store.storeRoom(room); client.store.storeRoom(room);
client.emit("Room", room); client.emit("Room", room);
defer.resolve(room); defer.resolve(room);
client._syncingRooms[room.roomId] = undefined;
}, function(err) { }, function(err) {
defer.reject(err); defer.reject(err);
client._syncingRooms[room.roomId] = undefined;
}); });
return defer.promise; return defer.promise;
} }