Skip to content
Snippets Groups Projects
Cache.cpp 143 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * 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>
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    #include <variant>
    
    #include <QByteArray>
    
    #include <QCoreApplication>
    
    #include <QFile>
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    #include <QHash>
    
    #include <QStandardPaths>
    
    
    #include <mtx/responses/common.hpp>
    
    #include "Cache.h"
    
    #include "Cache_p.h"
    
    #include "ChatPage.h"
    
    #include "EventAccessors.h"
    
    #include "Logging.h"
    
    #include "MatrixClient.h"
    
    #include "Olm.h"
    
    #include "Utils.h"
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    //! Should be changed when a breaking change occurs in the cache format.
    //! This will reset client's data.
    
    static const std::string CURRENT_CACHE_FORMAT_VERSION("2020.10.20");
    
    static const std::string SECRET("secret");
    
    static lmdb::val NEXT_BATCH_KEY("next_batch");
    static lmdb::val OLM_ACCOUNT_KEY("olm_account");
    static lmdb::val CACHE_FORMAT_VERSION_KEY("cache_format_version");
    
    constexpr size_t MAX_RESTORED_MESSAGES = 30'000;
    
    constexpr auto DB_SIZE    = 32ULL * 1024ULL * 1024ULL * 1024ULL; // 32 GB
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    constexpr auto MAX_DBS    = 32384UL;
    
    constexpr auto BATCH_SIZE = 100;
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    //! Cache databases and their format.
    //!
    //! Contains UI information for the joined rooms. (i.e name, topic, avatar url etc).
    //! Format: room_id -> RoomInfo
    
    constexpr auto ROOMS_DB("rooms");
    constexpr auto INVITES_DB("invites");
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    //! Keeps already downloaded media for reuse.
    //! Format: matrix_url -> binary data.
    
    constexpr auto MEDIA_DB("media");
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    //! Information that  must be kept between sync requests.
    
    constexpr auto SYNC_STATE_DB("sync_state");
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    //! Read receipts per room/event.
    
    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.
    
    constexpr auto ENCRYPTED_ROOMS_DB("encrypted_rooms");
    
    //! room_id -> pickled OlmInboundGroupSession
    
    constexpr auto INBOUND_MEGOLM_SESSIONS_DB("inbound_megolm_sessions");
    //! MegolmSessionIndex -> pickled OlmOutboundGroupSession
    constexpr auto OUTBOUND_MEGOLM_SESSIONS_DB("outbound_megolm_sessions");
    
    using CachedReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
    using Receipts       = std::map<std::string, std::map<std::string, uint64_t>>;
    
    
    Q_DECLARE_METATYPE(RoomMember)
    Q_DECLARE_METATYPE(mtx::responses::Timeline)
    Q_DECLARE_METATYPE(RoomSearchResult)
    Q_DECLARE_METATYPE(RoomInfo)
    
    Q_DECLARE_METATYPE(mtx::responses::QueryKeys)
    
    std::unique_ptr<Cache> instance_ = nullptr;
    
    bool
    Cache::isHiddenEvent(lmdb::txn &txn,
                         mtx::events::collections::TimelineEvents e,
                         const std::string &room_id)
    
    {
            using namespace mtx::events;
            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);
                    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::Event<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::Event<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);
    }
    
    
    Cache::Cache(const QString &userId, QObject *parent)
      : QObject{parent}
      , env_{nullptr}
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
      , syncStateDb_{0}
      , roomsDb_{0}
    
      , invitesDb_{0}
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
      , mediaDb_{0}
    
      , readReceiptsDb_{0}
    
      , devicesDb_{0}
      , deviceKeysDb_{0}
      , inboundMegolmSessionDb_{0}
      , outboundMegolmSessionDb_{0}
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
      , localUserId_{userId}
    
            connect(this, &Cache::userKeysUpdate, this, &Cache::updateUserKeys, Qt::QueuedConnection);
    
            nhlog::db()->debug("setting up cache");
    
            auto statePath = QString("%1/%2")
    
                               .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
                               .arg(QString::fromUtf8(localUserId_.toUtf8().toHex()));
    
            cacheDirectory_ = QString("%1/%2")
                                .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
                                .arg(QString::fromUtf8(localUserId_.toUtf8().toHex()));
    
            bool isInitial = !QFile::exists(statePath);
    
            env_.set_mapsize(DB_SIZE);
            env_.set_max_dbs(MAX_DBS);
    
                    nhlog::db()->info("initializing LMDB");
    
                    if (!QDir().mkpath(statePath)) {
                            throw std::runtime_error(
                              ("Unable to create state directory:" + statePath).toStdString().c_str());
                    }
            }
    
                    // 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(statePath.toStdString().c_str(), MDB_NOMETASYNC);
    
            } 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()));
                    }
    
                    nhlog::db()->warn("resetting cache due to LMDB version mismatch: {}", e.what());
    
                    for (const auto &file : stateDir.entryList(QDir::NoDotAndDotDot)) {
                            if (!stateDir.remove(file))
                                    throw std::runtime_error(
                                      ("Unable to delete file " + file).toStdString().c_str());
                    }
    
                    env_.open(statePath.toStdString().c_str());
            }
    
            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);
            invitesDb_       = lmdb::dbi::open(txn, INVITES_DB, MDB_CREATE);
            mediaDb_         = lmdb::dbi::open(txn, MEDIA_DB, MDB_CREATE);
            readReceiptsDb_  = lmdb::dbi::open(txn, READ_RECEIPTS_DB, MDB_CREATE);
            notificationsDb_ = lmdb::dbi::open(txn, NOTIFICATIONS_DB, MDB_CREATE);
    
    
            // 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);
    
            txn.commit();
    }
    
    
    Cache::setEncryptedRoom(lmdb::txn &txn, const std::string &room_id)
    
            nhlog::db()->info("mark room {} as encrypted", room_id);
    
            auto db = lmdb::dbi::open(txn, ENCRYPTED_ROOMS_DB, MDB_CREATE);
    
            lmdb::dbi_put(txn, db, lmdb::val(room_id), lmdb::val("0"));
    }
    
    bool
    Cache::isRoomEncrypted(const std::string &room_id)
    {
            lmdb::val unused;
    
            auto txn = lmdb::txn::begin(env_);
            auto db  = lmdb::dbi::open(txn, ENCRYPTED_ROOMS_DB, MDB_CREATE);
            auto res = lmdb::dbi_get(txn, db, lmdb::val(room_id), unused);
            txn.commit();
    
            return res;
    }
    
    
    mtx::crypto::ExportedSessionKeys
    Cache::exportSessionKeys()
    {
            using namespace mtx::crypto;
    
            ExportedSessionKeys keys;
    
            auto txn    = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
            auto cursor = lmdb::cursor::open(txn, inboundMegolmSessionDb_);
    
            std::string key, value;
            while (cursor.get(key, value, MDB_NEXT)) {
                    ExportedSession exported;
    
                    MegolmSessionIndex index;
    
    
                    auto saved_session = unpickle<InboundSessionObject>(value, SECRET);
    
    
                    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;
                    }
    
    
                    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());
    
                    keys.sessions.push_back(exported);
            }
    
            cursor.close();
            txn.commit();
    
            return keys;
    }
    
    void
    Cache::importSessionKeys(const mtx::crypto::ExportedSessionKeys &keys)
    {
            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;
    
                    auto exported_session = mtx::crypto::import_session(s.session_key);
    
                    saveInboundMegolmSession(index, std::move(exported_session));
            }
    }
    
    
    //
    // Session Management
    //
    
    void
    Cache::saveInboundMegolmSession(const MegolmSessionIndex &index,
                                    mtx::crypto::InboundGroupSessionPtr session)
    {
            using namespace mtx::crypto;
    
            const auto key     = json(index).dump();
    
            const auto pickled = pickle<InboundSessionObject>(session.get(), SECRET);
    
            auto txn = lmdb::txn::begin(env_);
            lmdb::dbi_put(txn, inboundMegolmSessionDb_, lmdb::val(key), lmdb::val(pickled));
            txn.commit();
    }
    
    
    mtx::crypto::InboundGroupSessionPtr
    
    Cache::getInboundMegolmSession(const MegolmSessionIndex &index)
    {
    
            using namespace mtx::crypto;
    
            try {
                    auto txn        = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
                    std::string key = json(index).dump();
                    lmdb::val value;
    
                    if (lmdb::dbi_get(txn, inboundMegolmSessionDb_, lmdb::val(key), value)) {
                            auto session = unpickle<InboundSessionObject>(
                              std::string(value.data(), value.size()), SECRET);
                            return session;
                    }
            } catch (std::exception &e) {
                    nhlog::db()->error("Failed to get inbound megolm session {}", e.what());
            }
    
            return nullptr;
    
    Cache::inboundMegolmSessionExists(const MegolmSessionIndex &index)
    
            using namespace mtx::crypto;
    
            try {
                    auto txn        = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
                    std::string key = json(index).dump();
                    lmdb::val value;
    
                    return lmdb::dbi_get(txn, inboundMegolmSessionDb_, lmdb::val(key), value);
            } catch (std::exception &e) {
                    nhlog::db()->error("Failed to get inbound megolm session {}", e.what());
            }
    
            return false;
    
    Cache::updateOutboundMegolmSession(const std::string &room_id,
                                       mtx::crypto::OutboundGroupSessionPtr &ptr)
    
    {
            using namespace mtx::crypto;
    
            if (!outboundMegolmSessionExists(room_id))
                    return;
    
            OutboundGroupSessionData data;
    
            data.message_index = olm_outbound_group_session_message_index(ptr.get());
            data.session_id    = mtx::crypto::session_id(ptr.get());
            data.session_key   = mtx::crypto::session_key(ptr.get());
    
    
            // Save the updated pickled data for the session.
            json j;
            j["data"]    = data;
    
            j["session"] = pickle<OutboundSessionObject>(ptr.get(), SECRET);
    
    
            auto txn = lmdb::txn::begin(env_);
            lmdb::dbi_put(txn, outboundMegolmSessionDb_, lmdb::val(room_id), lmdb::val(j.dump()));
            txn.commit();
    }
    
    
    void
    Cache::dropOutboundMegolmSession(const std::string &room_id)
    {
            using namespace mtx::crypto;
    
            if (!outboundMegolmSessionExists(room_id))
                    return;
    
            {
                    auto txn = lmdb::txn::begin(env_);
                    lmdb::dbi_del(txn, outboundMegolmSessionDb_, lmdb::val(room_id), nullptr);
                    txn.commit();
            }
    }
    
    
    Cache::saveOutboundMegolmSession(const std::string &room_id,
    
                                     const OutboundGroupSessionData &data,
                                     mtx::crypto::OutboundGroupSessionPtr session)
    {
            using namespace mtx::crypto;
            const auto pickled = pickle<OutboundSessionObject>(session.get(), SECRET);
    
            json j;
            j["data"]    = data;
            j["session"] = pickled;
    
            auto txn = lmdb::txn::begin(env_);
    
            lmdb::dbi_put(txn, outboundMegolmSessionDb_, lmdb::val(room_id), lmdb::val(j.dump()));
    
    Cache::outboundMegolmSessionExists(const std::string &room_id) noexcept
    
            try {
                    auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
                    lmdb::val value;
                    return lmdb::dbi_get(txn, outboundMegolmSessionDb_, lmdb::val(room_id), value);
            } catch (std::exception &e) {
                    nhlog::db()->error("Failed to retrieve outbound Megolm Session: {}", e.what());
                    return false;
            }
    
    Cache::getOutboundMegolmSession(const std::string &room_id)
    
            try {
                    using namespace mtx::crypto;
    
                    auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
                    lmdb::val value;
                    lmdb::dbi_get(txn, outboundMegolmSessionDb_, lmdb::val(room_id), value);
                    auto obj = json::parse(std::string_view(value.data(), value.size()));
    
                    OutboundGroupSessionDataRef ref{};
                    ref.data    = obj.at("data").get<OutboundGroupSessionData>();
                    ref.session = unpickle<OutboundSessionObject>(obj.at("session"), SECRET);
                    return ref;
            } catch (std::exception &e) {
                    nhlog::db()->error("Failed to retrieve outbound Megolm Session: {}", e.what());
                    return {};
            }
    
    Cache::saveOlmSession(const std::string &curve25519,
                          mtx::crypto::OlmSessionPtr session,
                          uint64_t timestamp)
    
    {
            using namespace mtx::crypto;
    
            auto txn = lmdb::txn::begin(env_);
    
            auto db  = getOlmSessionsDb(txn, curve25519);
    
            const auto pickled    = pickle<SessionObject>(session.get(), SECRET);
            const auto session_id = mtx::crypto::session_id(session.get());
    
    
            StoredOlmSession stored_session;
            stored_session.pickled_session = pickled;
            stored_session.last_message_ts = timestamp;
    
            lmdb::dbi_put(txn, db, lmdb::val(session_id), lmdb::val(json(stored_session).dump()));
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    std::optional<mtx::crypto::OlmSessionPtr>
    
    Cache::getOlmSession(const std::string &curve25519, const std::string &session_id)
    
            using namespace mtx::crypto;
    
            auto txn = lmdb::txn::begin(env_);
            auto db  = getOlmSessionsDb(txn, curve25519);
    
            lmdb::val pickled;
            bool found = lmdb::dbi_get(txn, db, lmdb::val(session_id), pickled);
    
            txn.commit();
    
            if (found) {
    
                    std::string_view raw(pickled.data(), pickled.size());
                    auto data = json::parse(raw).get<StoredOlmSession>();
                    return unpickle<SessionObject>(data.pickled_session, SECRET);
    
    Nicolas Werner's avatar
    Nicolas Werner committed
            return std::nullopt;
    
    std::optional<mtx::crypto::OlmSessionPtr>
    Cache::getLatestOlmSession(const std::string &curve25519)
    {
            using namespace mtx::crypto;
    
            auto txn = lmdb::txn::begin(env_);
            auto db  = getOlmSessionsDb(txn, curve25519);
    
            std::string session_id, pickled_session;
            std::vector<std::string> res;
    
            std::optional<StoredOlmSession> currentNewest;
    
            auto cursor = lmdb::cursor::open(txn, db);
            while (cursor.get(session_id, pickled_session, MDB_NEXT)) {
                    auto data =
                      json::parse(std::string_view(pickled_session.data(), pickled_session.size()))
                        .get<StoredOlmSession>();
                    if (!currentNewest || currentNewest->last_message_ts < data.last_message_ts)
                            currentNewest = data;
            }
            cursor.close();
    
            txn.commit();
    
            return currentNewest
                     ? std::optional(unpickle<SessionObject>(currentNewest->pickled_session, SECRET))
                     : std::nullopt;
    }
    
    
    std::vector<std::string>
    Cache::getOlmSessions(const std::string &curve25519)
    
            using namespace mtx::crypto;
    
            auto txn = lmdb::txn::begin(env_);
            auto db  = getOlmSessionsDb(txn, curve25519);
    
            std::string session_id, unused;
            std::vector<std::string> res;
    
            auto cursor = lmdb::cursor::open(txn, db);
            while (cursor.get(session_id, unused, MDB_NEXT))
                    res.emplace_back(session_id);
            cursor.close();
    
            txn.commit();
    
            return res;
    
    }
    
    void
    Cache::saveOlmAccount(const std::string &data)
    {
            auto txn = lmdb::txn::begin(env_);
            lmdb::dbi_put(txn, syncStateDb_, OLM_ACCOUNT_KEY, lmdb::val(data));
            txn.commit();
    }
    
    std::string
    Cache::restoreOlmAccount()
    {
            auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
            lmdb::val pickled;
            lmdb::dbi_get(txn, syncStateDb_, OLM_ACCOUNT_KEY, pickled);
    
    
            return std::string(pickled.data(), pickled.size());
    
    Cache::saveImage(const std::string &url, const std::string &img_data)
    
            if (url.empty() || img_data.empty())
                    return;
    
    
            try {
                    auto txn = lmdb::txn::begin(env_);
    
                    lmdb::dbi_put(txn,
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
                                  mediaDb_,
    
                                  lmdb::val(url.data(), url.size()),
                                  lmdb::val(img_data.data(), img_data.size()));
    
    
                    txn.commit();
            } catch (const lmdb::error &e) {
    
                    nhlog::db()->critical("saveImage: {}", e.what());
    
    void
    Cache::saveImage(const QString &url, const QByteArray &image)
    {
            saveImage(url.toStdString(), std::string(image.constData(), image.length()));
    }
    
    
    QByteArray
    Cache::image(lmdb::txn &txn, const std::string &url) const
    {
            if (url.empty())
                    return QByteArray();
    
            try {
                    lmdb::val image;
                    bool res = lmdb::dbi_get(txn, mediaDb_, lmdb::val(url), image);
    
                    if (!res)
                            return QByteArray();
    
                    return QByteArray(image.data(), image.size());
            } catch (const lmdb::error &e) {
    
                    nhlog::db()->critical("image: {}, {}", e.what(), url);
    
            }
    
            return QByteArray();
    }
    
    
    QByteArray
    Cache::image(const QString &url) const
    {
    
            if (url.isEmpty())
                    return QByteArray();
    
    
            auto key = url.toUtf8();
    
            try {
                    auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
    
                    lmdb::val image;
    
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
                    bool res = lmdb::dbi_get(txn, mediaDb_, lmdb::val(key.data(), key.size()), image);
    
    
                    txn.commit();
    
                    if (!res)
                            return QByteArray();
    
                    return QByteArray(image.data(), image.size());
            } catch (const lmdb::error &e) {
    
                    nhlog::db()->critical("image: {} {}", e.what(), url.toStdString());
    
    void
    Cache::removeInvite(lmdb::txn &txn, const std::string &room_id)
    {
            lmdb::dbi_del(txn, invitesDb_, lmdb::val(room_id), nullptr);
            lmdb::dbi_drop(txn, getInviteStatesDb(txn, room_id), true);
            lmdb::dbi_drop(txn, getInviteMembersDb(txn, room_id), true);
    }
    
    
    void
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    Cache::removeInvite(const std::string &room_id)
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            auto txn = lmdb::txn::begin(env_);
    
            removeInvite(txn, room_id);
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            txn.commit();
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    Cache::removeRoom(lmdb::txn &txn, const std::string &roomid)
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            lmdb::dbi_del(txn, roomsDb_, lmdb::val(roomid), nullptr);
    
            lmdb::dbi_drop(txn, getStatesDb(txn, roomid), true);
    
            lmdb::dbi_drop(txn, getAccountDataDb(txn, roomid), true);
    
            lmdb::dbi_drop(txn, getMembersDb(txn, roomid), true);
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    Cache::removeRoom(const std::string &roomid)
    
    {
            auto txn = lmdb::txn::begin(env_, nullptr, 0);
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            lmdb::dbi_del(txn, roomsDb_, lmdb::val(roomid), nullptr);
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
    Cache::setNextBatchToken(lmdb::txn &txn, const std::string &token)
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            lmdb::dbi_put(txn, syncStateDb_, NEXT_BATCH_KEY, lmdb::val(token.data(), token.size()));
    
    void
    
    Cache::setNextBatchToken(lmdb::txn &txn, const QString &token)
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            setNextBatchToken(txn, token.toStdString());
    
    bool
    
            auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
            lmdb::val token;
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            bool res = lmdb::dbi_get(txn, syncStateDb_, NEXT_BATCH_KEY, token);
    
            auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
            lmdb::val token;
    
            auto result = lmdb::dbi_get(txn, syncStateDb_, NEXT_BATCH_KEY, token);
    
            if (result)
                    return std::string(token.data(), token.size());
            else
                    return "";
    
    
    void
    Cache::deleteData()
    {
    
            // TODO: We need to remove the env_ while not accepting new requests.
            if (!cacheDirectory_.isEmpty()) {
    
                    QDir(cacheDirectory_).removeRecursively();
    
                    nhlog::db()->info("deleted cache files from disk");
    
    //! migrates db to the current format
    
    Cache::runMigrations()
    {
    
            auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
    
            lmdb::val current_version;
            bool res = lmdb::dbi_get(txn, syncStateDb_, CACHE_FORMAT_VERSION_KEY, current_version);
    
            txn.commit();
    
            if (!res)
                    return false;
    
            std::string stored_version(current_version.data(), current_version.size());
    
            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) {
    
                                       try {
                                               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);
                                                       lmdb::val 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);
                                                       }
                                                       // 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
                                                       saveTimelineMessages(txn, 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());
                                       }
    
                               }
                               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 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, dbName.c_str());
    
                                       std::string 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;
    
                                               nhlog::db()->debug("Not skipped");
    
                                               session.pickled_session = session_value;
                                               sessions.emplace_back(session_id, session);
                                       }
                                       cursor.close();
    
                                       olmDb.drop(txn, true);
    
                                       auto newDbName = dbName;
                                       newDbName.erase(0, sizeof("olm_sessions") - 1);
                                       newDbName = "olm_sessions.v2" + newDbName;
    
                                       auto newDb = lmdb::dbi::open(txn, newDbName.c_str(), MDB_CREATE);
    
                                       for (const auto &[key, value] : sessions) {
                                               nhlog::db()->debug("{}\n{}", key, json(value).dump());
                                               lmdb::dbi_put(txn,
                                                             newDb,
                                                             lmdb::val(key),
                                                             lmdb::val(json(value).dump()));
                                       }
                               }
                               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;
               }},
    
            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.");
    
    
            setCurrentFormat();
    
            return true;
    }
    
    cache::CacheVersion
    Cache::formatVersion()
    
    {
            auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
    
            lmdb::val current_version;
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
            bool res = lmdb::dbi_get(txn, syncStateDb_, CACHE_FORMAT_VERSION_KEY, current_version);
    
                    return cache::CacheVersion::Older;
    
    
            std::string stored_version(current_version.data(), current_version.size());
    
    
            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;
    
    }
    
    void
    Cache::setCurrentFormat()
    {
            auto txn = lmdb::txn::begin(env_);
    
            lmdb::dbi_put(
              txn,
    
    Konstantinos Sideris's avatar
    Konstantinos Sideris committed
              syncStateDb_,
    
              CACHE_FORMAT_VERSION_KEY,
              lmdb::val(CURRENT_CACHE_FORMAT_VERSION.data(), CURRENT_CACHE_FORMAT_VERSION.size()));
    
            txn.commit();
    }
    
    Cache::readReceipts(const QString &event_id, const QString &room_id)
    {
    
    
            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_view(value.data(), value.size()));
                            auto values = json_response.get<std::map<std::string, uint64_t>>();
    
                            for (const auto &v : values)
    
                                    // timestamp, user_id
                                    receipts.emplace(v.second, v.first);
    
                    }
    
            } catch (const lmdb::error &e) {
    
                    nhlog::db()->critical("readReceipts: {}", e.what());
    
    Cache::updateReadReceipt(lmdb::txn &txn, const std::string &room_id, const Receipts &receipts)