Texthandler edited

Move the handling of adding whether the message has been edited to texthandler.
This commit is contained in:
James Graham
2023-04-08 20:58:44 +00:00
parent e5f2e209a2
commit 4bf65339f8
7 changed files with 81 additions and 16 deletions

View File

@@ -60,6 +60,8 @@ private Q_SLOTS:
void receiveRichMxcUrl();
void receiveRichPlainUrl();
void receiveRichEmote();
void receiveRichEdited_data();
void receiveRichEdited();
};
#ifdef QUOTIENT_07
@@ -147,7 +149,7 @@ void TextHandlerTest::initTestCase()
"sender": "@example:example.org",
"type": "m.room.message",
"unsigned": {
"age": 1235
"age": 1232
}
},
{
@@ -163,7 +165,28 @@ void TextHandlerTest::initTestCase()
"sender": "@example:example.org",
"type": "m.room.message",
"unsigned": {
"age": 1236
"age": 1231
}
},
{
"content": {
"body": "tested",
"msgtype": "m.text"
},
"event_id": "$zrCiBxBnqqTn0Z5FY78qSZAszno_w8nJJXzfBULG-3E",
"origin_server_ts": 1680948575928,
"room_id": "!jEsUZKDJdhlrceRyVU:example.org",
"sender": "@example:example.org",
"type": "m.room.message",
"unsigned": {
"age": 1747776,
"m.relations": {
"m.replace": {
"event_id": "$UX0PlpyI7vYO32iHMuuYEP7ECMh4sX3XLGiB2SwM4mQ",
"origin_server_ts": 1680948580992,
"sender": "@example:example.org"
}
}
}
}
],
@@ -529,5 +552,28 @@ void TextHandlerTest::receiveRichEmote()
QCOMPARE(testTextHandler.handleRecieveRichText(Qt::RichText, room, room->messageEvents().at(1).get()), testOutputString);
}
void TextHandlerTest::receiveRichEdited_data()
{
QTest::addColumn<QString>("testInputString");
QTest::addColumn<QString>("testOutputString");
QTest::newRow("basic") << QStringLiteral("Edited") << QStringLiteral("Edited <span style=\"color:#000000\">(edited)</span>");
QTest::newRow("multiple paragraphs") << QStringLiteral("<p>Edited</p>\n<p>Edited</p>")
<< QStringLiteral("<p>Edited</p>\n<p>Edited <span style=\"color:#000000\">(edited)</span></p>");
QTest::newRow("blockquote") << QStringLiteral("<blockquote>Edited</blockquote>")
<< QStringLiteral("<blockquote>Edited</blockquote><p> <span style=\"color:#000000\">(edited)</span></p>");
}
void TextHandlerTest::receiveRichEdited()
{
QFETCH(QString, testInputString);
QFETCH(QString, testOutputString);
TextHandler testTextHandler;
testTextHandler.setData(testInputString);
QCOMPARE(testTextHandler.handleRecieveRichText(Qt::RichText, room, room->messageEvents().at(2).get()), testOutputString);
}
QTEST_MAIN(TextHandlerTest)
#include "texthandlertest.moc"

View File

@@ -57,7 +57,6 @@ QHash<int, QByteArray> MessageEventModel::roleNames() const
roles[ReadMarkersStringRole] = "readMarkersString";
roles[ShowReadMarkersRole] = "showReadMarkers";
roles[ReactionRole] = "reaction";
roles[IsEditedRole] = "isEdited";
roles[SourceRole] = "source";
roles[MimeTypeRole] = "mimeType";
roles[FormattedBodyRole] = "formattedBody";
@@ -662,14 +661,6 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const
return EventStatus::Normal;
}
if (role == IsEditedRole) {
if (auto e = eventCast<const RoomMessageEvent>(&evt)) {
return !e->unsignedJson().isEmpty() && e->unsignedJson().contains("m.relations")
&& e->unsignedJson()["m.relations"].toObject().contains("m.replace");
}
return false;
}
if (role == EventIdRole) {
return !evt.id().isEmpty() ? evt.id() : evt.transactionId();
}

