2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2018-05-16 23:30:50 +03:00
|
|
|
#include <limits>
|
2017-07-29 11:49:00 +03:00
|
|
|
#include <stdexcept>
|
2019-12-14 19:08:36 +03:00
|
|
|
#include <variant>
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-22 01:00:48 +03:00
|
|
|
#include <QByteArray>
|
2019-06-27 21:53:44 +03:00
|
|
|
#include <QCoreApplication>
|
2020-11-10 05:28:41 +03:00
|
|
|
#include <QCryptographicHash>
|
2017-07-29 11:49:00 +03:00
|
|
|
#include <QFile>
|
2018-04-21 16:34:50 +03:00
|
|
|
#include <QHash>
|
2019-08-13 05:09:40 +03:00
|
|
|
#include <QMap>
|
2021-08-31 02:51:03 +03:00
|
|
|
#include <QMessageBox>
|
2017-07-29 11:49:00 +03:00
|
|
|
#include <QStandardPaths>
|
|
|
|
|
2020-12-18 05:21:17 +03:00
|
|
|
#if __has_include(<keychain.h>)
|
|
|
|
#include <keychain.h>
|
|
|
|
#else
|
2020-12-17 00:10:09 +03:00
|
|
|
#include <qt5keychain/keychain.h>
|
2020-12-18 05:21:17 +03:00
|
|
|
#endif
|
2020-12-17 00:10:09 +03:00
|
|
|
|
2018-06-28 16:16:43 +03:00
|
|
|
#include <mtx/responses/common.hpp>
|
2018-03-26 20:41:16 +03:00
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
#include "Cache.h"
|
2019-12-15 04:56:04 +03:00
|
|
|
#include "Cache_p.h"
|
2020-07-06 19:02:21 +03:00
|
|
|
#include "ChatPage.h"
|
2020-05-30 17:37:51 +03:00
|
|
|
#include "EventAccessors.h"
|
2020-01-31 18:36:58 +03:00
|
|
|
#include "Logging.h"
|
2020-07-06 19:02:21 +03:00
|
|
|
#include "MatrixClient.h"
|
2020-11-10 05:28:41 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2018-04-29 15:42:40 +03:00
|
|
|
#include "Utils.h"
|
2021-10-14 23:53:11 +03:00
|
|
|
#include "encryption/Olm.h"
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
//! Should be changed when a breaking change occurs in the cache format.
|
|
|
|
//! This will reset client's data.
|
2021-08-31 05:06:51 +03:00
|
|
|
static const std::string CURRENT_CACHE_FORMAT_VERSION("2021.08.31");
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2021-03-03 02:08:33 +03:00
|
|
|
//! Keys used for the DB
|
2021-03-03 01:15:12 +03:00
|
|
|
static const std::string_view NEXT_BATCH_KEY("next_batch");
|
|
|
|
static const std::string_view OLM_ACCOUNT_KEY("olm_account");
|
|
|
|
static const std::string_view CACHE_FORMAT_VERSION_KEY("cache_format_version");
|
2021-08-17 04:23:51 +03:00
|
|
|
static const std::string_view CURRENT_ONLINE_BACKUP_VERSION("current_online_backup_version");
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2020-02-24 03:07:25 +03:00
|
|
|
constexpr size_t MAX_RESTORED_MESSAGES = 30'000;
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2020-07-05 06:29:07 +03:00
|
|
|
constexpr auto DB_SIZE = 32ULL * 1024ULL * 1024ULL * 1024ULL; // 32 GB
|
2020-11-26 02:19:09 +03:00
|
|
|
constexpr auto MAX_DBS = 32384UL;
|
2020-07-05 06:29:07 +03:00
|
|
|
constexpr auto BATCH_SIZE = 100;
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
//! Cache databases and their format.
|
|
|
|
//!
|
|
|
|
//! Contains UI information for the joined rooms. (i.e name, topic, avatar url etc).
|
|
|
|
//! Format: room_id -> RoomInfo
|
2018-06-10 20:03:45 +03:00
|
|
|
constexpr auto ROOMS_DB("rooms");
|
|
|
|
constexpr auto INVITES_DB("invites");
|
2021-06-16 01:09:45 +03:00
|
|
|
//! maps each room to its parent space (id->id)
|
|
|
|
constexpr auto SPACES_PARENTS_DB("space_parents");
|
|
|
|
//! maps each space to its current children (id->id)
|
|
|
|
constexpr auto SPACES_CHILDREN_DB("space_children");
|
2018-04-21 16:34:50 +03:00
|
|
|
//! Information that must be kept between sync requests.
|
2018-06-10 20:03:45 +03:00
|
|
|
constexpr auto SYNC_STATE_DB("sync_state");
|
2018-04-21 16:34:50 +03:00
|
|
|
//! Read receipts per room/event.
|
2018-06-10 20:03:45 +03:00
|
|
|
constexpr auto READ_RECEIPTS_DB("read_receipts");
|
|
|
|
constexpr auto NOTIFICATIONS_DB("sent_notifications");
|
|
|
|
|
|
|
|
//! Encryption related databases.
|
|
|
|
|
|
|
|
//! user_id -> list of devices
|
|
|
|
constexpr auto DEVICES_DB("devices");
|
|
|
|
//! device_id -> device keys
|
|
|
|
constexpr auto DEVICE_KEYS_DB("device_keys");
|
|
|
|
//! room_ids that have encryption enabled.
|
2018-06-12 09:45:26 +03:00
|
|
|
constexpr auto ENCRYPTED_ROOMS_DB("encrypted_rooms");
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2018-06-15 01:35:31 +03:00
|
|
|
//! room_id -> pickled OlmInboundGroupSession
|
2018-06-10 20:03:45 +03:00
|
|
|
constexpr auto INBOUND_MEGOLM_SESSIONS_DB("inbound_megolm_sessions");
|
|
|
|
//! MegolmSessionIndex -> pickled OlmOutboundGroupSession
|
|
|
|
constexpr auto OUTBOUND_MEGOLM_SESSIONS_DB("outbound_megolm_sessions");
|
2021-07-17 02:27:37 +03:00
|
|
|
//! MegolmSessionIndex -> session data about which devices have access to this
|
|
|
|
constexpr auto MEGOLM_SESSIONS_DATA_DB("megolm_sessions_data_db");
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2018-01-15 00:33:12 +03:00
|
|
|
using CachedReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
|
|
|
|
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
|
|
|
|
|
2019-12-15 01:39:02 +03:00
|
|
|
Q_DECLARE_METATYPE(RoomMember)
|
|
|
|
Q_DECLARE_METATYPE(mtx::responses::Timeline)
|
|
|
|
Q_DECLARE_METATYPE(RoomSearchResult)
|
|
|
|
Q_DECLARE_METATYPE(RoomInfo)
|
2020-10-02 02:14:42 +03:00
|
|
|
Q_DECLARE_METATYPE(mtx::responses::QueryKeys)
|
2019-12-15 01:39:02 +03:00
|
|
|
|
2018-05-08 20:30:09 +03:00
|
|
|
namespace {
|
2018-05-11 13:41:46 +03:00
|
|
|
std::unique_ptr<Cache> instance_ = nullptr;
|
2018-05-08 20:30:09 +03:00
|
|
|
}
|
|
|
|
|
2021-07-10 17:12:38 +03:00
|
|
|
struct RO_txn
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
~RO_txn() { txn.reset(); }
|
|
|
|
operator MDB_txn *() const noexcept { return txn.handle(); }
|
|
|
|
operator lmdb::txn &() noexcept { return txn; }
|
2021-07-10 17:12:38 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::txn &txn;
|
2021-07-10 17:12:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
RO_txn
|
|
|
|
ro_txn(lmdb::env &env)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
thread_local lmdb::txn txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
|
|
|
|
thread_local int reuse_counter = 0;
|
2021-07-10 17:12:38 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (reuse_counter >= 100 || txn.env() != env.handle()) {
|
|
|
|
txn.abort();
|
|
|
|
txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
|
|
|
|
reuse_counter = 0;
|
|
|
|
} else if (reuse_counter > 0) {
|
|
|
|
try {
|
|
|
|
txn.renew();
|
|
|
|
} catch (...) {
|
|
|
|
txn.abort();
|
|
|
|
txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
|
|
|
|
reuse_counter = 0;
|
2021-07-10 17:12:38 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
reuse_counter++;
|
2021-07-10 17:12:38 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return RO_txn{txn};
|
2021-07-10 17:12:38 +03:00
|
|
|
}
|
|
|
|
|
2021-03-15 19:11:02 +03:00
|
|
|
template<class T>
|
|
|
|
bool
|
|
|
|
containsStateUpdates(const T &e)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::visit([](const auto &ev) { return Cache::isStateEvent_<decltype(ev)>; }, e);
|
2021-03-15 19:11:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
containsStateUpdates(const mtx::events::collections::StrippedEvents &e)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2021-03-15 19:11:02 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::holds_alternative<StrippedEvent<state::Avatar>>(e) ||
|
|
|
|
std::holds_alternative<StrippedEvent<CanonicalAlias>>(e) ||
|
|
|
|
std::holds_alternative<StrippedEvent<Name>>(e) ||
|
|
|
|
std::holds_alternative<StrippedEvent<Member>>(e) ||
|
|
|
|
std::holds_alternative<StrippedEvent<Topic>>(e);
|
2021-03-15 19:11:02 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 22:49:05 +03:00
|
|
|
bool
|
|
|
|
Cache::isHiddenEvent(lmdb::txn &txn,
|
|
|
|
mtx::events::collections::TimelineEvents e,
|
|
|
|
const std::string &room_id)
|
2020-08-18 00:30:36 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
2021-01-27 04:45:33 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Always hide edits
|
|
|
|
if (mtx::accessors::relations(e).replaces())
|
|
|
|
return true;
|
2020-08-18 00:30:36 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (auto encryptedEvent = std::get_if<EncryptedEvent<msg::Encrypted>>(&e)) {
|
|
|
|
MegolmSessionIndex index;
|
|
|
|
index.room_id = room_id;
|
|
|
|
index.session_id = encryptedEvent->content.session_id;
|
|
|
|
index.sender_key = encryptedEvent->content.sender_key;
|
|
|
|
|
|
|
|
auto result = olm::decryptEvent(index, *encryptedEvent, true);
|
|
|
|
if (!result.error)
|
|
|
|
e = result.event.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx::events::account_data::nheko_extensions::HiddenEvents hiddenEvents;
|
|
|
|
hiddenEvents.hidden_event_types = {
|
|
|
|
EventType::Reaction, EventType::CallCandidates, EventType::Unsupported};
|
|
|
|
|
|
|
|
if (auto temp = getAccountData(txn, mtx::events::EventType::NhekoHiddenEvents, ""))
|
|
|
|
hiddenEvents =
|
|
|
|
std::move(std::get<mtx::events::AccountDataEvent<
|
|
|
|
mtx::events::account_data::nheko_extensions::HiddenEvents>>(*temp)
|
|
|
|
.content);
|
|
|
|
if (auto temp = getAccountData(txn, mtx::events::EventType::NhekoHiddenEvents, room_id))
|
|
|
|
hiddenEvents =
|
|
|
|
std::move(std::get<mtx::events::AccountDataEvent<
|
|
|
|
mtx::events::account_data::nheko_extensions::HiddenEvents>>(*temp)
|
|
|
|
.content);
|
|
|
|
|
|
|
|
return std::visit(
|
|
|
|
[hiddenEvents](const auto &ev) {
|
|
|
|
return std::any_of(hiddenEvents.hidden_event_types.begin(),
|
|
|
|
hiddenEvents.hidden_event_types.end(),
|
|
|
|
[ev](EventType type) { return type == ev.type; });
|
|
|
|
},
|
|
|
|
e);
|
2020-08-18 00:30:36 +03:00
|
|
|
}
|
|
|
|
|
2018-01-21 22:43:21 +03:00
|
|
|
Cache::Cache(const QString &userId, QObject *parent)
|
|
|
|
: QObject{parent}
|
|
|
|
, env_{nullptr}
|
2018-04-21 16:34:50 +03:00
|
|
|
, localUserId_{userId}
|
2018-06-12 20:36:16 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
connect(this, &Cache::userKeysUpdate, this, &Cache::updateUserKeys, Qt::QueuedConnection);
|
2021-09-18 01:21:14 +03:00
|
|
|
connect(
|
|
|
|
this,
|
|
|
|
&Cache::verificationStatusChanged,
|
|
|
|
this,
|
|
|
|
[this](const std::string &u) {
|
|
|
|
if (u == localUserId_.toStdString()) {
|
|
|
|
auto status = verificationStatus(u);
|
2021-11-02 00:20:15 +03:00
|
|
|
emit selfVerificationStatusChanged();
|
2021-09-18 01:21:14 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Qt::QueuedConnection);
|
2021-11-07 05:38:48 +03:00
|
|
|
setup();
|
2018-06-12 20:36:16 +03:00
|
|
|
}
|
2017-10-03 21:16:31 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
Cache::setup()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto settings = UserSettings::instance();
|
2017-10-03 21:16:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->debug("setting up cache");
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Previous location of the cache directory
|
|
|
|
auto oldCache = QString("%1/%2%3")
|
|
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
|
|
|
|
.arg(QString::fromUtf8(localUserId_.toUtf8().toHex()))
|
|
|
|
.arg(QString::fromUtf8(settings->profile().toUtf8().toHex()));
|
2021-01-19 21:44:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cacheDirectory_ = QString("%1/%2%3")
|
|
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation))
|
|
|
|
.arg(QString::fromUtf8(localUserId_.toUtf8().toHex()))
|
|
|
|
.arg(QString::fromUtf8(settings->profile().toUtf8().toHex()));
|
2017-10-03 21:16:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool isInitial = !QFile::exists(cacheDirectory_);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// NOTE: If both cache directories exist it's better to do nothing: it
|
|
|
|
// could mean a previous migration failed or was interrupted.
|
|
|
|
bool needsMigration = isInitial && QFile::exists(oldCache);
|
2021-01-19 21:44:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (needsMigration) {
|
|
|
|
nhlog::db()->info("found old state directory, migrating");
|
|
|
|
if (!QDir().rename(oldCache, cacheDirectory_)) {
|
|
|
|
throw std::runtime_error(("Unable to migrate the old state directory (" + oldCache +
|
|
|
|
") to the new location (" + cacheDirectory_ + ")")
|
|
|
|
.toStdString()
|
|
|
|
.c_str());
|
2021-01-19 21:44:22 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->info("completed state migration");
|
|
|
|
}
|
2021-01-19 21:44:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
env_ = lmdb::env::create();
|
|
|
|
env_.set_mapsize(DB_SIZE);
|
|
|
|
env_.set_max_dbs(MAX_DBS);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (isInitial) {
|
|
|
|
nhlog::db()->info("initializing LMDB");
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!QDir().mkpath(cacheDirectory_)) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
("Unable to create state directory:" + cacheDirectory_).toStdString().c_str());
|
2017-08-26 13:49:16 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
// NOTE(Nico): We may want to use (MDB_MAPASYNC | MDB_WRITEMAP) in the future, but
|
|
|
|
// it can really mess up our database, so we shouldn't. For now, hopefully
|
|
|
|
// NOMETASYNC is fast enough.
|
|
|
|
env_.open(cacheDirectory_.toStdString().c_str(), MDB_NOMETASYNC | MDB_NOSYNC);
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
if (e.code() != MDB_VERSION_MISMATCH && e.code() != MDB_INVALID) {
|
|
|
|
throw std::runtime_error("LMDB initialization failed" + std::string(e.what()));
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->warn("resetting cache due to LMDB version mismatch: {}", e.what());
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QDir stateDir(cacheDirectory_);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &file : stateDir.entryList(QDir::NoDotAndDotDot)) {
|
|
|
|
if (!stateDir.remove(file))
|
|
|
|
throw std::runtime_error(("Unable to delete file " + file).toStdString().c_str());
|
2017-08-26 13:49:16 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
env_.open(cacheDirectory_.toStdString().c_str());
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
syncStateDb_ = lmdb::dbi::open(txn, SYNC_STATE_DB, MDB_CREATE);
|
|
|
|
roomsDb_ = lmdb::dbi::open(txn, ROOMS_DB, MDB_CREATE);
|
|
|
|
spacesChildrenDb_ = lmdb::dbi::open(txn, SPACES_CHILDREN_DB, MDB_CREATE | MDB_DUPSORT);
|
|
|
|
spacesParentsDb_ = lmdb::dbi::open(txn, SPACES_PARENTS_DB, MDB_CREATE | MDB_DUPSORT);
|
|
|
|
invitesDb_ = lmdb::dbi::open(txn, INVITES_DB, MDB_CREATE);
|
|
|
|
readReceiptsDb_ = lmdb::dbi::open(txn, READ_RECEIPTS_DB, MDB_CREATE);
|
|
|
|
notificationsDb_ = lmdb::dbi::open(txn, NOTIFICATIONS_DB, MDB_CREATE);
|
2021-07-31 18:59:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Device management
|
|
|
|
devicesDb_ = lmdb::dbi::open(txn, DEVICES_DB, MDB_CREATE);
|
|
|
|
deviceKeysDb_ = lmdb::dbi::open(txn, DEVICE_KEYS_DB, MDB_CREATE);
|
|
|
|
|
|
|
|
// Session management
|
|
|
|
inboundMegolmSessionDb_ = lmdb::dbi::open(txn, INBOUND_MEGOLM_SESSIONS_DB, MDB_CREATE);
|
|
|
|
outboundMegolmSessionDb_ = lmdb::dbi::open(txn, OUTBOUND_MEGOLM_SESSIONS_DB, MDB_CREATE);
|
|
|
|
megolmSessionDataDb_ = lmdb::dbi::open(txn, MEGOLM_SESSIONS_DATA_DB, MDB_CREATE);
|
2021-05-15 00:35:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// What rooms are encrypted
|
|
|
|
encryptedRooms_ = lmdb::dbi::open(txn, ENCRYPTED_ROOMS_DB, MDB_CREATE);
|
|
|
|
[[maybe_unused]] auto verificationDb = getVerificationDb(txn);
|
|
|
|
[[maybe_unused]] auto userKeysDb = getUserKeysDb(txn);
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
|
2021-11-07 05:38:48 +03:00
|
|
|
loadSecrets({
|
|
|
|
{mtx::secret_storage::secrets::cross_signing_master, false},
|
|
|
|
{mtx::secret_storage::secrets::cross_signing_self_signing, false},
|
|
|
|
{mtx::secret_storage::secrets::cross_signing_user_signing, false},
|
|
|
|
{mtx::secret_storage::secrets::megolm_backup_v1, false},
|
|
|
|
{"pickle_secret", true},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fatalSecretError()
|
|
|
|
{
|
|
|
|
QMessageBox::critical(
|
|
|
|
ChatPage::instance(),
|
|
|
|
QCoreApplication::translate("SecretStorage", "Failed to connect to secret storage"),
|
|
|
|
QCoreApplication::translate(
|
|
|
|
"SecretStorage",
|
|
|
|
"Nheko could not connect to the secure storage to save encryption secrets to. This can "
|
|
|
|
"have multiple reasons. Check if your D-Bus service is running and you have configured a "
|
|
|
|
"service like KWallet, Gnome Keyring, KeePassXC or the equivalent for your platform. If "
|
|
|
|
"you are having trouble, feel free to open an issue here: "
|
|
|
|
"https://github.com/Nheko-Reborn/nheko/issues"));
|
|
|
|
|
|
|
|
QCoreApplication::exit(1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString
|
|
|
|
secretName(std::string name, bool internal)
|
|
|
|
{
|
|
|
|
auto settings = UserSettings::instance();
|
|
|
|
return (internal ? "nheko." : "matrix.") +
|
|
|
|
QString(
|
|
|
|
QCryptographicHash::hash(settings->profile().toUtf8(), QCryptographicHash::Sha256)
|
|
|
|
.toBase64()) +
|
|
|
|
"." + QString::fromStdString(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::loadSecrets(std::vector<std::pair<std::string, bool>> toLoad)
|
|
|
|
{
|
|
|
|
if (toLoad.empty()) {
|
|
|
|
this->databaseReady_ = true;
|
|
|
|
emit databaseReady();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto [name_, internal] = toLoad.front();
|
|
|
|
|
|
|
|
auto job = new QKeychain::ReadPasswordJob(QCoreApplication::applicationName());
|
|
|
|
job->setAutoDelete(true);
|
|
|
|
job->setInsecureFallback(true);
|
|
|
|
job->setSettings(UserSettings::instance()->qsettings());
|
|
|
|
auto name = secretName(name_, internal);
|
|
|
|
job->setKey(name);
|
|
|
|
|
|
|
|
connect(job,
|
|
|
|
&QKeychain::ReadPasswordJob::finished,
|
|
|
|
this,
|
|
|
|
[this, name, toLoad, job](QKeychain::Job *) mutable {
|
|
|
|
const QString secret = job->textData();
|
|
|
|
if (job->error() && job->error() != QKeychain::Error::EntryNotFound) {
|
|
|
|
nhlog::db()->error("Restoring secret '{}' failed ({}): {}",
|
|
|
|
name.toStdString(),
|
|
|
|
job->error(),
|
|
|
|
job->errorString().toStdString());
|
|
|
|
|
|
|
|
fatalSecretError();
|
|
|
|
}
|
|
|
|
if (secret.isEmpty()) {
|
|
|
|
nhlog::db()->debug("Restored empty secret '{}'.", name.toStdString());
|
|
|
|
} else {
|
|
|
|
std::unique_lock lock(secret_storage.mtx);
|
|
|
|
secret_storage.secrets[name.toStdString()] = secret.toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// load next secret
|
|
|
|
toLoad.erase(toLoad.begin());
|
|
|
|
|
|
|
|
// You can't start a job from the finish signal of a job.
|
|
|
|
QTimer::singleShot(0, [this, toLoad] { loadSecrets(toLoad); });
|
|
|
|
});
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string>
|
|
|
|
Cache::secret(const std::string name_, bool internal)
|
|
|
|
{
|
|
|
|
auto name = secretName(name_, internal);
|
|
|
|
std::unique_lock lock(secret_storage.mtx);
|
|
|
|
if (auto secret = secret_storage.secrets.find(name.toStdString());
|
|
|
|
secret != secret_storage.secrets.end())
|
|
|
|
return secret->second;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::storeSecret(const std::string name_, const std::string secret, bool internal)
|
|
|
|
{
|
|
|
|
auto name = secretName(name_, internal);
|
|
|
|
{
|
|
|
|
std::unique_lock lock(secret_storage.mtx);
|
|
|
|
secret_storage.secrets[name.toStdString()] = secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto settings = UserSettings::instance();
|
|
|
|
auto job = new QKeychain::WritePasswordJob(QCoreApplication::applicationName());
|
|
|
|
job->setAutoDelete(true);
|
|
|
|
job->setInsecureFallback(true);
|
|
|
|
job->setSettings(UserSettings::instance()->qsettings());
|
|
|
|
|
|
|
|
job->setKey(name);
|
|
|
|
|
|
|
|
job->setTextData(QString::fromStdString(secret));
|
|
|
|
|
|
|
|
QObject::connect(
|
|
|
|
job,
|
|
|
|
&QKeychain::WritePasswordJob::finished,
|
|
|
|
this,
|
|
|
|
[name_, this](QKeychain::Job *job) {
|
|
|
|
if (job->error()) {
|
|
|
|
nhlog::db()->warn(
|
|
|
|
"Storing secret '{}' failed: {}", name_, job->errorString().toStdString());
|
|
|
|
fatalSecretError();
|
|
|
|
} else {
|
|
|
|
// if we emit the signal directly, qtkeychain breaks and won't execute new
|
|
|
|
// jobs. You can't start a job from the finish signal of a job.
|
|
|
|
QTimer::singleShot(0, [this, name_] { emit secretChanged(name_); });
|
|
|
|
nhlog::db()->info("Storing secret '{}' successful", name_);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Qt::ConnectionType::DirectConnection);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::deleteSecret(const std::string name, bool internal)
|
|
|
|
{
|
|
|
|
auto name_ = secretName(name, internal);
|
|
|
|
{
|
|
|
|
std::unique_lock lock(secret_storage.mtx);
|
|
|
|
secret_storage.secrets.erase(name_.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto settings = UserSettings::instance();
|
|
|
|
auto job = new QKeychain::DeletePasswordJob(QCoreApplication::applicationName());
|
|
|
|
job->setAutoDelete(true);
|
|
|
|
job->setInsecureFallback(true);
|
|
|
|
job->setSettings(UserSettings::instance()->qsettings());
|
|
|
|
|
|
|
|
job->setKey(name_);
|
|
|
|
|
|
|
|
job->connect(
|
|
|
|
job, &QKeychain::Job::finished, this, [this, name]() { emit secretChanged(name); });
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Cache::pickleSecret()
|
|
|
|
{
|
|
|
|
if (pickle_secret_.empty()) {
|
|
|
|
auto s = secret("pickle_secret", true);
|
|
|
|
if (!s) {
|
|
|
|
this->pickle_secret_ = mtx::client::utils::random_token(64, true);
|
|
|
|
storeSecret("pickle_secret", pickle_secret_, true);
|
|
|
|
} else {
|
|
|
|
this->pickle_secret_ = *s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pickle_secret_;
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
2018-06-12 09:45:26 +03:00
|
|
|
void
|
2018-06-18 18:36:19 +03:00
|
|
|
Cache::setEncryptedRoom(lmdb::txn &txn, const std::string &room_id)
|
2018-06-12 09:45:26 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->info("mark room {} as encrypted", room_id);
|
2018-06-12 09:45:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
encryptedRooms_.put(txn, room_id, "0");
|
2018-06-12 09:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Cache::isRoomEncrypted(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view unused;
|
2018-06-12 09:45:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto res = encryptedRooms_.get(txn, room_id, unused);
|
2018-06-12 09:45:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return res;
|
2018-06-12 09:45:26 +03:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:24:01 +03:00
|
|
|
std::optional<mtx::events::state::Encryption>
|
|
|
|
Cache::roomEncryptionSettings(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2021-03-15 18:24:01 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto statesdb = getStatesDb(txn, room_id);
|
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomEncryption), event);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<Encryption> msg = json::parse(event);
|
|
|
|
|
|
|
|
return msg.content;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.encryption event: {}", e.what());
|
|
|
|
return Encryption{};
|
|
|
|
}
|
2021-03-15 18:24:01 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (lmdb::error &) {
|
|
|
|
}
|
2021-03-15 18:24:01 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::nullopt;
|
2021-03-15 18:24:01 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 23:52:14 +03:00
|
|
|
mtx::crypto::ExportedSessionKeys
|
|
|
|
Cache::exportSessionKeys()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
ExportedSessionKeys keys;
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, inboundMegolmSessionDb_);
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view key, value;
|
|
|
|
while (cursor.get(key, value, MDB_NEXT)) {
|
|
|
|
ExportedSession exported;
|
|
|
|
MegolmSessionIndex index;
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto saved_session = unpickle<InboundSessionObject>(std::string(value), pickle_secret_);
|
2018-09-16 11:19:18 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
index = nlohmann::json::parse(key).get<MegolmSessionIndex>();
|
|
|
|
} catch (const nlohmann::json::exception &e) {
|
|
|
|
nhlog::db()->critical("failed to export megolm session: {}", e.what());
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
exported.room_id = index.room_id;
|
|
|
|
exported.sender_key = index.sender_key;
|
|
|
|
exported.session_id = index.session_id;
|
|
|
|
exported.session_key = export_session(saved_session.get(), -1);
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
keys.sessions.push_back(exported);
|
|
|
|
}
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return keys;
|
2018-09-15 23:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::importSessionKeys(const mtx::crypto::ExportedSessionKeys &keys)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &s : keys.sessions) {
|
|
|
|
MegolmSessionIndex index;
|
|
|
|
index.room_id = s.room_id;
|
|
|
|
index.session_id = s.session_id;
|
|
|
|
index.sender_key = s.sender_key;
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
GroupSessionData data{};
|
|
|
|
data.forwarding_curve25519_key_chain = s.forwarding_curve25519_key_chain;
|
|
|
|
if (s.sender_claimed_keys.count("ed25519"))
|
|
|
|
data.sender_claimed_ed25519_key = s.sender_claimed_keys.at("ed25519");
|
2021-07-17 02:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto exported_session = mtx::crypto::import_session(s.session_key);
|
2018-09-15 23:52:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
saveInboundMegolmSession(index, std::move(exported_session), data);
|
|
|
|
ChatPage::instance()->receivedSessionKey(index.room_id, index.session_id);
|
|
|
|
}
|
2018-09-15 23:52:14 +03:00
|
|
|
}
|
2018-06-10 20:03:45 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Session Management
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::saveInboundMegolmSession(const MegolmSessionIndex &index,
|
2021-07-17 02:27:37 +03:00
|
|
|
mtx::crypto::InboundGroupSessionPtr session,
|
|
|
|
const GroupSessionData &data)
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
|
|
|
const auto key = json(index).dump();
|
|
|
|
const auto pickled = pickle<InboundSessionObject>(session.get(), pickle_secret_);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2021-04-20 15:19:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view value;
|
|
|
|
if (inboundMegolmSessionDb_.get(txn, key, value)) {
|
|
|
|
auto oldSession = unpickle<InboundSessionObject>(std::string(value), pickle_secret_);
|
|
|
|
if (olm_inbound_group_session_first_known_index(session.get()) >
|
|
|
|
olm_inbound_group_session_first_known_index(oldSession.get())) {
|
|
|
|
nhlog::crypto()->warn("Not storing inbound session with newer first known index");
|
|
|
|
return;
|
2021-04-20 15:19:07 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-04-20 15:19:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
inboundMegolmSessionDb_.put(txn, key, pickled);
|
|
|
|
megolmSessionDataDb_.put(txn, key, json(data).dump());
|
|
|
|
txn.commit();
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
2020-11-27 06:19:03 +03:00
|
|
|
mtx::crypto::InboundGroupSessionPtr
|
2018-06-10 20:03:45 +03:00
|
|
|
Cache::getInboundMegolmSession(const MegolmSessionIndex &index)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2020-11-27 06:19:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::string key = json(index).dump();
|
|
|
|
std::string_view value;
|
|
|
|
|
|
|
|
if (inboundMegolmSessionDb_.get(txn, key, value)) {
|
|
|
|
auto session = unpickle<InboundSessionObject>(std::string(value), pickle_secret_);
|
|
|
|
return session;
|
2020-11-27 06:19:03 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to get inbound megolm session {}", e.what());
|
|
|
|
}
|
2020-11-27 06:19:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return nullptr;
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-09-15 23:52:14 +03:00
|
|
|
Cache::inboundMegolmSessionExists(const MegolmSessionIndex &index)
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2020-11-27 06:19:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::string key = json(index).dump();
|
|
|
|
std::string_view value;
|
2020-11-27 06:19:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return inboundMegolmSessionDb_.get(txn, key, value);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to get inbound megolm session {}", e.what());
|
|
|
|
}
|
2020-11-27 06:19:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return false;
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 12:28:00 +03:00
|
|
|
void
|
2020-11-27 06:56:44 +03:00
|
|
|
Cache::updateOutboundMegolmSession(const std::string &room_id,
|
2021-07-17 02:27:37 +03:00
|
|
|
const GroupSessionData &data_,
|
2020-11-27 06:56:44 +03:00
|
|
|
mtx::crypto::OutboundGroupSessionPtr &ptr)
|
2018-06-13 12:28:00 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!outboundMegolmSessionExists(room_id))
|
|
|
|
return;
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
GroupSessionData data = data_;
|
|
|
|
data.message_index = olm_outbound_group_session_message_index(ptr.get());
|
|
|
|
MegolmSessionIndex index;
|
|
|
|
index.room_id = room_id;
|
|
|
|
index.sender_key = olm::client()->identity_keys().ed25519;
|
|
|
|
index.session_id = mtx::crypto::session_id(ptr.get());
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Save the updated pickled data for the session.
|
|
|
|
json j;
|
|
|
|
j["session"] = pickle<OutboundSessionObject>(ptr.get(), pickle_secret_);
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
outboundMegolmSessionDb_.put(txn, room_id, j.dump());
|
|
|
|
megolmSessionDataDb_.put(txn, json(index).dump(), json(data).dump());
|
|
|
|
txn.commit();
|
2018-06-13 12:28:00 +03:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:38:28 +03:00
|
|
|
void
|
|
|
|
Cache::dropOutboundMegolmSession(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2020-10-03 19:38:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!outboundMegolmSessionExists(room_id))
|
|
|
|
return;
|
2020-10-03 19:38:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
outboundMegolmSessionDb_.del(txn, room_id);
|
|
|
|
// don't delete session data, so that we can still share the session.
|
|
|
|
txn.commit();
|
|
|
|
}
|
2020-10-03 19:38:28 +03:00
|
|
|
}
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
void
|
2018-06-12 22:35:10 +03:00
|
|
|
Cache::saveOutboundMegolmSession(const std::string &room_id,
|
2021-07-17 02:27:37 +03:00
|
|
|
const GroupSessionData &data_,
|
2020-11-30 02:26:27 +03:00
|
|
|
mtx::crypto::OutboundGroupSessionPtr &session)
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
|
|
|
const auto pickled = pickle<OutboundSessionObject>(session.get(), pickle_secret_);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
GroupSessionData data = data_;
|
|
|
|
data.message_index = olm_outbound_group_session_message_index(session.get());
|
|
|
|
MegolmSessionIndex index;
|
|
|
|
index.room_id = room_id;
|
|
|
|
index.sender_key = olm::client()->identity_keys().ed25519;
|
|
|
|
index.session_id = mtx::crypto::session_id(session.get());
|
2021-07-17 02:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
json j;
|
|
|
|
j["session"] = pickled;
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
outboundMegolmSessionDb_.put(txn, room_id, j.dump());
|
|
|
|
megolmSessionDataDb_.put(txn, json(index).dump(), json(data).dump());
|
|
|
|
txn.commit();
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-06-13 12:28:00 +03:00
|
|
|
Cache::outboundMegolmSessionExists(const std::string &room_id) noexcept
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::string_view value;
|
|
|
|
return outboundMegolmSessionDb_.get(txn, room_id, value);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to retrieve outbound Megolm Session: {}", e.what());
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
OutboundGroupSessionDataRef
|
2018-06-13 12:28:00 +03:00
|
|
|
Cache::getOutboundMegolmSession(const std::string &room_id)
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
using namespace mtx::crypto;
|
2020-11-27 06:56:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::string_view value;
|
|
|
|
outboundMegolmSessionDb_.get(txn, room_id, value);
|
|
|
|
auto obj = json::parse(value);
|
2021-07-17 02:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
OutboundGroupSessionDataRef ref{};
|
|
|
|
ref.session = unpickle<OutboundSessionObject>(obj.at("session"), pickle_secret_);
|
2021-07-17 02:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
MegolmSessionIndex index;
|
|
|
|
index.room_id = room_id;
|
|
|
|
index.sender_key = olm::client()->identity_keys().ed25519;
|
|
|
|
index.session_id = mtx::crypto::session_id(ref.session.get());
|
2021-07-17 02:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (megolmSessionDataDb_.get(txn, json(index).dump(), value)) {
|
|
|
|
ref.data = nlohmann::json::parse(value).get<GroupSessionData>();
|
2020-11-27 06:56:44 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
return ref;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to retrieve outbound Megolm Session: {}", e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
2021-07-17 03:14:44 +03:00
|
|
|
std::optional<GroupSessionData>
|
|
|
|
Cache::getMegolmSessionData(const MegolmSessionIndex &index)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
using namespace mtx::crypto;
|
2021-07-17 03:14:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2021-07-17 03:14:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view value;
|
|
|
|
if (megolmSessionDataDb_.get(txn, json(index).dump(), value)) {
|
|
|
|
return nlohmann::json::parse(value).get<GroupSessionData>();
|
2021-07-17 03:14:44 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to retrieve Megolm Session Data: {}", e.what());
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-07-17 03:14:44 +03:00
|
|
|
}
|
2018-06-15 01:35:31 +03:00
|
|
|
//
|
|
|
|
// OLM sessions.
|
|
|
|
//
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
void
|
2020-10-20 14:46:05 +03:00
|
|
|
Cache::saveOlmSession(const std::string &curve25519,
|
|
|
|
mtx::crypto::OlmSessionPtr session,
|
|
|
|
uint64_t timestamp)
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getOlmSessionsDb(txn, curve25519);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto pickled = pickle<SessionObject>(session.get(), pickle_secret_);
|
|
|
|
const auto session_id = mtx::crypto::session_id(session.get());
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
StoredOlmSession stored_session;
|
|
|
|
stored_session.pickled_session = pickled;
|
|
|
|
stored_session.last_message_ts = timestamp;
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
db.put(txn, session_id, json(stored_session).dump());
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
2019-12-14 19:08:36 +03:00
|
|
|
std::optional<mtx::crypto::OlmSessionPtr>
|
2018-06-15 01:35:31 +03:00
|
|
|
Cache::getOlmSession(const std::string &curve25519, const std::string &session_id)
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getOlmSessionsDb(txn, curve25519);
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view pickled;
|
|
|
|
bool found = db.get(txn, session_id, pickled);
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (found) {
|
|
|
|
auto data = json::parse(pickled).get<StoredOlmSession>();
|
|
|
|
return unpickle<SessionObject>(data.pickled_session, pickle_secret_);
|
|
|
|
}
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::nullopt;
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
2020-10-20 14:46:05 +03:00
|
|
|
std::optional<mtx::crypto::OlmSessionPtr>
|
|
|
|
Cache::getLatestOlmSession(const std::string &curve25519)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getOlmSessionsDb(txn, curve25519);
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view session_id, pickled_session;
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::optional<StoredOlmSession> currentNewest;
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
|
|
|
while (cursor.get(session_id, pickled_session, MDB_NEXT)) {
|
|
|
|
auto data = json::parse(pickled_session).get<StoredOlmSession>();
|
|
|
|
if (!currentNewest || currentNewest->last_message_ts < data.last_message_ts)
|
|
|
|
currentNewest = data;
|
|
|
|
}
|
|
|
|
cursor.close();
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return currentNewest ? std::optional(unpickle<SessionObject>(currentNewest->pickled_session,
|
|
|
|
pickle_secret_))
|
|
|
|
: std::nullopt;
|
2020-10-20 14:46:05 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 01:35:31 +03:00
|
|
|
std::vector<std::string>
|
|
|
|
Cache::getOlmSessions(const std::string &curve25519)
|
2018-06-10 20:03:45 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::crypto;
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getOlmSessionsDb(txn, curve25519);
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view session_id, unused;
|
|
|
|
std::vector<std::string> res;
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
|
|
|
while (cursor.get(session_id, unused, MDB_NEXT))
|
|
|
|
res.emplace_back(session_id);
|
|
|
|
cursor.close();
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return res;
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::saveOlmAccount(const std::string &data)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
syncStateDb_.put(txn, OLM_ACCOUNT_KEY, data);
|
|
|
|
txn.commit();
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Cache::restoreOlmAccount()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2021-08-31 05:06:51 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view pickled;
|
|
|
|
syncStateDb_.get(txn, OLM_ACCOUNT_KEY, pickled);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::string(pickled.data(), pickled.size());
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2021-08-17 04:23:51 +03:00
|
|
|
void
|
|
|
|
Cache::saveBackupVersion(const OnlineBackupVersion &data)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
syncStateDb_.put(txn, CURRENT_ONLINE_BACKUP_VERSION, nlohmann::json(data).dump());
|
|
|
|
txn.commit();
|
2021-08-17 04:23:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::deleteBackupVersion()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
syncStateDb_.del(txn, CURRENT_ONLINE_BACKUP_VERSION);
|
|
|
|
txn.commit();
|
2021-08-17 04:23:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<OnlineBackupVersion>
|
|
|
|
Cache::backupVersion()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::string_view v;
|
|
|
|
syncStateDb_.get(txn, CURRENT_ONLINE_BACKUP_VERSION, v);
|
2021-08-17 04:23:51 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return nlohmann::json::parse(v).get<OnlineBackupVersion>();
|
|
|
|
} catch (...) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-08-17 04:23:51 +03:00
|
|
|
}
|
|
|
|
|
2018-04-22 12:26:41 +03:00
|
|
|
void
|
|
|
|
Cache::removeInvite(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
invitesDb_.del(txn, room_id);
|
|
|
|
getInviteStatesDb(txn, room_id).drop(txn, true);
|
|
|
|
getInviteMembersDb(txn, room_id).drop(txn, true);
|
2018-04-22 12:26:41 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
Cache::removeInvite(const std::string &room_id)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
removeInvite(txn, room_id);
|
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
Cache::removeRoom(lmdb::txn &txn, const std::string &roomid)
|
2017-10-01 19:49:36 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
roomsDb_.del(txn, roomid);
|
|
|
|
getStatesDb(txn, roomid).drop(txn, true);
|
|
|
|
getAccountDataDb(txn, roomid).drop(txn, true);
|
|
|
|
getMembersDb(txn, roomid).drop(txn, true);
|
2017-10-01 19:49:36 +03:00
|
|
|
}
|
|
|
|
|
2017-12-19 23:36:12 +03:00
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
Cache::removeRoom(const std::string &roomid)
|
2017-12-19 23:36:12 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, 0);
|
|
|
|
roomsDb_.del(txn, roomid);
|
|
|
|
txn.commit();
|
2017-12-19 23:36:12 +03:00
|
|
|
}
|
|
|
|
|
2018-01-21 22:43:21 +03:00
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
Cache::setNextBatchToken(lmdb::txn &txn, const std::string &token)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
syncStateDb_.put(txn, NEXT_BATCH_KEY, token);
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
bool
|
2021-03-03 01:15:12 +03:00
|
|
|
Cache::isInitialized()
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!env_.handle())
|
|
|
|
return false;
|
2021-08-14 00:13:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::string_view token;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool res = syncStateDb_.get(txn, NEXT_BATCH_KEY, token);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return res;
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
std::string
|
2021-03-03 01:15:12 +03:00
|
|
|
Cache::nextBatchToken()
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!env_.handle())
|
|
|
|
throw lmdb::error("Env already closed", MDB_INVALID);
|
2021-03-12 19:13:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::string_view token;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool result = syncStateDb_.get(txn, NEXT_BATCH_KEY, token);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (result)
|
|
|
|
return std::string(token.data(), token.size());
|
|
|
|
else
|
|
|
|
return "";
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
2017-10-22 19:03:55 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
Cache::deleteData()
|
|
|
|
{
|
2021-11-02 00:35:32 +03:00
|
|
|
if (this->databaseReady_) {
|
|
|
|
this->databaseReady_ = false;
|
|
|
|
// TODO: We need to remove the env_ while not accepting new requests.
|
|
|
|
lmdb::dbi_close(env_, syncStateDb_);
|
|
|
|
lmdb::dbi_close(env_, roomsDb_);
|
|
|
|
lmdb::dbi_close(env_, invitesDb_);
|
|
|
|
lmdb::dbi_close(env_, readReceiptsDb_);
|
|
|
|
lmdb::dbi_close(env_, notificationsDb_);
|
2020-12-17 00:10:09 +03:00
|
|
|
|
2021-11-02 00:35:32 +03:00
|
|
|
lmdb::dbi_close(env_, devicesDb_);
|
|
|
|
lmdb::dbi_close(env_, deviceKeysDb_);
|
2020-12-17 00:10:09 +03:00
|
|
|
|
2021-11-02 00:35:32 +03:00
|
|
|
lmdb::dbi_close(env_, inboundMegolmSessionDb_);
|
|
|
|
lmdb::dbi_close(env_, outboundMegolmSessionDb_);
|
|
|
|
lmdb::dbi_close(env_, megolmSessionDataDb_);
|
2020-12-17 00:10:09 +03:00
|
|
|
|
2021-11-02 00:35:32 +03:00
|
|
|
env_.close();
|
2020-12-17 00:10:09 +03:00
|
|
|
|
2021-11-02 00:35:32 +03:00
|
|
|
verification_storage.status.clear();
|
2020-12-17 00:10:09 +03:00
|
|
|
|
2021-11-02 00:35:32 +03:00
|
|
|
if (!cacheDirectory_.isEmpty()) {
|
|
|
|
QDir(cacheDirectory_).removeRecursively();
|
|
|
|
nhlog::db()->info("deleted cache files from disk");
|
|
|
|
}
|
2020-12-17 00:10:09 +03:00
|
|
|
|
2021-11-02 00:35:32 +03:00
|
|
|
deleteSecret(mtx::secret_storage::secrets::megolm_backup_v1);
|
|
|
|
deleteSecret(mtx::secret_storage::secrets::cross_signing_master);
|
|
|
|
deleteSecret(mtx::secret_storage::secrets::cross_signing_user_signing);
|
|
|
|
deleteSecret(mtx::secret_storage::secrets::cross_signing_self_signing);
|
|
|
|
deleteSecret("pickle_secret", true);
|
|
|
|
}
|
2017-10-22 19:03:55 +03:00
|
|
|
}
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2020-05-02 17:44:50 +03:00
|
|
|
//! migrates db to the current format
|
2017-12-10 13:51:44 +03:00
|
|
|
bool
|
2020-05-02 17:44:50 +03:00
|
|
|
Cache::runMigrations()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string stored_version;
|
|
|
|
{
|
|
|
|
auto txn = ro_txn(env_);
|
2020-05-02 18:24:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view current_version;
|
|
|
|
bool res = syncStateDb_.get(txn, CACHE_FORMAT_VERSION_KEY, current_version);
|
2020-05-02 18:24:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
stored_version = std::string(current_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::function<bool()>>> migrations{
|
|
|
|
{"2020.05.01",
|
|
|
|
[this]() {
|
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr);
|
|
|
|
auto pending_receipts = lmdb::dbi::open(txn, "pending_receipts", MDB_CREATE);
|
|
|
|
lmdb::dbi_drop(txn, pending_receipts, true);
|
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &) {
|
|
|
|
nhlog::db()->critical("Failed to delete pending_receipts database in migration!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::db()->info("Successfully deleted pending receipts database.");
|
|
|
|
return true;
|
|
|
|
}},
|
|
|
|
{"2020.07.05",
|
|
|
|
[this]() {
|
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr);
|
|
|
|
auto room_ids = getRoomIds(txn);
|
|
|
|
|
|
|
|
for (const auto &room_id : room_ids) {
|
2020-07-05 06:29:07 +03:00
|
|
|
try {
|
2021-09-18 01:22:33 +03:00
|
|
|
auto messagesDb =
|
|
|
|
lmdb::dbi::open(txn, std::string(room_id + "/messages").c_str());
|
|
|
|
|
|
|
|
// keep some old messages and batch token
|
|
|
|
{
|
|
|
|
auto roomsCursor = lmdb::cursor::open(txn, messagesDb);
|
|
|
|
std::string_view ts, stored_message;
|
|
|
|
bool start = true;
|
|
|
|
mtx::responses::Timeline oldMessages;
|
|
|
|
while (
|
|
|
|
roomsCursor.get(ts, stored_message, start ? MDB_FIRST : MDB_NEXT)) {
|
|
|
|
start = false;
|
|
|
|
|
|
|
|
auto j = json::parse(
|
|
|
|
std::string_view(stored_message.data(), stored_message.size()));
|
|
|
|
|
|
|
|
if (oldMessages.prev_batch.empty())
|
|
|
|
oldMessages.prev_batch = j["token"].get<std::string>();
|
|
|
|
else if (j["token"] != oldMessages.prev_batch)
|
|
|
|
break;
|
|
|
|
|
|
|
|
mtx::events::collections::TimelineEvent te;
|
|
|
|
mtx::events::collections::from_json(j["event"], te);
|
|
|
|
oldMessages.events.push_back(te.data);
|
2020-07-05 06:29:07 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
// messages were stored in reverse order, so we
|
|
|
|
// need to reverse them
|
|
|
|
std::reverse(oldMessages.events.begin(), oldMessages.events.end());
|
|
|
|
// save messages using the new method
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
saveTimelineMessages(txn, eventsDb, room_id, oldMessages);
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete old messages db
|
|
|
|
lmdb::dbi_drop(txn, messagesDb, true);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"While migrating messages from {}, ignoring error {}", room_id, e.what());
|
2020-07-05 06:29:07 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &) {
|
|
|
|
nhlog::db()->critical("Failed to delete messages database in migration!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::db()->info("Successfully deleted pending receipts database.");
|
|
|
|
return true;
|
|
|
|
}},
|
|
|
|
{"2020.10.20",
|
|
|
|
[this]() {
|
|
|
|
try {
|
|
|
|
using namespace mtx::crypto;
|
|
|
|
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
|
|
|
|
auto mainDb = lmdb::dbi::open(txn, nullptr);
|
|
|
|
|
|
|
|
std::string_view dbName, ignored;
|
|
|
|
auto olmDbCursor = lmdb::cursor::open(txn, mainDb);
|
|
|
|
while (olmDbCursor.get(dbName, ignored, MDB_NEXT)) {
|
|
|
|
// skip every db but olm session dbs
|
|
|
|
nhlog::db()->debug("Db {}", dbName);
|
|
|
|
if (dbName.find("olm_sessions/") != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nhlog::db()->debug("Migrating {}", dbName);
|
|
|
|
|
|
|
|
auto olmDb = lmdb::dbi::open(txn, std::string(dbName).c_str());
|
|
|
|
|
|
|
|
std::string_view session_id, session_value;
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, StoredOlmSession>> sessions;
|
|
|
|
|
|
|
|
auto cursor = lmdb::cursor::open(txn, olmDb);
|
|
|
|
while (cursor.get(session_id, session_value, MDB_NEXT)) {
|
|
|
|
nhlog::db()->debug(
|
|
|
|
"session_id {}, session_value {}", session_id, session_value);
|
|
|
|
StoredOlmSession session;
|
|
|
|
bool invalid = false;
|
|
|
|
for (auto c : session_value)
|
|
|
|
if (!isprint(c)) {
|
|
|
|
invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (invalid)
|
|
|
|
continue;
|
2020-07-05 06:29:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->debug("Not skipped");
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
session.pickled_session = session_value;
|
|
|
|
sessions.emplace_back(session_id, session);
|
|
|
|
}
|
|
|
|
cursor.close();
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
olmDb.drop(txn, true);
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto newDbName = std::string(dbName);
|
|
|
|
newDbName.erase(0, sizeof("olm_sessions") - 1);
|
|
|
|
newDbName = "olm_sessions.v2" + newDbName;
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto newDb = lmdb::dbi::open(txn, newDbName.c_str(), MDB_CREATE);
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &[key, value] : sessions) {
|
|
|
|
// nhlog::db()->debug("{}\n{}", key, json(value).dump());
|
|
|
|
newDb.put(txn, key, json(value).dump());
|
2020-10-20 14:46:05 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
olmDbCursor.close();
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &) {
|
|
|
|
nhlog::db()->critical("Failed to migrate olm sessions,");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::db()->info("Successfully migrated olm sessions.");
|
|
|
|
return true;
|
|
|
|
}},
|
|
|
|
{"2021.08.22",
|
|
|
|
[this]() {
|
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr);
|
|
|
|
auto try_drop = [&txn](const std::string &dbName) {
|
2021-08-22 15:45:57 +03:00
|
|
|
try {
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = lmdb::dbi::open(txn, dbName.c_str());
|
|
|
|
db.drop(txn, true);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->warn("Failed to drop '{}': {}", dbName, e.what());
|
2021-08-22 15:45:57 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto room_ids = getRoomIds(txn);
|
|
|
|
|
|
|
|
for (const auto &room : room_ids) {
|
|
|
|
try_drop(room + "/state");
|
|
|
|
try_drop(room + "/state_by_key");
|
|
|
|
try_drop(room + "/account_data");
|
|
|
|
try_drop(room + "/members");
|
|
|
|
try_drop(room + "/mentions");
|
|
|
|
try_drop(room + "/events");
|
|
|
|
try_drop(room + "/event_order");
|
|
|
|
try_drop(room + "/event2order");
|
|
|
|
try_drop(room + "/msg2order");
|
|
|
|
try_drop(room + "/order2msg");
|
|
|
|
try_drop(room + "/pending");
|
|
|
|
try_drop(room + "/related");
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear db, don't delete
|
|
|
|
roomsDb_.drop(txn, false);
|
|
|
|
setNextBatchToken(txn, "");
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &) {
|
|
|
|
nhlog::db()->critical("Failed to clear cache!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::db()->info("Successfully cleared the cache. Will do a clean sync after startup.");
|
|
|
|
return true;
|
|
|
|
}},
|
|
|
|
{"2021.08.31",
|
|
|
|
[this]() {
|
|
|
|
storeSecret("pickle_secret", "secret", true);
|
|
|
|
return true;
|
|
|
|
}},
|
|
|
|
};
|
|
|
|
|
|
|
|
nhlog::db()->info("Running migrations, this may take a while!");
|
|
|
|
for (const auto &[target_version, migration] : migrations) {
|
|
|
|
if (target_version > stored_version)
|
|
|
|
if (!migration()) {
|
|
|
|
nhlog::db()->critical("migration failure!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nhlog::db()->info("Migrations finished.");
|
2021-08-22 15:45:57 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setCurrentFormat();
|
|
|
|
return true;
|
2020-05-02 17:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cache::CacheVersion
|
|
|
|
Cache::formatVersion()
|
2017-12-10 13:51:44 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view current_version;
|
|
|
|
bool res = syncStateDb_.get(txn, CACHE_FORMAT_VERSION_KEY, current_version);
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!res)
|
|
|
|
return cache::CacheVersion::Older;
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string stored_version(current_version.data(), current_version.size());
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (stored_version < CURRENT_CACHE_FORMAT_VERSION)
|
|
|
|
return cache::CacheVersion::Older;
|
|
|
|
else if (stored_version > CURRENT_CACHE_FORMAT_VERSION)
|
|
|
|
return cache::CacheVersion::Older;
|
|
|
|
else
|
|
|
|
return cache::CacheVersion::Current;
|
2017-12-10 13:51:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::setCurrentFormat()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
syncStateDb_.put(txn, CACHE_FORMAT_VERSION_KEY, CURRENT_CACHE_FORMAT_VERSION);
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2017-12-10 13:51:44 +03:00
|
|
|
}
|
2017-12-19 23:36:12 +03:00
|
|
|
|
2018-01-15 00:33:12 +03:00
|
|
|
CachedReceipts
|
2018-01-03 19:05:49 +03:00
|
|
|
Cache::readReceipts(const QString &event_id, const QString &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
CachedReceipts receipts;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
ReadReceiptKey receipt_key{event_id.toStdString(), room_id.toStdString()};
|
|
|
|
nlohmann::json json_key = receipt_key;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto key = json_key.dump();
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view value;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool res = readReceiptsDb_.get(txn, key, value);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
auto json_response = json::parse(std::string_view(value.data(), value.size()));
|
|
|
|
auto values = json_response.get<std::map<std::string, uint64_t>>();
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &v : values)
|
|
|
|
// timestamp, user_id
|
|
|
|
receipts.emplace(v.second, v.first);
|
2018-01-03 19:05:49 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->critical("readReceipts: {}", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return receipts;
|
2018-01-03 19:05:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-04-21 17:14:16 +03:00
|
|
|
Cache::updateReadReceipt(lmdb::txn &txn, const std::string &room_id, const Receipts &receipts)
|
2018-01-03 19:05:49 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto user_id = this->localUserId_.toStdString();
|
|
|
|
for (const auto &receipt : receipts) {
|
|
|
|
const auto event_id = receipt.first;
|
|
|
|
auto event_receipts = receipt.second;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
ReadReceiptKey receipt_key{event_id, room_id};
|
|
|
|
nlohmann::json json_key = receipt_key;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
const auto key = json_key.dump();
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view prev_value;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool exists = readReceiptsDb_.get(txn, key, prev_value);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::map<std::string, uint64_t> saved_receipts;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// If an entry for the event id already exists, we would
|
|
|
|
// merge the existing receipts with the new ones.
|
|
|
|
if (exists) {
|
|
|
|
auto json_value =
|
|
|
|
json::parse(std::string_view(prev_value.data(), prev_value.size()));
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Retrieve the saved receipts.
|
|
|
|
saved_receipts = json_value.get<std::map<std::string, uint64_t>>();
|
|
|
|
}
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Append the new ones.
|
|
|
|
for (const auto &[read_by, timestamp] : event_receipts) {
|
|
|
|
if (read_by == user_id) {
|
|
|
|
emit removeNotification(QString::fromStdString(room_id),
|
|
|
|
QString::fromStdString(event_id));
|
|
|
|
}
|
|
|
|
saved_receipts.emplace(read_by, timestamp);
|
|
|
|
}
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Save back the merged (or only the new) receipts.
|
|
|
|
nlohmann::json json_updated_value = saved_receipts;
|
|
|
|
std::string merged_receipts = json_updated_value.dump();
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
readReceiptsDb_.put(txn, key, merged_receipts);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->critical("updateReadReceipts: {}", e.what());
|
2018-01-03 19:05:49 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-01-03 19:05:49 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2018-09-13 19:15:58 +03:00
|
|
|
void
|
|
|
|
Cache::calculateRoomReadStatus()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto joined_rooms = joinedRooms();
|
2018-09-13 19:15:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::map<QString, bool> readStatus;
|
2018-09-13 19:15:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &room : joined_rooms)
|
|
|
|
readStatus.emplace(QString::fromStdString(room), calculateRoomReadStatus(room));
|
2018-09-13 19:15:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
emit roomReadStatus(readStatus);
|
2018-09-13 19:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Cache::calculateRoomReadStatus(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string last_event_id_, fullyReadEventId_;
|
|
|
|
{
|
|
|
|
auto txn = ro_txn(env_);
|
2018-09-13 19:15:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Get last event id on the room.
|
|
|
|
const auto last_event_id = getLastEventId(txn, room_id);
|
|
|
|
const auto localUser = utils::localUser().toStdString();
|
2018-09-13 19:15:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string fullyReadEventId;
|
|
|
|
if (auto ev = getAccountData(txn, mtx::events::EventType::FullyRead, room_id)) {
|
|
|
|
if (auto fr =
|
|
|
|
std::get_if<mtx::events::AccountDataEvent<mtx::events::account_data::FullyRead>>(
|
|
|
|
&ev.value())) {
|
|
|
|
fullyReadEventId = fr->content.event_id;
|
|
|
|
}
|
2021-07-10 17:12:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (last_event_id.empty() || fullyReadEventId.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (last_event_id == fullyReadEventId)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
last_event_id_ = std::string(last_event_id);
|
|
|
|
fullyReadEventId_ = std::string(fullyReadEventId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve all read receipts for that event.
|
|
|
|
return getEventIndex(room_id, last_event_id_) > getEventIndex(room_id, fullyReadEventId_);
|
2018-07-17 23:50:18 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
void
|
|
|
|
Cache::saveState(const mtx::responses::Sync &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
auto local_user_id = this->localUserId_.toStdString();
|
2018-09-28 15:40:51 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto currentBatchToken = nextBatchToken();
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setNextBatchToken(txn, res.next_batch);
|
2020-08-27 22:49:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!res.account_data.events.empty()) {
|
|
|
|
auto accountDataDb = getAccountDataDb(txn, "");
|
|
|
|
for (const auto &ev : res.account_data.events)
|
|
|
|
std::visit(
|
|
|
|
[&txn, &accountDataDb](const auto &event) {
|
|
|
|
auto j = json(event);
|
|
|
|
accountDataDb.put(txn, j["type"].get<std::string>(), j.dump());
|
|
|
|
},
|
|
|
|
ev);
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto userKeyCacheDb = getUserKeysDb(txn);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::set<std::string> spaces_with_updates;
|
|
|
|
std::set<std::string> rooms_with_space_updates;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Save joined rooms
|
|
|
|
for (const auto &room : res.rooms.join) {
|
|
|
|
auto statesdb = getStatesDb(txn, room.first);
|
|
|
|
auto stateskeydb = getStatesKeyDb(txn, room.first);
|
|
|
|
auto membersdb = getMembersDb(txn, room.first);
|
|
|
|
auto eventsDb = getEventsDb(txn, room.first);
|
|
|
|
|
|
|
|
saveStateEvents(
|
|
|
|
txn, statesdb, stateskeydb, membersdb, eventsDb, room.first, room.second.state.events);
|
|
|
|
saveStateEvents(
|
|
|
|
txn, statesdb, stateskeydb, membersdb, eventsDb, room.first, room.second.timeline.events);
|
|
|
|
|
|
|
|
saveTimelineMessages(txn, eventsDb, room.first, room.second.timeline);
|
|
|
|
|
|
|
|
RoomInfo updatedInfo;
|
|
|
|
updatedInfo.name = getRoomName(txn, statesdb, membersdb).toStdString();
|
|
|
|
updatedInfo.topic = getRoomTopic(txn, statesdb).toStdString();
|
|
|
|
updatedInfo.avatar_url = getRoomAvatarUrl(txn, statesdb, membersdb).toStdString();
|
|
|
|
updatedInfo.version = getRoomVersion(txn, statesdb).toStdString();
|
|
|
|
updatedInfo.is_space = getRoomIsSpace(txn, statesdb);
|
|
|
|
|
|
|
|
if (updatedInfo.is_space) {
|
|
|
|
bool space_updates = false;
|
|
|
|
for (const auto &e : room.second.state.events)
|
|
|
|
if (std::holds_alternative<StateEvent<state::space::Child>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<state::PowerLevels>>(e))
|
|
|
|
space_updates = true;
|
|
|
|
for (const auto &e : room.second.timeline.events)
|
|
|
|
if (std::holds_alternative<StateEvent<state::space::Child>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<state::PowerLevels>>(e))
|
|
|
|
space_updates = true;
|
|
|
|
|
|
|
|
if (space_updates)
|
|
|
|
spaces_with_updates.insert(room.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
bool room_has_space_update = false;
|
|
|
|
for (const auto &e : room.second.state.events) {
|
|
|
|
if (auto se = std::get_if<StateEvent<state::space::Parent>>(&e)) {
|
|
|
|
spaces_with_updates.insert(se->state_key);
|
|
|
|
room_has_space_update = true;
|
2021-01-22 05:18:36 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
for (const auto &e : room.second.timeline.events) {
|
|
|
|
if (auto se = std::get_if<StateEvent<state::space::Parent>>(&e)) {
|
|
|
|
spaces_with_updates.insert(se->state_key);
|
|
|
|
room_has_space_update = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (room_has_space_update)
|
|
|
|
rooms_with_space_updates.insert(room.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_new_tags = false;
|
|
|
|
// Process the account_data associated with this room
|
|
|
|
if (!room.second.account_data.events.empty()) {
|
|
|
|
auto accountDataDb = getAccountDataDb(txn, room.first);
|
|
|
|
|
|
|
|
for (const auto &evt : room.second.account_data.events) {
|
|
|
|
std::visit(
|
|
|
|
[&txn, &accountDataDb](const auto &event) {
|
|
|
|
auto j = json(event);
|
|
|
|
accountDataDb.put(txn, j["type"].get<std::string>(), j.dump());
|
|
|
|
},
|
|
|
|
evt);
|
|
|
|
|
|
|
|
// for tag events
|
|
|
|
if (std::holds_alternative<AccountDataEvent<account_data::Tags>>(evt)) {
|
|
|
|
auto tags_evt = std::get<AccountDataEvent<account_data::Tags>>(evt);
|
|
|
|
has_new_tags = true;
|
|
|
|
for (const auto &tag : tags_evt.content.tags) {
|
|
|
|
updatedInfo.tags.push_back(tag.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (auto fr = std::get_if<
|
|
|
|
mtx::events::AccountDataEvent<mtx::events::account_data::FullyRead>>(&evt)) {
|
|
|
|
nhlog::db()->debug("Fully read: {}", fr->content.event_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!has_new_tags) {
|
|
|
|
// retrieve the old tags, they haven't changed
|
|
|
|
std::string_view data;
|
|
|
|
if (roomsDb_.get(txn, room.first, data)) {
|
|
|
|
try {
|
|
|
|
RoomInfo tmp = json::parse(std::string_view(data.data(), data.size()));
|
|
|
|
updatedInfo.tags = tmp.tags;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse room info: room_id ({}), {}: {}",
|
|
|
|
room.first,
|
|
|
|
std::string(data.data(), data.size()),
|
|
|
|
e.what());
|
2018-09-28 15:40:51 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-28 15:40:51 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
roomsDb_.put(txn, room.first, json(updatedInfo).dump());
|
2018-04-21 17:14:16 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &e : room.second.ephemeral.events) {
|
|
|
|
if (auto receiptsEv =
|
|
|
|
std::get_if<mtx::events::EphemeralEvent<mtx::events::ephemeral::Receipt>>(&e)) {
|
|
|
|
Receipts receipts;
|
2021-01-06 00:10:40 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &[event_id, userReceipts] : receiptsEv->content.receipts) {
|
|
|
|
for (const auto &[user_id, receipt] : userReceipts.users) {
|
|
|
|
receipts[event_id][user_id] = receipt.ts;
|
|
|
|
}
|
2021-01-06 00:10:40 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
updateReadReceipt(txn, room.first, receipts);
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Clean up non-valid invites.
|
|
|
|
removeInvite(txn, room.first);
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
saveInvites(txn, res.rooms.invite);
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
savePresence(txn, res.presence);
|
2020-07-06 19:02:21 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
markUserKeysOutOfDate(txn, userKeyCacheDb, res.device_lists.changed, currentBatchToken);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
removeLeftRooms(txn, res.rooms.leave);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
updateSpaces(txn, spaces_with_updates, std::move(rooms_with_space_updates));
|
|
|
|
|
|
|
|
txn.commit();
|
2018-07-17 23:50:18 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::map<QString, bool> readStatus;
|
|
|
|
|
|
|
|
for (const auto &room : res.rooms.join) {
|
|
|
|
for (const auto &e : room.second.ephemeral.events) {
|
|
|
|
if (auto receiptsEv =
|
|
|
|
std::get_if<mtx::events::EphemeralEvent<mtx::events::ephemeral::Receipt>>(&e)) {
|
|
|
|
std::vector<QString> receipts;
|
|
|
|
|
|
|
|
for (const auto &[event_id, userReceipts] : receiptsEv->content.receipts) {
|
|
|
|
for (const auto &[user_id, receipt] : userReceipts.users) {
|
|
|
|
(void)receipt;
|
|
|
|
|
|
|
|
if (user_id != local_user_id) {
|
|
|
|
receipts.push_back(QString::fromStdString(event_id));
|
|
|
|
break;
|
2020-05-06 20:21:31 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-04-30 23:42:56 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!receipts.empty())
|
|
|
|
emit newReadReceipts(QString::fromStdString(room.first), receipts);
|
|
|
|
}
|
2018-07-17 23:50:18 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
readStatus.emplace(QString::fromStdString(room.first), calculateRoomReadStatus(room.first));
|
|
|
|
}
|
2018-09-13 19:15:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
emit roomReadStatus(readStatus);
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::saveInvites(lmdb::txn &txn, const std::map<std::string, mtx::responses::InvitedRoom> &rooms)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &room : rooms) {
|
|
|
|
auto statesdb = getInviteStatesDb(txn, room.first);
|
|
|
|
auto membersdb = getInviteMembersDb(txn, room.first);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
saveInvite(txn, statesdb, membersdb, room.second);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
RoomInfo updatedInfo;
|
|
|
|
updatedInfo.name = getInviteRoomName(txn, statesdb, membersdb).toStdString();
|
|
|
|
updatedInfo.topic = getInviteRoomTopic(txn, statesdb).toStdString();
|
|
|
|
updatedInfo.avatar_url = getInviteRoomAvatarUrl(txn, statesdb, membersdb).toStdString();
|
|
|
|
updatedInfo.is_space = getInviteRoomIsSpace(txn, statesdb);
|
|
|
|
updatedInfo.is_invite = true;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
invitesDb_.put(txn, room.first, json(updatedInfo).dump());
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::saveInvite(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &statesdb,
|
|
|
|
lmdb::dbi &membersdb,
|
|
|
|
const mtx::responses::InvitedRoom &room)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &e : room.invite_state) {
|
|
|
|
if (auto msg = std::get_if<StrippedEvent<Member>>(&e)) {
|
|
|
|
auto display_name =
|
|
|
|
msg->content.display_name.empty() ? msg->state_key : msg->content.display_name;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-11-21 09:06:37 +03:00
|
|
|
MemberInfo tmp{display_name, msg->content.avatar_url, msg->content.is_direct};
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
membersdb.put(txn, msg->state_key, json(tmp).dump());
|
|
|
|
} else {
|
|
|
|
std::visit(
|
|
|
|
[&txn, &statesdb](auto msg) {
|
|
|
|
auto j = json(msg);
|
|
|
|
bool res = statesdb.put(txn, j["type"].get<std::string>(), j.dump());
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
nhlog::db()->warn("couldn't save data: {}", json(msg).dump());
|
|
|
|
},
|
|
|
|
e);
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 02:45:24 +03:00
|
|
|
void
|
|
|
|
Cache::savePresence(
|
|
|
|
lmdb::txn &txn,
|
|
|
|
const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presenceUpdates)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &update : presenceUpdates) {
|
|
|
|
auto presenceDb = getPresenceDb(txn);
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
presenceDb.put(txn, update.sender, json(update.content).dump());
|
|
|
|
}
|
2020-06-08 02:45:24 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
std::vector<std::string>
|
|
|
|
Cache::roomsWithStateUpdates(const mtx::responses::Sync &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> rooms;
|
|
|
|
for (const auto &room : res.rooms.join) {
|
|
|
|
bool hasUpdates = false;
|
|
|
|
for (const auto &s : room.second.state.events) {
|
|
|
|
if (containsStateUpdates(s)) {
|
|
|
|
hasUpdates = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &s : room.second.timeline.events) {
|
|
|
|
if (containsStateUpdates(s)) {
|
|
|
|
hasUpdates = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (hasUpdates)
|
|
|
|
rooms.emplace_back(room.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &room : res.rooms.invite) {
|
|
|
|
for (const auto &s : room.second.invite_state) {
|
|
|
|
if (containsStateUpdates(s)) {
|
|
|
|
rooms.emplace_back(room.first);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return rooms;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2018-05-05 22:40:24 +03:00
|
|
|
RoomInfo
|
|
|
|
Cache::singleRoomInfo(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2018-05-05 22:40:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto statesdb = getStatesDb(txn, room_id);
|
|
|
|
|
|
|
|
std::string_view data;
|
|
|
|
|
|
|
|
// Check if the room is joined.
|
|
|
|
if (roomsDb_.get(txn, room_id, data)) {
|
|
|
|
try {
|
|
|
|
RoomInfo tmp = json::parse(data);
|
|
|
|
tmp.member_count = getMembersDb(txn, room_id).size(txn);
|
|
|
|
tmp.join_rule = getRoomJoinRule(txn, statesdb);
|
|
|
|
tmp.guest_access = getRoomGuestAccess(txn, statesdb);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse room info: room_id ({}), {}: {}",
|
|
|
|
room_id,
|
|
|
|
std::string(data.data(), data.size()),
|
|
|
|
e.what());
|
|
|
|
}
|
2018-05-05 22:40:24 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->warn("failed to read room info from db: room_id ({}), {}", room_id, e.what());
|
|
|
|
}
|
2018-05-05 22:40:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return RoomInfo();
|
2018-05-05 22:40:24 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
std::map<QString, RoomInfo>
|
|
|
|
Cache::getRoomInfo(const std::vector<std::string> &rooms)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::map<QString, RoomInfo> room_info;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// TODO This should be read only.
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &room : rooms) {
|
|
|
|
std::string_view data;
|
|
|
|
auto statesdb = getStatesDb(txn, room);
|
|
|
|
|
|
|
|
// Check if the room is joined.
|
|
|
|
if (roomsDb_.get(txn, room, data)) {
|
|
|
|
try {
|
|
|
|
RoomInfo tmp = json::parse(data);
|
|
|
|
tmp.member_count = getMembersDb(txn, room).size(txn);
|
|
|
|
tmp.join_rule = getRoomJoinRule(txn, statesdb);
|
|
|
|
tmp.guest_access = getRoomGuestAccess(txn, statesdb);
|
|
|
|
|
|
|
|
room_info.emplace(QString::fromStdString(room), std::move(tmp));
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse room info: room_id ({}), {}: {}",
|
|
|
|
room,
|
|
|
|
std::string(data.data(), data.size()),
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check if the room is an invite.
|
|
|
|
if (invitesDb_.get(txn, room, data)) {
|
|
|
|
try {
|
|
|
|
RoomInfo tmp = json::parse(std::string_view(data));
|
|
|
|
tmp.member_count = getInviteMembersDb(txn, room).size(txn);
|
|
|
|
|
|
|
|
room_info.emplace(QString::fromStdString(room), std::move(tmp));
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse room info for invite: "
|
|
|
|
"room_id ({}), {}: {}",
|
|
|
|
room,
|
|
|
|
std::string(data.data(), data.size()),
|
|
|
|
e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return room_info;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 15:06:28 +03:00
|
|
|
std::vector<QString>
|
|
|
|
Cache::roomIds()
|
2018-06-28 16:16:43 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<QString> rooms;
|
|
|
|
std::string_view room_id, unused;
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto roomsCursor = lmdb::cursor::open(txn, roomsDb_);
|
|
|
|
while (roomsCursor.get(room_id, unused, MDB_NEXT))
|
|
|
|
rooms.push_back(QString::fromStdString(std::string(room_id)));
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
roomsCursor.close();
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return rooms;
|
2018-06-28 16:16:43 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 05:09:40 +03:00
|
|
|
QMap<QString, mtx::responses::Notifications>
|
2019-08-10 06:34:44 +03:00
|
|
|
Cache::getTimelineMentions()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
// TODO: Should be read-only, but getMentionsDb will attempt to create a DB
|
|
|
|
// if it doesn't exist, throwing an error.
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr);
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QMap<QString, mtx::responses::Notifications> notifs;
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto room_ids = getRoomIds(txn);
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &room_id : room_ids) {
|
|
|
|
auto roomNotifs = getTimelineMentionsForRoom(txn, room_id);
|
|
|
|
notifs[QString::fromStdString(room_id)] = roomNotifs;
|
|
|
|
}
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return notifs;
|
2019-08-10 06:34:44 +03:00
|
|
|
}
|
|
|
|
|
2020-07-13 01:08:58 +03:00
|
|
|
std::string
|
|
|
|
Cache::previousBatchToken(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr);
|
|
|
|
auto orderDb = getEventOrderDb(txn, room_id);
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
|
|
|
std::string_view indexVal, val;
|
|
|
|
if (!cursor.get(indexVal, val, MDB_FIRST)) {
|
|
|
|
return "";
|
|
|
|
}
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto j = json::parse(val);
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return j.value("prev_batch", "");
|
2020-07-13 01:08:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 06:29:07 +03:00
|
|
|
Cache::Messages
|
2020-07-13 01:08:58 +03:00
|
|
|
Cache::getTimelineMessages(lmdb::txn &txn, const std::string &room_id, uint64_t index, bool forward)
|
2018-06-28 16:16:43 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
// TODO(nico): Limit the messages returned by this maybe?
|
|
|
|
auto orderDb = getOrderToMessageDb(txn, room_id);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
Messages messages{};
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view indexVal, event_id;
|
2020-07-05 06:29:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
|
|
|
if (index == std::numeric_limits<uint64_t>::max()) {
|
|
|
|
if (cursor.get(indexVal, event_id, forward ? MDB_FIRST : MDB_LAST)) {
|
|
|
|
index = lmdb::from_sv<uint64_t>(indexVal);
|
2020-07-05 06:29:07 +03:00
|
|
|
} else {
|
2021-09-18 01:22:33 +03:00
|
|
|
messages.end_of_cache = true;
|
|
|
|
return messages;
|
2020-07-05 06:29:07 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} else {
|
|
|
|
if (cursor.get(indexVal, event_id, MDB_SET)) {
|
|
|
|
index = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
} else {
|
|
|
|
messages.end_of_cache = true;
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
}
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
int counter = 0;
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool ret;
|
|
|
|
while ((ret = cursor.get(indexVal,
|
|
|
|
event_id,
|
|
|
|
counter == 0 ? (forward ? MDB_FIRST : MDB_LAST)
|
|
|
|
: (forward ? MDB_NEXT : MDB_PREV))) &&
|
|
|
|
counter++ < BATCH_SIZE) {
|
|
|
|
std::string_view event;
|
|
|
|
bool success = eventsDb.get(txn, event_id, event);
|
|
|
|
if (!success)
|
|
|
|
continue;
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::collections::TimelineEvent te;
|
|
|
|
try {
|
|
|
|
mtx::events::collections::from_json(json::parse(event), te);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to parse message from cache {}", e.what());
|
|
|
|
continue;
|
2018-06-28 16:16:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
messages.timeline.events.push_back(std::move(te.data));
|
|
|
|
}
|
|
|
|
cursor.close();
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// std::reverse(timeline.events.begin(), timeline.events.end());
|
|
|
|
messages.next_index = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
messages.end_of_cache = !ret;
|
|
|
|
|
|
|
|
return messages;
|
2018-06-28 16:16:43 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 04:43:14 +03:00
|
|
|
std::optional<mtx::events::collections::TimelineEvent>
|
|
|
|
Cache::getEvent(const std::string &room_id, const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
2020-07-06 04:43:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event{};
|
|
|
|
bool success = eventsDb.get(txn, event_id, event);
|
|
|
|
if (!success)
|
|
|
|
return {};
|
2020-07-06 04:43:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::collections::TimelineEvent te;
|
|
|
|
try {
|
|
|
|
mtx::events::collections::from_json(json::parse(event), te);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to parse message from cache {}", e.what());
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-07-06 04:43:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return te;
|
2020-07-06 04:43:14 +03:00
|
|
|
}
|
2020-07-10 02:37:55 +03:00
|
|
|
void
|
|
|
|
Cache::storeEvent(const std::string &room_id,
|
|
|
|
const std::string &event_id,
|
|
|
|
const mtx::events::collections::TimelineEvent &event)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
auto event_json = mtx::accessors::serialize_event(event.data);
|
|
|
|
eventsDb.put(txn, event_id, event_json.dump());
|
|
|
|
txn.commit();
|
2020-07-10 02:37:55 +03:00
|
|
|
}
|
2020-07-06 04:43:14 +03:00
|
|
|
|
2021-06-17 22:27:37 +03:00
|
|
|
void
|
|
|
|
Cache::replaceEvent(const std::string &room_id,
|
|
|
|
const std::string &event_id,
|
|
|
|
const mtx::events::collections::TimelineEvent &event)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
auto relationsDb = getRelationsDb(txn, room_id);
|
|
|
|
auto event_json = mtx::accessors::serialize_event(event.data).dump();
|
2021-06-17 22:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
eventsDb.del(txn, event_id);
|
|
|
|
eventsDb.put(txn, event_id, event_json);
|
|
|
|
for (auto relation : mtx::accessors::relations(event.data).relations) {
|
|
|
|
relationsDb.put(txn, relation.event_id, event_id);
|
2021-06-17 22:27:37 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-06-17 22:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2021-06-17 22:27:37 +03:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:22:54 +03:00
|
|
|
std::vector<std::string>
|
|
|
|
Cache::relatedEvents(const std::string &room_id, const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto relationsDb = getRelationsDb(txn, room_id);
|
2020-07-19 13:22:54 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> related_ids;
|
2020-07-19 13:22:54 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto related_cursor = lmdb::cursor::open(txn, relationsDb);
|
|
|
|
std::string_view related_to = event_id, related_event;
|
|
|
|
bool first = true;
|
2020-07-19 13:22:54 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
if (!related_cursor.get(related_to, related_event, MDB_SET))
|
|
|
|
return {};
|
2020-07-19 13:22:54 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
while (
|
|
|
|
related_cursor.get(related_to, related_event, first ? MDB_FIRST_DUP : MDB_NEXT_DUP)) {
|
|
|
|
first = false;
|
|
|
|
if (event_id != std::string_view(related_to.data(), related_to.size()))
|
|
|
|
break;
|
2020-07-19 13:22:54 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
related_ids.emplace_back(related_event.data(), related_event.size());
|
2020-07-19 13:22:54 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->error("related events error: {}", e.what());
|
|
|
|
}
|
2020-07-19 13:22:54 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return related_ids;
|
2020-07-19 13:22:54 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 17:22:06 +03:00
|
|
|
size_t
|
|
|
|
Cache::memberCount(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
return getMembersDb(txn, room_id).size(txn);
|
2021-06-18 17:22:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
QMap<QString, RoomInfo>
|
2018-04-22 11:27:00 +03:00
|
|
|
Cache::roomInfo(bool withInvites)
|
2018-04-21 16:34:50 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QMap<QString, RoomInfo> result;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view room_id;
|
|
|
|
std::string_view room_data;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Gather info about the joined rooms.
|
|
|
|
auto roomsCursor = lmdb::cursor::open(txn, roomsDb_);
|
|
|
|
while (roomsCursor.get(room_id, room_data, MDB_NEXT)) {
|
|
|
|
RoomInfo tmp = json::parse(std::move(room_data));
|
|
|
|
tmp.member_count = getMembersDb(txn, std::string(room_id)).size(txn);
|
|
|
|
result.insert(QString::fromStdString(std::string(room_id)), std::move(tmp));
|
|
|
|
}
|
|
|
|
roomsCursor.close();
|
|
|
|
|
|
|
|
if (withInvites) {
|
|
|
|
// Gather info about the invites.
|
|
|
|
auto invitesCursor = lmdb::cursor::open(txn, invitesDb_);
|
|
|
|
while (invitesCursor.get(room_id, room_data, MDB_NEXT)) {
|
|
|
|
RoomInfo tmp = json::parse(room_data);
|
|
|
|
tmp.member_count = getInviteMembersDb(txn, std::string(room_id)).size(txn);
|
|
|
|
result.insert(QString::fromStdString(std::string(room_id)), std::move(tmp));
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
invitesCursor.close();
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return result;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 03:07:25 +03:00
|
|
|
std::string
|
|
|
|
Cache::getLastEventId(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::dbi orderDb;
|
|
|
|
try {
|
|
|
|
orderDb = getOrderToMessageDb(txn, room_id);
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"Can't open db for room '{}', probably doesn't exist yet. ({})", room_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2020-02-24 03:07:25 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view indexVal, val;
|
2020-02-24 03:07:25 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
|
|
|
if (!cursor.get(indexVal, val, MDB_LAST)) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-02-24 03:07:25 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::string(val.data(), val.size());
|
2020-02-24 03:07:25 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 00:15:22 +03:00
|
|
|
std::optional<Cache::TimelineRange>
|
|
|
|
Cache::getTimelineRange(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
lmdb::dbi orderDb;
|
|
|
|
try {
|
|
|
|
orderDb = getOrderToMessageDb(txn, room_id);
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"Can't open db for room '{}', probably doesn't exist yet. ({})", room_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view indexVal, val;
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
|
|
|
if (!cursor.get(indexVal, val, MDB_LAST)) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
TimelineRange range{};
|
|
|
|
range.last = lmdb::from_sv<uint64_t>(indexVal);
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!cursor.get(indexVal, val, MDB_FIRST)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
range.first = lmdb::from_sv<uint64_t>(indexVal);
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return range;
|
2020-07-10 00:15:22 +03:00
|
|
|
}
|
2020-07-13 01:08:58 +03:00
|
|
|
std::optional<uint64_t>
|
2020-07-10 00:15:22 +03:00
|
|
|
Cache::getTimelineIndex(const std::string &room_id, std::string_view event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (event_id.empty() || room_id.empty())
|
|
|
|
return {};
|
2021-03-12 19:13:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2020-10-09 19:32:21 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::dbi orderDb;
|
|
|
|
try {
|
|
|
|
orderDb = getMessageToOrderDb(txn, room_id);
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"Can't open db for room '{}', probably doesn't exist yet. ({})", room_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view indexVal{event_id.data(), event_id.size()}, val;
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool success = orderDb.get(txn, indexVal, val);
|
|
|
|
if (!success) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return lmdb::from_sv<uint64_t>(val);
|
2020-07-10 00:15:22 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 03:03:20 +03:00
|
|
|
std::optional<uint64_t>
|
|
|
|
Cache::getEventIndex(const std::string &room_id, std::string_view event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (room_id.empty() || event_id.empty())
|
|
|
|
return {};
|
2021-02-10 03:03:20 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2021-02-10 03:03:20 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::dbi orderDb;
|
|
|
|
try {
|
|
|
|
orderDb = getEventToOrderDb(txn, room_id);
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"Can't open db for room '{}', probably doesn't exist yet. ({})", room_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2021-02-10 03:03:20 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view val;
|
2021-02-10 03:03:20 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool success = orderDb.get(txn, event_id, val);
|
|
|
|
if (!success) {
|
|
|
|
return {};
|
|
|
|
}
|
2021-02-10 03:03:20 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return lmdb::from_sv<uint64_t>(val);
|
2021-02-10 03:03:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::pair<uint64_t, std::string>>
|
|
|
|
Cache::lastInvisibleEventAfter(const std::string &room_id, std::string_view event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (room_id.empty() || event_id.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
|
|
|
|
lmdb::dbi orderDb;
|
|
|
|
lmdb::dbi eventOrderDb;
|
|
|
|
lmdb::dbi timelineDb;
|
|
|
|
try {
|
|
|
|
orderDb = getEventToOrderDb(txn, room_id);
|
|
|
|
eventOrderDb = getEventOrderDb(txn, room_id);
|
|
|
|
timelineDb = getMessageToOrderDb(txn, room_id);
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"Can't open db for room '{}', probably doesn't exist yet. ({})", room_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view indexVal;
|
|
|
|
|
|
|
|
bool success = orderDb.get(txn, event_id, indexVal);
|
|
|
|
if (!success) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
uint64_t prevIdx = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
std::string prevId{event_id};
|
|
|
|
|
|
|
|
auto cursor = lmdb::cursor::open(txn, eventOrderDb);
|
|
|
|
cursor.get(indexVal, MDB_SET);
|
|
|
|
while (cursor.get(indexVal, event_id, MDB_NEXT)) {
|
|
|
|
std::string evId = json::parse(event_id)["event_id"].get<std::string>();
|
|
|
|
std::string_view temp;
|
|
|
|
if (timelineDb.get(txn, evId, temp)) {
|
2021-07-13 03:45:03 +03:00
|
|
|
return std::pair{prevIdx, std::string(prevId)};
|
2021-09-18 01:22:33 +03:00
|
|
|
} else {
|
|
|
|
prevIdx = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
prevId = std::move(evId);
|
|
|
|
}
|
2021-07-13 03:45:03 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
return std::pair{prevIdx, std::string(prevId)};
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error("Failed to get last invisible event after {}", event_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2021-02-10 03:03:20 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 04:45:33 +03:00
|
|
|
std::optional<uint64_t>
|
|
|
|
Cache::getArrivalIndex(const std::string &room_id, std::string_view event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2021-01-27 04:45:33 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
lmdb::dbi orderDb;
|
|
|
|
try {
|
|
|
|
orderDb = getEventToOrderDb(txn, room_id);
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"Can't open db for room '{}', probably doesn't exist yet. ({})", room_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2021-01-27 04:45:33 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view val;
|
2021-01-27 04:45:33 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool success = orderDb.get(txn, event_id, val);
|
|
|
|
if (!success) {
|
|
|
|
return {};
|
|
|
|
}
|
2021-01-27 04:45:33 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return lmdb::from_sv<uint64_t>(val);
|
2021-01-27 04:45:33 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 00:15:22 +03:00
|
|
|
std::optional<std::string>
|
2020-07-13 01:08:58 +03:00
|
|
|
Cache::getTimelineEventId(const std::string &room_id, uint64_t index)
|
2020-07-10 00:15:22 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
lmdb::dbi orderDb;
|
|
|
|
try {
|
|
|
|
orderDb = getOrderToMessageDb(txn, room_id);
|
|
|
|
} catch (lmdb::runtime_error &e) {
|
|
|
|
nhlog::db()->error(
|
|
|
|
"Can't open db for room '{}', probably doesn't exist yet. ({})", room_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view val;
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool success = orderDb.get(txn, lmdb::to_sv(index), val);
|
|
|
|
if (!success) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::string(val);
|
2020-07-10 00:15:22 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 15:04:07 +03:00
|
|
|
QHash<QString, RoomInfo>
|
2018-04-22 14:19:05 +03:00
|
|
|
Cache::invites()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QHash<QString, RoomInfo> result;
|
2018-04-22 14:19:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, invitesDb_);
|
2018-04-22 14:19:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view room_id, room_data;
|
2018-04-22 14:19:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
while (cursor.get(room_id, room_data, MDB_NEXT)) {
|
|
|
|
try {
|
|
|
|
RoomInfo tmp = json::parse(room_data);
|
|
|
|
tmp.member_count = getInviteMembersDb(txn, std::string(room_id)).size(txn);
|
|
|
|
result.insert(QString::fromStdString(std::string(room_id)), std::move(tmp));
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse room info for invite: "
|
|
|
|
"room_id ({}), {}: {}",
|
|
|
|
room_id,
|
|
|
|
std::string(room_data),
|
|
|
|
e.what());
|
2021-05-24 15:04:07 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-22 14:19:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
2021-05-24 15:04:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return result;
|
2021-05-24 15:04:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<RoomInfo>
|
|
|
|
Cache::invite(std::string_view roomid)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::optional<RoomInfo> result;
|
2021-05-24 15:04:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2021-05-24 15:04:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view room_data;
|
2021-05-24 15:04:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (invitesDb_.get(txn, roomid, room_data)) {
|
|
|
|
try {
|
|
|
|
RoomInfo tmp = json::parse(room_data);
|
|
|
|
tmp.member_count = getInviteMembersDb(txn, std::string(roomid)).size(txn);
|
|
|
|
result = std::move(tmp);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse room info for invite: "
|
|
|
|
"room_id ({}), {}: {}",
|
|
|
|
roomid,
|
|
|
|
std::string(room_data),
|
|
|
|
e.what());
|
2021-05-24 15:04:07 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-22 14:19:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return result;
|
2018-04-22 14:19:05 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
QString
|
2020-11-26 00:45:33 +03:00
|
|
|
Cache::getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb)
|
2018-04-21 16:34:50 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomAvatar), event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<Avatar> msg = json::parse(std::string_view(event.data(), event.size()));
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!msg.content.url.empty())
|
|
|
|
return QString::fromStdString(msg.content.url);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.avatar event: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// We don't use an avatar for group chats.
|
|
|
|
if (membersdb.size(txn) > 2)
|
|
|
|
return QString();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, membersdb);
|
|
|
|
std::string_view user_id;
|
|
|
|
std::string_view member_data;
|
|
|
|
std::string fallback_url;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Resolve avatar for 1-1 chats.
|
|
|
|
while (cursor.get(user_id, member_data, MDB_NEXT)) {
|
|
|
|
try {
|
|
|
|
MemberInfo m = json::parse(member_data);
|
|
|
|
if (user_id == localUserId_.toStdString()) {
|
|
|
|
fallback_url = m.avatar_url;
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
return QString::fromStdString(m.avatar_url);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse member info: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Default case when there is only one member.
|
|
|
|
return QString::fromStdString(fallback_url);
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Cache::getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomName), event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<Name> msg = json::parse(std::string_view(event.data(), event.size()));
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!msg.content.name.empty())
|
|
|
|
return QString::fromStdString(msg.content.name);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.name event: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
res = statesdb.get(txn, to_string(mtx::events::EventType::RoomCanonicalAlias), event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<CanonicalAlias> msg =
|
|
|
|
json::parse(std::string_view(event.data(), event.size()));
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!msg.content.alias.empty())
|
|
|
|
return QString::fromStdString(msg.content.alias);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.canonical_alias event: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, membersdb);
|
|
|
|
const auto total = membersdb.size(txn);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::size_t ii = 0;
|
|
|
|
std::string_view user_id;
|
|
|
|
std::string_view member_data;
|
|
|
|
std::map<std::string, MemberInfo> members;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
while (cursor.get(user_id, member_data, MDB_NEXT) && ii < 3) {
|
|
|
|
try {
|
|
|
|
members.emplace(user_id, json::parse(member_data));
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse member info: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
ii++;
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (total == 1 && !members.empty())
|
|
|
|
return QString::fromStdString(members.begin()->second.name);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto first_member = [&members, this]() {
|
|
|
|
for (const auto &m : members) {
|
|
|
|
if (m.first != localUserId_.toStdString())
|
|
|
|
return QString::fromStdString(m.second.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return localUserId_;
|
|
|
|
}();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (total == 2)
|
|
|
|
return first_member;
|
|
|
|
else if (total > 2)
|
|
|
|
return QString("%1 and %2 others").arg(first_member).arg(total - 1);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return "Empty Room";
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2019-12-15 01:39:02 +03:00
|
|
|
mtx::events::state::JoinRule
|
2018-05-13 01:31:58 +03:00
|
|
|
Cache::getRoomJoinRule(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-05-13 01:31:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomJoinRules), event);
|
2018-05-13 01:31:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<state::JoinRules> msg = json::parse(event);
|
|
|
|
return msg.content.join_rule;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.join_rule event: {}", e.what());
|
2018-05-13 01:31:58 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return state::JoinRule::Knock;
|
2018-05-13 01:31:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Cache::getRoomGuestAccess(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-05-13 01:31:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomGuestAccess), event);
|
2018-05-13 01:31:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<GuestAccess> msg = json::parse(event);
|
|
|
|
return msg.content.guest_access == AccessState::CanJoin;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.guest_access event: {}", e.what());
|
2018-05-13 01:31:58 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return false;
|
2018-05-13 01:31:58 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
QString
|
|
|
|
Cache::getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomTopic), event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<Topic> msg = json::parse(event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!msg.content.topic.empty())
|
|
|
|
return QString::fromStdString(msg.content.topic);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.topic event: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString();
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 21:53:44 +03:00
|
|
|
QString
|
|
|
|
Cache::getRoomVersion(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomCreate), event);
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<Create> msg = json::parse(event);
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!msg.content.room_version.empty())
|
|
|
|
return QString::fromStdString(msg.content.room_version);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.create event: {}", e.what());
|
2019-06-27 21:53:44 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->warn("m.room.create event is missing room version, assuming version \"1\"");
|
|
|
|
return QString("1");
|
2019-06-27 21:53:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 01:09:45 +03:00
|
|
|
bool
|
|
|
|
Cache::getRoomIsSpace(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomCreate), event);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<Create> msg = json::parse(event);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return msg.content.type == mtx::events::state::room_type::space;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.create event: {}", e.what());
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->warn("m.room.create event is missing room version, assuming version \"1\"");
|
|
|
|
return false;
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 20:36:06 +03:00
|
|
|
std::optional<mtx::events::state::CanonicalAlias>
|
|
|
|
Cache::getRoomAliases(const std::string &roomid)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2021-01-10 20:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto statesdb = getStatesDb(txn, roomid);
|
2021-01-10 20:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomCanonicalAlias), event);
|
2021-01-10 20:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<CanonicalAlias> msg = json::parse(event);
|
2021-01-10 20:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return msg.content;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.canonical_alias event: {}", e.what());
|
2021-01-10 20:36:06 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-01-10 20:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::nullopt;
|
2021-01-10 20:36:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
QString
|
|
|
|
Cache::getInviteRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomName), event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StrippedEvent<state::Name> msg = json::parse(event);
|
|
|
|
return QString::fromStdString(msg.content.name);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.name event: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, membersdb);
|
|
|
|
std::string_view user_id, member_data;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
while (cursor.get(user_id, member_data, MDB_NEXT)) {
|
|
|
|
if (user_id == localUserId_.toStdString())
|
|
|
|
continue;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
MemberInfo tmp = json::parse(member_data);
|
|
|
|
cursor.close();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString::fromStdString(tmp.name);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse member info: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString("Empty Room");
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Cache::getInviteRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = statesdb.get(txn, to_string(mtx::events::EventType::RoomAvatar), event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StrippedEvent<state::Avatar> msg = json::parse(event);
|
|
|
|
return QString::fromStdString(msg.content.url);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.avatar event: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, membersdb);
|
|
|
|
std::string_view user_id, member_data;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
while (cursor.get(user_id, member_data, MDB_NEXT)) {
|
|
|
|
if (user_id == localUserId_.toStdString())
|
|
|
|
continue;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
MemberInfo tmp = json::parse(member_data);
|
|
|
|
cursor.close();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString::fromStdString(tmp.avatar_url);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse member info: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString();
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Cache::getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &db)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = db.get(txn, to_string(mtx::events::EventType::RoomTopic), event);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StrippedEvent<Topic> msg = json::parse(event);
|
|
|
|
return QString::fromStdString(msg.content.topic);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.topic event: {}", e.what());
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString();
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 01:09:45 +03:00
|
|
|
bool
|
|
|
|
Cache::getInviteRoomIsSpace(lmdb::txn &txn, lmdb::dbi &db)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = db.get(txn, to_string(mtx::events::EventType::RoomCreate), event);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StrippedEvent<Create> msg = json::parse(event);
|
|
|
|
return msg.content.type == mtx::events::state::room_type::space;
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.topic event: {}", e.what());
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return false;
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
std::vector<std::string>
|
|
|
|
Cache::joinedRooms()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto roomsCursor = lmdb::cursor::open(txn, roomsDb_);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view id, data;
|
|
|
|
std::vector<std::string> room_ids;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Gather the room ids for the joined rooms.
|
|
|
|
while (roomsCursor.get(id, data, MDB_NEXT))
|
|
|
|
room_ids.emplace_back(id);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
roomsCursor.close();
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return room_ids;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2020-10-22 21:48:28 +03:00
|
|
|
std::optional<MemberInfo>
|
|
|
|
Cache::getMember(const std::string &room_id, const std::string &user_id)
|
2018-04-21 16:34:50 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (user_id.empty() || !env_.handle())
|
|
|
|
return std::nullopt;
|
2021-04-16 18:09:38 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto membersdb = getMembersDb(txn, room_id);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view info;
|
|
|
|
if (membersdb.get(txn, user_id, info)) {
|
|
|
|
MemberInfo m = json::parse(info);
|
|
|
|
return m;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->warn(
|
|
|
|
"Failed to read member ({}) in room ({}): {}", user_id, room_id, e.what());
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
2018-05-01 19:35:28 +03:00
|
|
|
std::vector<RoomMember>
|
|
|
|
Cache::getMembers(const std::string &room_id, std::size_t startIndex, std::size_t len)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto db = getMembersDb(txn, room_id);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::size_t currentIndex = 0;
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto endIndex = std::min(startIndex + len, db.size(txn));
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<RoomMember> members;
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view user_id, user_data;
|
|
|
|
while (cursor.get(user_id, user_data, MDB_NEXT)) {
|
|
|
|
if (currentIndex < startIndex) {
|
|
|
|
currentIndex += 1;
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (currentIndex >= endIndex)
|
|
|
|
break;
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
MemberInfo tmp = json::parse(user_data);
|
|
|
|
members.emplace_back(RoomMember{QString::fromStdString(std::string(user_id)),
|
|
|
|
QString::fromStdString(tmp.name)});
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("{}", e.what());
|
2018-05-01 19:35:28 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
currentIndex += 1;
|
|
|
|
}
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
|
|
|
|
return members;
|
2018-05-01 19:35:28 +03:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:31:23 +03:00
|
|
|
std::vector<RoomMember>
|
2021-09-12 02:45:01 +03:00
|
|
|
Cache::getMembersFromInvite(const std::string &room_id, std::size_t startIndex, std::size_t len)
|
2021-09-10 04:31:23 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto db = getInviteMembersDb(txn, room_id);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
2021-09-10 04:31:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::size_t currentIndex = 0;
|
2021-09-10 04:31:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto endIndex = std::min(startIndex + len, db.size(txn));
|
2021-09-10 04:31:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<RoomMember> members;
|
2021-09-10 04:31:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view user_id, user_data;
|
|
|
|
while (cursor.get(user_id, user_data, MDB_NEXT)) {
|
|
|
|
if (currentIndex < startIndex) {
|
|
|
|
currentIndex += 1;
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-10 04:31:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (currentIndex >= endIndex)
|
|
|
|
break;
|
2021-09-10 04:31:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
MemberInfo tmp = json::parse(user_data);
|
|
|
|
members.emplace_back(RoomMember{QString::fromStdString(std::string(user_id)),
|
2021-11-21 09:06:37 +03:00
|
|
|
QString::fromStdString(tmp.name),
|
|
|
|
tmp.is_direct});
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("{}", e.what());
|
2021-09-10 04:31:23 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
currentIndex += 1;
|
|
|
|
}
|
2021-09-10 04:31:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
|
|
|
|
return members;
|
2021-09-10 04:31:23 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 21:40:11 +03:00
|
|
|
bool
|
|
|
|
Cache::isRoomMember(const std::string &user_id, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto db = getMembersDb(txn, room_id);
|
2018-07-21 21:40:11 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view value;
|
|
|
|
bool res = db.get(txn, user_id, value);
|
2018-07-21 21:40:11 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return res;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->warn(
|
|
|
|
"Failed to read member membership ({}) in room ({}): {}", user_id, room_id, e.what());
|
|
|
|
}
|
|
|
|
return false;
|
2018-07-21 21:40:11 +03:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:43:49 +03:00
|
|
|
void
|
|
|
|
Cache::savePendingMessage(const std::string &room_id,
|
|
|
|
const mtx::events::collections::TimelineEvent &message)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::responses::Timeline timeline;
|
|
|
|
timeline.events.push_back(message.data);
|
|
|
|
saveTimelineMessages(txn, eventsDb, room_id, timeline);
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto pending = getPendingMessagesDb(txn, room_id);
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
int64_t now = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
pending.put(txn, lmdb::to_sv(now), mtx::accessors::event_id(message.data));
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-07-18 18:43:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<mtx::events::collections::TimelineEvent>
|
|
|
|
Cache::firstPendingMessage(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto pending = getPendingMessagesDb(txn, room_id);
|
|
|
|
|
|
|
|
{
|
|
|
|
auto pendingCursor = lmdb::cursor::open(txn, pending);
|
|
|
|
std::string_view tsIgnored, pendingTxn;
|
|
|
|
while (pendingCursor.get(tsIgnored, pendingTxn, MDB_NEXT)) {
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
std::string_view event;
|
|
|
|
if (!eventsDb.get(txn, pendingTxn, event)) {
|
|
|
|
pending.del(txn, tsIgnored, pendingTxn);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
mtx::events::collections::TimelineEvent te;
|
|
|
|
mtx::events::collections::from_json(json::parse(event), te);
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
pendingCursor.close();
|
|
|
|
txn.commit();
|
|
|
|
return te;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to parse message from cache {}", e.what());
|
|
|
|
pending.del(txn, tsIgnored, pendingTxn);
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-18 18:43:49 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::nullopt;
|
2020-07-18 18:43:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::removePendingStatus(const std::string &room_id, const std::string &txn_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto pending = getPendingMessagesDb(txn, room_id);
|
2020-07-30 19:13:19 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
auto pendingCursor = lmdb::cursor::open(txn, pending);
|
|
|
|
std::string_view tsIgnored, pendingTxn;
|
|
|
|
while (pendingCursor.get(tsIgnored, pendingTxn, MDB_NEXT)) {
|
|
|
|
if (std::string_view(pendingTxn.data(), pendingTxn.size()) == txn_id)
|
|
|
|
lmdb::cursor_del(pendingCursor);
|
2020-07-18 18:43:49 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-07-18 18:43:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-07-18 18:43:49 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 16:16:43 +03:00
|
|
|
void
|
|
|
|
Cache::saveTimelineMessages(lmdb::txn &txn,
|
2021-06-16 23:59:41 +03:00
|
|
|
lmdb::dbi &eventsDb,
|
2018-06-28 16:16:43 +03:00
|
|
|
const std::string &room_id,
|
|
|
|
const mtx::responses::Timeline &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res.events.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto relationsDb = getRelationsDb(txn, room_id);
|
|
|
|
|
|
|
|
auto orderDb = getEventOrderDb(txn, room_id);
|
|
|
|
auto evToOrderDb = getEventToOrderDb(txn, room_id);
|
|
|
|
auto msg2orderDb = getMessageToOrderDb(txn, room_id);
|
|
|
|
auto order2msgDb = getOrderToMessageDb(txn, room_id);
|
|
|
|
auto pending = getPendingMessagesDb(txn, room_id);
|
|
|
|
|
|
|
|
if (res.limited) {
|
|
|
|
lmdb::dbi_drop(txn, orderDb, false);
|
|
|
|
lmdb::dbi_drop(txn, evToOrderDb, false);
|
|
|
|
lmdb::dbi_drop(txn, msg2orderDb, false);
|
|
|
|
lmdb::dbi_drop(txn, order2msgDb, false);
|
|
|
|
lmdb::dbi_drop(txn, pending, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
|
|
|
|
|
|
|
std::string_view indexVal, val;
|
|
|
|
uint64_t index = std::numeric_limits<uint64_t>::max() / 2;
|
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
|
|
|
if (cursor.get(indexVal, val, MDB_LAST)) {
|
|
|
|
index = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t msgIndex = std::numeric_limits<uint64_t>::max() / 2;
|
|
|
|
auto msgCursor = lmdb::cursor::open(txn, order2msgDb);
|
|
|
|
if (msgCursor.get(indexVal, val, MDB_LAST)) {
|
|
|
|
msgIndex = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
for (const auto &e : res.events) {
|
|
|
|
auto event = mtx::accessors::serialize_event(e);
|
|
|
|
auto txn_id = mtx::accessors::transaction_id(e);
|
|
|
|
|
|
|
|
std::string event_id_val = event.value("event_id", "");
|
|
|
|
if (event_id_val.empty()) {
|
|
|
|
nhlog::db()->error("Event without id!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view event_id = event_id_val;
|
|
|
|
|
|
|
|
json orderEntry = json::object();
|
|
|
|
orderEntry["event_id"] = event_id_val;
|
|
|
|
if (first && !res.prev_batch.empty())
|
|
|
|
orderEntry["prev_batch"] = res.prev_batch;
|
|
|
|
|
|
|
|
std::string_view txn_order;
|
|
|
|
if (!txn_id.empty() && evToOrderDb.get(txn, txn_id, txn_order)) {
|
|
|
|
eventsDb.put(txn, event_id, event.dump());
|
|
|
|
eventsDb.del(txn, txn_id);
|
|
|
|
|
|
|
|
std::string_view msg_txn_order;
|
|
|
|
if (msg2orderDb.get(txn, txn_id, msg_txn_order)) {
|
|
|
|
order2msgDb.put(txn, msg_txn_order, event_id);
|
|
|
|
msg2orderDb.put(txn, event_id, msg_txn_order);
|
|
|
|
msg2orderDb.del(txn, txn_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
orderDb.put(txn, txn_order, orderEntry.dump());
|
|
|
|
evToOrderDb.put(txn, event_id, txn_order);
|
|
|
|
evToOrderDb.del(txn, txn_id);
|
|
|
|
|
|
|
|
auto relations = mtx::accessors::relations(e);
|
|
|
|
if (!relations.relations.empty()) {
|
|
|
|
for (const auto &r : relations.relations) {
|
|
|
|
if (!r.event_id.empty()) {
|
|
|
|
relationsDb.del(txn, r.event_id, txn_id);
|
|
|
|
relationsDb.put(txn, r.event_id, event_id);
|
|
|
|
}
|
2020-07-25 15:08:13 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pendingCursor = lmdb::cursor::open(txn, pending);
|
|
|
|
std::string_view tsIgnored, pendingTxn;
|
|
|
|
while (pendingCursor.get(tsIgnored, pendingTxn, MDB_NEXT)) {
|
|
|
|
if (std::string_view(pendingTxn.data(), pendingTxn.size()) == txn_id)
|
|
|
|
lmdb::cursor_del(pendingCursor);
|
|
|
|
}
|
|
|
|
} else if (auto redaction =
|
|
|
|
std::get_if<mtx::events::RedactionEvent<mtx::events::msg::Redaction>>(&e)) {
|
|
|
|
if (redaction->redacts.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string_view oldEvent;
|
|
|
|
bool success = eventsDb.get(txn, redaction->redacts, oldEvent);
|
|
|
|
if (!success)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mtx::events::collections::TimelineEvent te;
|
|
|
|
try {
|
|
|
|
mtx::events::collections::from_json(
|
|
|
|
json::parse(std::string_view(oldEvent.data(), oldEvent.size())), te);
|
|
|
|
// overwrite the content and add redation data
|
|
|
|
std::visit(
|
|
|
|
[redaction](auto &ev) {
|
|
|
|
ev.unsigned_data.redacted_because = *redaction;
|
|
|
|
ev.unsigned_data.redacted_by = redaction->event_id;
|
|
|
|
},
|
|
|
|
te.data);
|
|
|
|
event = mtx::accessors::serialize_event(te.data);
|
|
|
|
event["content"].clear();
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to parse message from cache {}", e.what());
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-25 15:08:13 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
eventsDb.put(txn, redaction->redacts, event.dump());
|
|
|
|
eventsDb.put(txn, redaction->event_id, json(*redaction).dump());
|
|
|
|
} else {
|
|
|
|
first = false;
|
2020-07-05 06:29:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// This check protects against duplicates in the timeline. If the event_id
|
|
|
|
// is already in the DB, we skip putting it (again) in ordered DBs, and only
|
|
|
|
// update the event itself and its relations.
|
|
|
|
std::string_view unused_read;
|
|
|
|
if (!evToOrderDb.get(txn, event_id, unused_read)) {
|
|
|
|
++index;
|
2020-07-05 06:29:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::db()->debug("saving '{}'", orderEntry.dump());
|
2020-07-08 03:02:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.put(lmdb::to_sv(index), orderEntry.dump(), MDB_APPEND);
|
|
|
|
evToOrderDb.put(txn, event_id, lmdb::to_sv(index));
|
2021-03-03 01:15:12 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// TODO(Nico): Allow blacklisting more event types in UI
|
|
|
|
if (!isHiddenEvent(txn, e, room_id)) {
|
|
|
|
++msgIndex;
|
|
|
|
msgCursor.put(lmdb::to_sv(msgIndex), event_id, MDB_APPEND);
|
2021-09-06 00:00:29 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
msg2orderDb.put(txn, event_id, lmdb::to_sv(msgIndex));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nhlog::db()->warn("duplicate event '{}'", orderEntry.dump());
|
|
|
|
}
|
|
|
|
eventsDb.put(txn, event_id, event.dump());
|
|
|
|
|
|
|
|
auto relations = mtx::accessors::relations(e);
|
|
|
|
if (!relations.relations.empty()) {
|
|
|
|
for (const auto &r : relations.relations) {
|
|
|
|
if (!r.event_id.empty()) {
|
|
|
|
relationsDb.put(txn, r.event_id, event_id);
|
|
|
|
}
|
2020-07-01 21:15:39 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-06-28 16:16:43 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-06-28 16:16:43 +03:00
|
|
|
}
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2020-07-13 01:08:58 +03:00
|
|
|
uint64_t
|
|
|
|
Cache::saveOldMessages(const std::string &room_id, const mtx::responses::Messages &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
auto relationsDb = getRelationsDb(txn, room_id);
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto orderDb = getEventOrderDb(txn, room_id);
|
|
|
|
auto evToOrderDb = getEventToOrderDb(txn, room_id);
|
|
|
|
auto msg2orderDb = getMessageToOrderDb(txn, room_id);
|
|
|
|
auto order2msgDb = getOrderToMessageDb(txn, room_id);
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view indexVal, val;
|
|
|
|
uint64_t index = std::numeric_limits<uint64_t>::max() / 2;
|
|
|
|
{
|
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
|
|
|
if (cursor.get(indexVal, val, MDB_FIRST)) {
|
|
|
|
index = lmdb::from_sv<uint64_t>(indexVal);
|
2020-10-23 20:42:12 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
uint64_t msgIndex = std::numeric_limits<uint64_t>::max() / 2;
|
|
|
|
{
|
|
|
|
auto msgCursor = lmdb::cursor::open(txn, order2msgDb);
|
|
|
|
if (msgCursor.get(indexVal, val, MDB_FIRST)) {
|
|
|
|
msgIndex = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.chunk.empty()) {
|
|
|
|
if (orderDb.get(txn, lmdb::to_sv(index), val)) {
|
|
|
|
auto orderEntry = json::parse(val);
|
|
|
|
orderEntry["prev_batch"] = res.end;
|
|
|
|
orderDb.put(txn, lmdb::to_sv(index), orderEntry.dump());
|
|
|
|
txn.commit();
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string event_id_val;
|
|
|
|
for (const auto &e : res.chunk) {
|
|
|
|
if (std::holds_alternative<mtx::events::RedactionEvent<mtx::events::msg::Redaction>>(e))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto event = mtx::accessors::serialize_event(e);
|
|
|
|
event_id_val = event["event_id"].get<std::string>();
|
|
|
|
std::string_view event_id = event_id_val;
|
|
|
|
|
|
|
|
// This check protects against duplicates in the timeline. If the event_id is
|
|
|
|
// already in the DB, we skip putting it (again) in ordered DBs, and only update the
|
|
|
|
// event itself and its relations.
|
|
|
|
std::string_view unused_read;
|
|
|
|
if (!evToOrderDb.get(txn, event_id, unused_read)) {
|
|
|
|
--index;
|
|
|
|
|
|
|
|
json orderEntry = json::object();
|
|
|
|
orderEntry["event_id"] = event_id_val;
|
|
|
|
|
|
|
|
orderDb.put(txn, lmdb::to_sv(index), orderEntry.dump());
|
|
|
|
evToOrderDb.put(txn, event_id, lmdb::to_sv(index));
|
|
|
|
|
|
|
|
// TODO(Nico): Allow blacklisting more event types in UI
|
|
|
|
if (!isHiddenEvent(txn, e, room_id)) {
|
|
|
|
--msgIndex;
|
|
|
|
order2msgDb.put(txn, lmdb::to_sv(msgIndex), event_id);
|
|
|
|
|
|
|
|
msg2orderDb.put(txn, event_id, lmdb::to_sv(msgIndex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eventsDb.put(txn, event_id, event.dump());
|
|
|
|
|
|
|
|
auto relations = mtx::accessors::relations(e);
|
|
|
|
if (!relations.relations.empty()) {
|
|
|
|
for (const auto &r : relations.relations) {
|
|
|
|
if (!r.event_id.empty()) {
|
|
|
|
relationsDb.put(txn, r.event_id, event_id);
|
2020-07-13 01:08:58 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-07-13 01:08:58 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
json orderEntry = json::object();
|
|
|
|
orderEntry["event_id"] = event_id_val;
|
|
|
|
orderEntry["prev_batch"] = res.end;
|
|
|
|
orderDb.put(txn, lmdb::to_sv(index), orderEntry.dump());
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-07-13 01:08:58 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return msgIndex;
|
2020-07-13 01:08:58 +03:00
|
|
|
}
|
|
|
|
|
2020-08-10 00:36:47 +03:00
|
|
|
void
|
|
|
|
Cache::clearTimeline(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
auto relationsDb = getRelationsDb(txn, room_id);
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto orderDb = getEventOrderDb(txn, room_id);
|
|
|
|
auto evToOrderDb = getEventToOrderDb(txn, room_id);
|
|
|
|
auto msg2orderDb = getMessageToOrderDb(txn, room_id);
|
|
|
|
auto order2msgDb = getOrderToMessageDb(txn, room_id);
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view indexVal, val;
|
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool start = true;
|
|
|
|
bool passed_pagination_token = false;
|
|
|
|
while (cursor.get(indexVal, val, start ? MDB_LAST : MDB_PREV)) {
|
|
|
|
start = false;
|
|
|
|
json obj;
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
obj = json::parse(std::string_view(val.data(), val.size()));
|
|
|
|
} catch (std::exception &) {
|
|
|
|
// workaround bug in the initial db format, where we sometimes didn't store
|
|
|
|
// json...
|
|
|
|
obj = {{"event_id", std::string(val.data(), val.size())}};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (passed_pagination_token) {
|
|
|
|
if (obj.count("event_id") != 0) {
|
|
|
|
std::string event_id = obj["event_id"].get<std::string>();
|
|
|
|
|
|
|
|
if (!event_id.empty()) {
|
|
|
|
evToOrderDb.del(txn, event_id);
|
|
|
|
eventsDb.del(txn, event_id);
|
|
|
|
relationsDb.del(txn, event_id);
|
|
|
|
|
|
|
|
std::string_view order{};
|
|
|
|
bool exists = msg2orderDb.get(txn, event_id, order);
|
|
|
|
if (exists) {
|
|
|
|
order2msgDb.del(txn, order);
|
|
|
|
msg2orderDb.del(txn, event_id);
|
|
|
|
}
|
2020-08-10 00:36:47 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
lmdb::cursor_del(cursor);
|
|
|
|
} else {
|
|
|
|
if (obj.count("prev_batch") != 0)
|
|
|
|
passed_pagination_token = true;
|
2020-08-10 00:36:47 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto msgCursor = lmdb::cursor::open(txn, order2msgDb);
|
|
|
|
start = true;
|
|
|
|
while (msgCursor.get(indexVal, val, start ? MDB_LAST : MDB_PREV)) {
|
|
|
|
start = false;
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view eventId;
|
|
|
|
bool innerStart = true;
|
|
|
|
bool found = false;
|
|
|
|
while (cursor.get(indexVal, eventId, innerStart ? MDB_LAST : MDB_PREV)) {
|
|
|
|
innerStart = false;
|
|
|
|
|
|
|
|
json obj;
|
|
|
|
try {
|
|
|
|
obj = json::parse(std::string_view(eventId.data(), eventId.size()));
|
|
|
|
} catch (std::exception &) {
|
|
|
|
obj = {{"event_id", std::string(eventId.data(), eventId.size())}};
|
|
|
|
}
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (obj["event_id"] == std::string(val.data(), val.size())) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-10 00:36:47 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!found)
|
|
|
|
break;
|
|
|
|
}
|
2020-08-10 00:36:47 +03:00
|
|
|
|
2021-11-06 02:40:39 +03:00
|
|
|
if (!start) {
|
|
|
|
do {
|
|
|
|
lmdb::cursor_del(msgCursor);
|
|
|
|
} while (msgCursor.get(indexVal, val, MDB_PREV));
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
msgCursor.close();
|
|
|
|
txn.commit();
|
2020-08-10 00:36:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 06:00:07 +03:00
|
|
|
mtx::responses::Notifications
|
2019-08-10 06:34:44 +03:00
|
|
|
Cache::getTimelineMentionsForRoom(lmdb::txn &txn, const std::string &room_id)
|
2019-08-06 06:00:07 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = getMentionsDb(txn, room_id);
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (db.size(txn) == 0) {
|
|
|
|
return mtx::responses::Notifications{};
|
|
|
|
}
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::responses::Notifications notif;
|
|
|
|
std::string_view event_id, msg;
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
while (cursor.get(event_id, msg, MDB_NEXT)) {
|
|
|
|
auto obj = json::parse(msg);
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (obj.count("event") == 0)
|
|
|
|
continue;
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::responses::Notification notification;
|
|
|
|
mtx::responses::from_json(obj, notification);
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
notif.notifications.push_back(notification);
|
|
|
|
}
|
|
|
|
cursor.close();
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::reverse(notif.notifications.begin(), notif.notifications.end());
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return notif;
|
2019-08-06 06:00:07 +03:00
|
|
|
}
|
2019-08-10 06:34:44 +03:00
|
|
|
|
|
|
|
//! Add all notifications containing a user mention to the db.
|
|
|
|
void
|
|
|
|
Cache::saveTimelineMentions(const mtx::responses::Notifications &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QMap<std::string, QList<mtx::responses::Notification>> notifsByRoom;
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Sort into room-specific 'buckets'
|
|
|
|
for (const auto ¬if : res.notifications) {
|
|
|
|
json val = notif;
|
|
|
|
notifsByRoom[notif.room_id].push_back(notif);
|
|
|
|
}
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
// Insert the entire set of mentions for each room at a time.
|
|
|
|
QMap<std::string, QList<mtx::responses::Notification>>::const_iterator it =
|
|
|
|
notifsByRoom.constBegin();
|
|
|
|
auto end = notifsByRoom.constEnd();
|
|
|
|
while (it != end) {
|
|
|
|
nhlog::db()->debug("Storing notifications for " + it.key());
|
|
|
|
saveTimelineMentions(txn, it.key(), std::move(it.value()));
|
|
|
|
++it;
|
|
|
|
}
|
2019-08-10 06:34:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2019-08-10 06:34:44 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 06:00:07 +03:00
|
|
|
void
|
|
|
|
Cache::saveTimelineMentions(lmdb::txn &txn,
|
|
|
|
const std::string &room_id,
|
2019-08-10 06:34:44 +03:00
|
|
|
const QList<mtx::responses::Notification> &res)
|
2019-08-06 06:00:07 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = getMentionsDb(txn, room_id);
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto ¬if : res) {
|
|
|
|
const auto event_id = mtx::accessors::event_id(notif.event);
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// double check that we have the correct room_id...
|
|
|
|
if (room_id.compare(notif.room_id) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
json obj = notif;
|
2019-08-06 06:00:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
db.put(txn, event_id, obj.dump());
|
|
|
|
}
|
2019-08-06 06:00:07 +03:00
|
|
|
}
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2018-05-05 16:38:41 +03:00
|
|
|
void
|
|
|
|
Cache::markSentNotification(const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
notificationsDb_.put(txn, event_id, "");
|
|
|
|
txn.commit();
|
2018-05-05 16:38:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::removeReadNotification(const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2018-05-05 16:38:41 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
notificationsDb_.del(txn, event_id);
|
2018-05-05 16:38:41 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2018-05-05 16:38:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Cache::isNotificationSent(const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2018-05-05 16:38:41 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view value;
|
|
|
|
bool res = notificationsDb_.get(txn, event_id, value);
|
2018-05-05 16:38:41 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return res;
|
2018-05-05 16:38:41 +03:00
|
|
|
}
|
|
|
|
|
2018-08-25 21:08:43 +03:00
|
|
|
std::vector<std::string>
|
|
|
|
Cache::getRoomIds(lmdb::txn &txn)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, roomsDb_);
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> rooms;
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view room_id, _unused;
|
|
|
|
while (cursor.get(room_id, _unused, MDB_NEXT))
|
|
|
|
rooms.emplace_back(room_id);
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return rooms;
|
2018-08-25 21:08:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::deleteOldMessages()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view indexVal, val;
|
2020-07-05 06:29:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto room_ids = getRoomIds(txn);
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &room_id : room_ids) {
|
|
|
|
auto orderDb = getEventOrderDb(txn, room_id);
|
|
|
|
auto evToOrderDb = getEventToOrderDb(txn, room_id);
|
|
|
|
auto o2m = getOrderToMessageDb(txn, room_id);
|
|
|
|
auto m2o = getMessageToOrderDb(txn, room_id);
|
|
|
|
auto eventsDb = getEventsDb(txn, room_id);
|
|
|
|
auto relationsDb = getRelationsDb(txn, room_id);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, orderDb);
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
uint64_t first, last;
|
|
|
|
if (cursor.get(indexVal, val, MDB_LAST)) {
|
|
|
|
last = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cursor.get(indexVal, val, MDB_FIRST)) {
|
|
|
|
first = lmdb::from_sv<uint64_t>(indexVal);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-25 21:08:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
size_t message_count = static_cast<size_t>(last - first);
|
|
|
|
if (message_count < MAX_RESTORED_MESSAGES)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool start = true;
|
|
|
|
while (cursor.get(indexVal, val, start ? MDB_FIRST : MDB_NEXT) &&
|
|
|
|
message_count-- > MAX_RESTORED_MESSAGES) {
|
|
|
|
start = false;
|
|
|
|
auto obj = json::parse(std::string_view(val.data(), val.size()));
|
|
|
|
|
|
|
|
if (obj.count("event_id") != 0) {
|
|
|
|
std::string event_id = obj["event_id"].get<std::string>();
|
|
|
|
evToOrderDb.del(txn, event_id);
|
|
|
|
eventsDb.del(txn, event_id);
|
|
|
|
|
|
|
|
relationsDb.del(txn, event_id);
|
|
|
|
|
|
|
|
std::string_view order{};
|
|
|
|
bool exists = m2o.get(txn, event_id, order);
|
|
|
|
if (exists) {
|
|
|
|
o2m.del(txn, order);
|
|
|
|
m2o.del(txn, event_id);
|
2018-08-25 21:08:43 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
cursor.del();
|
2018-08-25 21:08:43 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
txn.commit();
|
2018-08-25 21:08:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::deleteOldData() noexcept
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
deleteOldMessages();
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->error("failed to delete old messages: {}", e.what());
|
|
|
|
}
|
2018-08-25 21:08:43 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 01:09:45 +03:00
|
|
|
void
|
|
|
|
Cache::updateSpaces(lmdb::txn &txn,
|
|
|
|
const std::set<std::string> &spaces_with_updates,
|
|
|
|
std::set<std::string> rooms_with_updates)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (spaces_with_updates.empty() && rooms_with_updates.empty())
|
|
|
|
return;
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &space : spaces_with_updates) {
|
|
|
|
// delete old entries
|
|
|
|
{
|
|
|
|
auto cursor = lmdb::cursor::open(txn, spacesChildrenDb_);
|
|
|
|
bool first = true;
|
|
|
|
std::string_view sp = space, space_child = "";
|
|
|
|
|
|
|
|
if (cursor.get(sp, space_child, MDB_SET)) {
|
|
|
|
while (cursor.get(sp, space_child, first ? MDB_FIRST_DUP : MDB_NEXT_DUP)) {
|
|
|
|
first = false;
|
|
|
|
spacesParentsDb_.del(txn, space_child, space);
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
cursor.close();
|
|
|
|
spacesChildrenDb_.del(txn, space);
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &event :
|
|
|
|
getStateEventsWithType<mtx::events::state::space::Child>(txn, space)) {
|
|
|
|
if (event.content.via.has_value() && event.state_key.size() > 3 &&
|
|
|
|
event.state_key.at(0) == '!') {
|
|
|
|
spacesChildrenDb_.put(txn, space, event.state_key);
|
|
|
|
spacesParentsDb_.put(txn, event.state_key, space);
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-10-20 16:04:36 +03:00
|
|
|
|
|
|
|
for (const auto &r : getRoomIds(txn)) {
|
|
|
|
if (auto parent = getStateEvent<mtx::events::state::space::Parent>(txn, r, space)) {
|
|
|
|
rooms_with_updates.insert(r);
|
|
|
|
}
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-10-20 16:04:36 +03:00
|
|
|
const auto space_event_type = to_string(mtx::events::EventType::SpaceChild);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &room : rooms_with_updates) {
|
|
|
|
for (const auto &event :
|
|
|
|
getStateEventsWithType<mtx::events::state::space::Parent>(txn, room)) {
|
|
|
|
if (event.content.via.has_value() && event.state_key.size() > 3 &&
|
|
|
|
event.state_key.at(0) == '!') {
|
|
|
|
const std::string &space = event.state_key;
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto pls = getStateEvent<mtx::events::state::PowerLevels>(txn, space);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!pls)
|
|
|
|
continue;
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (pls->content.user_level(event.sender) >=
|
|
|
|
pls->content.state_level(space_event_type)) {
|
|
|
|
spacesChildrenDb_.put(txn, space, room);
|
|
|
|
spacesParentsDb_.put(txn, room, space);
|
2021-10-20 16:04:36 +03:00
|
|
|
} else {
|
|
|
|
nhlog::db()->debug("Skipping {} in {} because of missing PL. {}: {} < {}",
|
|
|
|
room,
|
|
|
|
space,
|
|
|
|
event.sender,
|
|
|
|
pls->content.user_level(event.sender),
|
|
|
|
pls->content.state_level(space_event_type));
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, std::optional<RoomInfo>>
|
|
|
|
Cache::spaces()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
|
|
|
|
QMap<QString, std::optional<RoomInfo>> ret;
|
|
|
|
{
|
|
|
|
auto cursor = lmdb::cursor::open(txn, spacesChildrenDb_);
|
|
|
|
bool first = true;
|
|
|
|
std::string_view space_id, space_child;
|
|
|
|
while (cursor.get(space_id, space_child, first ? MDB_FIRST : MDB_NEXT)) {
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
if (!space_child.empty()) {
|
|
|
|
std::string_view room_data;
|
|
|
|
if (roomsDb_.get(txn, space_id, room_data)) {
|
|
|
|
RoomInfo tmp = json::parse(std::move(room_data));
|
|
|
|
ret.insert(QString::fromUtf8(space_id.data(), space_id.size()), tmp);
|
|
|
|
} else {
|
|
|
|
ret.insert(QString::fromUtf8(space_id.data(), space_id.size()), std::nullopt);
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return ret;
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
Cache::getParentRoomIds(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> roomids;
|
|
|
|
{
|
|
|
|
auto cursor = lmdb::cursor::open(txn, spacesParentsDb_);
|
|
|
|
bool first = true;
|
|
|
|
std::string_view sp = room_id, space_parent;
|
|
|
|
if (cursor.get(sp, space_parent, MDB_SET)) {
|
|
|
|
while (cursor.get(sp, space_parent, first ? MDB_FIRST_DUP : MDB_NEXT_DUP)) {
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
if (!space_parent.empty())
|
|
|
|
roomids.emplace_back(space_parent);
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return roomids;
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
Cache::getChildRoomIds(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> roomids;
|
|
|
|
{
|
|
|
|
auto cursor = lmdb::cursor::open(txn, spacesChildrenDb_);
|
|
|
|
bool first = true;
|
|
|
|
std::string_view sp = room_id, space_child;
|
|
|
|
if (cursor.get(sp, space_child, MDB_SET)) {
|
|
|
|
while (cursor.get(sp, space_child, first ? MDB_FIRST_DUP : MDB_NEXT_DUP)) {
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
if (!space_child.empty())
|
|
|
|
roomids.emplace_back(space_child);
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return roomids;
|
2021-06-16 01:09:45 +03:00
|
|
|
}
|
|
|
|
|
2021-07-19 04:02:30 +03:00
|
|
|
std::vector<ImagePackInfo>
|
2021-07-21 14:37:57 +03:00
|
|
|
Cache::getImagePacks(const std::string &room_id, std::optional<bool> stickers)
|
2021-07-15 21:37:52 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::vector<ImagePackInfo> infos;
|
|
|
|
|
|
|
|
auto addPack = [&infos, stickers](const mtx::events::msc2545::ImagePack &pack,
|
|
|
|
const std::string &source_room,
|
|
|
|
const std::string &state_key) {
|
|
|
|
if (!pack.pack || !stickers.has_value() ||
|
|
|
|
(stickers.value() ? pack.pack->is_sticker() : pack.pack->is_emoji())) {
|
|
|
|
ImagePackInfo info;
|
|
|
|
info.source_room = source_room;
|
|
|
|
info.state_key = state_key;
|
|
|
|
info.pack.pack = pack.pack;
|
|
|
|
|
|
|
|
for (const auto &img : pack.images) {
|
|
|
|
if (stickers.has_value() && img.second.overrides_usage() &&
|
|
|
|
(stickers ? !img.second.is_sticker() : !img.second.is_emoji()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
info.pack.images.insert(img);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info.pack.images.empty())
|
|
|
|
infos.push_back(std::move(info));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// packs from account data
|
|
|
|
if (auto accountpack =
|
|
|
|
getAccountData(txn, mtx::events::EventType::ImagePackInAccountData, "")) {
|
|
|
|
auto tmp =
|
|
|
|
std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePack>>(&*accountpack);
|
|
|
|
if (tmp)
|
|
|
|
addPack(tmp->content, "", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
// packs from rooms, that were enabled globally
|
|
|
|
if (auto roomPacks = getAccountData(txn, mtx::events::EventType::ImagePackRooms, "")) {
|
|
|
|
auto tmp = std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePackRooms>>(
|
|
|
|
&*roomPacks);
|
|
|
|
if (tmp) {
|
|
|
|
for (const auto &[room_id2, state_to_d] : tmp->content.rooms) {
|
|
|
|
// don't add stickers from this room twice
|
|
|
|
if (room_id2 == room_id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const auto &[state_id, d] : state_to_d) {
|
|
|
|
(void)d;
|
|
|
|
if (auto pack =
|
|
|
|
getStateEvent<mtx::events::msc2545::ImagePack>(txn, room_id2, state_id))
|
|
|
|
addPack(pack->content, room_id2, state_id);
|
2021-07-19 04:02:30 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-07-19 04:02:30 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-07-19 04:02:30 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// packs from current room
|
|
|
|
if (auto pack = getStateEvent<mtx::events::msc2545::ImagePack>(txn, room_id)) {
|
|
|
|
addPack(pack->content, room_id, "");
|
|
|
|
}
|
|
|
|
for (const auto &pack : getStateEventsWithType<mtx::events::msc2545::ImagePack>(txn, room_id)) {
|
|
|
|
addPack(pack.content, room_id, pack.state_key);
|
|
|
|
}
|
2021-07-19 04:02:30 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return infos;
|
2021-07-15 21:37:52 +03:00
|
|
|
}
|
|
|
|
|
2021-07-21 14:37:57 +03:00
|
|
|
std::optional<mtx::events::collections::RoomAccountDataEvents>
|
|
|
|
Cache::getAccountData(mtx::events::EventType type, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
return getAccountData(txn, type, room_id);
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 22:49:05 +03:00
|
|
|
std::optional<mtx::events::collections::RoomAccountDataEvents>
|
|
|
|
Cache::getAccountData(lmdb::txn &txn, mtx::events::EventType type, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto db = getAccountDataDb(txn, room_id);
|
|
|
|
|
|
|
|
std::string_view data;
|
|
|
|
if (db.get(txn, to_string(type), data)) {
|
|
|
|
mtx::responses::utils::RoomAccountDataEvents events;
|
|
|
|
json j = json::array({
|
|
|
|
json::parse(data),
|
|
|
|
});
|
|
|
|
mtx::responses::utils::parse_room_account_data_events(j, events);
|
|
|
|
if (events.size() == 1)
|
|
|
|
return events.front();
|
2020-08-27 22:49:05 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2020-08-27 22:49:05 +03:00
|
|
|
}
|
|
|
|
|
2018-05-16 23:30:50 +03:00
|
|
|
bool
|
|
|
|
Cache::hasEnoughPowerLevel(const std::vector<mtx::events::EventType> &eventTypes,
|
|
|
|
const std::string &room_id,
|
|
|
|
const std::string &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getStatesDb(txn, room_id);
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
int64_t min_event_level = std::numeric_limits<int64_t>::max();
|
|
|
|
int64_t user_level = std::numeric_limits<int64_t>::min();
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view event;
|
|
|
|
bool res = db.get(txn, to_string(EventType::RoomPowerLevels), event);
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
try {
|
|
|
|
StateEvent<PowerLevels> msg = json::parse(std::string_view(event.data(), event.size()));
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
user_level = msg.content.user_level(user_id);
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &ty : eventTypes)
|
|
|
|
min_event_level = std::min(min_event_level, msg.content.state_level(to_string(ty)));
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::db()->warn("failed to parse m.room.power_levels event: {}", e.what());
|
2018-05-16 23:30:50 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2018-05-16 23:30:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return user_level >= min_event_level;
|
2018-05-16 23:30:50 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 12:28:00 +03:00
|
|
|
std::vector<std::string>
|
|
|
|
Cache::roomMembers(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> members;
|
|
|
|
std::string_view user_id, unused;
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = getMembersDb(txn, room_id);
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
|
|
|
while (cursor.get(user_id, unused, MDB_NEXT))
|
|
|
|
members.emplace_back(user_id);
|
|
|
|
cursor.close();
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return members;
|
2018-06-13 12:28:00 +03:00
|
|
|
}
|
|
|
|
|
2021-08-14 00:13:09 +03:00
|
|
|
crypto::Trust
|
|
|
|
Cache::roomVerificationStatus(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
crypto::Trust trust = crypto::Verified;
|
2021-08-14 00:13:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2021-08-14 03:41:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = getMembersDb(txn, room_id);
|
|
|
|
auto keysDb = getUserKeysDb(txn);
|
|
|
|
std::vector<std::string> keysToRequest;
|
2021-08-14 03:41:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view user_id, unused;
|
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
|
|
|
while (cursor.get(user_id, unused, MDB_NEXT)) {
|
|
|
|
auto verif = verificationStatus_(std::string(user_id), txn);
|
|
|
|
if (verif.unverified_device_count) {
|
2021-08-14 03:41:34 +03:00
|
|
|
trust = crypto::Unverified;
|
2021-09-18 01:22:33 +03:00
|
|
|
if (verif.verified_devices.empty() && verif.no_keys) {
|
|
|
|
// we probably don't have the keys yet, so query them
|
|
|
|
keysToRequest.push_back(std::string(user_id));
|
|
|
|
}
|
|
|
|
} else if (verif.user_verified == crypto::TOFU && trust == crypto::Verified)
|
|
|
|
trust = crypto::TOFU;
|
2021-08-14 00:13:09 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!keysToRequest.empty())
|
|
|
|
markUserKeysOutOfDate(txn, keysDb, keysToRequest, "");
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to calculate verification status for {}: {}", room_id, e.what());
|
|
|
|
trust = crypto::Unverified;
|
|
|
|
}
|
|
|
|
|
|
|
|
return trust;
|
2021-08-14 00:13:09 +03:00
|
|
|
}
|
|
|
|
|
2020-11-30 02:26:27 +03:00
|
|
|
std::map<std::string, std::optional<UserKeyCache>>
|
2021-08-01 01:59:46 +03:00
|
|
|
Cache::getMembersWithKeys(const std::string &room_id, bool verified_only)
|
2020-11-30 02:26:27 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view keys;
|
2020-11-30 02:26:27 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
std::map<std::string, std::optional<UserKeyCache>> members;
|
2020-11-30 02:26:27 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = getMembersDb(txn, room_id);
|
|
|
|
auto keysDb = getUserKeysDb(txn);
|
|
|
|
|
|
|
|
std::string_view user_id, unused;
|
|
|
|
auto cursor = lmdb::cursor::open(txn, db);
|
|
|
|
while (cursor.get(user_id, unused, MDB_NEXT)) {
|
|
|
|
auto res = keysDb.get(txn, user_id, keys);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
auto k = json::parse(keys).get<UserKeyCache>();
|
|
|
|
if (verified_only) {
|
|
|
|
auto verif = verificationStatus_(std::string(user_id), txn);
|
|
|
|
|
|
|
|
if (verif.user_verified == crypto::Trust::Verified ||
|
|
|
|
!verif.verified_devices.empty()) {
|
|
|
|
auto keyCopy = k;
|
|
|
|
keyCopy.device_keys.clear();
|
|
|
|
|
|
|
|
std::copy_if(
|
|
|
|
k.device_keys.begin(),
|
|
|
|
k.device_keys.end(),
|
|
|
|
std::inserter(keyCopy.device_keys, keyCopy.device_keys.end()),
|
|
|
|
[&verif](const auto &key) {
|
|
|
|
auto curve25519 = key.second.keys.find("curve25519:" + key.first);
|
|
|
|
if (curve25519 == key.second.keys.end())
|
|
|
|
return false;
|
|
|
|
if (auto t = verif.verified_device_keys.find(curve25519->second);
|
|
|
|
t == verif.verified_device_keys.end() ||
|
|
|
|
t->second != crypto::Trust::Verified)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return key.first == key.second.device_id &&
|
|
|
|
std::find(verif.verified_devices.begin(),
|
|
|
|
verif.verified_devices.end(),
|
|
|
|
key.first) != verif.verified_devices.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!keyCopy.device_keys.empty())
|
|
|
|
members[std::string(user_id)] = std::move(keyCopy);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
members[std::string(user_id)] = std::move(k);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!verified_only)
|
|
|
|
members[std::string(user_id)] = {};
|
|
|
|
}
|
2020-11-30 02:26:27 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
cursor.close();
|
|
|
|
|
|
|
|
return members;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->debug("Error retrieving members: {}", e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2020-11-30 02:26:27 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
QString
|
|
|
|
Cache::displayName(const QString &room_id, const QString &user_id)
|
|
|
|
{
|
2021-11-14 19:37:19 +03:00
|
|
|
return QString::fromStdString(displayName(room_id.toStdString(), user_id.toStdString()));
|
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-11-14 19:37:19 +03:00
|
|
|
static bool
|
|
|
|
isDisplaynameSafe(const std::string &s)
|
|
|
|
{
|
|
|
|
for (QChar c : QString::fromStdString(s).toUcs4()) {
|
|
|
|
if (c.isPrint() && !c.isSpace())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Cache::displayName(const std::string &room_id, const std::string &user_id)
|
|
|
|
{
|
2021-11-14 19:37:19 +03:00
|
|
|
if (auto info = getMember(room_id, user_id); info && !isDisplaynameSafe(info->name))
|
2021-09-18 01:22:33 +03:00
|
|
|
return info->name;
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return user_id;
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Cache::avatarUrl(const QString &room_id, const QString &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (auto info = getMember(room_id.toStdString(), user_id.toStdString());
|
|
|
|
info && !info->avatar_url.empty())
|
|
|
|
return QString::fromStdString(info->avatar_url);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return "";
|
2018-04-21 16:34:50 +03:00
|
|
|
}
|
2019-01-26 05:53:43 +03:00
|
|
|
|
2020-06-08 02:45:24 +03:00
|
|
|
mtx::presence::PresenceState
|
|
|
|
Cache::presenceState(const std::string &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (user_id.empty())
|
|
|
|
return {};
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view presenceVal;
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getPresenceDb(txn);
|
|
|
|
auto res = db.get(txn, user_id, presenceVal);
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::presence::PresenceState state = mtx::presence::offline;
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
mtx::events::presence::Presence presence =
|
|
|
|
json::parse(std::string_view(presenceVal.data(), presenceVal.size()));
|
|
|
|
state = presence.presence;
|
|
|
|
}
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return state;
|
2020-06-08 02:45:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Cache::statusMessage(const std::string &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (user_id.empty())
|
|
|
|
return {};
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view presenceVal;
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getPresenceDb(txn);
|
|
|
|
auto res = db.get(txn, user_id, presenceVal);
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string status_msg;
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
mtx::events::presence::Presence presence = json::parse(presenceVal);
|
|
|
|
status_msg = presence.status_msg;
|
|
|
|
}
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return status_msg;
|
2020-06-08 02:45:24 +03:00
|
|
|
}
|
|
|
|
|
2020-06-28 18:31:34 +03:00
|
|
|
void
|
2020-10-02 02:14:42 +03:00
|
|
|
to_json(json &j, const UserKeyCache &info)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
j["device_keys"] = info.device_keys;
|
|
|
|
j["seen_device_keys"] = info.seen_device_keys;
|
|
|
|
j["seen_device_ids"] = info.seen_device_ids;
|
|
|
|
j["master_keys"] = info.master_keys;
|
|
|
|
j["master_key_changed"] = info.master_key_changed;
|
|
|
|
j["user_signing_keys"] = info.user_signing_keys;
|
|
|
|
j["self_signing_keys"] = info.self_signing_keys;
|
|
|
|
j["updated_at"] = info.updated_at;
|
|
|
|
j["last_changed"] = info.last_changed;
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-02 02:14:42 +03:00
|
|
|
from_json(const json &j, UserKeyCache &info)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
info.device_keys = j.value("device_keys", std::map<std::string, mtx::crypto::DeviceKeys>{});
|
|
|
|
info.seen_device_keys = j.value("seen_device_keys", std::set<std::string>{});
|
|
|
|
info.seen_device_ids = j.value("seen_device_ids", std::set<std::string>{});
|
|
|
|
info.master_keys = j.value("master_keys", mtx::crypto::CrossSigningKeys{});
|
|
|
|
info.master_key_changed = j.value("master_key_changed", false);
|
|
|
|
info.user_signing_keys = j.value("user_signing_keys", mtx::crypto::CrossSigningKeys{});
|
|
|
|
info.self_signing_keys = j.value("self_signing_keys", mtx::crypto::CrossSigningKeys{});
|
|
|
|
info.updated_at = j.value("updated_at", "");
|
|
|
|
info.last_changed = j.value("last_changed", "");
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
std::optional<UserKeyCache>
|
|
|
|
Cache::userKeys(const std::string &user_id)
|
2021-08-14 00:13:09 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
return userKeys_(user_id, txn);
|
2021-08-14 00:13:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<UserKeyCache>
|
|
|
|
Cache::userKeys_(const std::string &user_id, lmdb::txn &txn)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view keys;
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
auto db = getUserKeysDb(txn);
|
|
|
|
auto res = db.get(txn, user_id, keys);
|
2020-07-01 15:17:10 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res) {
|
|
|
|
return json::parse(keys).get<UserKeyCache>();
|
|
|
|
} else {
|
|
|
|
return {};
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to retrieve user keys for {}: {}", user_id, e.what());
|
|
|
|
return {};
|
|
|
|
}
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
void
|
|
|
|
Cache::updateUserKeys(const std::string &sync_token, const mtx::responses::QueryKeys &keyQuery)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getUserKeysDb(txn);
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::map<std::string, UserKeyCache> updates;
|
|
|
|
|
|
|
|
for (const auto &[user, keys] : keyQuery.device_keys)
|
|
|
|
updates[user].device_keys = keys;
|
|
|
|
for (const auto &[user, keys] : keyQuery.master_keys)
|
|
|
|
updates[user].master_keys = keys;
|
|
|
|
for (const auto &[user, keys] : keyQuery.user_signing_keys)
|
|
|
|
updates[user].user_signing_keys = keys;
|
|
|
|
for (const auto &[user, keys] : keyQuery.self_signing_keys)
|
|
|
|
updates[user].self_signing_keys = keys;
|
2021-05-07 18:01:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (auto &[user, update] : updates) {
|
|
|
|
nhlog::db()->debug("Updated user keys: {}", user);
|
|
|
|
|
|
|
|
auto updateToWrite = update;
|
|
|
|
|
|
|
|
std::string_view oldKeys;
|
|
|
|
auto res = db.get(txn, user, oldKeys);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
updateToWrite = json::parse(oldKeys).get<UserKeyCache>();
|
|
|
|
auto last_changed = updateToWrite.last_changed;
|
|
|
|
// skip if we are tracking this and expect it to be up to date with the last
|
|
|
|
// sync token
|
|
|
|
if (!last_changed.empty() && last_changed != sync_token) {
|
|
|
|
nhlog::db()->debug("Not storing update for user {}, because "
|
|
|
|
"last_changed {}, but we fetched update for {}",
|
|
|
|
user,
|
|
|
|
last_changed,
|
|
|
|
sync_token);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!updateToWrite.master_keys.keys.empty() &&
|
|
|
|
update.master_keys.keys != updateToWrite.master_keys.keys) {
|
|
|
|
nhlog::db()->debug("Master key of {} changed:\nold: {}\nnew: {}",
|
|
|
|
user,
|
|
|
|
updateToWrite.master_keys.keys.size(),
|
|
|
|
update.master_keys.keys.size());
|
|
|
|
updateToWrite.master_key_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateToWrite.master_keys = update.master_keys;
|
|
|
|
updateToWrite.self_signing_keys = update.self_signing_keys;
|
|
|
|
updateToWrite.user_signing_keys = update.user_signing_keys;
|
|
|
|
|
|
|
|
auto oldDeviceKeys = std::move(updateToWrite.device_keys);
|
|
|
|
updateToWrite.device_keys.clear();
|
|
|
|
|
|
|
|
// Don't insert keys, which we have seen once already
|
|
|
|
for (const auto &[device_id, device_keys] : update.device_keys) {
|
|
|
|
if (oldDeviceKeys.count(device_id) &&
|
|
|
|
oldDeviceKeys.at(device_id).keys == device_keys.keys) {
|
|
|
|
// this is safe, since the keys are the same
|
|
|
|
updateToWrite.device_keys[device_id] = device_keys;
|
|
|
|
} else {
|
|
|
|
bool keyReused = false;
|
|
|
|
for (const auto &[key_id, key] : device_keys.keys) {
|
|
|
|
(void)key_id;
|
|
|
|
if (updateToWrite.seen_device_keys.count(key)) {
|
|
|
|
nhlog::crypto()->warn(
|
|
|
|
"Key '{}' reused by ({}: {})", key, user, device_id);
|
|
|
|
keyReused = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (updateToWrite.seen_device_ids.count(device_id)) {
|
|
|
|
nhlog::crypto()->warn("device_id '{}' reused by ({})", device_id, user);
|
|
|
|
keyReused = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keyReused && !oldDeviceKeys.count(device_id)) {
|
|
|
|
// ensure the key has a valid signature from itself
|
|
|
|
std::string device_signing_key = "ed25519:" + device_keys.device_id;
|
|
|
|
if (device_id != device_keys.device_id) {
|
|
|
|
nhlog::crypto()->warn("device {}:{} has a different device id "
|
|
|
|
"in the body: {}",
|
|
|
|
user,
|
|
|
|
device_id,
|
|
|
|
device_keys.device_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!device_keys.signatures.count(user) ||
|
|
|
|
!device_keys.signatures.at(user).count(device_signing_key)) {
|
|
|
|
nhlog::crypto()->warn("device {}:{} has no signature", user, device_id);
|
|
|
|
continue;
|
2021-05-07 18:01:03 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!mtx::crypto::ed25519_verify_signature(
|
|
|
|
device_keys.keys.at(device_signing_key),
|
|
|
|
json(device_keys),
|
|
|
|
device_keys.signatures.at(user).at(device_signing_key))) {
|
|
|
|
nhlog::crypto()->warn(
|
|
|
|
"device {}:{} has an invalid signature", user, device_id);
|
|
|
|
continue;
|
2021-05-07 18:01:03 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
updateToWrite.device_keys[device_id] = device_keys;
|
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
for (const auto &[key_id, key] : device_keys.keys) {
|
|
|
|
(void)key_id;
|
|
|
|
updateToWrite.seen_device_keys.insert(key);
|
|
|
|
}
|
|
|
|
updateToWrite.seen_device_ids.insert(device_id);
|
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
updateToWrite.updated_at = sync_token;
|
|
|
|
db.put(txn, user, json(updateToWrite).dump());
|
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
txn.commit();
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::map<std::string, VerificationStatus> tmp;
|
|
|
|
const auto local_user = utils::localUser().toStdString();
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(verification_storage.verification_storage_mtx);
|
|
|
|
for (auto &[user_id, update] : updates) {
|
|
|
|
(void)update;
|
|
|
|
if (user_id == local_user) {
|
|
|
|
std::swap(tmp, verification_storage.status);
|
|
|
|
} else {
|
|
|
|
verification_storage.status.erase(user_id);
|
|
|
|
}
|
2020-10-08 00:03:14 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-05-22 15:31:38 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (auto &[user_id, update] : updates) {
|
|
|
|
(void)update;
|
|
|
|
if (user_id == local_user) {
|
|
|
|
for (const auto &[user, status] : tmp) {
|
|
|
|
(void)status;
|
|
|
|
emit verificationStatusChanged(user);
|
|
|
|
}
|
2021-11-08 18:26:16 +03:00
|
|
|
} else {
|
|
|
|
emit verificationStatusChanged(user_id);
|
2020-10-08 00:03:14 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 23:55:12 +03:00
|
|
|
void
|
|
|
|
Cache::markUserKeysOutOfDate(const std::vector<std::string> &user_ids)
|
|
|
|
{
|
|
|
|
auto currentBatchToken = nextBatchToken();
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getUserKeysDb(txn);
|
|
|
|
markUserKeysOutOfDate(txn, db, user_ids, currentBatchToken);
|
|
|
|
txn.commit();
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
void
|
|
|
|
Cache::markUserKeysOutOfDate(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &db,
|
|
|
|
const std::vector<std::string> &user_ids,
|
|
|
|
const std::string &sync_token)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::requests::QueryKeys query;
|
|
|
|
query.token = sync_token;
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &user : user_ids) {
|
|
|
|
nhlog::db()->debug("Marking user keys out of date: {}", user);
|
2021-08-14 03:41:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view oldKeys;
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
UserKeyCache cacheEntry;
|
|
|
|
auto res = db.get(txn, user, oldKeys);
|
|
|
|
if (res) {
|
|
|
|
cacheEntry =
|
|
|
|
json::parse(std::string_view(oldKeys.data(), oldKeys.size())).get<UserKeyCache>();
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
cacheEntry.last_changed = sync_token;
|
|
|
|
|
|
|
|
db.put(txn, user, json(cacheEntry).dump());
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
query.device_keys[user] = {};
|
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!query.device_keys.empty())
|
|
|
|
http::client()->query_keys(
|
|
|
|
query,
|
|
|
|
[this, sync_token](const mtx::responses::QueryKeys &keys, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to query device keys: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit userKeysUpdate(sync_token, keys);
|
|
|
|
});
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:45:28 +03:00
|
|
|
void
|
|
|
|
Cache::query_keys(const std::string &user_id,
|
|
|
|
std::function<void(const UserKeyCache &, mtx::http::RequestErr)> cb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::requests::QueryKeys req;
|
|
|
|
std::string last_changed;
|
|
|
|
{
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto cache_ = userKeys_(user_id, txn);
|
2021-05-22 15:31:38 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (cache_.has_value()) {
|
|
|
|
if (cache_->updated_at == cache_->last_changed) {
|
|
|
|
cb(cache_.value(), {});
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
nhlog::db()->info("Keys outdated for {}: {} vs {}",
|
|
|
|
user_id,
|
|
|
|
cache_->updated_at,
|
|
|
|
cache_->last_changed);
|
|
|
|
} else
|
|
|
|
nhlog::db()->info("No keys found for {}", user_id);
|
|
|
|
|
|
|
|
req.device_keys[user_id] = {};
|
|
|
|
|
|
|
|
if (cache_)
|
|
|
|
last_changed = cache_->last_changed;
|
|
|
|
req.token = last_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use context object so that we can disconnect again
|
|
|
|
QObject *context{new QObject(this)};
|
|
|
|
QObject::connect(
|
|
|
|
this,
|
|
|
|
&Cache::verificationStatusChanged,
|
|
|
|
context,
|
|
|
|
[cb, user_id, context_ = context, this](std::string updated_user) mutable {
|
|
|
|
if (user_id == updated_user) {
|
|
|
|
context_->deleteLater();
|
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
auto keys = this->userKeys_(user_id, txn);
|
|
|
|
cb(keys.value_or(UserKeyCache{}), {});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Qt::QueuedConnection);
|
|
|
|
|
|
|
|
http::client()->query_keys(
|
|
|
|
req,
|
|
|
|
[cb, user_id, last_changed, this](const mtx::responses::QueryKeys &res,
|
|
|
|
mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to query device keys: {},{}",
|
|
|
|
mtx::errors::to_string(err->matrix_error.errcode),
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
cb({}, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit userKeysUpdate(last_changed, res);
|
|
|
|
});
|
2020-10-27 19:45:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-28 18:31:34 +03:00
|
|
|
void
|
2020-10-02 02:14:42 +03:00
|
|
|
to_json(json &j, const VerificationCache &info)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
j["device_verified"] = info.device_verified;
|
|
|
|
j["device_blocked"] = info.device_blocked;
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-02 02:14:42 +03:00
|
|
|
from_json(const json &j, VerificationCache &info)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
info.device_verified = j.at("device_verified").get<std::set<std::string>>();
|
|
|
|
info.device_blocked = j.at("device_blocked").get<std::set<std::string>>();
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2021-08-17 04:23:51 +03:00
|
|
|
void
|
|
|
|
to_json(json &j, const OnlineBackupVersion &info)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
j["v"] = info.version;
|
|
|
|
j["a"] = info.algorithm;
|
2021-08-17 04:23:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
from_json(const json &j, OnlineBackupVersion &info)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
info.version = j.at("v").get<std::string>();
|
|
|
|
info.algorithm = j.at("a").get<std::string>();
|
2021-08-17 04:23:51 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
std::optional<VerificationCache>
|
2021-08-14 00:13:09 +03:00
|
|
|
Cache::verificationCache(const std::string &user_id, lmdb::txn &txn)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view verifiedVal;
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto db = getVerificationDb(txn);
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
VerificationCache verified_state;
|
|
|
|
auto res = db.get(txn, user_id, verifiedVal);
|
|
|
|
if (res) {
|
|
|
|
verified_state = json::parse(verifiedVal);
|
|
|
|
return verified_state;
|
|
|
|
} else {
|
|
|
|
return {};
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (std::exception &) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
void
|
|
|
|
Cache::markDeviceVerified(const std::string &user_id, const std::string &key)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
std::string_view val;
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getVerificationDb(txn);
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
VerificationCache verified_state;
|
|
|
|
auto res = db.get(txn, user_id, val);
|
|
|
|
if (res) {
|
|
|
|
verified_state = json::parse(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &device : verified_state.device_verified)
|
|
|
|
if (device == key)
|
|
|
|
return;
|
|
|
|
|
|
|
|
verified_state.device_verified.insert(key);
|
|
|
|
db.put(txn, user_id, json(verified_state).dump());
|
|
|
|
txn.commit();
|
|
|
|
} catch (std::exception &) {
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto local_user = utils::localUser().toStdString();
|
|
|
|
std::map<std::string, VerificationStatus> tmp;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(verification_storage.verification_storage_mtx);
|
2020-10-08 00:03:14 +03:00
|
|
|
if (user_id == local_user) {
|
2021-09-18 01:22:33 +03:00
|
|
|
std::swap(tmp, verification_storage.status);
|
2021-11-02 00:20:15 +03:00
|
|
|
verification_storage.status.clear();
|
2020-10-08 00:03:14 +03:00
|
|
|
} else {
|
2021-09-18 01:22:33 +03:00
|
|
|
verification_storage.status.erase(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (user_id == local_user) {
|
|
|
|
for (const auto &[user, status] : tmp) {
|
|
|
|
(void)status;
|
|
|
|
emit verificationStatusChanged(user);
|
2020-10-08 00:03:14 +03:00
|
|
|
}
|
2021-11-08 18:26:16 +03:00
|
|
|
} else {
|
|
|
|
emit verificationStatusChanged(user_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::markDeviceUnverified(const std::string &user_id, const std::string &key)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string_view val;
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
auto db = getVerificationDb(txn);
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
VerificationCache verified_state;
|
|
|
|
auto res = db.get(txn, user_id, val);
|
|
|
|
if (res) {
|
|
|
|
verified_state = json::parse(val);
|
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
verified_state.device_verified.erase(key);
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
db.put(txn, user_id, json(verified_state).dump());
|
|
|
|
txn.commit();
|
|
|
|
} catch (std::exception &) {
|
|
|
|
}
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto local_user = utils::localUser().toStdString();
|
|
|
|
std::map<std::string, VerificationStatus> tmp;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(verification_storage.verification_storage_mtx);
|
2020-10-08 00:03:14 +03:00
|
|
|
if (user_id == local_user) {
|
2021-09-18 01:22:33 +03:00
|
|
|
std::swap(tmp, verification_storage.status);
|
2020-10-08 00:03:14 +03:00
|
|
|
} else {
|
2021-09-18 01:22:33 +03:00
|
|
|
verification_storage.status.erase(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (user_id == local_user) {
|
|
|
|
for (const auto &[user, status] : tmp) {
|
|
|
|
(void)status;
|
|
|
|
emit verificationStatusChanged(user);
|
2020-10-08 00:03:14 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-11-02 00:20:15 +03:00
|
|
|
emit verificationStatusChanged(user_id);
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
|
|
|
|
2020-10-08 00:03:14 +03:00
|
|
|
VerificationStatus
|
|
|
|
Cache::verificationStatus(const std::string &user_id)
|
2021-08-14 00:13:09 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto txn = ro_txn(env_);
|
|
|
|
return verificationStatus_(user_id, txn);
|
2021-08-14 00:13:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VerificationStatus
|
|
|
|
Cache::verificationStatus_(const std::string &user_id, lmdb::txn &txn)
|
2020-10-02 02:14:42 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::unique_lock<std::mutex> lock(verification_storage.verification_storage_mtx);
|
|
|
|
if (verification_storage.status.count(user_id))
|
|
|
|
return verification_storage.status.at(user_id);
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
VerificationStatus status;
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// assume there is at least one unverified device until we have checked we have the device
|
|
|
|
// list for that user.
|
|
|
|
status.unverified_device_count = 1;
|
|
|
|
status.no_keys = true;
|
2021-08-14 00:13:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (auto verifCache = verificationCache(user_id, txn)) {
|
|
|
|
status.verified_devices = verifCache->device_verified;
|
|
|
|
}
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto local_user = utils::localUser().toStdString();
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
crypto::Trust trustlevel = crypto::Trust::Unverified;
|
|
|
|
if (user_id == local_user) {
|
|
|
|
status.verified_devices.insert(http::client()->device_id());
|
|
|
|
trustlevel = crypto::Trust::Verified;
|
|
|
|
}
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto verifyAtLeastOneSig = [](const auto &toVerif,
|
|
|
|
const std::map<std::string, std::string> &keys,
|
|
|
|
const std::string &keyOwner) {
|
|
|
|
if (!toVerif.signatures.count(keyOwner))
|
|
|
|
return false;
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &[key_id, signature] : toVerif.signatures.at(keyOwner)) {
|
|
|
|
if (!keys.count(key_id))
|
|
|
|
continue;
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (mtx::crypto::ed25519_verify_signature(keys.at(key_id), json(toVerif), signature))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto updateUnverifiedDevices = [&status](auto &theirDeviceKeys) {
|
|
|
|
int currentVerifiedDevices = 0;
|
|
|
|
for (auto device_id : status.verified_devices) {
|
|
|
|
if (theirDeviceKeys.count(device_id))
|
|
|
|
currentVerifiedDevices++;
|
|
|
|
}
|
|
|
|
status.unverified_device_count =
|
|
|
|
static_cast<int>(theirDeviceKeys.size()) - currentVerifiedDevices;
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
// for local user verify this device_key -> our master_key -> our self_signing_key
|
|
|
|
// -> our device_keys
|
|
|
|
//
|
|
|
|
// for other user verify this device_key -> our master_key -> our user_signing_key
|
|
|
|
// -> their master_key -> their self_signing_key -> their device_keys
|
|
|
|
//
|
|
|
|
// This means verifying the other user adds 2 extra steps,verifying our user_signing
|
|
|
|
// key and their master key
|
|
|
|
auto ourKeys = userKeys_(local_user, txn);
|
|
|
|
auto theirKeys = userKeys_(user_id, txn);
|
|
|
|
if (theirKeys)
|
|
|
|
status.no_keys = false;
|
|
|
|
|
|
|
|
if (!ourKeys || !theirKeys) {
|
|
|
|
verification_storage.status[user_id] = status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update verified devices count to count without cross-signing
|
|
|
|
updateUnverifiedDevices(theirKeys->device_keys);
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
|
|
|
auto &mk = ourKeys->master_keys;
|
|
|
|
std::string dev_id = "ed25519:" + http::client()->device_id();
|
|
|
|
if (!mk.signatures.count(local_user) || !mk.signatures.at(local_user).count(dev_id) ||
|
|
|
|
!mtx::crypto::ed25519_verify_signature(olm::client()->identity_keys().ed25519,
|
|
|
|
json(mk),
|
|
|
|
mk.signatures.at(local_user).at(dev_id))) {
|
|
|
|
nhlog::crypto()->debug("We have not verified our own master key");
|
|
|
|
verification_storage.status[user_id] = status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
2021-08-14 00:13:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto master_keys = ourKeys->master_keys.keys;
|
2021-08-14 00:13:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (user_id != local_user) {
|
|
|
|
bool theirMasterKeyVerified =
|
|
|
|
verifyAtLeastOneSig(ourKeys->user_signing_keys, master_keys, local_user) &&
|
|
|
|
verifyAtLeastOneSig(
|
|
|
|
theirKeys->master_keys, ourKeys->user_signing_keys.keys, local_user);
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (theirMasterKeyVerified)
|
|
|
|
trustlevel = crypto::Trust::Verified;
|
|
|
|
else if (!theirKeys->master_key_changed)
|
|
|
|
trustlevel = crypto::Trust::TOFU;
|
|
|
|
else {
|
|
|
|
verification_storage.status[user_id] = status;
|
|
|
|
return status;
|
|
|
|
}
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
master_keys = theirKeys->master_keys.keys;
|
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
status.user_verified = trustlevel;
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
verification_storage.status[user_id] = status;
|
|
|
|
if (!verifyAtLeastOneSig(theirKeys->self_signing_keys, master_keys, user_id))
|
|
|
|
return status;
|
2020-10-08 00:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &[device, device_key] : theirKeys->device_keys) {
|
|
|
|
(void)device;
|
|
|
|
try {
|
|
|
|
auto identkey = device_key.keys.at("curve25519:" + device_key.device_id);
|
|
|
|
if (verifyAtLeastOneSig(device_key, theirKeys->self_signing_keys.keys, user_id)) {
|
|
|
|
status.verified_devices.insert(device_key.device_id);
|
|
|
|
status.verified_device_keys[identkey] = trustlevel;
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
}
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
updateUnverifiedDevices(theirKeys->device_keys);
|
|
|
|
verification_storage.status[user_id] = status;
|
|
|
|
return status;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
nhlog::db()->error("Failed to calculate verification status of {}: {}", user_id, e.what());
|
|
|
|
return status;
|
|
|
|
}
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 21:53:44 +03:00
|
|
|
void
|
|
|
|
to_json(json &j, const RoomInfo &info)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
j["name"] = info.name;
|
|
|
|
j["topic"] = info.topic;
|
|
|
|
j["avatar_url"] = info.avatar_url;
|
|
|
|
j["version"] = info.version;
|
|
|
|
j["is_invite"] = info.is_invite;
|
|
|
|
j["is_space"] = info.is_space;
|
|
|
|
j["join_rule"] = info.join_rule;
|
|
|
|
j["guest_access"] = info.guest_access;
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (info.member_count != 0)
|
|
|
|
j["member_count"] = info.member_count;
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (info.tags.size() != 0)
|
|
|
|
j["tags"] = info.tags;
|
2019-06-27 21:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
from_json(const json &j, RoomInfo &info)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
info.name = j.at("name");
|
|
|
|
info.topic = j.at("topic");
|
|
|
|
info.avatar_url = j.at("avatar_url");
|
|
|
|
info.version = j.value(
|
|
|
|
"version", QCoreApplication::translate("RoomInfo", "no version stored").toStdString());
|
|
|
|
info.is_invite = j.at("is_invite");
|
|
|
|
info.is_space = j.value("is_space", false);
|
|
|
|
info.join_rule = j.at("join_rule");
|
|
|
|
info.guest_access = j.at("guest_access");
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (j.count("member_count"))
|
|
|
|
info.member_count = j.at("member_count");
|
2019-06-27 21:53:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (j.count("tags"))
|
|
|
|
info.tags = j.at("tags").get<std::vector<std::string>>();
|
2019-06-27 21:53:44 +03:00
|
|
|
}
|
2019-12-15 01:39:02 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
to_json(json &j, const ReadReceiptKey &key)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
j = json{{"event_id", key.event_id}, {"room_id", key.room_id}};
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
from_json(const json &j, ReadReceiptKey &key)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
key.event_id = j.at("event_id").get<std::string>();
|
|
|
|
key.room_id = j.at("room_id").get<std::string>();
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
to_json(json &j, const MemberInfo &info)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
j["name"] = info.name;
|
|
|
|
j["avatar_url"] = info.avatar_url;
|
2021-11-21 09:06:37 +03:00
|
|
|
if (info.is_direct)
|
|
|
|
j["is_direct"] = info.is_direct;
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
from_json(const json &j, MemberInfo &info)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
info.name = j.at("name");
|
|
|
|
info.avatar_url = j.at("avatar_url");
|
2021-11-21 09:06:37 +03:00
|
|
|
info.is_direct = j.value("is_direct", false);
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
2020-11-30 02:26:27 +03:00
|
|
|
void
|
2021-07-17 02:27:37 +03:00
|
|
|
to_json(nlohmann::json &obj, const DeviceKeysToMsgIndex &msg)
|
2020-11-30 02:26:27 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["deviceids"] = msg.deviceids;
|
2020-11-30 02:26:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-07-17 02:27:37 +03:00
|
|
|
from_json(const nlohmann::json &obj, DeviceKeysToMsgIndex &msg)
|
2020-11-30 02:26:27 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.deviceids = obj.at("deviceids").get<decltype(msg.deviceids)>();
|
2020-11-30 02:26:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
to_json(nlohmann::json &obj, const SharedWithUsers &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["keys"] = msg.keys;
|
2020-11-30 02:26:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
from_json(const nlohmann::json &obj, SharedWithUsers &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.keys = obj.at("keys").get<std::map<std::string, DeviceKeysToMsgIndex>>();
|
2020-11-30 02:26:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-15 01:39:02 +03:00
|
|
|
void
|
2021-07-17 02:27:37 +03:00
|
|
|
to_json(nlohmann::json &obj, const GroupSessionData &msg)
|
2019-12-15 01:39:02 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["message_index"] = msg.message_index;
|
|
|
|
obj["ts"] = msg.timestamp;
|
|
|
|
obj["trust"] = msg.trusted;
|
2020-11-30 02:26:27 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["sender_claimed_ed25519_key"] = msg.sender_claimed_ed25519_key;
|
|
|
|
obj["forwarding_curve25519_key_chain"] = msg.forwarding_curve25519_key_chain;
|
2021-07-17 02:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["currently"] = msg.currently;
|
2021-08-08 00:54:35 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["indices"] = msg.indices;
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-07-17 02:27:37 +03:00
|
|
|
from_json(const nlohmann::json &obj, GroupSessionData &msg)
|
2019-12-15 01:39:02 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.message_index = obj.at("message_index");
|
|
|
|
msg.timestamp = obj.value("ts", 0ULL);
|
|
|
|
msg.trusted = obj.value("trust", true);
|
2020-11-30 02:26:27 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.sender_claimed_ed25519_key = obj.value("sender_claimed_ed25519_key", "");
|
|
|
|
msg.forwarding_curve25519_key_chain =
|
|
|
|
obj.value("forwarding_curve25519_key_chain", std::vector<std::string>{});
|
2021-07-17 02:27:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.currently = obj.value("currently", SharedWithUsers{});
|
2021-08-08 00:54:35 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.indices = obj.value("indices", std::map<uint32_t, std::string>());
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
to_json(nlohmann::json &obj, const DevicePublicKeys &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["ed25519"] = msg.ed25519;
|
|
|
|
obj["curve25519"] = msg.curve25519;
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
from_json(const nlohmann::json &obj, DevicePublicKeys &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.ed25519 = obj.at("ed25519");
|
|
|
|
msg.curve25519 = obj.at("curve25519");
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
to_json(nlohmann::json &obj, const MegolmSessionIndex &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["room_id"] = msg.room_id;
|
|
|
|
obj["session_id"] = msg.session_id;
|
|
|
|
obj["sender_key"] = msg.sender_key;
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
from_json(const nlohmann::json &obj, MegolmSessionIndex &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.room_id = obj.at("room_id");
|
|
|
|
msg.session_id = obj.at("session_id");
|
|
|
|
msg.sender_key = obj.at("sender_key");
|
2019-12-15 01:39:02 +03:00
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
|
2020-10-20 14:46:05 +03:00
|
|
|
void
|
|
|
|
to_json(nlohmann::json &obj, const StoredOlmSession &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
obj["ts"] = msg.last_message_ts;
|
|
|
|
obj["s"] = msg.pickled_session;
|
2020-10-20 14:46:05 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
from_json(const nlohmann::json &obj, StoredOlmSession &msg)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
msg.last_message_ts = obj.at("ts").get<uint64_t>();
|
|
|
|
msg.pickled_session = obj.at("s").get<std::string>();
|
2020-10-20 14:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
namespace cache {
|
|
|
|
void
|
|
|
|
init(const QString &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
qRegisterMetaType<RoomMember>();
|
|
|
|
qRegisterMetaType<RoomSearchResult>();
|
|
|
|
qRegisterMetaType<RoomInfo>();
|
|
|
|
qRegisterMetaType<QMap<QString, RoomInfo>>();
|
|
|
|
qRegisterMetaType<std::map<QString, RoomInfo>>();
|
|
|
|
qRegisterMetaType<std::map<QString, mtx::responses::Timeline>>();
|
|
|
|
qRegisterMetaType<mtx::responses::QueryKeys>();
|
2019-12-15 04:56:04 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_ = std::make_unique<Cache>(user_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Cache *
|
|
|
|
client()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_.get();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
displayName(const std::string &room_id, const std::string &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->displayName(room_id, user_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
displayName(const QString &room_id, const QString &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->displayName(room_id, user_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
QString
|
|
|
|
avatarUrl(const QString &room_id, const QString &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->avatarUrl(room_id, user_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 02:45:24 +03:00
|
|
|
mtx::presence::PresenceState
|
|
|
|
presenceState(const std::string &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!instance_)
|
|
|
|
return {};
|
|
|
|
return instance_->presenceState(user_id);
|
2020-06-08 02:45:24 +03:00
|
|
|
}
|
|
|
|
std::string
|
|
|
|
statusMessage(const std::string &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->statusMessage(user_id);
|
2020-06-08 02:45:24 +03:00
|
|
|
}
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
// user cache stores user keys
|
|
|
|
std::optional<UserKeyCache>
|
|
|
|
userKeys(const std::string &user_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->userKeys(user_id);
|
2020-10-02 02:14:42 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
updateUserKeys(const std::string &sync_token, const mtx::responses::QueryKeys &keyQuery)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->updateUserKeys(sync_token, keyQuery);
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
// device & user verification cache
|
2020-10-08 00:03:14 +03:00
|
|
|
std::optional<VerificationStatus>
|
2020-10-02 02:14:42 +03:00
|
|
|
verificationStatus(const std::string &user_id)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->verificationStatus(user_id);
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
void
|
2020-10-08 00:03:14 +03:00
|
|
|
markDeviceVerified(const std::string &user_id, const std::string &device)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->markDeviceVerified(user_id, device);
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
void
|
2020-10-08 00:03:14 +03:00
|
|
|
markDeviceUnverified(const std::string &user_id, const std::string &device)
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->markDeviceUnverified(user_id, device);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
joinedRooms()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->joinedRooms();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, RoomInfo>
|
|
|
|
roomInfo(bool withInvites)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->roomInfo(withInvites);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2021-05-24 15:04:07 +03:00
|
|
|
QHash<QString, RoomInfo>
|
2019-12-15 04:56:04 +03:00
|
|
|
invites()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->invites();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getRoomName(txn, statesdb, membersdb);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
mtx::events::state::JoinRule
|
|
|
|
getRoomJoinRule(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getRoomJoinRule(txn, statesdb);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
getRoomGuestAccess(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getRoomGuestAccess(txn, statesdb);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
QString
|
|
|
|
getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getRoomTopic(txn, statesdb);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
QString
|
2020-11-26 00:45:33 +03:00
|
|
|
getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb)
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getRoomAvatarUrl(txn, statesdb, membersdb);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RoomMember>
|
|
|
|
getMembers(const std::string &room_id, std::size_t startIndex, std::size_t len)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getMembers(room_id, startIndex, len);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 02:45:01 +03:00
|
|
|
std::vector<RoomMember>
|
|
|
|
getMembersFromInvite(const std::string &room_id, std::size_t startIndex, std::size_t len)
|
2021-09-10 04:31:23 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getMembersFromInvite(room_id, startIndex, len);
|
2021-09-10 04:31:23 +03:00
|
|
|
}
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
void
|
|
|
|
saveState(const mtx::responses::Sync &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->saveState(res);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
isInitialized()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->isInitialized();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
nextBatchToken()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->nextBatchToken();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
deleteData()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->deleteData();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
removeInvite(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->removeInvite(txn, room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
removeInvite(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->removeInvite(room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
removeRoom(lmdb::txn &txn, const std::string &roomid)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->removeRoom(txn, roomid);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
removeRoom(const std::string &roomid)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->removeRoom(roomid);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
removeRoom(const QString &roomid)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->removeRoom(roomid.toStdString());
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
setup()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->setup();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-05-02 17:44:50 +03:00
|
|
|
runMigrations()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->runMigrations();
|
2020-05-02 17:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cache::CacheVersion
|
|
|
|
formatVersion()
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->formatVersion();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2020-05-02 17:44:50 +03:00
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
void
|
|
|
|
setCurrentFormat()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->setCurrentFormat();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 15:06:28 +03:00
|
|
|
std::vector<QString>
|
|
|
|
roomIds()
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->roomIds();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, mtx::responses::Notifications>
|
|
|
|
getTimelineMentions()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getTimelineMentions();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Retrieve all the user ids from a room.
|
|
|
|
std::vector<std::string>
|
|
|
|
roomMembers(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->roomMembers(room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! 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)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->hasEnoughPowerLevel(eventTypes, room_id, user_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
updateReadReceipt(lmdb::txn &txn, const std::string &room_id, const Receipts &receipts)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->updateReadReceipt(txn, room_id, receipts);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UserReceipts
|
|
|
|
readReceipts(const QString &event_id, const QString &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->readReceipts(event_id, room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 03:03:20 +03:00
|
|
|
std::optional<uint64_t>
|
|
|
|
getEventIndex(const std::string &room_id, std::string_view event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getEventIndex(room_id, event_id);
|
2021-02-10 03:03:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::pair<uint64_t, std::string>>
|
|
|
|
lastInvisibleEventAfter(const std::string &room_id, std::string_view event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->lastInvisibleEventAfter(room_id, event_id);
|
2021-02-10 03:03:20 +03:00
|
|
|
}
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
RoomInfo
|
|
|
|
singleRoomInfo(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->singleRoomInfo(room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
std::vector<std::string>
|
|
|
|
roomsWithStateUpdates(const mtx::responses::Sync &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->roomsWithStateUpdates(res);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2021-06-16 01:09:45 +03:00
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
std::map<QString, RoomInfo>
|
|
|
|
getRoomInfo(const std::vector<std::string> &rooms)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getRoomInfo(rooms);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Calculates which the read status of a room.
|
|
|
|
//! Whether all the events in the timeline have been read.
|
|
|
|
bool
|
|
|
|
calculateRoomReadStatus(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->calculateRoomReadStatus(room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
calculateRoomReadStatus()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->calculateRoomReadStatus();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
markSentNotification(const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->markSentNotification(event_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
//! Removes an event from the sent notifications.
|
|
|
|
void
|
|
|
|
removeReadNotification(const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->removeReadNotification(event_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
//! Check if we have sent a desktop notification for the given event id.
|
|
|
|
bool
|
|
|
|
isNotificationSent(const std::string &event_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->isNotificationSent(event_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Add all notifications containing a user mention to the db.
|
|
|
|
void
|
|
|
|
saveTimelineMentions(const mtx::responses::Notifications &res)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->saveTimelineMentions(res);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Remove old unused data.
|
|
|
|
void
|
|
|
|
deleteOldMessages()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->deleteOldMessages();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
deleteOldData() noexcept
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->deleteOldData();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
//! Retrieve all saved room ids.
|
|
|
|
std::vector<std::string>
|
|
|
|
getRoomIds(lmdb::txn &txn)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getRoomIds(txn);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Mark a room that uses e2e encryption.
|
|
|
|
void
|
|
|
|
setEncryptedRoom(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->setEncryptedRoom(txn, room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
isRoomEncrypted(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->isRoomEncrypted(room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Check if a user is a member of the room.
|
|
|
|
bool
|
|
|
|
isRoomMember(const std::string &user_id, const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->isRoomMember(user_id, room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Outbound Megolm Sessions
|
|
|
|
//
|
|
|
|
void
|
|
|
|
saveOutboundMegolmSession(const std::string &room_id,
|
2021-07-17 02:27:37 +03:00
|
|
|
const GroupSessionData &data,
|
2020-11-30 02:26:27 +03:00
|
|
|
mtx::crypto::OutboundGroupSessionPtr &session)
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->saveOutboundMegolmSession(room_id, data, session);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
OutboundGroupSessionDataRef
|
|
|
|
getOutboundMegolmSession(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getOutboundMegolmSession(room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
outboundMegolmSessionExists(const std::string &room_id) noexcept
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->outboundMegolmSessionExists(room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
void
|
2020-11-27 06:56:44 +03:00
|
|
|
updateOutboundMegolmSession(const std::string &room_id,
|
2021-07-17 02:27:37 +03:00
|
|
|
const GroupSessionData &data,
|
2020-11-27 06:56:44 +03:00
|
|
|
mtx::crypto::OutboundGroupSessionPtr &session)
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->updateOutboundMegolmSession(room_id, data, session);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2020-10-03 19:38:28 +03:00
|
|
|
void
|
|
|
|
dropOutboundMegolmSession(const std::string &room_id)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->dropOutboundMegolmSession(room_id);
|
2020-10-03 19:38:28 +03:00
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
importSessionKeys(const mtx::crypto::ExportedSessionKeys &keys)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->importSessionKeys(keys);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
mtx::crypto::ExportedSessionKeys
|
|
|
|
exportSessionKeys()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->exportSessionKeys();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Inbound Megolm Sessions
|
|
|
|
//
|
|
|
|
void
|
|
|
|
saveInboundMegolmSession(const MegolmSessionIndex &index,
|
2021-07-17 02:27:37 +03:00
|
|
|
mtx::crypto::InboundGroupSessionPtr session,
|
|
|
|
const GroupSessionData &data)
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->saveInboundMegolmSession(index, std::move(session), data);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2020-11-27 06:19:03 +03:00
|
|
|
mtx::crypto::InboundGroupSessionPtr
|
2019-12-15 04:56:04 +03:00
|
|
|
getInboundMegolmSession(const MegolmSessionIndex &index)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getInboundMegolmSession(index);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
inboundMegolmSessionExists(const MegolmSessionIndex &index)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->inboundMegolmSessionExists(index);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2021-07-17 03:14:44 +03:00
|
|
|
std::optional<GroupSessionData>
|
|
|
|
getMegolmSessionData(const MegolmSessionIndex &index)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getMegolmSessionData(index);
|
2021-07-17 03:14:44 +03:00
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Olm Sessions
|
|
|
|
//
|
|
|
|
void
|
2020-10-20 14:46:05 +03:00
|
|
|
saveOlmSession(const std::string &curve25519,
|
|
|
|
mtx::crypto::OlmSessionPtr session,
|
|
|
|
uint64_t timestamp)
|
2019-12-15 04:56:04 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->saveOlmSession(curve25519, std::move(session), timestamp);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
std::vector<std::string>
|
|
|
|
getOlmSessions(const std::string &curve25519)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getOlmSessions(curve25519);
|
2019-12-15 04:56:04 +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
|
|
|
return instance_->getOlmSession(curve25519, session_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2020-10-20 14:46:05 +03:00
|
|
|
std::optional<mtx::crypto::OlmSessionPtr>
|
|
|
|
getLatestOlmSession(const std::string &curve25519)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->getLatestOlmSession(curve25519);
|
2020-10-20 14:46:05 +03:00
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
saveOlmAccount(const std::string &pickled)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->saveOlmAccount(pickled);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
std::string
|
|
|
|
restoreOlmAccount()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->restoreOlmAccount();
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
2020-12-17 00:10:09 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
storeSecret(const std::string &name, const std::string &secret)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_->storeSecret(name, secret);
|
2020-12-17 00:10:09 +03:00
|
|
|
}
|
|
|
|
std::optional<std::string>
|
|
|
|
secret(const std::string &name)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return instance_->secret(name);
|
2020-12-17 00:10:09 +03:00
|
|
|
}
|
2019-12-15 04:56:04 +03:00
|
|
|
} // namespace cache
|