You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2026-01-03 23:22:30 +03:00
Fix sending of oh_hais on bad sessions
Fix a bunch of bugs in the code which tried to send an oh_hai message when we got a message with an unknown megolm session.
This commit is contained in:
@@ -86,6 +86,7 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
|
||||
|
||||
_registerEventHandlers(this, eventEmitter);
|
||||
|
||||
// map from userId -> deviceId -> roomId -> timestamp
|
||||
this._lastNewDeviceMessageTsByUserDeviceRoom = {};
|
||||
}
|
||||
|
||||
@@ -872,13 +873,21 @@ Crypto.prototype.decryptEvent = function(event) {
|
||||
payload.keysProved = r.keysProved;
|
||||
return payload;
|
||||
} else {
|
||||
// We've got a message for a session we don't have.
|
||||
// Maybe the sender forgot to tell us about the session.
|
||||
// Remind the sender that we exist so that they might
|
||||
// tell us about the sender.
|
||||
if (event.getRoomId !== undefined && event.getSender !== undefined) {
|
||||
// We've got a message for a session we don't have. Maybe the sender
|
||||
// forgot to tell us about the session. Remind the sender that we
|
||||
// exist so that they might tell us about the session on their next
|
||||
// send.
|
||||
//
|
||||
// (Alternatively, it might be that we are just looking at
|
||||
// scrollback... at least we rate-limit the m.new_device events :/)
|
||||
//
|
||||
// XXX: this is a band-aid which masks symptoms of other bugs. It would
|
||||
// be nice to get rid of it.
|
||||
if (event.room_id !== undefined && event.sender !== undefined &&
|
||||
event.content.device_id !== undefined
|
||||
) {
|
||||
this._sendPingToDevice(
|
||||
event.getSender(), event.content.device, event.getRoomId
|
||||
event.sender, event.content.device_id, event.room_id
|
||||
);
|
||||
}
|
||||
|
||||
@@ -897,34 +906,40 @@ Crypto.prototype.decryptEvent = function(event) {
|
||||
* @param {string} roomId The ID of the room we want to remind them about.
|
||||
*/
|
||||
Crypto.prototype._sendPingToDevice = function(userId, deviceId, roomId) {
|
||||
if (deviceId === undefined) {
|
||||
deviceId = "*";
|
||||
var lastMessageTsMap = this._lastNewDeviceMessageTsByUserDeviceRoom;
|
||||
|
||||
var lastTsByDevice = lastMessageTsMap[userId];
|
||||
if (!lastTsByDevice) {
|
||||
lastTsByDevice = lastMessageTsMap[userId] = {};
|
||||
}
|
||||
|
||||
var lastTsByRoom = lastTsByDevice[deviceId];
|
||||
if (!lastTsByRoom) {
|
||||
lastTsByRoom = lastTsByDevice[deviceId] = {};
|
||||
}
|
||||
|
||||
var lastMessageTsMap = this._lastNewDeviceMessageTsByUserDeviceRoom;
|
||||
var lastTsByDevice = lastMessageTsMap[userId] || {};
|
||||
var lastTsByRoom = lastTsByDevice[deviceId] || {};
|
||||
var lastTs = lastTsByRoom[roomId];
|
||||
var timeNowMs = Date.now();
|
||||
var oneHourMs = 1000 * 60 * 60;
|
||||
|
||||
if (lastTs === undefined || lastTs + oneHourMs < timeNowMs) {
|
||||
var content = {
|
||||
userId: {
|
||||
deviceId: {
|
||||
device_id: this._deviceId,
|
||||
rooms: [roomId],
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
lastTsByRoom[roomId] = timeNowMs;
|
||||
|
||||
this._baseApis.sendToDevice(
|
||||
"m.new_device", // OH HAI!
|
||||
content
|
||||
).done(function() {});
|
||||
if (lastTs !== undefined && lastTs + oneHourMs > timeNowMs) {
|
||||
// rate-limiting
|
||||
return;
|
||||
}
|
||||
|
||||
var content = {};
|
||||
content[userId] = {};
|
||||
content[userId][deviceId] = {
|
||||
device_id: this._deviceId,
|
||||
rooms: [roomId],
|
||||
};
|
||||
|
||||
this._baseApis.sendToDevice(
|
||||
"m.new_device", // OH HAI!
|
||||
content
|
||||
).done();
|
||||
|
||||
lastTsByRoom[roomId] = timeNowMs;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user