diff options
author | Tobias Fella <fella@posteo.de> | 2022-03-06 22:54:01 +0100 |
---|---|---|
committer | Tobias Fella <fella@posteo.de> | 2022-05-16 20:47:17 +0200 |
commit | 6f5ac9b7315d75692960e5eac7b1eb6867c0d203 (patch) | |
tree | f20f68dbf9ff687f212c7e074802c80370ef53dd /lib/database.cpp | |
parent | 3eb7ad8b0a1ac0f6f9cda679108937a01268f184 (diff) | |
download | libquotient-6f5ac9b7315d75692960e5eac7b1eb6867c0d203.tar.gz libquotient-6f5ac9b7315d75692960e5eac7b1eb6867c0d203.zip |
Keep log of where we send keys and send keys to new devices and users
Diffstat (limited to 'lib/database.cpp')
-rw-r--r-- | lib/database.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/database.cpp b/lib/database.cpp index 8cb3a9d1..4a28fd4c 100644 --- a/lib/database.cpp +++ b/lib/database.cpp @@ -13,6 +13,9 @@ #include "e2ee/e2ee.h" #include "e2ee/qolmsession.h" #include "e2ee/qolminboundsession.h" +#include "connection.h" +#include "user.h" +#include "room.h" using namespace Quotient; Database::Database(const QString& matrixId, const QString& deviceId, QObject* parent) @@ -91,6 +94,7 @@ void Database::migrateTo1() execute(QStringLiteral("CREATE TABLE tracked_users (matrixId TEXT);")); execute(QStringLiteral("CREATE TABLE outdated_users (matrixId TEXT);")); execute(QStringLiteral("CREATE TABLE tracked_devices (matrixId TEXT, deviceId TEXT, curveKeyId TEXT, curveKey TEXT, edKeyId TEXT, edKey TEXT);")); + execute(QStringLiteral("CREATE TABLE sent_megolm_sessions (roomId TEXT, userId TEXT, deviceId TEXT, identityKey TEXT, sessionId TEXT, i INTEGER);")); execute(QStringLiteral("PRAGMA user_version = 1;")); commit(); @@ -331,3 +335,43 @@ QOlmOutboundGroupSessionPtr Database::loadCurrentOutboundMegolmSession(const QSt } return nullptr; } + +void Database::setDevicesReceivedKey(const QString& roomId, QHash<User *, QStringList> devices, const QString& sessionId, int index) +{ + //TODO this better + auto connection = dynamic_cast<Connection *>(parent()); + transaction(); + for (const auto& user : devices.keys()) { + for (const auto& device : devices[user]) { + auto query = prepareQuery(QStringLiteral("INSERT INTO sent_megolm_sessions(roomId, userId, deviceId, identityKey, sessionId, i) VALUES(:roomId, :userId, :deviceId, :identityKey, :sessionId, :i);")); + query.bindValue(":roomId", roomId); + query.bindValue(":userId", user->id()); + query.bindValue(":deviceId", device); + query.bindValue(":identityKey", connection->curveKeyForUserDevice(user->id(), device)); + query.bindValue(":sessionId", sessionId); + query.bindValue(":i", index); + execute(query); + } + } + commit(); +} + +QHash<QString, QStringList> Database::devicesWithoutKey(Room* room, const QString &sessionId) +{ + auto connection = dynamic_cast<Connection *>(parent()); + QHash<QString, QStringList> devices; + for (const auto& user : room->users()) {//TODO does this include invited & left? + devices[user->id()] = connection->devicesForUser(user); + } + + auto query = prepareQuery(QStringLiteral("SELECT userId, deviceId FROM sent_megolm_sessions WHERE roomId=:roomId AND sessionId=:sessionId")); + query.bindValue(":roomId", room->id()); + query.bindValue(":sessionId", sessionId); + transaction(); + execute(query); + commit(); + while (query.next()) { + devices[query.value("userId").toString()].removeAll(query.value("deviceId").toString()); + } + return devices; +} |