diff --git a/src/EventAccessors.cpp b/src/EventAccessors.cpp index f022263c..d14a1e3e 100644 --- a/src/EventAccessors.cpp +++ b/src/EventAccessors.cpp @@ -15,12 +15,12 @@ namespace { struct IsStateEvent { template - bool operator()(const mtx::events::StateEvent &) + constexpr bool operator()(const mtx::events::StateEvent &) { return true; } template - bool operator()(const mtx::events::Event &) + constexpr bool operator()(const mtx::events::Event &) { return false; } @@ -39,28 +39,6 @@ struct EventMsgType } }; -struct EventRoomName -{ - template - std::string operator()(const T &e) - { - if constexpr (std::is_same_v, T>) - return e.content.name; - return ""; - } -}; - -struct EventRoomTopic -{ - template - std::string operator()(const T &e) - { - if constexpr (std::is_same_v, T>) - return e.content.topic; - return ""; - } -}; - struct CallType { template @@ -86,37 +64,37 @@ struct CallType struct EventBody { template - std::string operator()(const mtx::events::Event &e) + const std::string *operator()(const mtx::events::Event &e) { if constexpr (requires(decltype(e) t) { t.content.body.value(); }) - return e.content.body ? e.content.body.value() : ""; + return e.content.body ? &e.content.body.value() : nullptr; else if constexpr (requires(decltype(e) t) { std::string{t.content.body}; }) - return e.content.body; - return ""; + return &e.content.body; + return nullptr; } }; struct EventFormattedBody { template - std::string operator()(const mtx::events::RoomEvent &e) + const std::string *operator()(const mtx::events::RoomEvent &e) { if constexpr (requires { T::formatted_body; }) { if (e.content.format == "org.matrix.custom.html") - return e.content.formatted_body; + return &e.content.formatted_body; } - return ""; + return nullptr; } }; struct EventFile { template - std::optional operator()(const mtx::events::Event &e) + const std::optional *operator()(const mtx::events::Event &e) { if constexpr (requires { T::file; }) - return e.content.file; - return std::nullopt; + return &e.content.file; + return nullptr; } }; @@ -137,8 +115,8 @@ struct EventUrl std::string operator()(const mtx::events::Event &e) { if constexpr (requires { T::url; }) { - if (auto file = EventFile{}(e)) - return file->url; + if (auto file = EventFile{}(e); file && *file) + return (*file)->url; return e.content.url; } return ""; @@ -351,15 +329,23 @@ mtx::accessors::msg_type(const mtx::events::collections::TimelineEvents &event) { return std::visit(EventMsgType{}, event); } + std::string mtx::accessors::room_name(const mtx::events::collections::TimelineEvents &event) { - return std::visit(EventRoomName{}, event); + if (auto c = std::get_if>(&event)) + return c->content.name; + else + return ""; } + std::string mtx::accessors::room_topic(const mtx::events::collections::TimelineEvents &event) { - return std::visit(EventRoomTopic{}, event); + if (auto c = std::get_if>(&event)) + return c->content.topic; + else + return ""; } std::string @@ -371,13 +357,15 @@ mtx::accessors::call_type(const mtx::events::collections::TimelineEvents &event) std::string mtx::accessors::body(const mtx::events::collections::TimelineEvents &event) { - return std::visit(EventBody{}, event); + auto body = std::visit(EventBody{}, event); + return body ? *body : std::string{}; } std::string mtx::accessors::formatted_body(const mtx::events::collections::TimelineEvents &event) { - return std::visit(EventFormattedBody{}, event); + auto body = std::visit(EventFormattedBody{}, event); + return body ? *body : std::string{}; } QString @@ -395,7 +383,11 @@ mtx::accessors::formattedBodyWithFallback(const mtx::events::collections::Timeli std::optional mtx::accessors::file(const mtx::events::collections::TimelineEvents &event) { - return std::visit(EventFile{}, event); + auto temp = std::visit(EventFile{}, event); + if (temp) + return *temp; + else + return {}; } std::optional