2019-12-15 04:56:04 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2019 The nheko authors
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-07-05 06:29:07 +03:00
|
|
|
#include <limits>
|
2019-12-15 04:56:04 +03:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QString>
|
|
|
|
|
2020-01-24 20:39:26 +03:00
|
|
|
#if __has_include(<lmdbxx/lmdb++.h>)
|
|
|
|
#include <lmdbxx/lmdb++.h>
|
|
|
|
#else
|
2019-12-15 04:56:04 +03:00
|
|
|
#include <lmdb++.h>
|
2020-01-24 20:39:26 +03:00
|
|
|
#endif
|
2019-12-15 04:56:04 +03:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <mtx/responses/messages.hpp>
|
|
|
|
#include <mtx/responses/notifications.hpp>
|
|
|
|
#include <mtx/responses/sync.hpp>
|
2019-12-15 04:56:04 +03:00
|
|
|
#include <mtxclient/crypto/client.hpp>
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <mtxclient/http/client.hpp>
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
#include "CacheCryptoStructs.h"
|
|
|
|
#include "CacheStructs.h"
|
|
|
|
|
|
|
|
class Cache : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
Cache(const QString &userId, QObject *parent = nullptr);
|
|
|
|
|
2020-10-22 21:48:28 +03:00
|
|
|
std::string displayName(const std::string &room_id, const std::string &user_id);
|
|
|
|
QString displayName(const QString &room_id, const QString &user_id);
|
|
|
|
QString avatarUrl(const QString &room_id, const QString &user_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
|
2020-06-08 02:45:24 +03:00
|
|
|
// presence
|
|
|
|
mtx::presence::PresenceState presenceState(const std::string &user_id);
|
|
|
|
std::string statusMessage(const std::string &user_id);
|
|
|
|
|
2020-06-28 18:31:34 +03:00
|
|
|
// user cache stores user keys
|
2020-10-02 02:14:42 +03:00
|
|
|
std::optional<UserKeyCache> userKeys(const std::string &user_id);
|
2020-11-30 02:26:27 +03:00
|
|
|
std::map<std::string, std::optional<UserKeyCache>> getMembersWithKeys(
|
|
|
|
const std::string &room_id);
|
2020-10-02 02:14:42 +03:00
|
|
|
void updateUserKeys(const std::string &sync_token,
|
|
|
|
const mtx::responses::QueryKeys &keyQuery);
|
|
|
|
void markUserKeysOutOfDate(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &db,
|
|
|
|
const std::vector<std::string> &user_ids,
|
|
|
|
const std::string &sync_token);
|
|
|
|
void deleteUserKeys(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &db,
|
|
|
|
const std::vector<std::string> &user_ids);
|
2020-10-27 19:45:28 +03:00
|
|
|
void query_keys(const std::string &user_id,
|
|
|
|
std::function<void(const UserKeyCache &, mtx::http::RequestErr)> cb);
|
2020-10-02 02:14:42 +03:00
|
|
|
|
|
|
|
// device & user verification cache
|
2020-10-08 00:03:14 +03:00
|
|
|
VerificationStatus verificationStatus(const std::string &user_id);
|
|
|
|
void markDeviceVerified(const std::string &user_id, const std::string &device);
|
|
|
|
void markDeviceUnverified(const std::string &user_id, const std::string &device);
|
2020-06-28 18:31:34 +03:00
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
std::vector<std::string> joinedRooms();
|
|
|
|
|
|
|
|
QMap<QString, RoomInfo> roomInfo(bool withInvites = true);
|
2021-01-10 20:36:06 +03:00
|
|
|
std::optional<mtx::events::state::CanonicalAlias> getRoomAliases(const std::string &roomid);
|
2019-12-15 04:56:04 +03:00
|
|
|
std::map<QString, bool> invites();
|
|
|
|
|
|
|
|
//! Calculate & return the name of the room.
|
|
|
|
QString getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
|
|
|
//! Get room join rules
|
|
|
|
mtx::events::state::JoinRule getRoomJoinRule(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
bool getRoomGuestAccess(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
//! Retrieve the topic of the room if any.
|
|
|
|
QString getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
//! Retrieve the room avatar's url if any.
|
2020-11-26 00:45:33 +03:00
|
|
|
QString getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
2019-12-15 04:56:04 +03:00
|
|
|
//! Retrieve the version of the room if any.
|
|
|
|
QString getRoomVersion(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
|
|
|
|
//! Retrieve member info from a room.
|
|
|
|
std::vector<RoomMember> getMembers(const std::string &room_id,
|
|
|
|
std::size_t startIndex = 0,
|
|
|
|
std::size_t len = 30);
|
|
|
|
|
|
|
|
void saveState(const mtx::responses::Sync &res);
|
|
|
|
bool isInitialized() const;
|
|
|
|
|
|
|
|
std::string nextBatchToken() const;
|
|
|
|
|
|
|
|
void deleteData();
|
|
|
|
|
|
|
|
void removeInvite(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
void removeInvite(const std::string &room_id);
|
|
|
|
void removeRoom(lmdb::txn &txn, const std::string &roomid);
|
|
|
|
void removeRoom(const std::string &roomid);
|
|
|
|
void setup();
|
|
|
|
|
2020-05-02 17:44:50 +03:00
|
|
|
cache::CacheVersion formatVersion();
|
2019-12-15 04:56:04 +03:00
|
|
|
void setCurrentFormat();
|
2020-05-02 17:44:50 +03:00
|
|
|
bool runMigrations();
|
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
|
|
|
QMap<QString, mtx::responses::Notifications> getTimelineMentions();
|
|
|
|
|
|
|
|
//! Retrieve all the user ids from a room.
|
|
|
|
std::vector<std::string> roomMembers(const std::string &room_id);
|
|
|
|
|
|
|
|
//! Check if the given user has power leve greater than than
|
|
|
|
//! lowest power level of the given events.
|
|
|
|
bool hasEnoughPowerLevel(const std::vector<mtx::events::EventType> &eventTypes,
|
|
|
|
const std::string &room_id,
|
|
|
|
const std::string &user_id);
|
|
|
|
|
|
|
|
//! Retrieves the saved room avatar.
|
|
|
|
QImage getRoomAvatar(const QString &id);
|
|
|
|
QImage getRoomAvatar(const std::string &id);
|
|
|
|
|
|
|
|
//! Adds a user to the read list for the given event.
|
|
|
|
//!
|
|
|
|
//! There should be only one user id present in a receipt list per room.
|
|
|
|
//! The user id should be removed from any other lists.
|
|
|
|
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
|
|
|
|
void updateReadReceipt(lmdb::txn &txn,
|
|
|
|
const std::string &room_id,
|
|
|
|
const Receipts &receipts);
|
|
|
|
|
|
|
|
//! Retrieve all the read receipts for the given event id and room.
|
|
|
|
//!
|
|
|
|
//! Returns a map of user ids and the time of the read receipt in milliseconds.
|
|
|
|
using UserReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
|
|
|
|
UserReceipts readReceipts(const QString &event_id, const QString &room_id);
|
|
|
|
|
|
|
|
QByteArray image(const QString &url) const;
|
|
|
|
QByteArray image(lmdb::txn &txn, const std::string &url) const;
|
|
|
|
void saveImage(const std::string &url, const std::string &data);
|
|
|
|
void saveImage(const QString &url, const QByteArray &data);
|
|
|
|
|
|
|
|
RoomInfo singleRoomInfo(const std::string &room_id);
|
|
|
|
std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res);
|
|
|
|
std::vector<std::string> roomsWithTagUpdates(const mtx::responses::Sync &res);
|
|
|
|
std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms);
|
|
|
|
|
|
|
|
//! 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);
|
|
|
|
void calculateRoomReadStatus();
|
|
|
|
|
|
|
|
std::vector<RoomSearchResult> searchRooms(const std::string &query,
|
|
|
|
std::uint8_t max_items = 5);
|
|
|
|
|
|
|
|
void markSentNotification(const std::string &event_id);
|
|
|
|
//! Removes an event from the sent notifications.
|
|
|
|
void removeReadNotification(const std::string &event_id);
|
|
|
|
//! Check if we have sent a desktop notification for the given event id.
|
|
|
|
bool isNotificationSent(const std::string &event_id);
|
|
|
|
|
|
|
|
//! Add all notifications containing a user mention to the db.
|
|
|
|
void saveTimelineMentions(const mtx::responses::Notifications &res);
|
|
|
|
|
2020-07-10 00:15:22 +03:00
|
|
|
//! retrieve events in timeline and related functions
|
|
|
|
struct Messages
|
|
|
|
{
|
|
|
|
mtx::responses::Timeline timeline;
|
|
|
|
uint64_t next_index;
|
|
|
|
bool end_of_cache = false;
|
|
|
|
};
|
|
|
|
Messages getTimelineMessages(lmdb::txn &txn,
|
|
|
|
const std::string &room_id,
|
2020-07-13 01:08:58 +03:00
|
|
|
uint64_t index = std::numeric_limits<uint64_t>::max(),
|
|
|
|
bool forward = false);
|
2020-07-10 00:15:22 +03:00
|
|
|
|
|
|
|
std::optional<mtx::events::collections::TimelineEvent> getEvent(
|
|
|
|
const std::string &room_id,
|
|
|
|
const std::string &event_id);
|
2020-07-10 02:37:55 +03:00
|
|
|
void storeEvent(const std::string &room_id,
|
|
|
|
const std::string &event_id,
|
|
|
|
const mtx::events::collections::TimelineEvent &event);
|
2020-07-19 13:22:54 +03:00
|
|
|
std::vector<std::string> relatedEvents(const std::string &room_id,
|
|
|
|
const std::string &event_id);
|
|
|
|
|
2020-07-10 00:15:22 +03:00
|
|
|
struct TimelineRange
|
|
|
|
{
|
2020-07-13 01:08:58 +03:00
|
|
|
uint64_t first, last;
|
2020-07-10 00:15:22 +03:00
|
|
|
};
|
|
|
|
std::optional<TimelineRange> getTimelineRange(const std::string &room_id);
|
2020-07-13 01:08:58 +03:00
|
|
|
std::optional<uint64_t> getTimelineIndex(const std::string &room_id,
|
|
|
|
std::string_view event_id);
|
2021-02-10 03:03:20 +03:00
|
|
|
std::optional<uint64_t> getEventIndex(const std::string &room_id,
|
|
|
|
std::string_view event_id);
|
|
|
|
std::optional<std::pair<uint64_t, std::string>> lastInvisibleEventAfter(
|
|
|
|
const std::string &room_id,
|
|
|
|
std::string_view event_id);
|
2020-07-13 01:08:58 +03:00
|
|
|
std::optional<std::string> getTimelineEventId(const std::string &room_id, uint64_t index);
|
2021-01-27 04:45:33 +03:00
|
|
|
std::optional<uint64_t> getArrivalIndex(const std::string &room_id,
|
|
|
|
std::string_view event_id);
|
2020-07-13 01:08:58 +03:00
|
|
|
|
|
|
|
std::string previousBatchToken(const std::string &room_id);
|
|
|
|
uint64_t saveOldMessages(const std::string &room_id, const mtx::responses::Messages &res);
|
2020-07-18 18:43:49 +03:00
|
|
|
void savePendingMessage(const std::string &room_id,
|
|
|
|
const mtx::events::collections::TimelineEvent &message);
|
|
|
|
std::optional<mtx::events::collections::TimelineEvent> firstPendingMessage(
|
|
|
|
const std::string &room_id);
|
|
|
|
void removePendingStatus(const std::string &room_id, const std::string &txn_id);
|
2020-07-10 00:15:22 +03:00
|
|
|
|
2020-08-10 00:36:47 +03:00
|
|
|
//! clear timeline keeping only the latest batch
|
|
|
|
void clearTimeline(const std::string &room_id);
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
//! Remove old unused data.
|
|
|
|
void deleteOldMessages();
|
|
|
|
void deleteOldData() noexcept;
|
|
|
|
//! Retrieve all saved room ids.
|
|
|
|
std::vector<std::string> getRoomIds(lmdb::txn &txn);
|
|
|
|
|
|
|
|
//! Mark a room that uses e2e encryption.
|
|
|
|
void setEncryptedRoom(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
bool isRoomEncrypted(const std::string &room_id);
|
|
|
|
|
|
|
|
//! Check if a user is a member of the room.
|
|
|
|
bool isRoomMember(const std::string &user_id, const std::string &room_id);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Outbound Megolm Sessions
|
|
|
|
//
|
|
|
|
void saveOutboundMegolmSession(const std::string &room_id,
|
|
|
|
const OutboundGroupSessionData &data,
|
2020-11-30 02:26:27 +03:00
|
|
|
mtx::crypto::OutboundGroupSessionPtr &session);
|
2019-12-15 04:56:04 +03:00
|
|
|
OutboundGroupSessionDataRef getOutboundMegolmSession(const std::string &room_id);
|
|
|
|
bool outboundMegolmSessionExists(const std::string &room_id) noexcept;
|
2020-11-27 06:56:44 +03:00
|
|
|
void updateOutboundMegolmSession(const std::string &room_id,
|
2020-11-30 02:26:27 +03:00
|
|
|
const OutboundGroupSessionData &data,
|
2020-11-27 06:56:44 +03:00
|
|
|
mtx::crypto::OutboundGroupSessionPtr &session);
|
2020-10-03 19:38:28 +03:00
|
|
|
void dropOutboundMegolmSession(const std::string &room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
void importSessionKeys(const mtx::crypto::ExportedSessionKeys &keys);
|
|
|
|
mtx::crypto::ExportedSessionKeys exportSessionKeys();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Inbound Megolm Sessions
|
|
|
|
//
|
|
|
|
void saveInboundMegolmSession(const MegolmSessionIndex &index,
|
|
|
|
mtx::crypto::InboundGroupSessionPtr session);
|
2020-11-27 06:19:03 +03:00
|
|
|
mtx::crypto::InboundGroupSessionPtr getInboundMegolmSession(
|
|
|
|
const MegolmSessionIndex &index);
|
2019-12-15 04:56:04 +03:00
|
|
|
bool inboundMegolmSessionExists(const MegolmSessionIndex &index);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Olm Sessions
|
|
|
|
//
|
2020-10-20 14:46:05 +03:00
|
|
|
void saveOlmSession(const std::string &curve25519,
|
|
|
|
mtx::crypto::OlmSessionPtr session,
|
|
|
|
uint64_t timestamp);
|
2019-12-15 04:56:04 +03:00
|
|
|
std::vector<std::string> getOlmSessions(const std::string &curve25519);
|
|
|
|
std::optional<mtx::crypto::OlmSessionPtr> getOlmSession(const std::string &curve25519,
|
|
|
|
const std::string &session_id);
|
2020-10-20 14:46:05 +03:00
|
|
|
std::optional<mtx::crypto::OlmSessionPtr> getLatestOlmSession(
|
|
|
|
const std::string &curve25519);
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
void saveOlmAccount(const std::string &pickled);
|
|
|
|
std::string restoreOlmAccount();
|
|
|
|
|
2020-12-17 00:10:09 +03:00
|
|
|
void storeSecret(const std::string &name, const std::string &secret);
|
|
|
|
void deleteSecret(const std::string &name);
|
|
|
|
std::optional<std::string> secret(const std::string &name);
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
signals:
|
|
|
|
void newReadReceipts(const QString &room_id, const std::vector<QString> &event_ids);
|
|
|
|
void roomReadStatus(const std::map<QString, bool> &status);
|
2020-04-09 21:52:50 +03:00
|
|
|
void removeNotification(const QString &room_id, const QString &event_id);
|
2020-10-02 02:14:42 +03:00
|
|
|
void userKeysUpdate(const std::string &sync_token,
|
|
|
|
const mtx::responses::QueryKeys &keyQuery);
|
2020-10-08 00:03:14 +03:00
|
|
|
void verificationStatusChanged(const std::string &userid);
|
2020-12-17 00:10:09 +03:00
|
|
|
void secretChanged(const std::string name);
|
2019-12-15 04:56:04 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
//! Save an invited room.
|
|
|
|
void saveInvite(lmdb::txn &txn,
|
|
|
|
lmdb::dbi &statesdb,
|
|
|
|
lmdb::dbi &membersdb,
|
|
|
|
const mtx::responses::InvitedRoom &room);
|
|
|
|
|
|
|
|
//! Add a notification containing a user mention to the db.
|
|
|
|
void saveTimelineMentions(lmdb::txn &txn,
|
|
|
|
const std::string &room_id,
|
|
|
|
const QList<mtx::responses::Notification> &res);
|
|
|
|
|
|
|
|
//! Get timeline items that a user was mentions in for a given room
|
|
|
|
mtx::responses::Notifications getTimelineMentionsForRoom(lmdb::txn &txn,
|
|
|
|
const std::string &room_id);
|
|
|
|
|
|
|
|
QString getInviteRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
|
|
|
QString getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb);
|
|
|
|
QString getInviteRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
|
|
|
|
|
2020-10-22 21:48:28 +03:00
|
|
|
std::optional<MemberInfo> getMember(const std::string &room_id, const std::string &user_id);
|
|
|
|
|
2020-02-24 03:07:25 +03:00
|
|
|
std::string getLastEventId(lmdb::txn &txn, const std::string &room_id);
|
2019-12-15 04:56:04 +03:00
|
|
|
DescInfo getLastMessageInfo(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
void saveTimelineMessages(lmdb::txn &txn,
|
|
|
|
const std::string &room_id,
|
|
|
|
const mtx::responses::Timeline &res);
|
|
|
|
|
2020-08-27 22:49:05 +03:00
|
|
|
//! retrieve a specific event from account data
|
|
|
|
//! pass empty room_id for global account data
|
|
|
|
std::optional<mtx::events::collections::RoomAccountDataEvents>
|
|
|
|
getAccountData(lmdb::txn &txn, mtx::events::EventType type, const std::string &room_id);
|
|
|
|
bool isHiddenEvent(lmdb::txn &txn,
|
|
|
|
mtx::events::collections::TimelineEvents e,
|
|
|
|
const std::string &room_id);
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
//! Remove a room from the cache.
|
|
|
|
// void removeLeftRoom(lmdb::txn &txn, const std::string &room_id);
|
|
|
|
template<class T>
|
|
|
|
void saveStateEvents(lmdb::txn &txn,
|
|
|
|
const lmdb::dbi &statesdb,
|
|
|
|
const lmdb::dbi &membersdb,
|
|
|
|
const std::string &room_id,
|
|
|
|
const std::vector<T> &events)
|
|
|
|
{
|
|
|
|
for (const auto &e : events)
|
|
|
|
saveStateEvent(txn, statesdb, membersdb, room_id, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void saveStateEvent(lmdb::txn &txn,
|
|
|
|
const lmdb::dbi &statesdb,
|
|
|
|
const lmdb::dbi &membersdb,
|
|
|
|
const std::string &room_id,
|
|
|
|
const T &event)
|
|
|
|
{
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
|
|
|
|
|
|
|
if (auto e = std::get_if<StateEvent<Member>>(&event); e != nullptr) {
|
|
|
|
switch (e->content.membership) {
|
|
|
|
//
|
|
|
|
// We only keep users with invite or join membership.
|
|
|
|
//
|
|
|
|
case Membership::Invite:
|
|
|
|
case Membership::Join: {
|
|
|
|
auto display_name = e->content.display_name.empty()
|
|
|
|
? e->state_key
|
|
|
|
: e->content.display_name;
|
|
|
|
|
|
|
|
// Lightweight representation of a member.
|
|
|
|
MemberInfo tmp{display_name, e->content.avatar_url};
|
|
|
|
|
|
|
|
lmdb::dbi_put(txn,
|
|
|
|
membersdb,
|
|
|
|
lmdb::val(e->state_key),
|
|
|
|
lmdb::val(json(tmp).dump()));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
lmdb::dbi_del(
|
|
|
|
txn, membersdb, lmdb::val(e->state_key), lmdb::val(""));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (std::holds_alternative<StateEvent<Encryption>>(event)) {
|
|
|
|
setEncryptedRoom(txn, room_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isStateEvent(event))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::visit(
|
|
|
|
[&txn, &statesdb](auto e) {
|
|
|
|
lmdb::dbi_put(
|
|
|
|
txn, statesdb, lmdb::val(to_string(e.type)), lmdb::val(json(e).dump()));
|
|
|
|
},
|
|
|
|
event);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
bool isStateEvent(const T &e)
|
|
|
|
{
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
|
|
|
|
|
|
|
return std::holds_alternative<StateEvent<Aliases>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<state::Avatar>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<CanonicalAlias>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<Create>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<GuestAccess>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<HistoryVisibility>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<JoinRules>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<Name>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<Member>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<PowerLevels>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<Topic>>(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
bool containsStateUpdates(const T &e)
|
|
|
|
{
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
|
|
|
|
|
|
|
return std::holds_alternative<StateEvent<state::Avatar>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<CanonicalAlias>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<Name>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<Member>>(e) ||
|
|
|
|
std::holds_alternative<StateEvent<Topic>>(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool containsStateUpdates(const mtx::events::collections::StrippedEvents &e)
|
|
|
|
{
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::state;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void saveInvites(lmdb::txn &txn,
|
|
|
|
const std::map<std::string, mtx::responses::InvitedRoom> &rooms);
|
|
|
|
|
2020-06-08 02:45:24 +03:00
|
|
|
void savePresence(
|
|
|
|
lmdb::txn &txn,
|
|
|
|
const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presenceUpdates);
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
//! Sends signals for the rooms that are removed.
|
|
|
|
void removeLeftRooms(lmdb::txn &txn,
|
|
|
|
const std::map<std::string, mtx::responses::LeftRoom> &rooms)
|
|
|
|
{
|
|
|
|
for (const auto &room : rooms) {
|
|
|
|
removeRoom(txn, room.first);
|
|
|
|
|
|
|
|
// Clean up leftover invites.
|
|
|
|
removeInvite(txn, room.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getPendingReceiptsDb(lmdb::txn &txn)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, "pending_receipts", MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:15:39 +03:00
|
|
|
lmdb::dbi getEventsDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/events").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
2020-07-04 03:09:12 +03:00
|
|
|
lmdb::dbi getEventOrderDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/event_order").c_str(), MDB_CREATE | MDB_INTEGERKEY);
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:43:49 +03:00
|
|
|
// inverse of EventOrderDb
|
|
|
|
lmdb::dbi getEventToOrderDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/event2order").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
2020-07-08 03:02:14 +03:00
|
|
|
lmdb::dbi getMessageToOrderDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/msg2order").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getOrderToMessageDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/order2msg").c_str(), MDB_CREATE | MDB_INTEGERKEY);
|
2020-07-18 18:43:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getPendingMessagesDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/pending").c_str(), MDB_CREATE | MDB_INTEGERKEY);
|
2020-07-08 03:02:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getRelationsDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/related").c_str(), MDB_CREATE | MDB_DUPSORT);
|
|
|
|
}
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
lmdb::dbi getInviteStatesDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/invite_state").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getInviteMembersDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/invite_members").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getStatesDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/state").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:49:05 +03:00
|
|
|
lmdb::dbi getAccountDataDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
|
|
|
txn, std::string(room_id + "/account_data").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
lmdb::dbi getMembersDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/members").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lmdb::dbi getMentionsDb(lmdb::txn &txn, const std::string &room_id)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, std::string(room_id + "/mentions").c_str(), MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:45:24 +03:00
|
|
|
lmdb::dbi getPresenceDb(lmdb::txn &txn)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(txn, "presence", MDB_CREATE);
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
lmdb::dbi getUserKeysDb(lmdb::txn &txn)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2020-10-02 02:14:42 +03:00
|
|
|
return lmdb::dbi::open(txn, "user_key", MDB_CREATE);
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
lmdb::dbi getVerificationDb(lmdb::txn &txn)
|
2020-06-28 18:31:34 +03:00
|
|
|
{
|
2020-07-06 19:02:21 +03:00
|
|
|
return lmdb::dbi::open(txn, "verified", MDB_CREATE);
|
2020-06-28 18:31:34 +03:00
|
|
|
}
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
//! Retrieves or creates the database that stores the open OLM sessions between our device
|
|
|
|
//! and the given curve25519 key which represents another device.
|
|
|
|
//!
|
|
|
|
//! Each entry is a map from the session_id to the pickled representation of the session.
|
|
|
|
lmdb::dbi getOlmSessionsDb(lmdb::txn &txn, const std::string &curve25519_key)
|
|
|
|
{
|
|
|
|
return lmdb::dbi::open(
|
2020-10-20 14:46:05 +03:00
|
|
|
txn, std::string("olm_sessions.v2/" + curve25519_key).c_str(), MDB_CREATE);
|
2019-12-15 04:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getDisplayName(const mtx::events::StateEvent<mtx::events::state::Member> &event)
|
|
|
|
{
|
|
|
|
if (!event.content.display_name.empty())
|
|
|
|
return QString::fromStdString(event.content.display_name);
|
|
|
|
|
|
|
|
return QString::fromStdString(event.state_key);
|
|
|
|
}
|
|
|
|
|
2020-10-08 00:03:14 +03:00
|
|
|
std::optional<VerificationCache> verificationCache(const std::string &user_id);
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
void setNextBatchToken(lmdb::txn &txn, const std::string &token);
|
|
|
|
void setNextBatchToken(lmdb::txn &txn, const QString &token);
|
|
|
|
|
|
|
|
lmdb::env env_;
|
|
|
|
lmdb::dbi syncStateDb_;
|
|
|
|
lmdb::dbi roomsDb_;
|
|
|
|
lmdb::dbi invitesDb_;
|
|
|
|
lmdb::dbi mediaDb_;
|
|
|
|
lmdb::dbi readReceiptsDb_;
|
|
|
|
lmdb::dbi notificationsDb_;
|
|
|
|
|
|
|
|
lmdb::dbi devicesDb_;
|
|
|
|
lmdb::dbi deviceKeysDb_;
|
|
|
|
|
|
|
|
lmdb::dbi inboundMegolmSessionDb_;
|
|
|
|
lmdb::dbi outboundMegolmSessionDb_;
|
|
|
|
|
|
|
|
QString localUserId_;
|
|
|
|
QString cacheDirectory_;
|
|
|
|
|
2020-10-08 00:03:14 +03:00
|
|
|
VerificationStorage verification_storage;
|
2019-12-15 04:56:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace cache {
|
|
|
|
Cache *
|
|
|
|
client();
|
|
|
|
}
|