diff options
author | Tobias Fella <fella@posteo.de> | 2022-03-08 22:35:56 +0100 |
---|---|---|
committer | Tobias Fella <fella@posteo.de> | 2022-03-08 22:35:56 +0100 |
commit | 01f7641292eb5dff236f434d7140259c429521ca (patch) | |
tree | cc5a94463a918e399887bf006741b08397af5945 | |
parent | ce32dc09a9257ce5cb3188879f549d9b2fa816a1 (diff) | |
download | libquotient-01f7641292eb5dff236f434d7140259c429521ca.tar.gz libquotient-01f7641292eb5dff236f434d7140259c429521ca.zip |
Store time of last decrypted message for each olm session
Is required to correctly choose a session to use for sending messages
-rw-r--r-- | lib/connection.cpp | 4 | ||||
-rw-r--r-- | lib/database.cpp | 16 | ||||
-rw-r--r-- | lib/database.h | 3 |
3 files changed, 19 insertions, 4 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index 11c81edf..506a2bc0 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -216,7 +216,7 @@ public: qCWarning(E2EE) << "Failed to pickle olm session. Error" << std::get<QOlmError>(pickleResult); return; } - q->database()->saveOlmSession(senderKey, session->sessionId(), std::get<QByteArray>(pickleResult)); + q->database()->saveOlmSession(senderKey, session->sessionId(), std::get<QByteArray>(pickleResult), QDateTime::currentDateTime()); } QString sessionDecryptPrekey(const QOlmMessage& message, const QString &senderKey, std::unique_ptr<QOlmAccount>& olmAccount) { @@ -227,6 +227,7 @@ public: qCDebug(E2EE) << "Found inbound session"; const auto result = session->decrypt(message); if(std::holds_alternative<QString>(result)) { + q->database()->setOlmSessionLastReceived(QString(session->sessionId()), QDateTime::currentDateTime()); return std::get<QString>(result); } else { qCDebug(E2EE) << "Failed to decrypt prekey message"; @@ -261,6 +262,7 @@ public: for(auto& session : olmSessions[senderKey]) { const auto result = session->decrypt(message); if(std::holds_alternative<QString>(result)) { + q->database()->setOlmSessionLastReceived(QString(session->sessionId()), QDateTime::currentDateTime()); return std::get<QString>(result); } } diff --git a/lib/database.cpp b/lib/database.cpp index d070e1de..21e79d58 100644 --- a/lib/database.cpp +++ b/lib/database.cpp @@ -100,6 +100,7 @@ void Database::migrateTo2() qCDebug(DATABASE) << "Migrating database to version 2"; transaction(); execute(QStringLiteral("ALTER TABLE inbound_megolm_sessions ADD ed25519Key TEXT")); + execute(QStringLiteral("ALTER TABLE olm_sessions ADD lastReceived TEXT")); execute(QStringLiteral("PRAGMA user_version = 2;")); commit(); } @@ -141,12 +142,13 @@ void Database::clear() } -void Database::saveOlmSession(const QString& senderKey, const QString& sessionId, const QByteArray &pickle) +void Database::saveOlmSession(const QString& senderKey, const QString& sessionId, const QByteArray &pickle, const QDateTime& timestamp) { - auto query = prepareQuery(QStringLiteral("INSERT INTO olm_sessions(senderKey, sessionId, pickle) VALUES(:senderKey, :sessionId, :pickle);")); + auto query = prepareQuery(QStringLiteral("INSERT INTO olm_sessions(senderKey, sessionId, pickle, lastReceived) VALUES(:senderKey, :sessionId, :pickle, :lastReceived);")); query.bindValue(":senderKey", senderKey); query.bindValue(":sessionId", sessionId); query.bindValue(":pickle", pickle); + query.bindValue(":lastReceived", timestamp); transaction(); execute(query); commit(); @@ -253,3 +255,13 @@ void Database::clearRoomData(const QString& roomId) execute(query3); commit(); } + +void Database::setOlmSessionLastReceived(const QString& sessionId, const QDateTime& timestamp) +{ + auto query = prepareQuery(QStringLiteral("UPDATE olm_sessions SET lastReceived=:lastReceived WHERE sessionId=:sessionId;")); + query.bindValue(":lastReceived", timestamp); + query.bindValue(":sessionId", sessionId); + transaction(); + execute(query); + commit(); +} diff --git a/lib/database.h b/lib/database.h index f9a8df1a..cf241dbc 100644 --- a/lib/database.h +++ b/lib/database.h @@ -27,13 +27,14 @@ public: QByteArray accountPickle(); void setAccountPickle(const QByteArray &pickle); void clear(); - void saveOlmSession(const QString& senderKey, const QString& sessionId, const QByteArray &pickle); + void saveOlmSession(const QString& senderKey, const QString& sessionId, const QByteArray &pickle, const QDateTime& timestamp); UnorderedMap<QString, std::vector<QOlmSessionPtr>> loadOlmSessions(const PicklingMode& picklingMode); UnorderedMap<std::pair<QString, QString>, QOlmInboundGroupSessionPtr> loadMegolmSessions(const QString& roomId, const PicklingMode& picklingMode); void saveMegolmSession(const QString& roomId, const QString& senderKey, const QString& sessionKey, const QString& ed25519Key, const QByteArray& pickle); void addGroupSessionIndexRecord(const QString& roomId, const QString& sessionId, uint32_t index, const QString& eventId, qint64 ts); std::pair<QString, qint64> groupSessionIndexRecord(const QString& roomId, const QString& sessionId, qint64 index); void clearRoomData(const QString& roomId); + void setOlmSessionLastReceived(const QString& sessionId, const QDateTime& timestamp); private: void migrateTo1(); |