From 61ad8927325c8c994145c8611e7c26a8c4207f95 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 12 Nov 2023 18:46:04 +0000 Subject: [PATCH] Handle multiple line names Add function to get the display name for an author on a single line as nothing stops there being linebreaks. BUG:476731 --- autotests/data/test-eventhandler-sync.json | 29 ++++++++++++++++++++++ autotests/eventhandlertest.cpp | 19 ++++++++++++++ src/eventhandler.cpp | 22 ++++++++++++++++ src/eventhandler.h | 11 ++++++++ src/neochatroom.cpp | 2 +- 5 files changed, 82 insertions(+), 1 deletion(-) diff --git a/autotests/data/test-eventhandler-sync.json b/autotests/data/test-eventhandler-sync.json index 827089cf4..f6f79bd0a 100644 --- a/autotests/data/test-eventhandler-sync.json +++ b/autotests/data/test-eventhandler-sync.json @@ -135,6 +135,21 @@ "unsigned": { "age": 1234 } + }, + { + "content": { + "displayname": "Look\nat\nme\nI\nput\nnewlines\nin\nmy\ndisplay name", + "membership": "join" + }, + "event_id": "$143273582443PhrSh:example.org", + "origin_server_ts": 1432735824659, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@newline:example.org", + "state_key": "@newline:example.org", + "type": "m.room.member", + "unsigned": { + "age": 12345 + } } ] }, @@ -373,6 +388,20 @@ "unsigned": { "age": 1238 } + }, + { + "content": { + "body": "A message from someone who thought it was a good idea to put newlines in their display name.", + "msgtype": "m.text" + }, + "event_id": "$153456889:example.org", + "origin_server_ts": 14327358246589, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@newline:example.org", + "type": "m.room.message", + "unsigned": { + "age": 1230 + } } ], "limited": true, diff --git a/autotests/eventhandlertest.cpp b/autotests/eventhandlertest.cpp index 6a6bafd1c..818049de7 100644 --- a/autotests/eventhandlertest.cpp +++ b/autotests/eventhandlertest.cpp @@ -55,6 +55,8 @@ private Q_SLOTS: void nullAuthor(); void authorDisplayName(); void nullAuthorDisplayName(); + void singleLineSidplayName(); + void nullSingleLineDisplayName(); void time(); void nullTime(); void timeString(); @@ -203,6 +205,23 @@ void EventHandlerTest::nullAuthorDisplayName() QCOMPARE(noEventHandler.getAuthorDisplayName(), QString()); } +void EventHandlerTest::singleLineSidplayName() +{ + auto event = room->messageEvents().at(11).get(); + eventHandler.setEvent(event); + + QCOMPARE(eventHandler.singleLineAuthorDisplayname(), QStringLiteral("Look at me I put newlines in my display name")); +} + +void EventHandlerTest::nullSingleLineDisplayName() +{ + QTest::ignoreMessage(QtWarningMsg, "getAuthorDisplayName called with m_room set to nullptr."); + QCOMPARE(emptyHandler.singleLineAuthorDisplayname(), QString()); + + QTest::ignoreMessage(QtWarningMsg, "getAuthorDisplayName called with m_event set to nullptr."); + QCOMPARE(noEventHandler.singleLineAuthorDisplayname(), QString()); +} + void EventHandlerTest::time() { auto event = room->messageEvents().at(0).get(); diff --git a/src/eventhandler.cpp b/src/eventhandler.cpp index b27d6bd3d..c518f30a1 100644 --- a/src/eventhandler.cpp +++ b/src/eventhandler.cpp @@ -169,6 +169,28 @@ QString EventHandler::getAuthorDisplayName(bool isPending) const } } +QString EventHandler::singleLineAuthorDisplayname(bool isPending) const +{ + if (m_room == nullptr) { + qCWarning(EventHandling) << "getAuthorDisplayName called with m_room set to nullptr."; + return {}; + } + if (m_event == nullptr) { + qCWarning(EventHandling) << "getAuthorDisplayName called with m_event set to nullptr."; + return {}; + } + + const auto author = isPending ? m_room->localUser() : m_room->user(m_event->senderId()); + auto displayName = m_room->htmlSafeMemberName(author->id()); + displayName.replace(QStringLiteral("
\n"), QStringLiteral(" ")); + displayName.replace(QStringLiteral("
"), QStringLiteral(" ")); + displayName.replace(QStringLiteral("
\n"), QStringLiteral(" ")); + displayName.replace(QStringLiteral("
"), QStringLiteral(" ")); + displayName.replace(u'\n', QStringLiteral(" ")); + displayName.replace(u'\u2028', QStringLiteral(" ")); + return displayName; +} + QDateTime EventHandler::getTime(bool isPending, QDateTime lastUpdated) const { if (m_event == nullptr) { diff --git a/src/eventhandler.h b/src/eventhandler.h index fb4003bf2..edd619cd2 100644 --- a/src/eventhandler.h +++ b/src/eventhandler.h @@ -111,6 +111,17 @@ public: */ QString getAuthorDisplayName(bool isPending = false) const; + /** + * @brief Get the display name of the event author but with any newlines removed. + * + * Turns out you can put newlines in your display name so we need to handle that + * primarily for the room list subtitle. + * + * @param isPending whether the event is pending as this cannot be derived from + * just the event object. + */ + QString singleLineAuthorDisplayname(bool isPending = false) const; + /** * @brief Return a QDateTime object for the event timestamp. */ diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index f2de2b987..062c0671d 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -355,7 +355,7 @@ QString NeoChatRoom::lastEventToString(Qt::TextFormat format, bool stripNewlines body = eventHandler.getPlainBody(stripNewlines); } - return safeMemberName(event->senderId()) + (event->isStateEvent() ? QLatin1String(" ") : QLatin1String(": ")) + body; + return eventHandler.singleLineAuthorDisplayname() + (event->isStateEvent() ? QLatin1String(" ") : QLatin1String(": ")) + body; } return {}; }