2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-07-05 06:29:07 +03:00
|
|
|
#include <limits>
|
2019-12-15 04:56:04 +03:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QString>
|
|
|
|
|
2020-01-24 20:39:26 +03:00
|
|
|
#if __has_include(<lmdbxx/lmdb++.h>)
|
|
|
|
#include <lmdbxx/lmdb++.h>
|
|
|
|
#else
|
2019-12-15 04:56:04 +03:00
|
|
|
#include <lmdb++.h>
|
2020-01-24 20:39:26 +03:00
|
|
|
#endif
|
2019-12-15 04:56:04 +03:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <mtx/responses/notifications.hpp>
|
|
|
|
#include <mtx/responses/sync.hpp>
|
2022-06-18 02:35:30 +03:00
|
|
|
#include <mtxclient/crypto/types.hpp>
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <mtxclient/http/client.hpp>
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
#include "CacheCryptoStructs.h"
|
|
|
|
#include "CacheStructs.h"
|
2021-11-09 22:21:40 +03:00
|
|
|
#include "Logging.h"
|
2019-12-15 04:56:04 +03:00
|
|
|
|
2022-06-18 02:35:30 +03:00
|
|
|
namespace mtx::responses {
|
|
|
|
struct Messages;
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class Cache final : public QObject
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
public:
|
2021-09-18 01:22:33 +03:00
|
|
|
Cache(const QString &userId, QObject *parent = nullptr);
|
|
|
|
|
|
|
|
std::string displayName(const std::string &room_id, const std::string &user_id);
|
|
|
|
QString displayName(const QString &room_id, const QString &user_id);
|
|
|
|
QString avatarUrl(const QString &room_id, const QString &user_id);
|
|
|
|
|
|
|
|
// presence
|
2021-12-30 06:54:03 +03:00
|
|
|
mtx::events::presence::Presence presence(const std::string &user_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
// user cache stores user keys
|
2021-11-22 02:32:49 +03:00
|
|
|
std::map<std::string, std::optional<UserKeyCache>>
|
|
|
|
getMembersWithKeys(const std::string &room_id, bool verified_only);
|
2021-09-18 01:22:33 +03:00
|
|
|
void updateUserKeys(const std::string &sync_token, const mtx::responses::QueryKeys &keyQuery);
|
2021-09-19 23:55:12 +03:00
|
|
|
void markUserKeysOutOfDate(const std::vector<std::string> &user_ids);
|
2021-09-18 01:22:33 +03:00
|
|
|
void markUserKeysOutOfDate(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &db,
|
|
|
|
const std::vector<std::string> &user_ids,
|
|
|
|
const std::string &sync_token);
|
|
|
|
void query_keys(const std::string &user_id,
|
|
|
|
std::function<void(const UserKeyCache &, mtx::http::RequestErr)> cb);
|
|
|
|
|
|
|
|
// device & user verification cache
|
|
|
|
std::optional<UserKeyCache> userKeys(const std::string &user_id);
|
|
|
|
VerificationStatus verificationStatus(const std::string &user_id);
|
|
|
|
void markDeviceVerified(const std::string &user_id, const std::string &device);
|
|
|
|
void markDeviceUnverified(const std::string &user_id, const std::string &device);
|
|
|
|
crypto::Trust roomVerificationStatus(const std::string &room_id);
|
|
|
|
|
|
|
|
std::vector<std::string> joinedRooms();
|
2023-02-24 04:40:14 +03:00
|
|
|
std::map<std::string, RoomInfo> getCommonRooms(const std::string &user_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
QMap<QString, RoomInfo> roomInfo(bool withInvites = true);
|
|
|
|
QHash<QString, RoomInfo> invites();
|
|
|
|
std::optional<RoomInfo> invite(std::string_view roomid);
|
|
|
|
QMap<QString, std::optional<RoomInfo>> spaces();
|
|
|
|
|
|
|
|
//! Calculate & return the name of the room.
|
|
|
|
QString getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
|
|
|
//! Get room join rules
|
|
|
|
mtx::events::state::JoinRule getRoomJoinRule(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
bool getRoomGuestAccess(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
//! Retrieve the topic of the room if any.
|
|
|
|
QString getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
//! Retrieve the room avatar's url if any.
|
|
|
|
QString getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
|
|
|
//! Retrieve the version of the room if any.
|
|
|
|
QString getRoomVersion(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
//! Retrieve if the room is a space
|
|
|
|
bool getRoomIsSpace(lmdb::txn &txn, lmdb::dbi &statesdb);
|
2023-05-06 20:30:29 +03:00
|
|
|
//! Retrieve if the room is tombstoned (closed or replaced by a different room)
|
|
|
|
bool getRoomIsTombstoned(lmdb::txn &txn, lmdb::dbi &statesdb);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
//! Get a specific state event
|
|
|
|
template<typename T>
|
2021-11-22 02:32:49 +03:00
|
|
|
std::optional<mtx::events::StateEvent<T>>
|
|
|
|
getStateEvent(const std::string &room_id, std::string_view state_key = "")
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
return getStateEvent<T>(txn, room_id, state_key);
|
|
|
|
}
|
2021-12-01 02:02:41 +03:00
|
|
|
template<typename T>
|
2022-02-04 20:47:17 +03:00
|
|
|
std::vector<mtx::events::StateEvent<T>>
|
|
|
|
getStateEventsWithType(const std::string &room_id,
|
|
|
|
mtx::events::EventType type = mtx::events::state_content_to_type<T>)
|
2021-12-01 02:02:41 +03:00
|
|
|
{
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
2022-02-04 20:47:17 +03:00
|
|
|
return getStateEventsWithType<T>(txn, room_id, type);
|
2021-12-01 02:02:41 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
//! retrieve a specific event from account data
|
|
|
|
//! pass empty room_id for global account data
|
2021-11-22 02:32:49 +03:00
|
|
|
std::optional<mtx::events::collections::RoomAccountDataEvents>
|
|
|
|
getAccountData(mtx::events::EventType type, const std::string &room_id = "");
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
//! Retrieve member info from a room.
|
2021-11-22 02:32:49 +03:00
|
|
|
std::vector<RoomMember>
|
|
|
|
getMembers(const std::string &room_id, std::size_t startIndex = 0, std::size_t len = 30);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
std::vector<RoomMember> getMembersFromInvite(const std::string &room_id,
|
|
|
|
std::size_t startIndex = 0,
|
|
|
|
std::size_t len = 30);
|
|
|
|
size_t memberCount(const std::string &room_id);
|
|
|
|
|
2022-12-27 18:15:49 +03:00
|
|
|
void updateState(const std::string &room,
|
|
|
|
const mtx::responses::StateEvents &state,
|
|
|
|
bool wipe = false);
|
2021-09-18 01:22:33 +03:00
|
|
|
void saveState(const mtx::responses::Sync &res);
|
|
|
|
bool isInitialized();
|
|
|
|
bool isDatabaseReady() { return databaseReady_ && isInitialized(); }
|
|
|
|
|
|
|
|
std::string nextBatchToken();
|
|
|
|
|
|
|
|
void deleteData();
|
|
|
|
|
|
|
|
void removeInvite(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
void removeInvite(const std::string &room_id);
|
|
|
|
void removeRoom(lmdb::txn &txn, const std::string &roomid);
|
|
|
|
void removeRoom(const std::string &roomid);
|
|
|
|
void setup();
|
|
|
|
|
|
|
|
cache::CacheVersion formatVersion();
|
|
|
|
void setCurrentFormat();
|
|
|
|
bool runMigrations();
|
|
|
|
|
|
|
|
std::vector<QString> roomIds();
|
|
|
|
|
|
|
|
//! Retrieve all the user ids from a room.
|
|
|
|
std::vector<std::string> roomMembers(const std::string &room_id);
|
|
|
|
|
|
|
|
//! Check if the given user has power leve greater than than
|
|
|
|
//! lowest power level of the given events.
|
|
|
|
bool hasEnoughPowerLevel(const std::vector<mtx::events::EventType> &eventTypes,
|
|
|
|
const std::string &room_id,
|
|
|
|
const std::string &user_id);
|
|
|
|
|
|
|
|
//! Adds a user to the read list for the given event.
|
|
|
|
//!
|
|
|
|
//! There should be only one user id present in a receipt list per room.
|
|
|
|
//! The user id should be removed from any other lists.
|
|
|
|
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
|
|
|
|
void updateReadReceipt(lmdb::txn &txn, const std::string &room_id, const Receipts &receipts);
|
|
|
|
|
|
|
|
//! Retrieve all the read receipts for the given event id and room.
|
|
|
|
//!
|
|
|
|
//! Returns a map of user ids and the time of the read receipt in milliseconds.
|
|
|
|
using UserReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
|
|
|
|
UserReceipts readReceipts(const QString &event_id, const QString &room_id);
|
|
|
|
|
|
|
|
RoomInfo singleRoomInfo(const std::string &room_id);
|
|
|
|
std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res);
|
|
|
|
std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms);
|
2023-01-07 19:39:32 +03:00
|
|
|
|
|
|
|
std::vector<RoomNameAlias> roomNamesAndAliases();
|
|
|
|
|
2022-06-26 02:48:16 +03:00
|
|
|
void updateLastMessageTimestamp(const std::string &room_id, uint64_t ts);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
//! Calculates which the read status of a room.
|
|
|
|
//! Whether all the events in the timeline have been read.
|
2022-09-12 02:05:20 +03:00
|
|
|
std::string getFullyReadEventId(const std::string &room_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
bool calculateRoomReadStatus(const std::string &room_id);
|
|
|
|
void calculateRoomReadStatus();
|
|
|
|
|
|
|
|
void markSentNotification(const std::string &event_id);
|
|
|
|
//! Removes an event from the sent notifications.
|
|
|
|
void removeReadNotification(const std::string &event_id);
|
|
|
|
//! Check if we have sent a desktop notification for the given event id.
|
|
|
|
bool isNotificationSent(const std::string &event_id);
|
|
|
|
|
2023-04-11 01:11:46 +03:00
|
|
|
std::optional<mtx::events::collections::TimelineEvents>
|
2022-09-30 04:27:05 +03:00
|
|
|
getEvent(const std::string &room_id, std::string_view event_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
void storeEvent(const std::string &room_id,
|
|
|
|
const std::string &event_id,
|
2023-04-11 01:11:46 +03:00
|
|
|
const mtx::events::collections::TimelineEvents &event);
|
2021-09-18 01:22:33 +03:00
|
|
|
void replaceEvent(const std::string &room_id,
|
|
|
|
const std::string &event_id,
|
2023-04-11 01:11:46 +03:00
|
|
|
const mtx::events::collections::TimelineEvents &event);
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> relatedEvents(const std::string &room_id, const std::string &event_id);
|
|
|
|
|
|
|
|
struct TimelineRange
|
|
|
|
{
|
|
|
|
uint64_t first, last;
|
|
|
|
};
|
|
|
|
std::optional<TimelineRange> getTimelineRange(const std::string &room_id);
|
|
|
|
std::optional<uint64_t> getTimelineIndex(const std::string &room_id, std::string_view event_id);
|
|
|
|
std::optional<uint64_t> getEventIndex(const std::string &room_id, std::string_view event_id);
|
2021-11-22 02:32:49 +03:00
|
|
|
std::optional<std::pair<uint64_t, std::string>>
|
|
|
|
lastInvisibleEventAfter(const std::string &room_id, std::string_view event_id);
|
2022-09-12 02:05:20 +03:00
|
|
|
std::optional<std::pair<uint64_t, std::string>>
|
|
|
|
lastVisibleEvent(const std::string &room_id, std::string_view event_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
std::optional<std::string> getTimelineEventId(const std::string &room_id, uint64_t index);
|
|
|
|
|
|
|
|
std::string previousBatchToken(const std::string &room_id);
|
|
|
|
uint64_t saveOldMessages(const std::string &room_id, const mtx::responses::Messages &res);
|
|
|
|
void savePendingMessage(const std::string &room_id,
|
2023-04-11 01:11:46 +03:00
|
|
|
const mtx::events::collections::TimelineEvents &message);
|
2022-02-27 07:02:54 +03:00
|
|
|
std::vector<std::string> pendingEvents(const std::string &room_id);
|
2023-04-11 01:11:46 +03:00
|
|
|
std::optional<mtx::events::collections::TimelineEvents>
|
2021-11-22 02:32:49 +03:00
|
|
|
firstPendingMessage(const std::string &room_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
void removePendingStatus(const std::string &room_id, const std::string &txn_id);
|
|
|
|
|
|
|
|
//! clear timeline keeping only the latest batch
|
|
|
|
void clearTimeline(const std::string &room_id);
|
|
|
|
|
|
|
|
//! Remove old unused data.
|
|
|
|
void deleteOldMessages();
|
|
|
|
void deleteOldData() noexcept;
|
|
|
|
//! Retrieve all saved room ids.
|
|
|
|
std::vector<std::string> getRoomIds(lmdb::txn &txn);
|
|
|
|
std::vector<std::string> getParentRoomIds(const std::string &room_id);
|
|
|
|
std::vector<std::string> getChildRoomIds(const std::string &room_id);
|
|
|
|
|
2021-11-22 02:32:49 +03:00
|
|
|
std::vector<ImagePackInfo>
|
|
|
|
getImagePacks(const std::string &room_id, std::optional<bool> stickers);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
//! Mark a room that uses e2e encryption.
|
|
|
|
void setEncryptedRoom(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
bool isRoomEncrypted(const std::string &room_id);
|
2021-11-22 02:32:49 +03:00
|
|
|
std::optional<mtx::events::state::Encryption>
|
|
|
|
roomEncryptionSettings(const std::string &room_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
//! Check if a user is a member of the room.
|
|
|
|
bool isRoomMember(const std::string &user_id, const std::string &room_id);
|
2022-12-27 03:40:03 +03:00
|
|
|
std::optional<MemberInfo>
|
|
|
|
getInviteMember(const std::string &room_id, const std::string &user_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Outbound Megolm Sessions
|
|
|
|
//
|
|
|
|
void saveOutboundMegolmSession(const std::string &room_id,
|
|
|
|
const GroupSessionData &data,
|
|
|
|
mtx::crypto::OutboundGroupSessionPtr &session);
|
|
|
|
OutboundGroupSessionDataRef getOutboundMegolmSession(const std::string &room_id);
|
|
|
|
bool outboundMegolmSessionExists(const std::string &room_id) noexcept;
|
|
|
|
void updateOutboundMegolmSession(const std::string &room_id,
|
|
|
|
const GroupSessionData &data,
|
|
|
|
mtx::crypto::OutboundGroupSessionPtr &session);
|
|
|
|
void dropOutboundMegolmSession(const std::string &room_id);
|
|
|
|
|
|
|
|
void importSessionKeys(const mtx::crypto::ExportedSessionKeys &keys);
|
|
|
|
mtx::crypto::ExportedSessionKeys exportSessionKeys();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Inbound Megolm Sessions
|
|
|
|
//
|
|
|
|
void saveInboundMegolmSession(const MegolmSessionIndex &index,
|
|
|
|
mtx::crypto::InboundGroupSessionPtr session,
|
|
|
|
const GroupSessionData &data);
|
|
|
|
mtx::crypto::InboundGroupSessionPtr getInboundMegolmSession(const MegolmSessionIndex &index);
|
|
|
|
bool inboundMegolmSessionExists(const MegolmSessionIndex &index);
|
|
|
|
std::optional<GroupSessionData> getMegolmSessionData(const MegolmSessionIndex &index);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Olm Sessions
|
|
|
|
//
|
|
|
|
void saveOlmSession(const std::string &curve25519,
|
|
|
|
mtx::crypto::OlmSessionPtr session,
|
|
|
|
uint64_t timestamp);
|
2022-11-01 22:58:01 +03:00
|
|
|
void saveOlmSessions(std::vector<std::pair<std::string, mtx::crypto::OlmSessionPtr>> sessions,
|
|
|
|
uint64_t timestamp);
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> getOlmSessions(const std::string &curve25519);
|
2021-11-22 02:32:49 +03:00
|
|
|
std::optional<mtx::crypto::OlmSessionPtr>
|
|
|
|
getOlmSession(const std::string &curve25519, const std::string &session_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
std::optional<mtx::crypto::OlmSessionPtr> getLatestOlmSession(const std::string &curve25519);
|
|
|
|
|
|
|
|
void saveOlmAccount(const std::string &pickled);
|
|
|
|
std::string restoreOlmAccount();
|
|
|
|
|
|
|
|
void saveBackupVersion(const OnlineBackupVersion &data);
|
|
|
|
void deleteBackupVersion();
|
|
|
|
std::optional<OnlineBackupVersion> backupVersion();
|
|
|
|
|
2022-11-06 05:36:56 +03:00
|
|
|
void storeSecret(const std::string &name, const std::string &secret, bool internal = false);
|
|
|
|
void deleteSecret(const std::string &name, bool internal = false);
|
|
|
|
std::optional<std::string> secret(const std::string &name, bool internal = false);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
std::string pickleSecret();
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr static bool isStateEvent_ =
|
|
|
|
std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>,
|
|
|
|
mtx::events::StateEvent<decltype(std::declval<T>().content)>>;
|
|
|
|
|
|
|
|
static int compare_state_key(const MDB_val *a, const MDB_val *b)
|
|
|
|
{
|
|
|
|
auto get_skey = [](const MDB_val *v) {
|
2023-03-12 06:59:52 +03:00
|
|
|
auto temp = std::string_view(static_cast<const char *>(v->mv_data), v->mv_size);
|
|
|
|
// allow only passing the state key, in which case no null char will be in it and we
|
|
|
|
// return the whole string because rfind returns npos.
|
|
|
|
// We search from the back, because state keys could include nullbytes, event ids can
|
|
|
|
// not.
|
|
|
|
return temp.substr(0, temp.rfind('\0'));
|
2020-07-10 00:15:22 +03:00
|
|
|
};
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
return get_skey(a).compare(get_skey(b));
|
|
|
|
}
|
2021-11-07 05:38:48 +03:00
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
signals:
|
2021-09-18 01:22:33 +03:00
|
|
|
void newReadReceipts(const QString &room_id, const std::vector<QString> &event_ids);
|
|
|
|
void roomReadStatus(const std::map<QString, bool> &status);
|
|
|
|
void userKeysUpdate(const std::string &sync_token, const mtx::responses::QueryKeys &keyQuery);
|
2022-04-08 04:24:10 +03:00
|
|
|
void userKeysUpdateFinalize(const std::string &user_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
void verificationStatusChanged(const std::string &userid);
|
2021-11-02 00:20:15 +03:00
|
|
|
void selfVerificationStatusChanged();
|
2021-09-18 01:22:33 +03:00
|
|
|
void secretChanged(const std::string name);
|
2021-11-07 05:38:48 +03:00
|
|
|
void databaseReady();
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
private:
|
2022-11-06 05:36:56 +03:00
|
|
|
void loadSecretsFromStore(
|
|
|
|
std::vector<std::pair<std::string, bool>> toLoad,
|
|
|
|
std::function<void(const std::string &name, bool internal, const std::string &value)>
|
2023-01-07 03:09:36 +03:00
|
|
|
callback,
|
|
|
|
bool databaseReadyOnFinished = false);
|
2022-11-06 05:36:56 +03:00
|
|
|
void storeSecretInStore(const std::string name, const std::string secret);
|
|
|
|
void deleteSecretFromStore(const std::string name, bool internal);
|
2021-11-07 05:38:48 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
//! Save an invited room.
|
|
|
|
void saveInvite(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &statesdb,
|
|
|
|
lmdb::dbi &membersdb,
|
|
|
|
const mtx::responses::InvitedRoom &room);
|
|
|
|
|
|
|
|
QString getInviteRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
|
|
|
QString getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
QString getInviteRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
|
|
|
bool getInviteRoomIsSpace(lmdb::txn &txn, lmdb::dbi &db);
|
|
|
|
|
|
|
|
std::optional<MemberInfo> getMember(const std::string &room_id, const std::string &user_id);
|
|
|
|
|
|
|
|
std::string getLastEventId(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
void saveTimelineMessages(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &eventsDb,
|
|
|
|
const std::string &room_id,
|
|
|
|
const mtx::responses::Timeline &res);
|
|
|
|
|
|
|
|
//! retrieve a specific event from account data
|
|
|
|
//! pass empty room_id for global account data
|
|
|
|
std::optional<mtx::events::collections::RoomAccountDataEvents>
|
|
|
|
getAccountData(lmdb::txn &txn, mtx::events::EventType type, const std::string &room_id);
|
|
|
|
bool isHiddenEvent(lmdb::txn &txn,
|
|
|
|
mtx::events::collections::TimelineEvents e,
|
|
|
|
const std::string &room_id);
|
|
|
|
|
|
|
|
//! Remove a room from the cache.
|
|
|
|
// void removeLeftRoom(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
template<class T>
|
|
|
|
void saveStateEvents(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &statesdb,
|
|
|
|
lmdb::dbi &stateskeydb,
|
|
|
|
lmdb::dbi &membersdb,
|
|
|
|
lmdb::dbi &eventsDb,
|
|
|
|
const std::string &room_id,
|
|
|
|
const std::vector<T> &events)
|
|
|
|
{
|
|
|
|
for (const auto &e : events)
|
|
|
|
saveStateEvent(txn, statesdb, stateskeydb, membersdb, eventsDb, room_id, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void saveStateEvent(lmdb::txn &txn,
|
2019-12-15 04:56:04 +03:00
|
|
|
lmdb::dbi &statesdb,
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::dbi &stateskeydb,
|
2019-12-15 04:56:04 +03:00
|
|
|
lmdb::dbi &membersdb,
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::dbi &eventsDb,
|
|
|
|
const std::string &room_id,
|
|
|
|
const T &event)
|
|
|
|
{
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
|
|
|
|
|
|
|
if (auto e = std::get_if<StateEvent<Member>>(&event); e != nullptr) {
|
|
|
|
switch (e->content.membership) {
|
|
|
|
//
|
|
|
|
// We only keep users with invite or join membership.
|
|
|
|
//
|
|
|
|
case Membership::Invite:
|
|
|
|
case Membership::Join: {
|
|
|
|
auto display_name =
|
|
|
|
e->content.display_name.empty() ? e->state_key : e->content.display_name;
|
|
|
|
|
|
|
|
// Lightweight representation of a member.
|
2022-12-27 03:40:03 +03:00
|
|
|
MemberInfo tmp{display_name, e->content.avatar_url, e->content.reason};
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-06-17 23:03:54 +03:00
|
|
|
membersdb.put(txn, e->state_key, nlohmann::json(tmp).dump());
|
2021-09-18 01:22:33 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
membersdb.del(txn, e->state_key, "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 01:47:07 +03:00
|
|
|
} else if (auto encr = std::get_if<StateEvent<Encryption>>(&event)) {
|
|
|
|
if (!encr->state_key.empty())
|
|
|
|
return;
|
2022-12-27 18:19:59 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setEncryptedRoom(txn, room_id);
|
2023-02-26 01:47:07 +03:00
|
|
|
|
|
|
|
std::string_view temp;
|
|
|
|
// ensure we don't replace the event in the db
|
|
|
|
if (statesdb.get(txn, to_string(encr->type), temp)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::visit(
|
|
|
|
[&txn, &statesdb, &stateskeydb, &eventsDb, &membersdb](const auto &e) {
|
|
|
|
if constexpr (isStateEvent_<decltype(e)>) {
|
2022-06-17 23:03:54 +03:00
|
|
|
eventsDb.put(txn, e.event_id, nlohmann::json(e).dump());
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
if (e.type != EventType::Unsupported) {
|
|
|
|
if (std::is_same_v<std::remove_cv_t<std::remove_reference_t<decltype(e)>>,
|
|
|
|
StateEvent<mtx::events::msg::Redacted>>) {
|
2023-03-27 00:37:28 +03:00
|
|
|
// apply the redaction event
|
|
|
|
if (e.type == EventType::RoomMember) {
|
|
|
|
// membership is not revoked, but names are yeeted (so we set the name
|
|
|
|
// to the mxid)
|
|
|
|
MemberInfo tmp{e.state_key, ""};
|
|
|
|
membersdb.put(txn, e.state_key, nlohmann::json(tmp).dump());
|
|
|
|
} else if (e.state_key.empty()) {
|
|
|
|
// strictly speaking some stuff in those events can be redacted, but
|
|
|
|
// this is close enough. Ref:
|
|
|
|
// https://spec.matrix.org/v1.6/rooms/v10/#redactions
|
|
|
|
if (e.type != EventType::RoomCreate &&
|
|
|
|
e.type != EventType::RoomJoinRules &&
|
|
|
|
e.type != EventType::RoomPowerLevels &&
|
|
|
|
e.type != EventType::RoomHistoryVisibility)
|
|
|
|
statesdb.del(txn, to_string(e.type));
|
|
|
|
} else
|
2023-03-12 06:59:52 +03:00
|
|
|
stateskeydb.del(
|
|
|
|
txn, to_string(e.type), e.state_key + '\0' + e.event_id);
|
2023-02-26 01:47:07 +03:00
|
|
|
} else if (e.state_key.empty()) {
|
2022-06-17 23:03:54 +03:00
|
|
|
statesdb.put(txn, to_string(e.type), nlohmann::json(e).dump());
|
2023-02-26 01:47:07 +03:00
|
|
|
} else {
|
2023-03-12 06:59:52 +03:00
|
|
|
auto data = e.state_key + '\0' + e.event_id;
|
|
|
|
auto key = to_string(e.type);
|
2023-02-26 01:47:07 +03:00
|
|
|
|
|
|
|
// Work around https://bugs.openldap.org/show_bug.cgi?id=8447
|
|
|
|
stateskeydb.del(txn, key, data);
|
|
|
|
stateskeydb.put(txn, key, data);
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
event);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-11-22 02:32:49 +03:00
|
|
|
std::optional<mtx::events::StateEvent<T>>
|
|
|
|
getStateEvent(lmdb::txn &txn, const std::string &room_id, std::string_view state_key = "")
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
2021-11-21 22:10:19 +03:00
|
|
|
try {
|
|
|
|
constexpr auto type = mtx::events::state_content_to_type<T>;
|
|
|
|
static_assert(type != mtx::events::EventType::Unsupported,
|
|
|
|
"Not a supported type in state events.");
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2021-11-21 22:10:19 +03:00
|
|
|
if (room_id.empty())
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::nullopt;
|
2021-11-21 22:10:19 +03:00
|
|
|
const auto typeStr = to_string(type);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2021-11-21 22:10:19 +03:00
|
|
|
std::string_view value;
|
|
|
|
if (state_key.empty()) {
|
|
|
|
auto db = getStatesDb(txn, room_id);
|
|
|
|
if (!db.get(txn, typeStr, value)) {
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::nullopt;
|
2021-11-21 22:10:19 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-12 06:59:52 +03:00
|
|
|
auto db = getStatesKeyDb(txn, room_id);
|
|
|
|
// we can search using state key, since the compare functions defaults to the whole
|
|
|
|
// string, when there is no nullbyte
|
|
|
|
std::string_view data = state_key;
|
2021-11-21 22:10:19 +03:00
|
|
|
std::string_view typeStrV = typeStr;
|
|
|
|
|
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
|
|
|
if (!cursor.get(typeStrV, data, MDB_GET_BOTH))
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
2023-03-12 06:59:52 +03:00
|
|
|
auto eventid = data;
|
|
|
|
if (auto sep = data.rfind('\0'); sep != std::string_view::npos) {
|
|
|
|
if (!eventsDb.get(txn, eventid.substr(sep + 1), value))
|
|
|
|
return std::nullopt;
|
|
|
|
} else {
|
2021-11-21 22:10:19 +03:00
|
|
|
return std::nullopt;
|
2023-03-12 06:59:52 +03:00
|
|
|
}
|
|
|
|
|
2022-10-01 20:05:44 +03:00
|
|
|
} catch (std::exception &) {
|
2021-11-21 22:10:19 +03:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
|
2022-06-17 23:03:54 +03:00
|
|
|
return nlohmann::json::parse(value).get<mtx::events::StateEvent<T>>();
|
2022-10-01 20:05:44 +03:00
|
|
|
} catch (std::exception &) {
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::nullopt;
|
2021-04-18 21:21:03 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-04-18 21:21:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
template<typename T>
|
2021-11-22 02:32:49 +03:00
|
|
|
std::vector<mtx::events::StateEvent<T>>
|
2022-02-04 20:47:17 +03:00
|
|
|
getStateEventsWithType(lmdb::txn &txn,
|
|
|
|
const std::string &room_id,
|
|
|
|
mtx::events::EventType type = mtx::events::state_content_to_type<T>)
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
if (room_id.empty())
|
|
|
|
return {};
|
2019-12-15 04:56:04 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<mtx::events::StateEvent<T>> events;
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = getStatesKeyDb(txn, room_id);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
const auto typeStr = to_string(type);
|
|
|
|
std::string_view typeStrV = typeStr;
|
|
|
|
std::string_view data;
|
|
|
|
std::string_view value;
|
|
|
|
|
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
|
|
|
bool first = true;
|
|
|
|
if (cursor.get(typeStrV, data, MDB_SET)) {
|
|
|
|
while (cursor.get(typeStrV, data, first ? MDB_FIRST_DUP : MDB_NEXT_DUP)) {
|
|
|
|
first = false;
|
|
|
|
|
2021-11-09 22:21:40 +03:00
|
|
|
try {
|
2023-03-12 06:59:52 +03:00
|
|
|
auto eventid = data;
|
|
|
|
if (auto sep = data.rfind('\0'); sep != std::string_view::npos) {
|
|
|
|
if (eventsDb.get(txn, eventid.substr(sep + 1), value))
|
|
|
|
events.push_back(
|
|
|
|
nlohmann::json::parse(value).get<mtx::events::StateEvent<T>>());
|
|
|
|
}
|
2021-11-09 22:21:40 +03:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->warn("Failed to parse state event: {}", e.what());
|
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return events;
|
|
|
|
}
|
2021-11-22 02:32:49 +03:00
|
|
|
void
|
|
|
|
saveInvites(lmdb::txn &txn, const std::map<std::string, mtx::responses::InvitedRoom> &rooms);
|
2019-12-15 04:56:04 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
void savePresence(
|
|
|
|
lmdb::txn &txn,
|
|
|
|
const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presenceUpdates);
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
//! Sends signals for the rooms that are removed.
|
2021-11-22 02:32:49 +03:00
|
|
|
void
|
|
|
|
removeLeftRooms(lmdb::txn &txn, const std::map<std::string, mtx::responses::LeftRoom> &rooms)
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
for (const auto &room : rooms) {
|
|
|
|
removeRoom(txn, room.first);
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Clean up leftover invites.
|
|
|
|
removeInvite(txn, room.first);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void updateSpaces(lmdb::txn &txn,
|
|
|
|
const std::set<std::string> &spaces_with_updates,
|
|
|
|
std::set<std::string> rooms_with_updates);
|
|
|
|
|
|
|
|
lmdb::dbi getPendingReceiptsDb(lmdb::txn &txn)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, "pending_receipts", MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getEventsDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/events").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getEventOrderDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/event_order").c_str(), MDB_CREATE | MDB_INTEGERKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// inverse of EventOrderDb
|
|
|
|
lmdb::dbi getEventToOrderDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/event2order").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getMessageToOrderDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/msg2order").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getOrderToMessageDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/order2msg").c_str(), MDB_CREATE | MDB_INTEGERKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getPendingMessagesDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/pending").c_str(), MDB_CREATE | MDB_INTEGERKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getRelationsDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/related").c_str(), MDB_CREATE | MDB_DUPSORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getInviteStatesDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/invite_state").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getInviteMembersDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/invite_members").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getStatesDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/state").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getStatesKeyDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
auto db = lmdb::dbi::open(
|
2023-03-12 06:59:52 +03:00
|
|
|
txn, std::string(room_id + "/states_key").c_str(), MDB_CREATE | MDB_DUPSORT);
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::dbi_set_dupsort(txn, db, compare_state_key);
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getAccountDataDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/account_data").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getMembersDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/members").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getUserKeysDb(lmdb::txn &txn) { return lmdb::dbi::open(txn, "user_key", MDB_CREATE); }
|
|
|
|
|
|
|
|
lmdb::dbi getVerificationDb(lmdb::txn &txn)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, "verified", MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Retrieves or creates the database that stores the open OLM sessions between our device
|
|
|
|
//! and the given curve25519 key which represents another device.
|
|
|
|
//!
|
|
|
|
//! Each entry is a map from the session_id to the pickled representation of the session.
|
|
|
|
lmdb::dbi getOlmSessionsDb(lmdb::txn &txn, const std::string &curve25519_key)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string("olm_sessions.v2/" + curve25519_key).c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getDisplayName(const mtx::events::StateEvent<mtx::events::state::Member> &event)
|
|
|
|
{
|
|
|
|
if (!event.content.display_name.empty())
|
|
|
|
return QString::fromStdString(event.content.display_name);
|
|
|
|
|
|
|
|
return QString::fromStdString(event.state_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<VerificationCache> verificationCache(const std::string &user_id, lmdb::txn &txn);
|
|
|
|
VerificationStatus verificationStatus_(const std::string &user_id, lmdb::txn &txn);
|
|
|
|
std::optional<UserKeyCache> userKeys_(const std::string &user_id, lmdb::txn &txn);
|
|
|
|
|
|
|
|
void setNextBatchToken(lmdb::txn &txn, const std::string &token);
|
|
|
|
|
|
|
|
lmdb::env env_;
|
|
|
|
lmdb::dbi syncStateDb_;
|
|
|
|
lmdb::dbi roomsDb_;
|
|
|
|
lmdb::dbi spacesChildrenDb_, spacesParentsDb_;
|
|
|
|
lmdb::dbi invitesDb_;
|
|
|
|
lmdb::dbi readReceiptsDb_;
|
|
|
|
lmdb::dbi notificationsDb_;
|
2022-01-03 17:33:36 +03:00
|
|
|
lmdb::dbi presenceDb_;
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
lmdb::dbi devicesDb_;
|
|
|
|
lmdb::dbi deviceKeysDb_;
|
|
|
|
|
|
|
|
lmdb::dbi inboundMegolmSessionDb_;
|
|
|
|
lmdb::dbi outboundMegolmSessionDb_;
|
|
|
|
lmdb::dbi megolmSessionDataDb_;
|
|
|
|
|
|
|
|
lmdb::dbi encryptedRooms_;
|
|
|
|
|
|
|
|
QString localUserId_;
|
|
|
|
QString cacheDirectory_;
|
|
|
|
|
|
|
|
std::string pickle_secret_;
|
|
|
|
|
|
|
|
VerificationStorage verification_storage;
|
|
|
|
|
|
|
|
bool databaseReady_ = false;
|
2019-12-15 04:56:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace cache {
|
|
|
|
Cache *
|
|
|
|
client();
|
|
|
|
}
|