From ac4c9e024ec84bc49f349426dbc382e138197435 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 20 Feb 2023 01:04:49 +0100 Subject: [PATCH] More template bloat reduction --- src/Cache.cpp | 11 +-- src/timeline/EventStore.cpp | 131 ++++++++++++++++----------------- src/timeline/EventStore.h | 1 - src/timeline/TimelineModel.cpp | 79 +++++++++++--------- 4 files changed, 115 insertions(+), 107 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index 1371619a..c575ebf7 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -212,13 +212,10 @@ Cache::isHiddenEvent(lmdb::txn &txn, hiddenEvents = std::move(h.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); + return std::find(hiddenEvents.hidden_event_types->begin(), + hiddenEvents.hidden_event_types->end(), + std::visit([](const auto &ev) { return ev.type; }, e)) != + hiddenEvents.hidden_event_types->end(); } Cache::Cache(const QString &userId, QObject *parent) diff --git a/src/timeline/EventStore.cpp b/src/timeline/EventStore.cpp index 5f118895..d035d840 100644 --- a/src/timeline/EventStore.cpp +++ b/src/timeline/EventStore.cpp @@ -242,24 +242,23 @@ EventStore::EventStore(std::string room_id, QObject *) if (!session.session) continue; - std::visit( - [&pending_event, &original_encrypted, &session, this](auto &msg) { - nlohmann::json doc = {{"type", mtx::events::to_string(msg.type)}, + auto doc = std::visit( + [this](auto &msg) { + return nlohmann::json{{"type", mtx::events::to_string(msg.type)}, {"content", nlohmann::json(msg.content)}, {"room_id", room_id_}}; + }, + pending_event->data); - auto data = olm::encrypt_group_message_with_session( - session.session, http::client()->device_id(), doc); + auto data = olm::encrypt_group_message_with_session( + session.session, http::client()->device_id(), std::move(doc)); - session.data.message_index = - olm_outbound_group_session_message_index(session.session.get()); - cache::updateOutboundMegolmSession( - room_id_, session.data, session.session); + session.data.message_index = + olm_outbound_group_session_message_index(session.session.get()); + cache::updateOutboundMegolmSession(room_id_, session.data, session.session); - original_encrypted.content = data; - pending_event->data = original_encrypted; - }, - pending_event->data); + original_encrypted.content = data; + pending_event->data = original_encrypted; } cache::client()->replaceEvent(room_id_, pending_event_id, *pending_event); @@ -367,6 +366,58 @@ EventStore::receivedSessionKey(const std::string &session_id) } } +namespace { +template +struct overloaded : Ts... +{ + using Ts::operator()...; +}; +template +overloaded(Ts...) -> overloaded; +} + +static void +handle_room_verification(EventStore *self, const mtx::events::collections::TimelineEvents &event) +{ + std::visit( + overloaded{ + [self](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Request"); + emit self->startDMVerification(msg); + }, + [](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Cancel"); + ChatPage::instance()->receivedDeviceVerificationCancel(msg.content); + }, + [](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Accept"); + ChatPage::instance()->receivedDeviceVerificationAccept(msg.content); + }, + [](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Key"); + ChatPage::instance()->receivedDeviceVerificationKey(msg.content); + }, + [](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Mac"); + ChatPage::instance()->receivedDeviceVerificationMac(msg.content); + }, + [](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Ready"); + ChatPage::instance()->receivedDeviceVerificationReady(msg.content); + }, + [](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Done"); + ChatPage::instance()->receivedDeviceVerificationDone(msg.content); + }, + [](const mtx::events::RoomEvent &msg) { + nhlog::db()->debug("handle_room_verification: Start"); + ChatPage::instance()->receivedDeviceVerificationStart(msg.content, msg.sender); + }, + [](const auto &) {}, + }, + event); +} + void EventStore::handleSync(const mtx::responses::Timeline &events) { @@ -464,64 +515,12 @@ EventStore::handleSync(const mtx::responses::Timeline &events) if (d_event->event && std::visit([](auto e) { return (e.sender != utils::localUser().toStdString()); }, *d_event->event)) { - handle_room_verification(*d_event->event); + handle_room_verification(this, *d_event->event); } } } } -namespace { -template -struct overloaded : Ts... -{ - using Ts::operator()...; -}; -template -overloaded(Ts...) -> overloaded; -} - -void -EventStore::handle_room_verification(mtx::events::collections::TimelineEvents event) -{ - std::visit( - overloaded{ - [this](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Request"); - emit startDMVerification(msg); - }, - [](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Cancel"); - ChatPage::instance()->receivedDeviceVerificationCancel(msg.content); - }, - [](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Accept"); - ChatPage::instance()->receivedDeviceVerificationAccept(msg.content); - }, - [](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Key"); - ChatPage::instance()->receivedDeviceVerificationKey(msg.content); - }, - [](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Mac"); - ChatPage::instance()->receivedDeviceVerificationMac(msg.content); - }, - [](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Ready"); - ChatPage::instance()->receivedDeviceVerificationReady(msg.content); - }, - [](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Done"); - ChatPage::instance()->receivedDeviceVerificationDone(msg.content); - }, - [](const mtx::events::RoomEvent &msg) { - nhlog::db()->debug("handle_room_verification: Start"); - ChatPage::instance()->receivedDeviceVerificationStart(msg.content, msg.sender); - }, - [](const auto &) {}, - }, - event); -} - std::vector EventStore::edits(const std::string &event_id) { diff --git a/src/timeline/EventStore.h b/src/timeline/EventStore.h index 45dc0169..54d94c23 100644 --- a/src/timeline/EventStore.h +++ b/src/timeline/EventStore.h @@ -131,7 +131,6 @@ private: olm::DecryptionResult * decryptEvent(const IdIndex &idx, const mtx::events::EncryptedEvent &e); - void handle_room_verification(mtx::events::collections::TimelineEvents event); std::string room_id_; diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index f9df1cf9..ed83a98d 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -46,113 +46,126 @@ namespace { struct RoomEventType { template - qml_mtx_events::EventType operator()(const mtx::events::Event &e) + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &e) { return qml_mtx_events::toRoomEventType(e.type); } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::AudioMessage; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::ConfettiMessage; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::EmoteMessage; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::FileMessage; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::ImageMessage; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::NoticeMessage; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::TextMessage; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::VideoMessage; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationRequest; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationStart; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationMac; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationAccept; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationReady; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationCancel; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationKey; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::KeyVerificationDone; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::Redacted; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::CallInvite; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::CallAnswer; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::CallHangUp; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::CallCandidates; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::CallSelectAnswer; } - qml_mtx_events::EventType operator()(const mtx::events::Event &) + constexpr qml_mtx_events::EventType + operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::CallReject; } - qml_mtx_events::EventType + constexpr qml_mtx_events::EventType operator()(const mtx::events::Event &) { return qml_mtx_events::EventType::CallNegotiate; @@ -1060,7 +1073,7 @@ TimelineModel::addEvents(const mtx::responses::Timeline &timeline) } template -auto +static constexpr auto isMessage(const mtx::events::RoomEvent &e) -> std::enable_if_t::value, bool> { @@ -1068,42 +1081,42 @@ isMessage(const mtx::events::RoomEvent &e) } template -auto +static constexpr auto isMessage(const mtx::events::Event &) { return false; } template -auto +static constexpr auto isMessage(const mtx::events::EncryptedEvent &) { return true; } -auto +static constexpr auto isMessage(const mtx::events::RoomEvent &) { return true; } -auto +static constexpr auto isMessage(const mtx::events::RoomEvent &) { return true; } -auto +static constexpr auto isMessage(const mtx::events::RoomEvent &) { return true; } -auto +static constexpr auto isMessage(const mtx::events::RoomEvent &) { return true; } -auto +static constexpr auto isMessage(const mtx::events::RoomEvent &) { return true;