Debian Codegraph @

src/Cache.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <QDateTime>
8 #include <QString>
9
10 #if __has_include(<lmdbxx/lmdb++.h>)
11 #include <lmdbxx/lmdb++.h>
12 #else
13 #include <lmdb++.h>
14 #endif
15
16 #include <mtx/events/event_type.hpp>
17 #include <mtx/events/presence.hpp>
18 #include <mtx/responses/crypto.hpp>
19 #include <mtxclient/crypto/types.hpp>
20
21 #include "CacheCryptoStructs.h"
22 #include "CacheStructs.h"
23
24 namespace mtx::responses {
25 struct Notifications;
26 }
27
28 namespace cache {
29 void
30 setNeedsCompactFlag();
31
32 void
33 init(const QString &user_id);
34
35 std::string
36 displayName(const std::string &room_id, const std::string &user_id);
37 QString
38 displayName(const QString &room_id, const QString &user_id);
39 QString
40 avatarUrl(const QString &room_id, const QString &user_id);
41
42 // presence
43 mtx::events::presence::Presence
44 presence(const std::string &user_id);
45
46 // user cache stores user keys
47 std::optional<UserKeyCache>
48 userKeys(const std::string &user_id);
49 void
50 updateUserKeys(const std::string &sync_token, const mtx::responses::QueryKeys &keyQuery);
51
52 // device & user verification cache
53 std::optional<VerificationStatus>
54 verificationStatus(const std::string &user_id);
55 void
56 markDeviceVerified(const std::string &user_id, const std::string &device);
57 void
58 markDeviceUnverified(const std::string &user_id, const std::string &device);
59
60 std::vector<std::string>
61 joinedRooms();
62
63 QMap<QString, RoomInfo>
64 roomInfo(bool withInvites = true);
65 QHash<QString, RoomInfo>
66 invites();
67
68 //! Calculate & return the name of the room.
69 QString
70 getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
71 //! Get room join rules
72 mtx::events::state::JoinRule
73 getRoomJoinRule(lmdb::txn &txn, lmdb::dbi &statesdb);
74 bool
75 getRoomGuestAccess(lmdb::txn &txn, lmdb::dbi &statesdb);
76 //! Retrieve the topic of the room if any.
77 QString
78 getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb);
79 //! Retrieve the room avatar's url if any.
80 QString
81 getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
82
83 //! Retrieve member info from a room.
84 std::vector<RoomMember>
85 getMembers(const std::string &room_id, std::size_t startIndex = 0, std::size_t len = 30);
86 //! Retrive member info from an invite.
87 std::vector<RoomMember>
88 getMembersFromInvite(const std::string &room_id, std::size_t start_index = 0, std::size_t len = 30);
89
90 bool
91 isInitialized();
92
93 std::string
94 nextBatchToken();
95
96 void
97 deleteData();
98
99 void
100 removeInvite(lmdb::txn &txn, const std::string &room_id);
101 void
102 removeInvite(const std::string &room_id);
103 void
104 removeRoom(lmdb::txn &txn, const std::string &roomid);
105 void
106 removeRoom(const std::string &roomid);
107 void
108 removeRoom(const QString &roomid);
109 void
110 setup();
111
112 //! returns if the format is current, older or newer
113 cache::CacheVersion
114 formatVersion();
115 //! set the format version to the current version
116 void
117 setCurrentFormat();
118 //! migrates db to the current format
119 bool
120 runMigrations();
121
122 //! Retrieve all the user ids from a room.
123 std::vector<std::string>
124 roomMembers(const std::string &room_id);
125
126 //! Check if the given user has power level greater than than
127 //! lowest power level of the given events.
128 bool
129 hasEnoughPowerLevel(const std::vector<mtx::events::EventType> &eventTypes,
130 const std::string &room_id,
131 const std::string &user_id);
132
133 //! Adds a user to the read list for the given event.
134 //!
135 //! There should be only one user id present in a receipt list per room.
136 //! The user id should be removed from any other lists.
137 using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
138 void
139 updateReadReceipt(lmdb::txn &txn, const std::string &room_id, const Receipts &receipts);
140
141 //! Retrieve all the read receipts for the given event id and room.
142 //!
143 //! Returns a map of user ids and the time of the read receipt in milliseconds.
144 using UserReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
145 UserReceipts
146 readReceipts(const QString &event_id, const QString &room_id);
147
148 //! get index of the event in the event db, not representing the visual index
149 std::optional<uint64_t>
150 getEventIndex(const std::string &room_id, std::string_view event_id);
151 std::optional<std::pair<uint64_t, std::string>>
152 lastInvisibleEventAfter(const std::string &room_id, std::string_view event_id);
153 std::optional<std::pair<uint64_t, std::string>>
154 lastVisibleEvent(const std::string &room_id, std::string_view event_id);
155
156 RoomInfo
157 singleRoomInfo(const std::string &room_id);
158 std::map<QString, RoomInfo>
159 getRoomInfo(const std::vector<std::string> &rooms);
160
161 //! Calculates which the read status of a room.
162 //! Whether all the events in the timeline have been read.
163 std::string
164 getFullyReadEventId(const std::string &room_id);
165 bool
166 calculateRoomReadStatus(const std::string &room_id);
167 void
168 calculateRoomReadStatus();
169
170 void
171 markSentNotification(const std::string &event_id);
172 //! Removes an event from the sent notifications.
173 void
174 removeReadNotification(const std::string &event_id);
175 //! Check if we have sent a desktop notification for the given event id.
176 bool
177 isNotificationSent(const std::string &event_id);
178
179 //! Remove old unused data.
180 void
181 deleteOldMessages();
182 void
183 deleteOldData() noexcept;
184 //! Retrieve all saved room ids.
185 std::vector<std::string>
186 getRoomIds(lmdb::txn &txn);
187
188 //! Mark a room that uses e2e encryption.
189 void
190 setEncryptedRoom(lmdb::txn &txn, const std::string &room_id);
191 bool
192 isRoomEncrypted(const std::string &room_id);
193
194 //! Check if a user is a member of the room.
195 bool
196 isRoomMember(const std::string &user_id, const std::string &room_id);
197
198 //
199 // Outbound Megolm Sessions
200 //
201 void
202 saveOutboundMegolmSession(const std::string &room_id,
203 const GroupSessionData &data,
204 mtx::crypto::OutboundGroupSessionPtr &session);
205 OutboundGroupSessionDataRef
206 getOutboundMegolmSession(const std::string &room_id);
207 bool
208 outboundMegolmSessionExists(const std::string &room_id) noexcept;
209 void
210 updateOutboundMegolmSession(const std::string &room_id,
211 const GroupSessionData &data,
212 mtx::crypto::OutboundGroupSessionPtr &session);
213 void
214 dropOutboundMegolmSession(const std::string &room_id);
215
216 void
217 importSessionKeys(const mtx::crypto::ExportedSessionKeys &keys);
218 mtx::crypto::ExportedSessionKeys
219 exportSessionKeys();
220
221 //
222 // Inbound Megolm Sessions
223 //
224 void
225 saveInboundMegolmSession(const MegolmSessionIndex &index,
226 mtx::crypto::InboundGroupSessionPtr session,
227 const GroupSessionData &data);
228 mtx::crypto::InboundGroupSessionPtr
229 getInboundMegolmSession(const MegolmSessionIndex &index);
230 bool
231 inboundMegolmSessionExists(const MegolmSessionIndex &index);
232 std::optional<GroupSessionData>
233 getMegolmSessionData(const MegolmSessionIndex &index);
234
235 //
236 // Olm Sessions
237 //
238 void
239 saveOlmSession(const std::string &curve25519,
240 mtx::crypto::OlmSessionPtr session,
241 uint64_t timestamp);
242 std::vector<std::string>
243 getOlmSessions(const std::string &curve25519);
244 std::optional<mtx::crypto::OlmSessionPtr>
245 getOlmSession(const std::string &curve25519, const std::string &session_id);
246 std::optional<mtx::crypto::OlmSessionPtr>
247 getLatestOlmSession(const std::string &curve25519);
248
249 void
250 saveOlmAccount(const std::string &pickled);
251
252 std::string
253 restoreOlmAccount();
254
255 void
256 storeSecret(std::string_view name, const std::string &secret);
257 std::optional<std::string>
258 secret(std::string_view name);
259
260 std::vector<ImagePackInfo>
261 getImagePacks(const std::string &room_id, std::optional<bool> stickers);
262 }