Add initial support for displaying formatted messages

This commit is contained in:
Konstantinos Sideris 2018-09-06 22:34:41 +03:00
parent 36c5a8f410
commit 8ad01f520a
3 changed files with 35 additions and 22 deletions

4
deps/CMakeLists.txt vendored
View File

@ -38,10 +38,10 @@ set(BOOST_SHA256
set( set(
MTXCLIENT_URL MTXCLIENT_URL
https://github.com/mujx/mtxclient/archive/81d497adbb1c70e543482a7f7d5d83baa959cb81.tar.gz https://github.com/mujx/mtxclient/archive/cd8f571f8d97b315435a3acf861d5cc8351363f9.tar.gz
) )
set(MTXCLIENT_HASH set(MTXCLIENT_HASH
755697c07e4adc0ff380bde8ecd719f5da443b2baca757d4e9c59d30571f5773) 4aad9205cdc84283955bbac430908340fe6e2addc4a9b44c6403d5aa581d6541)
set( set(
TWEENY_URL TWEENY_URL

View File

@ -12,6 +12,7 @@
#include <QDateTime> #include <QDateTime>
#include <QPixmap> #include <QPixmap>
#include <mtx/events/collections.hpp> #include <mtx/events/collections.hpp>
#include <mtx/events/common.hpp>
namespace utils { namespace utils {
@ -195,4 +196,19 @@ humanReadableFingerprint(const std::string &ed25519);
QString QString
humanReadableFingerprint(const QString &ed25519); humanReadableFingerprint(const QString &ed25519);
//! Retrieve the message body taking into account the `formatted_body` field.
//! If the `format` of the message is not supported we fallback to `body`.
template<typename RoomMessageT>
QString
get_message_body(const RoomMessageT &event)
{
if (event.content.format.empty())
return QString::fromStdString(event.content.body);
if (event.content.format != common::FORMAT_MSG_TYPE)
return QString::fromStdString(event.content.body);
return QString::fromStdString(event.content.formatted_body);
}
} }

View File

@ -436,7 +436,9 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
event_id_ = QString::fromStdString(event.event_id); event_id_ = QString::fromStdString(event.event_id);
const auto sender = QString::fromStdString(event.sender); const auto sender = QString::fromStdString(event.sender);
const auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts); const auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
auto body = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
auto formatted_body = utils::get_message_body(event).trimmed();
auto body = QString::fromStdString(event.content.body).trimmed();
descriptionMsg_ = {Cache::displayName(room_id_, sender), descriptionMsg_ = {Cache::displayName(room_id_, sender),
sender, sender,
@ -446,20 +448,18 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
generateTimestamp(timestamp); generateTimestamp(timestamp);
body.replace(conf::strings::url_regex, conf::strings::url_html); formatted_body = "<i>" + formatted_body + "</i>";
body.replace("\n", "<br/>");
body = "<i>" + body + "</i>";
if (with_sender) { if (with_sender) {
auto displayName = Cache::displayName(room_id_, sender); auto displayName = Cache::displayName(room_id_, sender);
generateBody(sender, displayName, body); generateBody(sender, displayName, formatted_body);
setupAvatarLayout(displayName); setupAvatarLayout(displayName);
AvatarProvider::resolve( AvatarProvider::resolve(
room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); }); room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
} else { } else {
generateBody(body); generateBody(formatted_body);
setupSimpleLayout(); setupSimpleLayout();
} }
@ -484,26 +484,25 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
event_id_ = QString::fromStdString(event.event_id); event_id_ = QString::fromStdString(event.event_id);
const auto sender = QString::fromStdString(event.sender); const auto sender = QString::fromStdString(event.sender);
auto body = QString::fromStdString(event.content.body).trimmed(); auto formatted_body = utils::get_message_body(event).trimmed();
auto body = QString::fromStdString(event.content.body).trimmed();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts); auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
auto displayName = Cache::displayName(room_id_, sender); auto displayName = Cache::displayName(room_id_, sender);
auto emoteMsg = QString("* %1 %2").arg(displayName).arg(body); auto emoteMsg = QString("* %1 %2").arg(displayName).arg(formatted_body);
descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp}; descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp};
generateTimestamp(timestamp); generateTimestamp(timestamp);
emoteMsg = emoteMsg.toHtmlEscaped();
emoteMsg.replace(conf::strings::url_regex, conf::strings::url_html);
emoteMsg.replace("\n", "<br/>");
if (with_sender) { if (with_sender) {
generateBody(sender, displayName, emoteMsg); generateBody(sender, displayName, formatted_body);
setupAvatarLayout(displayName); setupAvatarLayout(displayName);
AvatarProvider::resolve( AvatarProvider::resolve(
room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); }); room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
} else { } else {
generateBody(emoteMsg); generateBody(formatted_body);
setupSimpleLayout(); setupSimpleLayout();
} }
@ -528,7 +527,9 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
event_id_ = QString::fromStdString(event.event_id); event_id_ = QString::fromStdString(event.event_id);
const auto sender = QString::fromStdString(event.sender); const auto sender = QString::fromStdString(event.sender);
auto body = QString::fromStdString(event.content.body).trimmed(); auto formatted_body = utils::get_message_body(event).trimmed();
auto body = QString::fromStdString(event.content.body).trimmed();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts); auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
auto displayName = Cache::displayName(room_id_, sender); auto displayName = Cache::displayName(room_id_, sender);
@ -541,18 +542,14 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
generateTimestamp(timestamp); generateTimestamp(timestamp);
body = body.toHtmlEscaped();
body.replace(conf::strings::url_regex, conf::strings::url_html);
body.replace("\n", "<br/>");
if (with_sender) { if (with_sender) {
generateBody(sender, displayName, body); generateBody(sender, displayName, formatted_body);
setupAvatarLayout(displayName); setupAvatarLayout(displayName);
AvatarProvider::resolve( AvatarProvider::resolve(
room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); }); room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
} else { } else {
generateBody(body); generateBody(formatted_body);
setupSimpleLayout(); setupSimpleLayout();
} }