Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the last olm session that got a message #776

Merged
merged 9 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- node # Latest stable version of nodejs.
- "10.11.0"
script:
- ./travis.sh
21 changes: 16 additions & 5 deletions src/crypto/OlmDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ OlmDevice.prototype.createInboundSession = async function(
this._storeAccount(txn, account);

const payloadString = session.decrypt(messageType, ciphertext);
// this counts as an received message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/an/a/

session.set_last_received_message_ts(Date.now());

this._saveSession(theirDeviceIdentityKey, session, txn);

Expand Down Expand Up @@ -558,13 +560,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looping through the sessionInfos array, and keeping track of the sessionId with the smallest timestamp/sessionId is approximately the same number of lines of code, and faster (O(n) rather than O(n log n)). i.e.

let idxOfMin = 0;
for (i = 1; i < sessionInfos.length; i++) {
  if (sessionInfos[i].lastReceivedMessageTs < sessionInfos[idxOfMin].lastReceiveMessageTs
      || (sessionInfos[i].lastReceivedMessageTs === sessionInfos[idxOfMin].lastReceiveMessageTs
          && sessionInfos[i].sessionId < sessinInfos[idxOfMin].sessionId)) {
    idxOfMin = i;
  }
return sessionInfos[idxOfMin].sessionId;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I was reluctant to bother changing it since it was already a sort, but fair enough

};

/**
Expand All @@ -589,6 +598,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 +659,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