1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Send an "oh hai" message to other e2e users

When we first complete an initial sync on a new device, send out an
m.new_device message for each user we share an e2e room with
This commit is contained in:
Richard van der Hoff
2016-09-08 07:30:48 +01:00
parent cacafb461d
commit 879da47f0e
2 changed files with 93 additions and 0 deletions

View File

@@ -88,6 +88,15 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
}
function _registerEventHandlers(crypto, eventEmitter) {
eventEmitter.on("sync", function(syncState, oldState, data) {
if (syncState == "PREPARED") {
// XXX ugh. we're assuming the eventEmitter is a MatrixClient.
// how can we avoid doing so?
var rooms = eventEmitter.getRooms();
crypto._onInitialSyncCompleted(rooms);
}
});
eventEmitter.on(
"RoomMember.membership",
crypto._onRoomMembership.bind(crypto)
@@ -815,6 +824,73 @@ Crypto.prototype._onCryptoEvent = function(event) {
}
};
/**
* handle the completion of the initial sync.
*
* Announces the new device.
*
* @private
* @param {module:models/room[]} rooms list of rooms the client knows about
*/
Crypto.prototype._onInitialSyncCompleted = function(rooms) {
if (this._sessionStore.getDeviceAnnounced()) {
return;
}
// we need to tell all the devices in all the rooms we are members of that
// we have arrived.
// build a list of rooms for each user.
var roomsByUser = {};
for (var i = 0; i < rooms.length; i++) {
var room = rooms[i];
// check for rooms with encryption enabled
var alg = this._roomAlgorithms[room.roomId];
if (!alg) {
continue;
}
// ignore any rooms which we have left
var me = room.getMember(this._userId);
if (!me || (
me.membership !== "join" && me.membership !== "invite"
)) {
continue;
}
var members = room.getJoinedMembers();
for (var j = 0; j < members.length; j++) {
var m = members[j];
if (!roomsByUser[m.userId]) {
roomsByUser[m.userId] = [];
}
roomsByUser[m.userId].push(room.roomId);
}
}
// build a per-device message for each user
var content = {};
for (var userId in roomsByUser) {
if (!roomsByUser.hasOwnProperty(userId)) {
continue;
}
content[userId] = {
"*": {
device_id: this._deviceId,
rooms: roomsByUser[userId],
},
};
}
var self = this;
this._baseApis.sendToDevice(
"m.new_device", // OH HAI!
content
).done(function() {
self._sessionStore.setDeviceAnnounced();
});
};
/**
* Handle a key event
*

View File

@@ -62,6 +62,22 @@ WebStorageSessionStore.prototype = {
return this.store.getItem(KEY_END_TO_END_ACCOUNT);
},
/**
* Store a flag indicating that we have announced the new device.
*/
setDeviceAnnounced: function() {
this.store.setItem(KEY_END_TO_END_ANNOUNCED, "true");
},
/**
* Check if the "device announced" flag is set
*
* @return {boolean} true if the "device announced" flag has been set.
*/
getDeviceAnnounced: function() {
return this.store.getItem(KEY_END_TO_END_ANNOUNCED) == "true";
},
/**
* Stores the known devices for a user.
* @param {string} userId The user's ID.
@@ -134,6 +150,7 @@ WebStorageSessionStore.prototype = {
};
var KEY_END_TO_END_ACCOUNT = E2E_PREFIX + "account";
var KEY_END_TO_END_ANNOUNCED = E2E_PREFIX + "announced";
function keyEndToEndDevicesForUser(userId) {
return E2E_PREFIX + "devices/" + userId;