Move the function to get a subtitle string to eventhandler

This commit is contained in:
James Graham
2023-12-10 13:19:13 +00:00
parent 4751ac6acc
commit 8d717b78ac
7 changed files with 43 additions and 44 deletions

View File

@@ -61,6 +61,8 @@ private Q_SLOTS:
void genericBody_data(); void genericBody_data();
void genericBody(); void genericBody();
void nullGenericBody(); void nullGenericBody();
void subtitle();
void nullSubtitle();
void mediaInfo(); void mediaInfo();
void nullMediaInfo(); void nullMediaInfo();
void linkPreviewer(); void linkPreviewer();
@@ -348,6 +350,25 @@ void EventHandlerTest::nullGenericBody()
QCOMPARE(noEventHandler.getGenericBody(), QString()); 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() void EventHandlerTest::mediaInfo()
{ {
auto event = room->messageEvents().at(4).get(); auto event = room->messageEvents().at(4).get();

View File

@@ -22,7 +22,6 @@ private:
private Q_SLOTS: private Q_SLOTS:
void initTestCase(); void initTestCase();
void subtitleTextTest();
void eventTest(); void eventTest();
}; };
@@ -32,12 +31,6 @@ void NeoChatRoomTest::initTestCase()
room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), "test-min-sync.json"_ls); 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() void NeoChatRoomTest::eventTest()
{ {
QCOMPARE(room->timelineSize(), 1); QCOMPARE(room->timelineSize(), 1);

View File

@@ -645,6 +645,15 @@ QString EventHandler::getGenericBody() const
i18n("Unknown event")); 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 QVariantMap EventHandler::getMediaInfo() const
{ {
if (m_room == nullptr) { if (m_room == nullptr) {

View File

@@ -206,6 +206,14 @@ public:
*/ */
QString getGenericBody() const; 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. * @brief Return the media info for the event.
* *

View File

@@ -4,6 +4,7 @@
#include "roomlistmodel.h" #include "roomlistmodel.h"
#include "controller.h" #include "controller.h"
#include "eventhandler.h"
#include "neochatconfig.h" #include "neochatconfig.h"
#include "neochatroom.h" #include "neochatroom.h"
#include "roommanager.h" #include "roommanager.h"
@@ -351,7 +352,10 @@ QVariant RoomListModel::data(const QModelIndex &index, int role) const
if (room->lastEventIsSpoiler()) { if (room->lastEventIsSpoiler()) {
return QString(); return QString();
} }
return room->lastEventToString(Qt::PlainText, true); EventHandler eventHandler;
eventHandler.setRoom(room);
eventHandler.setEvent(room->lastEvent());
return eventHandler.subtitleText();
} }
if (role == AvatarImageRole) { if (role == AvatarImageRole) {
return room->avatar(128); return room->avatar(128);

View File

@@ -341,25 +341,6 @@ bool NeoChatRoom::lastEventIsSpoiler() const
return false; 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 bool NeoChatRoom::isEventHighlighted(const RoomEvent *e) const
{ {
return highlights.contains(e); return highlights.contains(e);

View File

@@ -398,14 +398,6 @@ public:
*/ */
[[nodiscard]] const Quotient::RoomEvent *lastEvent() const; [[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. * @brief Convenient way to check if the last event looks like it has spoilers.
* *
@@ -492,15 +484,6 @@ public:
[[nodiscard]] bool readMarkerLoaded() const; [[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; [[nodiscard]] QString avatarMediaId() const;
Quotient::User *directChatRemoteUser() const; Quotient::User *directChatRemoteUser() const;