strip reply fallbacks from forwarded message

This commit is contained in:
targetakhil 2021-04-18 11:22:44 +05:30
parent 06e12a0a16
commit 2dfa40e017
4 changed files with 69 additions and 19 deletions

View file

@ -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 // get body, strip reply fallback, then transform the event to text, if it is a media event
// etc // etc
related.quoted_body = QString::fromStdString(mtx::accessors::body(event)); related.quoted_body = QString::fromStdString(mtx::accessors::body(event));
QRegularExpression plainQuote("^>.*?$\n?", QRegularExpression::MultilineOption); stripReplyFromBody(related.quoted_body);
while (related.quoted_body.startsWith(">"))
related.quoted_body.remove(plainQuote);
if (related.quoted_body.startsWith("\n"))
related.quoted_body.remove(0, 1);
related.quoted_body = utils::getQuoteBody(related); related.quoted_body = utils::getQuoteBody(related);
related.quoted_body.replace("@room", QString::fromUtf8("@\u2060room")); related.quoted_body.replace("@room", QString::fromUtf8("@\u2060room"));
// get quoted body and strip reply fallback // get quoted body and strip reply fallback
related.quoted_formatted_body = mtx::accessors::formattedBodyWithFallback(event); related.quoted_formatted_body = mtx::accessors::formattedBodyWithFallback(event);
related.quoted_formatted_body.remove(QRegularExpression( stripReplyFromFormattedBody(related.quoted_formatted_body);
"<mx-reply>.*</mx-reply>", QRegularExpression::DotMatchesEverythingOption));
related.quoted_formatted_body.replace("@room", "@\u2060aroom");
related.room = room_id_; related.room = room_id_;
return related; return related;

View file

@ -9,6 +9,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDateTime> #include <QDateTime>
#include <QPixmap> #include <QPixmap>
#include <QRegularExpression>
#include <mtx/events/collections.hpp> #include <mtx/events/collections.hpp>
#include <mtx/events/common.hpp> #include <mtx/events/common.hpp>
@ -40,6 +41,26 @@ namespace utils {
using TimelineEvent = mtx::events::collections::TimelineEvents; 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 RelatedInfo
stripReplyFallbacks(const TimelineEvent &event, std::string id, QString room_id_); stripReplyFallbacks(const TimelineEvent &event, std::string id, QString room_id_);

View file

@ -4,7 +4,6 @@
#include "TimelineViewManager.h" #include "TimelineViewManager.h"
#include <QBuffer>
#include <QDesktopServices> #include <QDesktopServices>
#include <QDropEvent> #include <QDropEvent>
#include <QMetaType> #include <QMetaType>
@ -26,7 +25,6 @@
#include "RoomsModel.h" #include "RoomsModel.h"
#include "UserSettingsPage.h" #include "UserSettingsPage.h"
#include "UsersModel.h" #include "UsersModel.h"
#include "blurhash.hpp"
#include "dialogs/ImageOverlay.h" #include "dialogs/ImageOverlay.h"
#include "emoji/EmojiModel.h" #include "emoji/EmojiModel.h"
#include "emoji/Provider.h" #include "emoji/Provider.h"
@ -623,10 +621,9 @@ void
TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEvents *e, TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEvents *e,
QString roomId) QString roomId)
{ {
auto elem = *e;
auto room = models.find(roomId); auto room = models.find(roomId);
auto content = mtx::accessors::url(elem); auto content = mtx::accessors::url(*e);
std::optional<mtx::crypto::EncryptedFile> encryptionInfo = mtx::accessors::file(elem); std::optional<mtx::crypto::EncryptedFile> encryptionInfo = mtx::accessors::file(*e);
if (encryptionInfo) { if (encryptionInfo) {
http::client()->download( http::client()->download(
@ -669,6 +666,8 @@ TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEven
} }
auto room = models.find(roomId); auto room = models.find(roomId);
removeReplyFallback(ev);
ev.content.relations.relations.clear();
room.value()->sendMessageEvent( room.value()->sendMessageEvent(
ev.content, ev.content,
mtx::events::EventType::RoomMessage); 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)> == if constexpr (mtx::events::message_content_to_type<decltype(e.content)> ==
mtx::events::EventType::RoomMessage) { mtx::events::EventType::RoomMessage) {
e.content.relations.relations.clear(); e.content.relations.relations.clear();
removeReplyFallback(e);
room.value()->sendMessageEvent(e.content, room.value()->sendMessageEvent(e.content,
mtx::events::EventType::RoomMessage); mtx::events::EventType::RoomMessage);
} }
}, },
elem); *e);
} }

View file

@ -13,7 +13,6 @@
#include <mtx/common.hpp> #include <mtx/common.hpp>
#include <mtx/responses/messages.hpp> #include <mtx/responses/messages.hpp>
#include <mtx/responses/sync.hpp> #include <mtx/responses/sync.hpp>
#include <type_traits>
#include "Cache.h" #include "Cache.h"
#include "CallManager.h" #include "CallManager.h"
@ -159,15 +158,51 @@ private:
typename nheko::detail::detector<nheko::nonesuch, void, Op, Args...>::value_t; typename nheko::detail::detector<nheko::nonesuch, void, Op, Args...>::value_t;
template<class Content> template<class Content>
using f_t = decltype(Content::file); using file_t = decltype(Content::file);
template<class Content> 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> template<typename T>
static constexpr bool messageWithFileAndUrl(const mtx::events::Event<T> &e) static constexpr bool messageWithFileAndUrl(const mtx::events::Event<T> &)
{ {
return is_detected<f_t, T>::value && is_detected<u_t, T>::value; return is_detected<file_t, T>::value && is_detected<url_t, T>::value;
}
template<typename T>
static constexpr void removeReplyFallback(mtx::events::Event<T> &e)
{
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: private: