From 06e12a0a16c8ded7deada5586a4772779f01dbe8 Mon Sep 17 00:00:00 2001 From: targetakhil Date: Sat, 17 Apr 2021 22:58:04 +0530 Subject: [PATCH] move detection code to nheko namespace and fix a few other bugs --- src/EventAccessors.cpp | 26 +------- src/EventAccessors.h | 26 ++++++++ src/timeline/TimelineModel.cpp | 2 +- src/timeline/TimelineModel.h | 4 +- src/timeline/TimelineViewManager.cpp | 93 +++++++++++++--------------- src/timeline/TimelineViewManager.h | 43 +++---------- 6 files changed, 80 insertions(+), 114 deletions(-) diff --git a/src/EventAccessors.cpp b/src/EventAccessors.cpp index cfc41a98..362bf4e9 100644 --- a/src/EventAccessors.cpp +++ b/src/EventAccessors.cpp @@ -11,32 +11,8 @@ #include namespace { -struct nonesuch -{ - ~nonesuch() = delete; - nonesuch(nonesuch const &) = delete; - void operator=(nonesuch const &) = delete; -}; - -namespace detail { -template class Op, class... Args> -struct detector -{ - using value_t = std::false_type; - using type = Default; -}; - -template class Op, class... Args> -struct detector>, Op, Args...> -{ - using value_t = std::true_type; - using type = Op; -}; - -} // namespace detail - template class Op, class... Args> -using is_detected = typename detail::detector::value_t; +using is_detected = typename nheko::detail::detector::value_t; struct IsStateEvent { diff --git a/src/EventAccessors.h b/src/EventAccessors.h index ced159c1..a58c7de0 100644 --- a/src/EventAccessors.h +++ b/src/EventAccessors.h @@ -11,6 +11,32 @@ #include +namespace nheko { +struct nonesuch +{ + ~nonesuch() = delete; + nonesuch(nonesuch const &) = delete; + void operator=(nonesuch const &) = delete; +}; + +namespace detail { +template class Op, class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +} // namespace detail +} + namespace mtx::accessors { std::string event_id(const mtx::events::collections::TimelineEvents &event); diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index e3efe5ad..2e83b831 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -829,7 +829,7 @@ TimelineModel::forwardMessage(QString eventId, QString roomId) if (!e) return; - emit forwardToRoom(e, roomId, cache::isRoomEncrypted(room_id_.toStdString())); + emit forwardToRoom(e, roomId); } void diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h index 3e6f6f15..fbe963d2 100644 --- a/src/timeline/TimelineModel.h +++ b/src/timeline/TimelineModel.h @@ -323,9 +323,7 @@ signals: void roomNameChanged(); void roomTopicChanged(); void roomAvatarUrlChanged(); - void forwardToRoom(mtx::events::collections::TimelineEvents *e, - QString roomId, - bool sentFromEncrypted); + void forwardToRoom(mtx::events::collections::TimelineEvents *e, QString roomId); private: template diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp index f71fd42b..a3d19950 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp @@ -621,68 +621,63 @@ TimelineViewManager::focusTimeline() void TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEvents *e, - QString roomId, - bool sentFromEncrypted) + QString roomId) { - auto elem = *e; - auto room = models.find(roomId); - auto messageType = mtx::accessors::msg_type(elem); - auto content = mtx::accessors::url(elem); - - if (sentFromEncrypted) { - std::optional encryptionInfo = - mtx::accessors::file(elem); + auto elem = *e; + auto room = models.find(roomId); + auto content = mtx::accessors::url(elem); + std::optional encryptionInfo = mtx::accessors::file(elem); + if (encryptionInfo) { http::client()->download( content, [this, roomId, e, encryptionInfo](const std::string &res, - const std::string &content_type, - const std::string &originalFilename, - mtx::http::RequestErr err) { + const std::string &content_type, + const std::string &originalFilename, + mtx::http::RequestErr err) { if (err) { return; } - assert(encryptionInfo); + auto data = mtx::crypto::to_string( + mtx::crypto::decrypt_file(res, encryptionInfo.value())); - auto data = mtx::crypto::to_string( - mtx::crypto::decrypt_file(res, encryptionInfo.value())); + http::client()->upload( + data, + content_type, + originalFilename, + [this, roomId, e](const mtx::responses::ContentURI &res, + mtx::http::RequestErr err) mutable { + if (err) { + nhlog::net()->warn("failed to upload media: {} {} ({})", + err->matrix_error.error, + to_string(err->matrix_error.errcode), + static_cast(err->status_code)); + return; + } - http::client()->upload( - data, - content_type, - originalFilename, - [this, roomId, e](const mtx::responses::ContentURI &res, - mtx::http::RequestErr err) mutable { - if (err) { - nhlog::net()->warn("failed to upload media: {} {} ({})", - err->matrix_error.error, - to_string(err->matrix_error.errcode), - static_cast(err->status_code)); - return; - } + std::visit( + [this, roomId, e, url = res.content_uri](auto ev) { + if constexpr (mtx::events::message_content_to_type< + decltype(ev.content)> == + mtx::events::EventType::RoomMessage) { + if constexpr (messageWithFileAndUrl(ev)) { + ev.content.relations.relations + .clear(); + ev.content.file.reset(); + ev.content.url = url; + } - std::visit( - [this, roomId, e, url = res.content_uri](auto ev) { - if constexpr (mtx::events::message_content_to_type< - decltype(ev.content)> == - mtx::events::EventType::RoomMessage) { - if constexpr (messageWithFileAndUrl(ev)) { - ev.content.relations.relations.clear(); - ev.content.file.reset(); - ev.content.url = url; + auto room = models.find(roomId); + room.value()->sendMessageEvent( + ev.content, + mtx::events::EventType::RoomMessage); + } + }, + *e); + }); - auto room = models.find(roomId); - room.value()->sendMessageEvent( - ev.content, - mtx::events::EventType::RoomMessage); - } - } - }, - *e); - }); - - return; + return; }); return; diff --git a/src/timeline/TimelineViewManager.h b/src/timeline/TimelineViewManager.h index 9d1b4b1d..e5dea7ce 100644 --- a/src/timeline/TimelineViewManager.h +++ b/src/timeline/TimelineViewManager.h @@ -32,33 +32,6 @@ class UserSettings; class ChatPage; class DeviceVerificationFlow; -struct nonesuch -{ - ~nonesuch() = delete; - nonesuch(nonesuch const &) = delete; - void operator=(nonesuch const &) = delete; -}; - -namespace detail { -template class Op, class... Args> -struct detector -{ - using value_t = std::false_type; - using type = Default; -}; - -template class Op, class... Args> -struct detector>, Op, Args...> -{ - using value_t = std::true_type; - using type = Op; -}; - -} // namespace detail - -template class Op, class... Args> -using is_detected = typename detail::detector::value_t; - class TimelineViewManager : public QObject { Q_OBJECT @@ -175,15 +148,17 @@ public slots: void backToRooms() { emit showRoomList(); } QObject *completerFor(QString completerName, QString roomId = ""); - void forwardMessageToRoom(mtx::events::collections::TimelineEvents *e, - QString roomId, - bool sentFromEncrypted); + void forwardMessageToRoom(mtx::events::collections::TimelineEvents *e, QString roomId); private slots: void openImageOverlayInternal(QString eventId, QImage img); private: - template + template class Op, class... Args> + using is_detected = + typename nheko::detail::detector::value_t; + + template using f_t = decltype(Content::file); template @@ -192,11 +167,7 @@ private: template static constexpr bool messageWithFileAndUrl(const mtx::events::Event &e) { - if constexpr (is_detected::value && is_detected::value) { - return true; - } - - return false; + return is_detected::value && is_detected::value; } private: