From e2f547149a5d54747ab44a6f777f91edd8d2f76e Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 27 Jan 2020 15:59:25 +0100 Subject: Allow sending messages as plain text --- src/timeline/TimelineViewManager.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/timeline/TimelineViewManager.cpp') diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp index b3fd65a1..578d075f 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp @@ -18,8 +18,7 @@ Q_DECLARE_METATYPE(mtx::events::collections::TimelineEvents) void TimelineViewManager::updateColorPalette() { - UserSettings settings; - if (settings.theme() == "light") { + if (settings->theme() == "light") { QPalette lightActive(/*windowText*/ QColor("#333"), /*button*/ QColor("#333"), /*light*/ QColor(), @@ -31,7 +30,7 @@ TimelineViewManager::updateColorPalette() /*window*/ QColor("white")); view->rootContext()->setContextProperty("currentActivePalette", lightActive); view->rootContext()->setContextProperty("currentInactivePalette", lightActive); - } else if (settings.theme() == "dark") { + } else if (settings->theme() == "dark") { QPalette darkActive(/*windowText*/ QColor("#caccd1"), /*button*/ QColor("#caccd1"), /*light*/ QColor(), @@ -50,9 +49,10 @@ TimelineViewManager::updateColorPalette() } } -TimelineViewManager::TimelineViewManager(QWidget *parent) +TimelineViewManager::TimelineViewManager(QSharedPointer userSettings, QWidget *parent) : imgProvider(new MxcImageProvider()) , colorImgProvider(new ColorImageProvider()) + , settings(userSettings) { qmlRegisterUncreatableMetaObject(qml_mtx_events::staticMetaObject, "im.nheko", @@ -207,6 +207,11 @@ TimelineViewManager::queueTextMessage(const QString &msg, const std::optionalrelated_event; } + if (!settings->isMarkdownEnabled()) { + text.format = ""; + text.formatted_body = ""; + } + if (timeline_) timeline_->sendMessage(text); } @@ -219,8 +224,10 @@ TimelineViewManager::queueEmoteMessage(const QString &msg) mtx::events::msg::Emote emote; emote.body = msg.trimmed().toStdString(); - if (html != msg.trimmed().toHtmlEscaped()) + if (html != msg.trimmed().toHtmlEscaped() && settings->isMarkdownEnabled()) { emote.formatted_body = html.toStdString(); + emote.format = "org.matrix.custom.html"; + } if (timeline_) timeline_->sendMessage(emote); -- cgit 1.5.1 From 9c368fa7f448715c2ce04c61097e0cb18091774b Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 27 Jan 2020 17:05:40 +0100 Subject: Fix reply rendering without markdown in riot --- src/timeline/TimelineModel.cpp | 23 ++++++++++++++++++++--- src/timeline/TimelineViewManager.cpp | 25 ++++++++++++++++--------- 2 files changed, 36 insertions(+), 12 deletions(-) (limited to 'src/timeline/TimelineViewManager.cpp') diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index fb8c0095..03b7d6a7 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -281,9 +281,26 @@ TimelineModel::data(const QString &id, int role) const case FormattedBody: { const static QRegularExpression replyFallback( ".*", QRegularExpression::DotMatchesEverythingOption); - return QVariant( - utils::replaceEmoji(utils::linkifyMessage(utils::escapeBlacklistedHtml( - formattedBodyWithFallback(event).remove(replyFallback))))); + + bool isReply = !in_reply_to_event(event).empty(); + + auto formattedBody_ = QString::fromStdString(formatted_body(event)); + if (formattedBody_.isEmpty()) { + auto body_ = QString::fromStdString(body(event)); + + if (isReply) { + while (body_.startsWith("> ")) + body_ = body_.right(body_.size() - body_.indexOf('\n') - 1); + if (body_.startsWith('\n')) + body_ = body_.right(body_.size() - 1); + } + formattedBody_ = body_.toHtmlEscaped(); + } else { + if (isReply) + formattedBody_ = formattedBody_.remove(replyFallback); + } + return QVariant(utils::replaceEmoji( + utils::linkifyMessage(utils::escapeBlacklistedHtml(formattedBody_)))); } case Url: return QVariant(QString::fromStdString(url(event))); diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp index 578d075f..9008d9d2 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp @@ -186,8 +186,11 @@ TimelineViewManager::queueTextMessage(const QString &msg, const std::optionalisMarkdownEnabled()) { + text.format = "org.matrix.custom.html"; + text.formatted_body = utils::markdownToHtml(msg).toStdString(); + } if (related) { QString body; @@ -202,14 +205,18 @@ TimelineViewManager::queueTextMessage(const QString &msg, const std::optionalrelated_event; - } - if (!settings->isMarkdownEnabled()) { - text.format = ""; - text.formatted_body = ""; + // NOTE(Nico): rich replies always need a formatted_body! + text.format = "org.matrix.custom.html"; + if (settings->isMarkdownEnabled()) + text.formatted_body = + utils::getFormattedQuoteBody(*related, utils::markdownToHtml(msg)) + .toStdString(); + else + text.formatted_body = + utils::getFormattedQuoteBody(*related, msg.toHtmlEscaped()).toStdString(); + + text.relates_to.in_reply_to.event_id = related->related_event; } if (timeline_) -- cgit 1.5.1 From e81a4e8f9abb7f2dbdc419929bcaf6a3d4065bea Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 27 Jan 2020 17:25:09 +0100 Subject: Don't send useless formatted_bodies --- src/Utils.cpp | 4 ++++ src/timeline/TimelineViewManager.cpp | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'src/timeline/TimelineViewManager.cpp') diff --git a/src/Utils.cpp b/src/Utils.cpp index 5a67ef62..76ffed31 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -397,6 +397,10 @@ utils::markdownToHtml(const QString &text) auto result = linkifyMessage(escapeBlacklistedHtml(QString::fromStdString(html))).trimmed(); + if (result.count("

") == 1 && result.startsWith("

") && result.endsWith("

")) { + result = result.mid(3, result.size() - 3 - 4); + } + return result; } diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp index 9008d9d2..a1f23cf5 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp @@ -188,8 +188,13 @@ TimelineViewManager::queueTextMessage(const QString &msg, const std::optionalisMarkdownEnabled()) { - text.format = "org.matrix.custom.html"; text.formatted_body = utils::markdownToHtml(msg).toStdString(); + + // Don't send formatted_body, when we don't need to + if (text.formatted_body == text.body) + text.formatted_body = ""; + else + text.format = "org.matrix.custom.html"; } if (related) { -- cgit 1.5.1