aboutsummaryrefslogtreecommitdiff
path: root/lib/database.cpp
diff options
context:
space:
mode:
authorTobias Fella <fella@posteo.de>2022-03-02 23:56:52 +0100
committerTobias Fella <fella@posteo.de>2022-03-10 19:50:47 +0100
commit30004d4e0d6aca06491777f77ed966a451eff50f (patch)
treec4c7304802f4cd999d628b0263ff58b6cf4e43a8 /lib/database.cpp
parent658e6db49364862be1ab8e264b03dc04a7bed721 (diff)
downloadlibquotient-30004d4e0d6aca06491777f77ed966a451eff50f.tar.gz
libquotient-30004d4e0d6aca06491777f77ed966a451eff50f.zip
Save and load outgoing megolm session
Diffstat (limited to 'lib/database.cpp')
-rw-r--r--lib/database.cpp41
1 files changed, 40 insertions, 1 deletions
diff --git a/lib/database.cpp b/lib/database.cpp
index 70dc1b9b..ad653073 100644
--- a/lib/database.cpp
+++ b/lib/database.cpp
@@ -85,7 +85,7 @@ void Database::migrateTo1()
execute(QStringLiteral("CREATE TABLE accounts (pickle TEXT);"));
execute(QStringLiteral("CREATE TABLE olm_sessions (senderKey TEXT, sessionId TEXT, pickle TEXT);"));
execute(QStringLiteral("CREATE TABLE inbound_megolm_sessions (roomId TEXT, senderKey TEXT, sessionId TEXT, pickle TEXT);"));
- execute(QStringLiteral("CREATE TABLE outbound_megolm_sessions (roomId TEXT, senderKey TEXT, sessionId TEXT, pickle TEXT);"));
+ execute(QStringLiteral("CREATE TABLE outbound_megolm_sessions (roomId TEXT, sessionId TEXT, pickle TEXT, creationTime TEXT, messageCount INTEGER);"));
execute(QStringLiteral("CREATE TABLE group_session_record_index (roomId TEXT, sessionId TEXT, i INTEGER, eventId TEXT, ts INTEGER);"));
execute(QStringLiteral("CREATE TABLE tracked_users (matrixId TEXT);"));
execute(QStringLiteral("CREATE TABLE outdated_users (matrixId TEXT);"));
@@ -271,3 +271,42 @@ void Database::setOlmSessionLastReceived(const QString& sessionId, const QDateTi
execute(query);
commit();
}
+
+void Database::saveCurrentOutboundMegolmSession(const QString& roomId, const PicklingMode& picklingMode, const QOlmOutboundGroupSessionPtr& session)
+{
+ const auto pickle = session->pickle(picklingMode);
+ if (std::holds_alternative<QByteArray>(pickle)) {
+ auto deleteQuery = prepareQuery(QStringLiteral("DELETE FROM outbound_megolm_sessions WHERE roomId=:roomId AND sessionId=:sessionId;"));
+ deleteQuery.bindValue(":roomId", roomId);
+ deleteQuery.bindValue(":sessionId", session->sessionId());
+
+ auto insertQuery = prepareQuery(QStringLiteral("INSERT INTO outbound_megolm_sessions(roomId, sessionId, pickle, creationTime, messageCount) VALUES(:roomId, :sessionId, :pickle, :creationTime, :messageCount);"));
+ insertQuery.bindValue(":roomId", roomId);
+ insertQuery.bindValue(":sessionId", session->sessionId());
+ insertQuery.bindValue(":pickle", std::get<QByteArray>(pickle));
+ insertQuery.bindValue(":creationTime", session->creationTime());
+ insertQuery.bindValue(":messageCount", session->messageCount());
+
+ transaction();
+ execute(deleteQuery);
+ execute(insertQuery);
+ commit();
+ }
+}
+
+QOlmOutboundGroupSessionPtr Database::loadCurrentOutboundMegolmSession(const QString& roomId, const PicklingMode& picklingMode)
+{
+ auto query = prepareQuery(QStringLiteral("SELECT * FROM outbound_megolm_sessions WHERE roomId=:roomId ORDER BY creationTime DESC;"));
+ query.bindValue(":roomId", roomId);
+ execute(query);
+ if (query.next()) {
+ auto sessionResult = QOlmOutboundGroupSession::unpickle(query.value("pickle").toByteArray(), picklingMode);
+ if (std::holds_alternative<QOlmOutboundGroupSessionPtr>(sessionResult)) {
+ auto session = std::move(std::get<QOlmOutboundGroupSessionPtr>(sessionResult));
+ session->setCreationTime(query.value("creationTime").toDateTime());
+ session->setMessageCount(query.value("messageCount").toInt());
+ return session;
+ }
+ }
+ return nullptr;
+}