Skip to content

Commit

Permalink
Use the last olm session that got a message
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Nov 1, 2018
1 parent 62b2c07 commit 28540ad
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/crypto/OlmDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,20 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK
* @return {Promise<?string>} session id, or null if no established session
*/
OlmDevice.prototype.getSessionIdForDevice = async function(theirDeviceIdentityKey) {
const sessionIds = await this.getSessionIdsForDevice(theirDeviceIdentityKey);
if (sessionIds.length === 0) {
const sessionInfos = await this.getSessionInfoForDevice(theirDeviceIdentityKey);
if (sessionInfos.length === 0) {
return null;
}
// Use the session with the lowest ID.
sessionIds.sort();
return sessionIds[0];
// Use the session that has most recently received a message
sessionInfos.sort((a, b) => {
if (a.lastReceivedMessageTs !== b.lastReceivedMessageTs) {
return a.lastReceivedMessageTs - b.lastReceivedMessageTs;
} else {
if (a.sessionId === b.sessionId) return 0;
return a.sessionId < b.sessionId ? -1 : 1;
}
});
return sessionInfos[sessionInfos.length - 1].sessionId;
};

/**
Expand All @@ -589,6 +596,7 @@ OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey)
for (const sessionId of sessionIds) {
this._unpickleSession(sessions[sessionId], (session) => {
info.push({
lastReceivedMessageTs: session.last_received_message_ts(),
hasReceivedMessage: session.has_received_message(),
sessionId: sessionId,
});
Expand Down Expand Up @@ -649,6 +657,7 @@ OlmDevice.prototype.decryptMessage = async function(
(txn) => {
this._getSession(theirDeviceIdentityKey, sessionId, txn, (session) => {
payloadString = session.decrypt(messageType, ciphertext);
session.set_last_received_message_ts(Date.now());
this._saveSession(theirDeviceIdentityKey, session, txn);
});
},
Expand Down

0 comments on commit 28540ad

Please sign in to comment.