libQuotient
A Qt library for building matrix clients
database.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Tobias Fella <fella@posteo.de>
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 
4 #pragma once
5 
6 #include <QtCore/QObject>
7 #include <QtSql/QSqlQuery>
8 #include <QtCore/QVector>
9 
10 #include <QtCore/QHash>
11 
12 #include "e2ee/e2ee_common.h"
13 
14 namespace Quotient {
15 
16 class QOlmAccount;
17 class QOlmSession;
18 class QOlmInboundGroupSession;
19 class QOlmOutboundGroupSession;
20 
21 class QUOTIENT_API Database
22 {
23 public:
24  Database(const QString& userId, const QString& deviceId,
25  PicklingKey&& picklingKey);
26 
27  int version();
28  void transaction();
29  void commit();
30  QSqlQuery execute(const QString &queryString);
31  void execute(QSqlQuery &query);
32  QSqlDatabase database() const;
33  QSqlQuery prepareQuery(const QString& queryString) const;
34 
35  void storeOlmAccount(const QOlmAccount& olmAccount);
36  std::optional<OlmErrorCode> setupOlmAccount(QOlmAccount &olmAccount);
37  void clear();
38  void saveOlmSession(const QByteArray& senderKey, const QOlmSession& session,
39  const QDateTime& timestamp);
40  std::unordered_map<QByteArray, std::vector<QOlmSession>> loadOlmSessions();
41  std::unordered_map<QByteArray, QOlmInboundGroupSession> loadMegolmSessions(
42  const QString& roomId);
43  void saveMegolmSession(const QString& roomId,
44  const QOlmInboundGroupSession& session,
45  const QByteArray& senderKey,
46  const QByteArray& senderClaimedEdKey);
47  void addGroupSessionIndexRecord(const QString& roomId,
48  const QString& sessionId, uint32_t index,
49  const QString& eventId, qint64 ts);
50  std::pair<QString, qint64> groupSessionIndexRecord(const QString& roomId,
51  const QString& sessionId,
52  qint64 index);
53  void clearRoomData(const QString& roomId);
54  void setOlmSessionLastReceived(const QByteArray& sessionId,
55  const QDateTime& timestamp);
56  std::optional<QOlmOutboundGroupSession> loadCurrentOutboundMegolmSession(const QString& roomId);
57  void saveCurrentOutboundMegolmSession(
58  const QString& roomId, const QOlmOutboundGroupSession& session);
59  void updateOlmSession(const QByteArray& senderKey,
60  const QOlmSession& session);
61 
62  // Returns a map UserId -> [DeviceId] that have not received key yet
63  QMultiHash<QString, QString> devicesWithoutKey(
64  const QString& roomId, QMultiHash<QString, QString> devices,
65  const QByteArray& sessionId);
66  // 'devices' contains tuples {userId, deviceId, curveKey}
67  void setDevicesReceivedKey(
68  const QString& roomId,
69  const QVector<std::tuple<QString, QString, QString>>& devices,
70  const QByteArray& sessionId, uint32_t index);
71 
72  bool isSessionVerified(const QString& edKey);
73  void setSessionVerified(const QString& edKeyId);
74  void setMasterKeyVerified(const QString& masterKey);
75 
76  QString edKeyForKeyId(const QString& userId, const QString& edKeyId);
77  void storeEncrypted(const QString& name, const QByteArray& key);
78  QByteArray loadEncrypted(const QString& name);
79 
80  QString userSigningPublicKey();
81  QString selfSigningPublicKey();
82 
83  QString edKeyForMegolmSession(const QString& sessionId);
84  QString senderKeyForMegolmSession(const QString& sessionId);
85 
86 private:
87  void migrateTo1();
88  void migrateTo2();
89  void migrateTo3();
90  void migrateTo4();
91  void migrateTo5();
92  void migrateTo6();
93  void migrateTo7();
94  void migrateTo8();
95  void migrateTo9();
96  void migrateTo10();
97 
98  QString m_userId;
99  QString m_deviceId;
100  PicklingKey m_picklingKey;
101 };
102 } // namespace Quotient