Linkify links before sending

This commit is contained in:
Nicolas Werner 2020-01-21 03:36:26 +01:00
parent f5b8c9bb31
commit dc44ac50a3
3 changed files with 33 additions and 13 deletions

View File

@ -308,7 +308,7 @@ QString
utils::linkifyMessage(const QString &body) utils::linkifyMessage(const QString &body)
{ {
// Convert to valid XML. // Convert to valid XML.
auto doc = QString("<html>%1</html>").arg(body); auto doc = body;
doc.replace(conf::strings::url_regex, conf::strings::url_html); doc.replace(conf::strings::url_regex, conf::strings::url_html);
return doc; return doc;
@ -382,7 +382,7 @@ utils::markdownToHtml(const QString &text)
// The buffer is no longer needed. // The buffer is no longer needed.
free((char *)tmp_buf); free((char *)tmp_buf);
auto result = escapeBlacklistedHtml(QString::fromStdString(html)).trimmed(); auto result = linkifyMessage(escapeBlacklistedHtml(QString::fromStdString(html))).trimmed();
return result; return result;
} }
@ -390,6 +390,27 @@ utils::markdownToHtml(const QString &text)
QString QString
utils::getFormattedQuoteBody(const RelatedInfo &related, const QString &html) utils::getFormattedQuoteBody(const RelatedInfo &related, const QString &html)
{ {
auto getFormattedBody = [related]() {
using MsgType = mtx::events::MessageType;
switch (related.type) {
case MsgType::File: {
return QString(QCoreApplication::translate("utils", "sent a file."));
}
case MsgType::Image: {
return QString(QCoreApplication::translate("utils", "sent an image."));
}
case MsgType::Audio: {
return QString(QCoreApplication::translate("utils", "sent an audio file."));
}
case MsgType::Video: {
return QString(QCoreApplication::translate("utils", "sent a video"));
}
default: {
return related.quoted_formatted_body;
}
}
};
return QString("<mx-reply><blockquote><a " return QString("<mx-reply><blockquote><a "
"href=\"https://matrix.to/#/%1/%2\">In reply " "href=\"https://matrix.to/#/%1/%2\">In reply "
"to</a> <a href=\"https://matrix.to/#/%3\">%4</a><br" "to</a> <a href=\"https://matrix.to/#/%3\">%4</a><br"
@ -398,7 +419,7 @@ utils::getFormattedQuoteBody(const RelatedInfo &related, const QString &html)
QString::fromStdString(related.related_event), QString::fromStdString(related.related_event),
related.quoted_user, related.quoted_user,
related.quoted_user, related.quoted_user,
getQuoteBody(related)) + getFormattedBody()) +
html; html;
} }

View File

@ -27,7 +27,7 @@ struct RelatedInfo
using MsgType = mtx::events::MessageType; using MsgType = mtx::events::MessageType;
MsgType type; MsgType type;
QString room; QString room;
QString quoted_body; QString quoted_body, quoted_formatted_body;
std::string related_event; std::string related_event;
QString quoted_user; QString quoted_user;
}; };

View File

@ -775,19 +775,18 @@ TimelineModel::replyAction(QString id)
event = decryptEvent(*e).event; event = decryptEvent(*e).event;
} }
RelatedInfo related = {}; RelatedInfo related = {};
related.quoted_user = QString::fromStdString(mtx::accessors::sender(event)); related.quoted_user = QString::fromStdString(mtx::accessors::sender(event));
related.related_event = mtx::accessors::event_id(event); related.related_event = mtx::accessors::event_id(event);
related.type = mtx::accessors::msg_type(event); related.type = mtx::accessors::msg_type(event);
related.quoted_body = mtx::accessors::formattedBodyWithFallback(event); related.quoted_body = QString::fromStdString(mtx::accessors::body(event));
related.quoted_body.remove(QRegularExpression( related.quoted_body = utils::getQuoteBody(related);
related.quoted_formatted_body = mtx::accessors::formattedBodyWithFallback(event);
related.quoted_formatted_body.remove(QRegularExpression(
"<mx-reply>.*</mx-reply>", QRegularExpression::DotMatchesEverythingOption)); "<mx-reply>.*</mx-reply>", QRegularExpression::DotMatchesEverythingOption));
nhlog::ui()->debug("after replacement: {}", related.quoted_body.toStdString()); nhlog::ui()->debug("after replacement: {}", related.quoted_body.toStdString());
related.room = room_id_; related.room = room_id_;
// if (related.quoted_body.isEmpty())
// return;
ChatPage::instance()->messageReply(related); ChatPage::instance()->messageReply(related);
} }