diff --git a/autotests/eventhandlertest.cpp b/autotests/eventhandlertest.cpp index d1101f014..940c7e396 100644 --- a/autotests/eventhandlertest.cpp +++ b/autotests/eventhandlertest.cpp @@ -61,6 +61,8 @@ private Q_SLOTS: void genericBody_data(); void genericBody(); void nullGenericBody(); + void subtitle(); + void nullSubtitle(); void mediaInfo(); void nullMediaInfo(); void linkPreviewer(); @@ -348,6 +350,25 @@ void EventHandlerTest::nullGenericBody() QCOMPARE(noEventHandler.getGenericBody(), QString()); } +void EventHandlerTest::subtitle() +{ + auto event = room->messageEvents().at(0).get(); + eventHandler.setEvent(event); + + QCOMPARE(eventHandler.subtitleText(), QStringLiteral("after: This is an example text message")); + + event = room->messageEvents().at(2).get(); + eventHandler.setEvent(event); + + QCOMPARE(eventHandler.subtitleText(), QStringLiteral("after: This is a highlight @bob:kde.org and this is a link https://kde.org")); +} + +void EventHandlerTest::nullSubtitle() +{ + QTest::ignoreMessage(QtWarningMsg, "subtitleText called with m_event set to nullptr."); + QCOMPARE(noEventHandler.subtitleText(), QString()); +} + void EventHandlerTest::mediaInfo() { auto event = room->messageEvents().at(4).get(); diff --git a/autotests/neochatroomtest.cpp b/autotests/neochatroomtest.cpp index e52e652c8..d696665c8 100644 --- a/autotests/neochatroomtest.cpp +++ b/autotests/neochatroomtest.cpp @@ -22,7 +22,6 @@ private: private Q_SLOTS: void initTestCase(); - void subtitleTextTest(); void eventTest(); }; @@ -32,12 +31,6 @@ void NeoChatRoomTest::initTestCase() room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), "test-min-sync.json"_ls); } -void NeoChatRoomTest::subtitleTextTest() -{ - QCOMPARE(room->timelineSize(), 1); - QCOMPARE(room->lastEventToString(), QStringLiteral("@example:example.org: This is an example\ntext message")); -} - void NeoChatRoomTest::eventTest() { QCOMPARE(room->timelineSize(), 1); diff --git a/src/eventhandler.cpp b/src/eventhandler.cpp index 00e1fbeb5..2ab0898e0 100644 --- a/src/eventhandler.cpp +++ b/src/eventhandler.cpp @@ -645,6 +645,15 @@ QString EventHandler::getGenericBody() const i18n("Unknown event")); } +QString EventHandler::subtitleText() const +{ + if (m_event == nullptr) { + qCWarning(EventHandling) << "subtitleText called with m_event set to nullptr."; + return {}; + } + return singleLineAuthorDisplayname() + (m_event->isStateEvent() ? QLatin1String(" ") : QLatin1String(": ")) + getPlainBody(true); +} + QVariantMap EventHandler::getMediaInfo() const { if (m_room == nullptr) { diff --git a/src/eventhandler.h b/src/eventhandler.h index edd619cd2..5206edff4 100644 --- a/src/eventhandler.h +++ b/src/eventhandler.h @@ -206,6 +206,14 @@ public: */ QString getGenericBody() const; + /** + * @brief Output a string for the event to be used as a RoomList subtitle. + * + * The output includes the username followed by the plain message, all with no + * line breaks. + */ + QString subtitleText() const; + /** * @brief Return the media info for the event. * diff --git a/src/models/roomlistmodel.cpp b/src/models/roomlistmodel.cpp index 1b7b28d1b..1a58c8b49 100644 --- a/src/models/roomlistmodel.cpp +++ b/src/models/roomlistmodel.cpp @@ -4,6 +4,7 @@ #include "roomlistmodel.h" #include "controller.h" +#include "eventhandler.h" #include "neochatconfig.h" #include "neochatroom.h" #include "roommanager.h" @@ -351,7 +352,10 @@ QVariant RoomListModel::data(const QModelIndex &index, int role) const if (room->lastEventIsSpoiler()) { return QString(); } - return room->lastEventToString(Qt::PlainText, true); + EventHandler eventHandler; + eventHandler.setRoom(room); + eventHandler.setEvent(room->lastEvent()); + return eventHandler.subtitleText(); } if (role == AvatarImageRole) { return room->avatar(128); diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 6748ebc9e..baa514628 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -341,25 +341,6 @@ bool NeoChatRoom::lastEventIsSpoiler() const return false; } -QString NeoChatRoom::lastEventToString(Qt::TextFormat format, bool stripNewlines) const -{ - if (auto event = lastEvent()) { - EventHandler eventHandler; - eventHandler.setRoom(this); - eventHandler.setEvent(event); - - QString body; - if (format == Qt::TextFormat::RichText) { - body = eventHandler.getRichBody(stripNewlines); - } else { - body = eventHandler.getPlainBody(stripNewlines); - } - - return eventHandler.singleLineAuthorDisplayname() + (event->isStateEvent() ? QLatin1String(" ") : QLatin1String(": ")) + body; - } - return {}; -} - bool NeoChatRoom::isEventHighlighted(const RoomEvent *e) const { return highlights.contains(e); diff --git a/src/neochatroom.h b/src/neochatroom.h index bb68692f9..b435bee0b 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -398,14 +398,6 @@ public: */ [[nodiscard]] const Quotient::RoomEvent *lastEvent() const; - /** - * @brief Convenient way to call eventToString on the last event. - * - * @sa lastEvent() - * @sa eventToString() - */ - [[nodiscard]] QString lastEventToString(Qt::TextFormat format = Qt::PlainText, bool stripNewlines = false) const; - /** * @brief Convenient way to check if the last event looks like it has spoilers. * @@ -492,15 +484,6 @@ public: [[nodiscard]] bool readMarkerLoaded() const; - /** - * @brief Get subtitle text for room - * - * Fetches last event and removes markdown formatting - * - * @see lastEventToString() - */ - [[nodiscard]] QString subtitleText(); - [[nodiscard]] QString avatarMediaId() const; Quotient::User *directChatRemoteUser() const;