Make replies format nicer

Also lays a bit of groundwork for better reply rendering
This commit is contained in:
Nicolas Werner 2019-11-05 17:16:04 +01:00
parent 88dc72df4f
commit 1268e9f11c
4 changed files with 49 additions and 16 deletions

View File

@ -14,12 +14,23 @@ RowLayout {
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
implicitHeight: contentItem.childrenRect.height implicitHeight: contentItem.height
MessageDelegate { Column {
Layout.fillWidth: true Layout.fillWidth: true
Layout.alignment: Qt.AlignTop Layout.alignment: Qt.AlignTop
id: contentItem id: contentItem
//property var replyTo: model.replyTo
//Text {
// property int idx: timelineManager.timeline.idToIndex(replyTo)
// text: "" + (idx != -1 ? timelineManager.timeline.data(timelineManager.timeline.index(idx, 0), 2) : "nothing")
//}
MessageDelegate {
width: parent.width
height: childrenRect.height
}
} }
StatusIndicator { StatusIndicator {

View File

@ -366,7 +366,7 @@ utils::getFormattedQuoteBody(const RelatedInfo &related, const QString &html)
{ {
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"
"/>%5</blockquote></mx-reply>") "/>%5</blockquote></mx-reply>")
.arg(related.room, .arg(related.room,
QString::fromStdString(related.related_event), QString::fromStdString(related.related_event),
@ -382,9 +382,6 @@ utils::getQuoteBody(const RelatedInfo &related)
using MsgType = mtx::events::MessageType; using MsgType = mtx::events::MessageType;
switch (related.type) { switch (related.type) {
case MsgType::Text: {
return markdownToHtml(related.quoted_body);
}
case MsgType::File: { case MsgType::File: {
return QString(QCoreApplication::translate("utils", "sent a file.")); return QString(QCoreApplication::translate("utils", "sent a file."));
} }

View File

@ -13,6 +13,8 @@
#include "Utils.h" #include "Utils.h"
#include "dialogs/RawMessage.h" #include "dialogs/RawMessage.h"
Q_DECLARE_METATYPE(QModelIndex)
namespace { namespace {
template<class T> template<class T>
QString QString
@ -80,12 +82,6 @@ eventFormattedBody(const mtx::events::RoomEvent<T> &e)
{ {
auto temp = e.content.formatted_body; auto temp = e.content.formatted_body;
if (!temp.empty()) { if (!temp.empty()) {
auto pos = temp.find("<mx-reply>");
if (pos != std::string::npos)
temp.erase(pos, std::string("<mx-reply>").size());
pos = temp.find("</mx-reply>");
if (pos != std::string::npos)
temp.erase(pos, std::string("</mx-reply>").size());
return QString::fromStdString(temp); return QString::fromStdString(temp);
} else { } else {
return QString::fromStdString(e.content.body).toHtmlEscaped().replace("\n", "<br>"); return QString::fromStdString(e.content.body).toHtmlEscaped().replace("\n", "<br>");
@ -182,6 +178,21 @@ eventMimeType(const mtx::events::RoomEvent<T> &e)
return QString::fromStdString(e.content.info.mimetype); return QString::fromStdString(e.content.info.mimetype);
} }
template<class T>
QString
eventRelatesTo(const mtx::events::Event<T> &)
{
return QString();
}
template<class T>
auto
eventRelatesTo(const mtx::events::RoomEvent<T> &e) -> std::enable_if_t<
std::is_same<decltype(e.content.relates_to.in_reply_to.event_id), std::string>::value,
QString>
{
return QString::fromStdString(e.content.relates_to.in_reply_to.event_id);
}
template<class T> template<class T>
qml_mtx_events::EventType qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<T> &e) toRoomEventType(const mtx::events::Event<T> &e)
@ -383,6 +394,7 @@ TimelineModel::roleNames() const
{Id, "id"}, {Id, "id"},
{State, "state"}, {State, "state"},
{IsEncrypted, "isEncrypted"}, {IsEncrypted, "isEncrypted"},
{ReplyTo, "replyTo"},
}; };
} }
int int
@ -450,8 +462,12 @@ TimelineModel::data(const QModelIndex &index, int role) const
return QVariant(utils::replaceEmoji(boost::apply_visitor( return QVariant(utils::replaceEmoji(boost::apply_visitor(
[](const auto &e) -> QString { return eventBody(e); }, event))); [](const auto &e) -> QString { return eventBody(e); }, event)));
case FormattedBody: case FormattedBody:
return QVariant(utils::replaceEmoji(boost::apply_visitor( return QVariant(
[](const auto &e) -> QString { return eventFormattedBody(e); }, event))); utils::replaceEmoji(
boost::apply_visitor(
[](const auto &e) -> QString { return eventFormattedBody(e); }, event))
.remove("<mx-reply>")
.remove("</mx-reply>"));
case Url: case Url:
return QVariant(boost::apply_visitor( return QVariant(boost::apply_visitor(
[](const auto &e) -> QString { return eventUrl(e); }, event)); [](const auto &e) -> QString { return eventUrl(e); }, event));
@ -501,6 +517,11 @@ TimelineModel::data(const QModelIndex &index, int role) const
return boost::get<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>( return boost::get<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
&tempEvent) != nullptr; &tempEvent) != nullptr;
} }
case ReplyTo: {
QString evId = boost::apply_visitor(
[](const auto &e) -> QString { return eventRelatesTo(e); }, event);
return QVariant(evId);
}
default: default:
return QVariant(); return QVariant();
} }
@ -825,8 +846,11 @@ TimelineModel::replyAction(QString id)
event); event);
related.type = mtx::events::getMessageType(boost::apply_visitor( related.type = mtx::events::getMessageType(boost::apply_visitor(
[](const auto &e) -> std::string { return eventMsgType(e); }, event)); [](const auto &e) -> std::string { return eventMsgType(e); }, event));
related.quoted_body = related.quoted_body = boost::apply_visitor(
boost::apply_visitor([](const auto &e) -> QString { return eventBody(e); }, event); [](const auto &e) -> QString { return eventFormattedBody(e); }, event);
related.quoted_body.remove(QRegularExpression(
"<mx-reply>.*</mx-reply>", QRegularExpression::DotMatchesEverythingOption));
nhlog::ui()->debug("after replacement: {}", related.quoted_body.toStdString());
related.room = room_id_; related.room = room_id_;
if (related.quoted_body.isEmpty()) if (related.quoted_body.isEmpty())

View File

@ -139,6 +139,7 @@ public:
Id, Id,
State, State,
IsEncrypted, IsEncrypted,
ReplyTo,
}; };
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;