summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authortargetakhil <targetakhil@gmail.com>2021-04-18 11:22:44 +0530
committertargetakhil <targetakhil@gmail.com>2021-04-18 11:22:44 +0530
commit2dfa40e017c9880164936b50a0ab698dd83a125b (patch)
tree1532ef861f3fb0c3ddcee3a5fbcbdab6b11fd420 /src
parentmove detection code to nheko namespace and fix a few other bugs (diff)
downloadnheko-2dfa40e017c9880164936b50a0ab698dd83a125b.tar.xz
strip reply fallbacks from forwarded message
Diffstat (limited to 'src')
-rw-r--r--src/Utils.cpp10
-rw-r--r--src/Utils.h21
-rw-r--r--src/timeline/TimelineViewManager.cpp12
-rw-r--r--src/timeline/TimelineViewManager.h45
4 files changed, 69 insertions, 19 deletions
diff --git a/src/Utils.cpp b/src/Utils.cpp

index ca2d3adc..11e7b1af 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp
@@ -63,19 +63,13 @@ utils::stripReplyFallbacks(const TimelineEvent &event, std::string id, QString r // get body, strip reply fallback, then transform the event to text, if it is a media event // etc related.quoted_body = QString::fromStdString(mtx::accessors::body(event)); - QRegularExpression plainQuote("^>.*?$\n?", QRegularExpression::MultilineOption); - while (related.quoted_body.startsWith(">")) - related.quoted_body.remove(plainQuote); - if (related.quoted_body.startsWith("\n")) - related.quoted_body.remove(0, 1); + stripReplyFromBody(related.quoted_body); related.quoted_body = utils::getQuoteBody(related); related.quoted_body.replace("@room", QString::fromUtf8("@\u2060room")); // get quoted body and strip reply fallback related.quoted_formatted_body = mtx::accessors::formattedBodyWithFallback(event); - related.quoted_formatted_body.remove(QRegularExpression( - "<mx-reply>.*</mx-reply>", QRegularExpression::DotMatchesEverythingOption)); - related.quoted_formatted_body.replace("@room", "@\u2060aroom"); + stripReplyFromFormattedBody(related.quoted_formatted_body); related.room = room_id_; return related; diff --git a/src/Utils.h b/src/Utils.h
index 7a9eb777..e62b2d34 100644 --- a/src/Utils.h +++ b/src/Utils.h
@@ -9,6 +9,7 @@ #include <QCoreApplication> #include <QDateTime> #include <QPixmap> +#include <QRegularExpression> #include <mtx/events/collections.hpp> #include <mtx/events/common.hpp> @@ -40,6 +41,26 @@ namespace utils { using TimelineEvent = mtx::events::collections::TimelineEvents; +//! Helper function to remove reply fallback from body +static void +stripReplyFromBody(QString &body) +{ + QRegularExpression plainQuote("^>.*?$\n?", QRegularExpression::MultilineOption); + while (body.startsWith(">")) + body.remove(plainQuote); + if (body.startsWith("\n")) + body.remove(0, 1); +} + +//! Helper function to remove reply fallback from formatted body +static void +stripReplyFromFormattedBody(QString &formatted_body) +{ + formatted_body.remove(QRegularExpression("<mx-reply>.*</mx-reply>", + QRegularExpression::DotMatchesEverythingOption)); + formatted_body.replace("@room", "@\u2060aroom"); +} + RelatedInfo stripReplyFallbacks(const TimelineEvent &event, std::string id, QString room_id_); diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp
index a3d19950..ff759625 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp
@@ -4,7 +4,6 @@ #include "TimelineViewManager.h" -#include <QBuffer> #include <QDesktopServices> #include <QDropEvent> #include <QMetaType> @@ -26,7 +25,6 @@ #include "RoomsModel.h" #include "UserSettingsPage.h" #include "UsersModel.h" -#include "blurhash.hpp" #include "dialogs/ImageOverlay.h" #include "emoji/EmojiModel.h" #include "emoji/Provider.h" @@ -623,10 +621,9 @@ void TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEvents *e, QString roomId) { - auto elem = *e; auto room = models.find(roomId); - auto content = mtx::accessors::url(elem); - std::optional<mtx::crypto::EncryptedFile> encryptionInfo = mtx::accessors::file(elem); + auto content = mtx::accessors::url(*e); + std::optional<mtx::crypto::EncryptedFile> encryptionInfo = mtx::accessors::file(*e); if (encryptionInfo) { http::client()->download( @@ -669,6 +666,8 @@ TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEven } auto room = models.find(roomId); + removeReplyFallback(ev); + ev.content.relations.relations.clear(); room.value()->sendMessageEvent( ev.content, mtx::events::EventType::RoomMessage); @@ -688,9 +687,10 @@ TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEven if constexpr (mtx::events::message_content_to_type<decltype(e.content)> == mtx::events::EventType::RoomMessage) { e.content.relations.relations.clear(); + removeReplyFallback(e); room.value()->sendMessageEvent(e.content, mtx::events::EventType::RoomMessage); } }, - elem); + *e); } \ No newline at end of file diff --git a/src/timeline/TimelineViewManager.h b/src/timeline/TimelineViewManager.h
index e5dea7ce..7e9632de 100644 --- a/src/timeline/TimelineViewManager.h +++ b/src/timeline/TimelineViewManager.h
@@ -13,7 +13,6 @@ #include <mtx/common.hpp> #include <mtx/responses/messages.hpp> #include <mtx/responses/sync.hpp> -#include <type_traits> #include "Cache.h" #include "CallManager.h" @@ -159,15 +158,51 @@ private: typename nheko::detail::detector<nheko::nonesuch, void, Op, Args...>::value_t; template<class Content> - using f_t = decltype(Content::file); + using file_t = decltype(Content::file); template<class Content> - using u_t = decltype(Content::url); + using url_t = decltype(Content::url); + + template<class Content> + using body_t = decltype(Content::body); + + template<class Content> + using formatted_body_t = decltype(Content::formatted_body); + + template<typename T> + static constexpr bool messageWithFileAndUrl(const mtx::events::Event<T> &) + { + return is_detected<file_t, T>::value && is_detected<url_t, T>::value; + } template<typename T> - static constexpr bool messageWithFileAndUrl(const mtx::events::Event<T> &e) + static constexpr void removeReplyFallback(mtx::events::Event<T> &e) { - return is_detected<f_t, T>::value && is_detected<u_t, T>::value; + if constexpr (is_detected<body_t, T>::value) { + if constexpr (std::is_same_v<std::optional<std::string>, + std::remove_cv_t<decltype(e.content.body)>>) { + if (e.content.body) { + QString body = QString::fromStdString(e.content.body); + utils::stripReplyFromBody(body); + e.content.body = body.toStdString(); + } + } else if constexpr (std::is_same_v< + std::string, + std::remove_cv_t<decltype(e.content.body)>>) { + QString body = QString::fromStdString(e.content.body); + utils::stripReplyFromBody(body); + e.content.body = body.toStdString(); + } + } + + if constexpr (is_detected<formatted_body_t, T>::value) { + if (e.content.format == "org.matrix.custom.html") { + QString formattedBody = + QString::fromStdString(e.content.formatted_body); + utils::stripReplyFromFormattedBody(formattedBody); + e.content.formatted_body = formattedBody.toStdString(); + } + } } private: