Cache decrypted events

This commit is contained in:
Nicolas Werner 2020-04-14 20:15:11 +02:00
parent c739a0422d
commit a70044e67b

View file

@ -4,6 +4,7 @@
#include <thread> #include <thread>
#include <type_traits> #include <type_traits>
#include <QCache>
#include <QFileDialog> #include <QFileDialog>
#include <QMimeDatabase> #include <QMimeDatabase>
#include <QRegularExpression> #include <QRegularExpression>
@ -22,6 +23,14 @@
Q_DECLARE_METATYPE(QModelIndex) Q_DECLARE_METATYPE(QModelIndex)
namespace std {
inline uint
qHash(const std::string &key, uint seed = 0)
{
return qHash(QByteArray::fromRawData(key.data(), key.length()), seed);
}
}
namespace { namespace {
struct RoomEventType struct RoomEventType
{ {
@ -705,6 +714,11 @@ TimelineModel::openUserProfile(QString userid) const
DecryptionResult DecryptionResult
TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &e) const TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &e) const
{ {
static QCache<std::string, DecryptionResult> decryptedEvents{300};
if (auto cachedEvent = decryptedEvents.object(e.event_id))
return *cachedEvent;
MegolmSessionIndex index; MegolmSessionIndex index;
index.room_id = room_id_.toStdString(); index.room_id = room_id_.toStdString();
index.session_id = e.content.session_id; index.session_id = e.content.session_id;
@ -726,6 +740,8 @@ TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::
index.session_id, index.session_id,
e.sender); e.sender);
// TODO: request megolm session_id & session_key from the sender. // TODO: request megolm session_id & session_key from the sender.
decryptedEvents.insert(
dummy.event_id, new DecryptionResult{dummy, false}, 1);
return {dummy, false}; return {dummy, false};
} }
} catch (const lmdb::error &e) { } catch (const lmdb::error &e) {
@ -734,6 +750,7 @@ TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::
"Placeholder, when the message can't be decrypted, because " "Placeholder, when the message can't be decrypted, because "
"the DB access failed when trying to lookup the session.") "the DB access failed when trying to lookup the session.")
.toStdString(); .toStdString();
decryptedEvents.insert(dummy.event_id, new DecryptionResult{dummy, false}, 1);
return {dummy, false}; return {dummy, false};
} }
@ -753,6 +770,7 @@ TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::
"Placeholder, when the message can't be decrypted, because the DB access " "Placeholder, when the message can't be decrypted, because the DB access "
"failed.") "failed.")
.toStdString(); .toStdString();
decryptedEvents.insert(dummy.event_id, new DecryptionResult{dummy, false}, 1);
return {dummy, false}; return {dummy, false};
} catch (const mtx::crypto::olm_exception &e) { } catch (const mtx::crypto::olm_exception &e) {
nhlog::crypto()->critical("failed to decrypt message with index ({}, {}, {}): {}", nhlog::crypto()->critical("failed to decrypt message with index ({}, {}, {}): {}",
@ -766,6 +784,7 @@ TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::
"decrytion returned an error, which is passed ad %1.") "decrytion returned an error, which is passed ad %1.")
.arg(e.what()) .arg(e.what())
.toStdString(); .toStdString();
decryptedEvents.insert(dummy.event_id, new DecryptionResult{dummy, false}, 1);
return {dummy, false}; return {dummy, false};
} }
@ -786,14 +805,17 @@ TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::
std::vector<mtx::events::collections::TimelineEvents> temp_events; std::vector<mtx::events::collections::TimelineEvents> temp_events;
mtx::responses::utils::parse_timeline_events(event_array, temp_events); mtx::responses::utils::parse_timeline_events(event_array, temp_events);
if (temp_events.size() == 1) if (temp_events.size() == 1) {
return {temp_events.at(0), true}; decryptedEvents.insert(e.event_id, new DecryptionResult{temp_events[0], true}, 1);
return {temp_events[0], true};
}
dummy.content.body = dummy.content.body =
tr("-- Encrypted Event (Unknown event type) --", tr("-- Encrypted Event (Unknown event type) --",
"Placeholder, when the message was decrypted, but we couldn't parse it, because " "Placeholder, when the message was decrypted, but we couldn't parse it, because "
"Nheko/mtxclient don't support that event type yet.") "Nheko/mtxclient don't support that event type yet.")
.toStdString(); .toStdString();
decryptedEvents.insert(dummy.event_id, new DecryptionResult{dummy, false}, 1);
return {dummy, false}; return {dummy, false};
} }