2017-07-29 11:49:00 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2017-12-22 01:00:48 +03:00
|
|
|
#include <QByteArray>
|
2017-07-29 11:49:00 +03:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
2018-03-26 20:41:16 +03:00
|
|
|
#include <variant.hpp>
|
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
#include "Cache.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "RoomState.h"
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2018-01-15 00:33:12 +03:00
|
|
|
static const std::string CURRENT_CACHE_FORMAT_VERSION("2018.01.14");
|
2017-12-10 13:51:44 +03:00
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
static const lmdb::val NEXT_BATCH_KEY("next_batch");
|
2017-12-10 13:51:44 +03:00
|
|
|
static const lmdb::val CACHE_FORMAT_VERSION_KEY("cache_format_version");
|
2017-07-29 11:49:00 +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>>;
|
|
|
|
|
2018-01-21 22:43:21 +03:00
|
|
|
Cache::Cache(const QString &userId, QObject *parent)
|
|
|
|
: QObject{parent}
|
|
|
|
, env_{nullptr}
|
2017-11-06 00:04:55 +03:00
|
|
|
, stateDb_{0}
|
|
|
|
, roomDb_{0}
|
2017-12-19 23:36:12 +03:00
|
|
|
, invitesDb_{0}
|
2017-12-22 01:00:48 +03:00
|
|
|
, imagesDb_{0}
|
2018-01-03 19:05:49 +03:00
|
|
|
, readReceiptsDb_{0}
|
2017-11-06 00:04:55 +03:00
|
|
|
, isMounted_{false}
|
|
|
|
, userId_{userId}
|
2017-10-15 22:08:51 +03:00
|
|
|
{}
|
2017-10-03 21:16:31 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
Cache::setup()
|
|
|
|
{
|
|
|
|
qDebug() << "Setting up cache";
|
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto statePath = QString("%1/%2/state")
|
|
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
|
|
|
|
.arg(QString::fromUtf8(userId_.toUtf8().toHex()));
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-10-03 21:16:31 +03:00
|
|
|
cacheDirectory_ = QString("%1/%2")
|
|
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
|
|
|
|
.arg(QString::fromUtf8(userId_.toUtf8().toHex()));
|
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
bool isInitial = !QFile::exists(statePath);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
env_ = lmdb::env::create();
|
2017-12-22 01:00:48 +03:00
|
|
|
env_.set_mapsize(256UL * 1024UL * 1024UL); /* 256 MB */
|
2017-08-26 13:49:16 +03:00
|
|
|
env_.set_max_dbs(1024UL);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
if (isInitial) {
|
2017-10-03 21:16:31 +03:00
|
|
|
qDebug() << "First time initializing LMDB";
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
if (!QDir().mkpath(statePath)) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
("Unable to create state directory:" + statePath).toStdString().c_str());
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
|
|
|
env_.open(statePath.toStdString().c_str());
|
|
|
|
} 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
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "Resetting cache due to LMDB version mismatch:" << e.what();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
QDir stateDir(statePath);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +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-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
env_.open(statePath.toStdString().c_str());
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
stateDb_ = lmdb::dbi::open(txn, "state", MDB_CREATE);
|
|
|
|
roomDb_ = lmdb::dbi::open(txn, "rooms", MDB_CREATE);
|
|
|
|
invitesDb_ = lmdb::dbi::open(txn, "invites", MDB_CREATE);
|
|
|
|
imagesDb_ = lmdb::dbi::open(txn, "images", MDB_CREATE);
|
|
|
|
readReceiptsDb_ = lmdb::dbi::open(txn, "read_receipts", MDB_CREATE);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-10-03 21:16:31 +03:00
|
|
|
isMounted_ = true;
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-12-22 01:00:48 +03:00
|
|
|
void
|
|
|
|
Cache::saveImage(const QString &url, const QByteArray &image)
|
|
|
|
{
|
|
|
|
if (!isMounted_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto key = url.toUtf8();
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
|
|
|
|
lmdb::dbi_put(txn,
|
|
|
|
imagesDb_,
|
|
|
|
lmdb::val(key.data(), key.size()),
|
|
|
|
lmdb::val(image.data(), image.size()));
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << "saveImage:" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray
|
|
|
|
Cache::image(const QString &url) const
|
|
|
|
{
|
|
|
|
auto key = url.toUtf8();
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
|
|
|
|
lmdb::val image;
|
|
|
|
|
|
|
|
bool res = lmdb::dbi_get(txn, imagesDb_, lmdb::val(key.data(), key.size()), image);
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
return QByteArray();
|
|
|
|
|
|
|
|
return QByteArray(image.data(), image.size());
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << "image:" << e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-01-13 18:15:47 +03:00
|
|
|
Cache::setState(const QString &nextBatchToken,
|
2018-01-24 21:46:37 +03:00
|
|
|
const std::map<QString, QSharedPointer<RoomState>> &states)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
if (!isMounted_)
|
|
|
|
return;
|
|
|
|
|
2017-10-21 21:17:01 +03:00
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-10-21 21:17:01 +03:00
|
|
|
setNextBatchToken(txn, nextBatchToken);
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
for (auto const &state : states)
|
|
|
|
insertRoomState(txn, state.first, state.second);
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-10-21 21:17:01 +03:00
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << "The cache couldn't be updated: " << e.what();
|
|
|
|
|
|
|
|
unmount();
|
|
|
|
deleteData();
|
|
|
|
}
|
2017-08-26 13:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-13 18:15:47 +03:00
|
|
|
Cache::insertRoomState(lmdb::txn &txn,
|
|
|
|
const QString &roomid,
|
|
|
|
const QSharedPointer<RoomState> &state)
|
2017-08-26 13:49:16 +03:00
|
|
|
{
|
2018-01-13 18:15:47 +03:00
|
|
|
auto stateEvents = state->serialize();
|
2017-08-26 13:49:16 +03:00
|
|
|
auto id = roomid.toUtf8();
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
lmdb::dbi_put(txn, roomDb_, lmdb::val(id.data(), id.size()), lmdb::val(stateEvents));
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2018-01-13 18:15:47 +03:00
|
|
|
for (const auto &membership : state->memberships) {
|
2017-08-26 13:49:16 +03:00
|
|
|
lmdb::dbi membersDb =
|
|
|
|
lmdb::dbi::open(txn, roomid.toStdString().c_str(), MDB_CREATE);
|
|
|
|
|
|
|
|
// The user_id this membership event relates to, is used
|
|
|
|
// as the index on the membership database.
|
2017-12-04 19:41:19 +03:00
|
|
|
auto key = membership.second.state_key;
|
|
|
|
|
|
|
|
// Serialize membership event.
|
|
|
|
nlohmann::json data = membership.second;
|
|
|
|
std::string memberEvent = data.dump();
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
switch (membership.second.content.membership) {
|
2017-10-01 12:11:33 +03:00
|
|
|
// We add or update (e.g invite -> join) a new user to the membership
|
|
|
|
// list.
|
2017-12-04 19:41:19 +03:00
|
|
|
case mtx::events::state::Membership::Invite:
|
|
|
|
case mtx::events::state::Membership::Join: {
|
|
|
|
lmdb::dbi_put(txn, membersDb, lmdb::val(key), lmdb::val(memberEvent));
|
2017-08-26 13:49:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// We remove the user from the membership list.
|
2017-12-04 19:41:19 +03:00
|
|
|
case mtx::events::state::Membership::Leave:
|
|
|
|
case mtx::events::state::Membership::Ban: {
|
|
|
|
lmdb::dbi_del(txn, membersDb, lmdb::val(key), lmdb::val(memberEvent));
|
2017-08-26 13:49:16 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-12-04 19:41:19 +03:00
|
|
|
case mtx::events::state::Membership::Knock: {
|
|
|
|
qWarning()
|
|
|
|
<< "Skipping knock membership" << roomid << QString::fromStdString(key);
|
2017-08-26 13:49:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
void
|
|
|
|
Cache::removeRoom(const QString &roomid)
|
|
|
|
{
|
2017-10-03 21:16:31 +03:00
|
|
|
if (!isMounted_)
|
|
|
|
return;
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, 0);
|
|
|
|
|
|
|
|
lmdb::dbi_del(txn, roomDb_, lmdb::val(roomid.toUtf8(), roomid.toUtf8().size()), nullptr);
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:36:12 +03:00
|
|
|
void
|
|
|
|
Cache::removeInvite(const QString &room_id)
|
|
|
|
{
|
|
|
|
if (!isMounted_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, 0);
|
|
|
|
|
|
|
|
lmdb::dbi_del(
|
|
|
|
txn, invitesDb_, lmdb::val(room_id.toUtf8(), room_id.toUtf8().size()), nullptr);
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:43:21 +03:00
|
|
|
void
|
2017-08-20 13:47:22 +03:00
|
|
|
Cache::states()
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
std::map<QString, RoomState> states;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, roomDb_);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
std::string room;
|
|
|
|
std::string stateData;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
// Retrieve all the room names.
|
|
|
|
while (cursor.get(room, stateData, MDB_NEXT)) {
|
|
|
|
auto roomid = QString::fromUtf8(room.data(), room.size());
|
2017-12-04 19:41:19 +03:00
|
|
|
auto json = nlohmann::json::parse(stateData);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
RoomState state;
|
2017-12-04 19:41:19 +03:00
|
|
|
state.parse(json);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto memberDb = lmdb::dbi::open(txn, roomid.toStdString().c_str(), MDB_CREATE);
|
2017-12-04 19:41:19 +03:00
|
|
|
std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>> members;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto memberCursor = lmdb::cursor::open(txn, memberDb);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
std::string memberId;
|
|
|
|
std::string memberContent;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
while (memberCursor.get(memberId, memberContent, MDB_NEXT)) {
|
2017-12-04 19:41:19 +03:00
|
|
|
auto userid = QString::fromStdString(memberId);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
auto data = nlohmann::json::parse(memberContent);
|
|
|
|
mtx::events::StateEvent<mtx::events::state::Member> member = data;
|
|
|
|
members.emplace(memberId, member);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
qWarning() << "Fault while parsing member event" << e.what()
|
|
|
|
<< QString::fromStdString(memberContent);
|
2017-08-26 13:49:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
qDebug() << members.size() << "members for" << roomid;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
state.memberships = members;
|
2018-01-24 21:46:37 +03:00
|
|
|
states.emplace(roomid, std::move(state));
|
2017-08-26 13:49:16 +03:00
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
qDebug() << "Retrieved" << states.size() << "rooms";
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
cursor.close();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2018-01-21 22:43:21 +03:00
|
|
|
emit statesLoaded(states);
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-08-26 13:49:16 +03:00
|
|
|
Cache::setNextBatchToken(lmdb::txn &txn, const QString &token)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
auto value = token.toUtf8();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
lmdb::dbi_put(txn, stateDb_, NEXT_BATCH_KEY, lmdb::val(value.data(), value.size()));
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
bool
|
2017-08-26 13:49:16 +03:00
|
|
|
Cache::isInitialized() const
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
lmdb::val token;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
bool res = lmdb::dbi_get(txn, stateDb_, NEXT_BATCH_KEY, token);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
return res;
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QString
|
2017-08-26 13:49:16 +03:00
|
|
|
Cache::nextBatchToken() const
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
lmdb::val token;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
lmdb::dbi_get(txn, stateDb_, NEXT_BATCH_KEY, token);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
return QString::fromUtf8(token.data(), token.size());
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
2017-10-22 19:03:55 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
Cache::deleteData()
|
|
|
|
{
|
|
|
|
qInfo() << "Deleting cache data";
|
|
|
|
|
|
|
|
if (!cacheDirectory_.isEmpty())
|
|
|
|
QDir(cacheDirectory_).removeRecursively();
|
|
|
|
}
|
2017-12-10 13:51:44 +03:00
|
|
|
|
|
|
|
bool
|
|
|
|
Cache::isFormatValid()
|
|
|
|
{
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
|
|
|
|
lmdb::val current_version;
|
|
|
|
bool res = lmdb::dbi_get(txn, stateDb_, CACHE_FORMAT_VERSION_KEY, current_version);
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string stored_version(current_version.data(), current_version.size());
|
|
|
|
|
|
|
|
if (stored_version != CURRENT_CACHE_FORMAT_VERSION) {
|
|
|
|
qWarning() << "Stored format version" << QString::fromStdString(stored_version);
|
|
|
|
qWarning() << "There are breaking changes in the cache format.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::setCurrentFormat()
|
|
|
|
{
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
|
|
|
|
lmdb::dbi_put(
|
|
|
|
txn,
|
|
|
|
stateDb_,
|
|
|
|
CACHE_FORMAT_VERSION_KEY,
|
|
|
|
lmdb::val(CURRENT_CACHE_FORMAT_VERSION.data(), CURRENT_CACHE_FORMAT_VERSION.size()));
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
}
|
2017-12-19 23:36:12 +03:00
|
|
|
|
|
|
|
std::map<std::string, mtx::responses::InvitedRoom>
|
|
|
|
Cache::invites()
|
|
|
|
{
|
|
|
|
std::map<std::string, mtx::responses::InvitedRoom> invites;
|
|
|
|
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, invitesDb_);
|
|
|
|
|
|
|
|
std::string room_id;
|
|
|
|
std::string payload;
|
|
|
|
|
|
|
|
mtx::responses::InvitedRoom state;
|
|
|
|
|
|
|
|
while (cursor.get(room_id, payload, MDB_NEXT)) {
|
|
|
|
state = nlohmann::json::parse(payload);
|
|
|
|
invites.emplace(room_id, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invites.size() > 0)
|
|
|
|
qDebug() << "Retrieved" << invites.size() << "invites";
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
|
|
|
|
return invites;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::setInvites(const std::map<std::string, mtx::responses::InvitedRoom> &invites)
|
|
|
|
{
|
|
|
|
if (!isMounted_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
|
|
|
|
for (auto it = invites.cbegin(); it != invites.cend(); ++it) {
|
|
|
|
nlohmann::json j;
|
|
|
|
|
|
|
|
for (const auto &e : it->second.invite_state) {
|
2018-03-26 20:41:16 +03:00
|
|
|
mpark::visit(
|
|
|
|
[&j](auto msg) { j["invite_state"]["events"].push_back(msg); },
|
|
|
|
e);
|
2017-12-19 23:36:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi_put(txn, invitesDb_, lmdb::val(it->first), lmdb::val(j.dump()));
|
|
|
|
}
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << "setInvitedRooms: " << e.what();
|
|
|
|
unmount();
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 19:05:49 +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)
|
|
|
|
{
|
2018-01-15 00:33:12 +03:00
|
|
|
CachedReceipts receipts;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
ReadReceiptKey receipt_key{event_id.toStdString(), room_id.toStdString()};
|
|
|
|
nlohmann::json json_key = receipt_key;
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
auto key = json_key.dump();
|
|
|
|
|
|
|
|
lmdb::val value;
|
|
|
|
|
|
|
|
bool res =
|
|
|
|
lmdb::dbi_get(txn, readReceiptsDb_, lmdb::val(key.data(), key.size()), value);
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
auto json_response = json::parse(std::string(value.data(), value.size()));
|
2018-01-15 00:33:12 +03:00
|
|
|
auto values = json_response.get<std::map<std::string, uint64_t>>();
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2018-02-28 13:12:07 +03:00
|
|
|
for (const auto &v : values)
|
2018-01-15 00:33:12 +03:00
|
|
|
// timestamp, user_id
|
|
|
|
receipts.emplace(v.second, v.first);
|
2018-01-03 19:05:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << "readReceipts:" << e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
return receipts;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::updateReadReceipt(const std::string &room_id, const Receipts &receipts)
|
|
|
|
{
|
2018-02-28 13:12:07 +03:00
|
|
|
for (const auto &receipt : receipts) {
|
2018-01-03 19:05:49 +03:00
|
|
|
const auto event_id = receipt.first;
|
|
|
|
auto event_receipts = receipt.second;
|
|
|
|
|
|
|
|
ReadReceiptKey receipt_key{event_id, room_id};
|
|
|
|
nlohmann::json json_key = receipt_key;
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto read_txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
const auto key = json_key.dump();
|
|
|
|
|
|
|
|
lmdb::val prev_value;
|
|
|
|
|
|
|
|
bool exists = lmdb::dbi_get(
|
|
|
|
read_txn, readReceiptsDb_, lmdb::val(key.data(), key.size()), prev_value);
|
|
|
|
|
|
|
|
read_txn.commit();
|
|
|
|
|
2018-01-15 00:33:12 +03:00
|
|
|
std::map<std::string, uint64_t> saved_receipts;
|
2018-01-03 19:05:49 +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(prev_value.data(), prev_value.size()));
|
|
|
|
|
|
|
|
// Retrieve the saved receipts.
|
2018-01-15 00:33:12 +03:00
|
|
|
saved_receipts = json_value.get<std::map<std::string, uint64_t>>();
|
2018-01-03 19:05:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append the new ones.
|
2018-02-28 13:12:07 +03:00
|
|
|
for (const auto &event_receipt : event_receipts)
|
2018-01-15 00:33:12 +03:00
|
|
|
saved_receipts.emplace(event_receipt.first, event_receipt.second);
|
2018-01-03 19:05:49 +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();
|
|
|
|
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
|
|
|
|
lmdb::dbi_put(txn,
|
|
|
|
readReceiptsDb_,
|
|
|
|
lmdb::val(key.data(), key.size()),
|
|
|
|
lmdb::val(merged_receipts.data(), merged_receipts.size()));
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << "updateReadReceipts:" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|