You've already forked matrix-js-sdk
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:
@@ -88,6 +88,15 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _registerEventHandlers(crypto, eventEmitter) {
|
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(
|
eventEmitter.on(
|
||||||
"RoomMember.membership",
|
"RoomMember.membership",
|
||||||
crypto._onRoomMembership.bind(crypto)
|
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
|
* Handle a key event
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -62,6 +62,22 @@ WebStorageSessionStore.prototype = {
|
|||||||
return this.store.getItem(KEY_END_TO_END_ACCOUNT);
|
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.
|
* Stores the known devices for a user.
|
||||||
* @param {string} userId The user's ID.
|
* @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_ACCOUNT = E2E_PREFIX + "account";
|
||||||
|
var KEY_END_TO_END_ANNOUNCED = E2E_PREFIX + "announced";
|
||||||
|
|
||||||
function keyEndToEndDevicesForUser(userId) {
|
function keyEndToEndDevicesForUser(userId) {
|
||||||
return E2E_PREFIX + "devices/" + userId;
|
return E2E_PREFIX + "devices/" + userId;
|
||||||
|
|||||||
Reference in New Issue
Block a user