View File

@@ -63,7 +63,6 @@ public:
ShowReadMarkersRole, /**< bool with whether there are any other user read markers to be shown. */
ReactionRole,
IsEditedRole,
SourceRole,
MediaUrlRole,
// For debugging

View File

@@ -96,7 +96,6 @@ Item {
RichLabel {
textMessage: reply.display
textFormat: Text.RichText
isReplyLabel: true
HoverHandler {
enabled: !hoveredLink

View File

@@ -14,7 +14,6 @@ TextEdit {
readonly property var isEmoji: /^(<span style='.*'>)?(\u00a9|\u00ae|[\u20D0-\u2fff]|[\u3190-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])+(<\/span>)?$/
readonly property var hasSpoiler: /data-mx-spoiler/g
property bool isReplyLabel: false
property string textMessage: model.display
property bool spoilerRevealed: !hasSpoiler.test(textMessage)
@@ -59,7 +58,7 @@ a{
background: " + Kirigami.Theme.textColor + ";
}
" : "") + "
</style>" + textMessage + (isEdited && !contentLabel.isReplyLabel ? (" <span style=\"color: " + Kirigami.Theme.disabledTextColor + "\">" + "<span style='font-size: " + Kirigami.Theme.defaultFont.pixelSize +"px'>" + i18n(" (edited)") + "</span>") : "")
</style>" + textMessage
color: Kirigami.Theme.textColor
selectedTextColor: Kirigami.Theme.highlightedTextColor

View File

@@ -7,10 +7,13 @@
#include <QUrl>
#include <events/roommessageevent.h>
#include <qstringliteral.h>
#include <util.h>
#include <cmark.h>
#include <Kirigami/PlatformTheme>
static const QStringList allowedTags = {
QStringLiteral("font"), QStringLiteral("del"), QStringLiteral("h1"), QStringLiteral("h2"), QStringLiteral("h3"), QStringLiteral("h4"),
QStringLiteral("h5"), QStringLiteral("h6"), QStringLiteral("blockquote"), QStringLiteral("p"), QStringLiteral("a"), QStringLiteral("ul"),
@@ -150,6 +153,31 @@ QString TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat, const Neo
}
}
if (auto e = eventCast<const Quotient::RoomMessageEvent>(event)) {
bool isEdited =
!e->unsignedJson().isEmpty() && e->unsignedJson().contains("m.relations") && e->unsignedJson()["m.relations"].toObject().contains("m.replace");
if (isEdited) {
Kirigami::PlatformTheme *theme = static_cast<Kirigami::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::PlatformTheme>(this, true));
QString editTextColor;
if (theme != nullptr) {
editTextColor = theme->disabledTextColor().name();
} else {
editTextColor = QStringLiteral("#000000");
}
QString editedString = QStringLiteral(" <span style=\"color:") + editTextColor + QStringLiteral("\">(edited)</span>");
if (outputString.endsWith(QStringLiteral("</p>"))) {
outputString.insert(outputString.length() - 4, editedString);
} else if (outputString.endsWith(QStringLiteral("</pre>")) || outputString.endsWith(QStringLiteral("</blockquote>"))
|| outputString.endsWith(QStringLiteral("</table>")) || outputString.endsWith(QStringLiteral("</ol>"))
|| outputString.endsWith(QStringLiteral("</ul>"))) {
outputString.append("<p>" + editedString + "</p>");
} else {
outputString.append(editedString);
}
}
}
/**
* Replace <del> with <s>
* Note: <s> is still not a valid tag for the message from the server. We

View File

@@ -4,6 +4,7 @@
#pragma once
#include <QHash>
#include <QObject>
#include <QRegularExpression>
#include <QString>
#include <QStringList>
@@ -39,8 +40,10 @@ static const QRegularExpression mxId(QStringLiteral(R"((^|[][[:space:](){}`'";])
* be present as per the matrix spec
* (https://spec.matrix.org/v1.5/client-server-api/#mroommessage-msgtypes).
*/
class TextHandler
class TextHandler : public QObject
{
Q_OBJECT
public:
/**
* @brief List of token types