More template bloat reduction

v0.11.2-next
Nicolas Werner 2 years ago
parent a1c4889339
commit ac4c9e024e
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
  1. 11
      src/Cache.cpp
  2. 131
      src/timeline/EventStore.cpp
  3. 1
      src/timeline/EventStore.h
  4. 79
      src/timeline/TimelineModel.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)

@ -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<class... Ts>
struct overloaded : Ts...
{
using Ts::operator()...;
};
template<class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
}
static void
handle_room_verification(EventStore *self, const mtx::events::collections::TimelineEvents &event)
{
std::visit(
overloaded{
[self](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &msg) {
nhlog::db()->debug("handle_room_verification: Request");
emit self->startDMVerification(msg);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationCancel> &msg) {
nhlog::db()->debug("handle_room_verification: Cancel");
ChatPage::instance()->receivedDeviceVerificationCancel(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationAccept> &msg) {
nhlog::db()->debug("handle_room_verification: Accept");
ChatPage::instance()->receivedDeviceVerificationAccept(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationKey> &msg) {
nhlog::db()->debug("handle_room_verification: Key");
ChatPage::instance()->receivedDeviceVerificationKey(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationMac> &msg) {
nhlog::db()->debug("handle_room_verification: Mac");
ChatPage::instance()->receivedDeviceVerificationMac(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationReady> &msg) {
nhlog::db()->debug("handle_room_verification: Ready");
ChatPage::instance()->receivedDeviceVerificationReady(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationDone> &msg) {
nhlog::db()->debug("handle_room_verification: Done");
ChatPage::instance()->receivedDeviceVerificationDone(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationStart> &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<class... Ts>
struct overloaded : Ts...
{
using Ts::operator()...;
};
template<class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
}
void
EventStore::handle_room_verification(mtx::events::collections::TimelineEvents event)
{
std::visit(
overloaded{
[this](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &msg) {
nhlog::db()->debug("handle_room_verification: Request");
emit startDMVerification(msg);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationCancel> &msg) {
nhlog::db()->debug("handle_room_verification: Cancel");
ChatPage::instance()->receivedDeviceVerificationCancel(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationAccept> &msg) {
nhlog::db()->debug("handle_room_verification: Accept");
ChatPage::instance()->receivedDeviceVerificationAccept(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationKey> &msg) {
nhlog::db()->debug("handle_room_verification: Key");
ChatPage::instance()->receivedDeviceVerificationKey(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationMac> &msg) {
nhlog::db()->debug("handle_room_verification: Mac");
ChatPage::instance()->receivedDeviceVerificationMac(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationReady> &msg) {
nhlog::db()->debug("handle_room_verification: Ready");
ChatPage::instance()->receivedDeviceVerificationReady(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationDone> &msg) {
nhlog::db()->debug("handle_room_verification: Done");
ChatPage::instance()->receivedDeviceVerificationDone(msg.content);
},
[](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationStart> &msg) {
nhlog::db()->debug("handle_room_verification: Start");
ChatPage::instance()->receivedDeviceVerificationStart(msg.content, msg.sender);
},
[](const auto &) {},
},
event);
}
std::vector<mtx::events::collections::TimelineEvents>
EventStore::edits(const std::string &event_id)
{

@ -131,7 +131,6 @@ private:
olm::DecryptionResult *
decryptEvent(const IdIndex &idx,
const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &e);
void handle_room_verification(mtx::events::collections::TimelineEvents event);
std::string room_id_;

@ -46,113 +46,126 @@ namespace {
struct RoomEventType
{
template<class T>
qml_mtx_events::EventType operator()(const mtx::events::Event<T> &e)
constexpr qml_mtx_events::EventType operator()(const mtx::events::Event<T> &e)
{
return qml_mtx_events::toRoomEventType(e.type);
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Audio> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Audio> &)
{
return qml_mtx_events::EventType::AudioMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Confetti> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Confetti> &)
{
return qml_mtx_events::EventType::ConfettiMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Emote> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Emote> &)
{
return qml_mtx_events::EventType::EmoteMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::File> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::File> &)
{
return qml_mtx_events::EventType::FileMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Image> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Image> &)
{
return qml_mtx_events::EventType::ImageMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Notice> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Notice> &)
{
return qml_mtx_events::EventType::NoticeMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Text> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Text> &)
{
return qml_mtx_events::EventType::TextMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Video> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Video> &)
{
return qml_mtx_events::EventType::VideoMessage;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationRequest> &)
{
return qml_mtx_events::EventType::KeyVerificationRequest;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationStart> &)
{
return qml_mtx_events::EventType::KeyVerificationStart;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationMac> &)
{
return qml_mtx_events::EventType::KeyVerificationMac;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationAccept> &)
{
return qml_mtx_events::EventType::KeyVerificationAccept;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationReady> &)
{
return qml_mtx_events::EventType::KeyVerificationReady;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationCancel> &)
{
return qml_mtx_events::EventType::KeyVerificationCancel;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationKey> &)
{
return qml_mtx_events::EventType::KeyVerificationKey;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationDone> &)
{
return qml_mtx_events::EventType::KeyVerificationDone;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Redacted> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::Redacted> &)
{
return qml_mtx_events::EventType::Redacted;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::voip::CallInvite> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::voip::CallInvite> &)
{
return qml_mtx_events::EventType::CallInvite;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::voip::CallAnswer> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::voip::CallAnswer> &)
{
return qml_mtx_events::EventType::CallAnswer;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::voip::CallHangUp> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::voip::CallHangUp> &)
{
return qml_mtx_events::EventType::CallHangUp;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::voip::CallCandidates> &)
{
return qml_mtx_events::EventType::CallCandidates;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::voip::CallSelectAnswer> &)
{
return qml_mtx_events::EventType::CallSelectAnswer;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::voip::CallReject> &)
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::voip::CallReject> &)
{
return qml_mtx_events::EventType::CallReject;
}
qml_mtx_events::EventType
constexpr qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::voip::CallNegotiate> &)
{
return qml_mtx_events::EventType::CallNegotiate;
@ -1060,7 +1073,7 @@ TimelineModel::addEvents(const mtx::responses::Timeline &timeline)
}
template<typename T>
auto
static constexpr auto
isMessage(const mtx::events::RoomEvent<T> &e)
-> std::enable_if_t<std::is_same<decltype(e.content.msgtype), std::string>::value, bool>
{
@ -1068,42 +1081,42 @@ isMessage(const mtx::events::RoomEvent<T> &e)
}
template<typename T>
auto
static constexpr auto
isMessage(const mtx::events::Event<T> &)
{
return false;
}
template<typename T>
auto
static constexpr auto
isMessage(const mtx::events::EncryptedEvent<T> &)
{
return true;
}
auto
static constexpr auto
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &)
{
return true;
}
auto
static constexpr auto
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &)
{
return true;
}
auto
static constexpr auto
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &)
{
return true;
}
auto
static constexpr auto
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallReject> &)
{
return true;
}
auto
static constexpr auto
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallSelectAnswer> &)
{
return true;

Loading…
Cancel
Save