aboutsummaryrefslogtreecommitdiff
path: root/lib/database.cpp
diff options
context:
space:
mode:
authorTobias Fella <fella@posteo.de>2022-03-06 22:54:01 +0100
committerTobias Fella <fella@posteo.de>2022-03-10 20:03:31 +0100
commit0ef080bdfa3a8a64d1faadf4a11a8b9dbb5bc055 (patch)
treeb808ebef49612bf6719fe463477a1d79cdd11c04 /lib/database.cpp
parent30004d4e0d6aca06491777f77ed966a451eff50f (diff)
downloadlibquotient-0ef080bdfa3a8a64d1faadf4a11a8b9dbb5bc055.tar.gz
libquotient-0ef080bdfa3a8a64d1faadf4a11a8b9dbb5bc055.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.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/database.cpp b/lib/database.cpp
index ad653073..863cbf0d 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)
@@ -90,6 +93,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();
@@ -310,3 +314,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;
+}