-
-
Notifications
You must be signed in to change notification settings - Fork 580
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
Changes from 3 commits
28540ad
37f0a9a
2a6a67c
fcadf6e
e17a39d
a30845f
5bc68c0
408407b
3c85bd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
session.set_last_received_message_ts(Date.now()); | ||
|
||
this._saveSession(theirDeviceIdentityKey, session, txn); | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
}; | ||
|
||
/** | ||
|
@@ -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, | ||
}); | ||
|
@@ -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); | ||
}); | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/an/a/