| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <limits> |
| 8 |
#include <string> |
| 9 |
|
| 10 |
#include <QCache> |
| 11 |
#include <QObject> |
| 12 |
#include <QVariant> |
| 13 |
|
| 14 |
#include <mtx/common.hpp> |
| 15 |
#include <mtx/events/collections.hpp> |
| 16 |
#include <mtx/responses/messages.hpp> |
| 17 |
#include <mtx/responses/sync.hpp> |
| 18 |
|
| 19 |
#include "encryption/Olm.h" |
| 20 |
|
| 21 |
class EventStore final : public QObject |
| 22 |
{ |
| 23 |
Q_OBJECT |
| 24 |
|
| 25 |
public: |
| 26 |
EventStore(std::string room_id, QObject *parent); |
| 27 |
|
| 28 |
void refetchOnlineKeyBackupKeys(); |
| 29 |
|
| 30 |
|
| 31 |
static size_t hashCombine(uint hash, uint seed) |
| 32 |
{ |
| 33 |
return seed ^ (hash + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 34 |
}; |
| 35 |
struct Index |
| 36 |
{ |
| 37 |
std::string room; |
| 38 |
uint64_t idx; |
| 39 |
|
| 40 |
friend size_t qHash(const Index &i, size_t seed = 0) noexcept |
| 41 |
{ |
| 42 |
seed = hashCombine(qHashBits(i.room.data(), i.room.size(), seed), seed); |
| 43 |
seed = hashCombine(qHash(i.idx, seed), seed); |
| 44 |
return seed; |
| 45 |
} |
| 46 |
|
| 47 |
friend bool operator==(const Index &a, const Index &b) noexcept |
| 48 |
{ |
| 49 |
return a.idx == b.idx && a.room == b.room; |
| 50 |
} |
| 51 |
}; |
| 52 |
struct IdIndex |
| 53 |
{ |
| 54 |
std::string room, id; |
| 55 |
|
| 56 |
friend size_t qHash(const IdIndex &i, size_t seed = 0) noexcept |
| 57 |
{ |
| 58 |
seed = hashCombine(qHashBits(i.room.data(), i.room.size(), seed), seed); |
| 59 |
seed = hashCombine(qHashBits(i.id.data(), i.id.size(), seed), seed); |
| 60 |
return seed; |
| 61 |
} |
| 62 |
|
| 63 |
friend bool operator==(const IdIndex &a, const IdIndex &b) noexcept |
| 64 |
{ |
| 65 |
return a.id == b.id && a.room == b.room; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
void fetchMore(); |
| 70 |
void handleSync(const mtx::responses::Timeline &events); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
mtx::events::collections::TimelineEvents const *get(const std::string &id, |
| 75 |
std::string_view related_to, |
| 76 |
bool decrypt = true, |
| 77 |
bool resolve_edits = true); |
| 78 |
|
| 79 |
mtx::events::collections::TimelineEvents const *get(int idx, bool decrypt = true); |
| 80 |
|
| 81 |
QVariantList reactions(const std::string &event_id); |
| 82 |
std::vector<mtx::events::collections::TimelineEvents> edits(const std::string &event_id); |
| 83 |
olm::DecryptionErrorCode decryptionError(std::string id); |
| 84 |
void |
| 85 |
requestSession(const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &ev, bool manual); |
| 86 |
|
| 87 |
int size() const |
| 88 |
{ |
| 89 |
return (last != std::numeric_limits<uint64_t>::max() && last >= first) |
| 90 |
? static_cast<int>(last - first) + 1 |
| 91 |
: 0; |
| 92 |
} |
| 93 |
int toExternalIdx(uint64_t idx) const { return static_cast<int>(idx - first); } |
| 94 |
uint64_t toInternalIdx(int idx) const { return first + idx; } |
| 95 |
|
| 96 |
std::optional<int> idToIndex(std::string_view id) const; |
| 97 |
std::optional<std::string> indexToId(int idx) const; |
| 98 |
|
| 99 |
signals: |
| 100 |
void beginInsertRows(int from, int to); |
| 101 |
void endInsertRows(); |
| 102 |
void beginResetModel(); |
| 103 |
void endResetModel(); |
| 104 |
void dataChanged(int from, int to); |
| 105 |
void pinsChanged(); |
| 106 |
void newEncryptedImage(mtx::crypto::EncryptedFile encryptionInfo); |
| 107 |
void eventFetched(std::string id, |
| 108 |
std::string relatedTo, |
| 109 |
const mtx::events::collections::TimelineEvents &timeline); |
| 110 |
void oldMessagesRetrieved(const mtx::responses::Messages &); |
| 111 |
void fetchedMore(); |
| 112 |
|
| 113 |
void processPending(); |
| 114 |
void messageSent(std::string txn_id, std::string event_id); |
| 115 |
void messageFailed(std::string txn_id); |
| 116 |
void startDMVerification( |
| 117 |
const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &msg); |
| 118 |
void updateFlowEventId(std::string event_id); |
| 119 |
|
| 120 |
public slots: |
| 121 |
void addPending(const mtx::events::collections::TimelineEvents &event); |
| 122 |
void receivedSessionKey(const std::string &session_id); |
| 123 |
void clearTimeline(); |
| 124 |
void enableKeyRequests(bool suppressKeyRequests_); |
| 125 |
|
| 126 |
private: |
| 127 |
olm::DecryptionResult const * |
| 128 |
decryptEvent(const IdIndex &idx, |
| 129 |
const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &e); |
| 130 |
|
| 131 |
std::string room_id_; |
| 132 |
|
| 133 |
uint64_t first = std::numeric_limits<uint64_t>::max(), |
| 134 |
last = std::numeric_limits<uint64_t>::max(); |
| 135 |
|
| 136 |
static QCache<IdIndex, olm::DecryptionResult> decryptedEvents_; |
| 137 |
static QCache<Index, mtx::events::collections::TimelineEvents> events_; |
| 138 |
static QCache<IdIndex, mtx::events::collections::TimelineEvents> events_by_id_; |
| 139 |
|
| 140 |
struct PendingKeyRequests |
| 141 |
{ |
| 142 |
std::string request_id; |
| 143 |
std::vector<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>> events; |
| 144 |
qint64 requested_at; |
| 145 |
}; |
| 146 |
std::map<std::string, PendingKeyRequests> pending_key_requests; |
| 147 |
|
| 148 |
std::string current_txn; |
| 149 |
int current_txn_error_count = 0; |
| 150 |
bool noMoreMessages = false; |
| 151 |
bool suppressKeyRequests = true; |
| 152 |
}; |