diff --git a/autotests/eventhandlertest.cpp b/autotests/eventhandlertest.cpp index de191d7f4..d7d8c2990 100644 --- a/autotests/eventhandlertest.cpp +++ b/autotests/eventhandlertest.cpp @@ -29,8 +29,6 @@ private: Connection *connection = nullptr; TestUtils::TestRoom *room = nullptr; - EventHandler emptyHandler = EventHandler(nullptr, nullptr); - private Q_SLOTS: void initTestCase(); @@ -43,7 +41,6 @@ private Q_SLOTS: void time(); void nullTime(); void timeString(); - void nullTimeString(); void highlighted(); void nullHighlighted(); void hidden(); @@ -65,10 +62,6 @@ private Q_SLOTS: void nullReplyId(); void replyAuthor(); void nullReplyAuthor(); - void replyBody(); - void nullReplyBody(); - void replyMediaInfo(); - void nullReplyMediaInfo(); void thread(); void nullThread(); void location(); @@ -83,157 +76,136 @@ void EventHandlerTest::initTestCase() void EventHandlerTest::eventId() { - EventHandler eventHandler(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandler.getId(), QStringLiteral("$153456789:example.org")); + QCOMPARE(EventHandler::id(room->messageEvents().at(0).get()), QStringLiteral("$153456789:example.org")); } void EventHandlerTest::nullEventId() { - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getId called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getId(), QString()); + QTest::ignoreMessage(QtWarningMsg, "id called with event set to nullptr."); + QCOMPARE(EventHandler::id(nullptr), QString()); } void EventHandlerTest::authorDisplayName() { - EventHandler eventHandler(room, room->messageEvents().at(1).get()); - QCOMPARE(eventHandler.getAuthorDisplayName(), QStringLiteral("before")); + QCOMPARE(EventHandler::authorDisplayName(room, room->messageEvents().at(1).get()), QStringLiteral("before")); } void EventHandlerTest::nullAuthorDisplayName() { - QTest::ignoreMessage(QtWarningMsg, "getAuthorDisplayName called with m_room set to nullptr."); - QCOMPARE(emptyHandler.getAuthorDisplayName(), QString()); + QTest::ignoreMessage(QtWarningMsg, "authorDisplayName called with room set to nullptr."); + QCOMPARE(EventHandler::authorDisplayName(nullptr, nullptr), QString()); - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getAuthorDisplayName called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getAuthorDisplayName(), QString()); + QTest::ignoreMessage(QtWarningMsg, "authorDisplayName called with event set to nullptr."); + QCOMPARE(EventHandler::authorDisplayName(room, nullptr), QString()); } void EventHandlerTest::singleLineSidplayName() { - EventHandler eventHandler(room, room->messageEvents().at(11).get()); - QCOMPARE(eventHandler.singleLineAuthorDisplayname(), QStringLiteral("Look at me I put newlines in my display name")); + QCOMPARE(EventHandler::singleLineAuthorDisplayname(room, room->messageEvents().at(11).get()), + 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, "singleLineAuthorDisplayname called with room set to nullptr."); + QCOMPARE(EventHandler::singleLineAuthorDisplayname(nullptr, nullptr), QString()); - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getAuthorDisplayName called with m_event set to nullptr."); - QCOMPARE(noEventHandler.singleLineAuthorDisplayname(), QString()); + QTest::ignoreMessage(QtWarningMsg, "singleLineAuthorDisplayname called with event set to nullptr."); + QCOMPARE(EventHandler::singleLineAuthorDisplayname(room, nullptr), QString()); } void EventHandlerTest::time() { - EventHandler eventHandler(room, room->messageEvents().at(0).get()); + const auto event = room->messageEvents().at(0).get(); - QCOMPARE(eventHandler.getTime(), QDateTime::fromMSecsSinceEpoch(1432735824654, Qt::UTC)); - QCOMPARE(eventHandler.getTime(true, QDateTime::fromMSecsSinceEpoch(1234, Qt::UTC)), QDateTime::fromMSecsSinceEpoch(1234, Qt::UTC)); + QCOMPARE(EventHandler::time(event), QDateTime::fromMSecsSinceEpoch(1432735824654, Qt::UTC)); + QCOMPARE(EventHandler::time(event, true, QDateTime::fromMSecsSinceEpoch(1234, Qt::UTC)), QDateTime::fromMSecsSinceEpoch(1234, Qt::UTC)); } void EventHandlerTest::nullTime() { - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getTime called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getTime(), QDateTime()); + QTest::ignoreMessage(QtWarningMsg, "time called with event set to nullptr."); + QCOMPARE(EventHandler::time(nullptr), QDateTime()); - EventHandler eventHandler(room, room->messageEvents().at(0).get()); QTest::ignoreMessage(QtWarningMsg, "a value must be provided for lastUpdated for a pending event."); - QCOMPARE(eventHandler.getTime(true), QDateTime()); + QCOMPARE(EventHandler::time(room->messageEvents().at(0).get(), true), QDateTime()); } void EventHandlerTest::timeString() { - EventHandler eventHandler(room, room->messageEvents().at(0).get()); + const auto event = room->messageEvents().at(0).get(); KFormat format; - QCOMPARE(eventHandler.getTimeString(false), + QCOMPARE(EventHandler::timeString(event, false), QLocale().toString(QDateTime::fromMSecsSinceEpoch(1432735824654, Qt::UTC).toLocalTime().time(), QLocale::ShortFormat)); - QCOMPARE(eventHandler.getTimeString(true), + QCOMPARE(EventHandler::timeString(event, true), format.formatRelativeDate(QDateTime::fromMSecsSinceEpoch(1432735824654, Qt::UTC).toLocalTime().date(), QLocale::ShortFormat)); - QCOMPARE(eventHandler.getTimeString(false, QLocale::ShortFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), + QCOMPARE(EventHandler::timeString(event, false, QLocale::ShortFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), QLocale().toString(QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC).toLocalTime().time(), QLocale::ShortFormat)); - QCOMPARE(eventHandler.getTimeString(true, QLocale::ShortFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), + QCOMPARE(EventHandler::timeString(event, true, QLocale::ShortFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), format.formatRelativeDate(QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC).toLocalTime().date(), QLocale::ShortFormat)); - QCOMPARE(eventHandler.getTimeString(false, QLocale::LongFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), + QCOMPARE(EventHandler::timeString(event, false, QLocale::LongFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), QLocale().toString(QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC).toLocalTime().time(), QLocale::LongFormat)); - QCOMPARE(eventHandler.getTimeString(true, QLocale::LongFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), + QCOMPARE(EventHandler::timeString(event, true, QLocale::LongFormat, true, QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC)), format.formatRelativeDate(QDateTime::fromMSecsSinceEpoch(1690699214545, Qt::UTC).toLocalTime().date(), QLocale::LongFormat)); - QCOMPARE(eventHandler.getTimeString(QStringLiteral("hh:mm")), QDateTime::fromMSecsSinceEpoch(1432735824654, Qt::UTC).toString(QStringLiteral("hh:mm"))); -} - -void EventHandlerTest::nullTimeString() -{ - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getTimeString called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getTimeString(false), QString()); - - EventHandler eventHandler(room, room->messageEvents().at(0).get()); - QTest::ignoreMessage(QtWarningMsg, "a value must be provided for lastUpdated for a pending event."); - QCOMPARE(eventHandler.getTimeString(false, QLocale::ShortFormat, true), QString()); + QCOMPARE(EventHandler::timeString(event, QStringLiteral("hh:mm")), + QDateTime::fromMSecsSinceEpoch(1432735824654, Qt::UTC).toString(QStringLiteral("hh:mm"))); } void EventHandlerTest::highlighted() { - EventHandler eventHandlerHighlight(room, room->messageEvents().at(2).get()); - QCOMPARE(eventHandlerHighlight.isHighlighted(), true); - - EventHandler eventHandlerNoHighlight(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandlerNoHighlight.isHighlighted(), false); + QCOMPARE(EventHandler::isHighlighted(room, room->messageEvents().at(2).get()), true); + QCOMPARE(EventHandler::isHighlighted(room, room->messageEvents().at(0).get()), false); } void EventHandlerTest::nullHighlighted() { - QTest::ignoreMessage(QtWarningMsg, "isHighlighted called with m_room set to nullptr."); - QCOMPARE(emptyHandler.isHighlighted(), false); + QTest::ignoreMessage(QtWarningMsg, "isHighlighted called with room set to nullptr."); + QCOMPARE(EventHandler::isHighlighted(nullptr, nullptr), false); - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "isHighlighted called with m_event set to nullptr."); - QCOMPARE(noEventHandler.isHighlighted(), false); + QTest::ignoreMessage(QtWarningMsg, "isHighlighted called with event set to nullptr."); + QCOMPARE(EventHandler::isHighlighted(room, nullptr), false); } void EventHandlerTest::hidden() { - EventHandler eventHandlerHidden(room, room->messageEvents().at(3).get()); - QCOMPARE(eventHandlerHidden.isHidden(), true); - - EventHandler eventHandlerNoHidden(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandlerNoHidden.isHidden(), false); + QCOMPARE(EventHandler::isHidden(room, room->messageEvents().at(3).get()), true); + QCOMPARE(EventHandler::isHidden(room, room->messageEvents().at(0).get()), false); } void EventHandlerTest::nullHidden() { - QTest::ignoreMessage(QtWarningMsg, "isHidden called with m_room set to nullptr."); - QCOMPARE(emptyHandler.isHidden(), false); + QTest::ignoreMessage(QtWarningMsg, "isHidden called with room set to nullptr."); + QCOMPARE(EventHandler::isHidden(nullptr, nullptr), false); - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "isHidden called with m_event set to nullptr."); - QCOMPARE(noEventHandler.isHidden(), false); + QTest::ignoreMessage(QtWarningMsg, "isHidden called with event set to nullptr."); + QCOMPARE(EventHandler::isHidden(room, nullptr), false); } void EventHandlerTest::body() { - EventHandler eventHandler(room, room->messageEvents().at(0).get()); + const auto event = room->messageEvents().at(0).get(); - QCOMPARE(eventHandler.getRichBody(), QStringLiteral("This is an example
text message
")); - QCOMPARE(eventHandler.getRichBody(true), QStringLiteral("This is an example text message")); - QCOMPARE(eventHandler.getPlainBody(), QStringLiteral("This is an example\ntext message")); - QCOMPARE(eventHandler.getPlainBody(true), QStringLiteral("This is an example text message")); + QCOMPARE(EventHandler::richBody(room, event), QStringLiteral("This is an example
text message
")); + QCOMPARE(EventHandler::richBody(room, event, true), QStringLiteral("This is an example text message")); + QCOMPARE(EventHandler::plainBody(room, event), QStringLiteral("This is an example\ntext message")); + QCOMPARE(EventHandler::plainBody(room, event, true), QStringLiteral("This is an example text message")); } void EventHandlerTest::nullBody() { - EventHandler noEventHandler(room, nullptr); + QTest::ignoreMessage(QtWarningMsg, "richBody called with room set to nullptr."); + QCOMPARE(EventHandler::richBody(nullptr, nullptr), QString()); - QTest::ignoreMessage(QtWarningMsg, "getRichBody called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getRichBody(), QString()); + QTest::ignoreMessage(QtWarningMsg, "richBody called with event set to nullptr."); + QCOMPARE(EventHandler::richBody(room, nullptr), QString()); - QTest::ignoreMessage(QtWarningMsg, "getPlainBody called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getPlainBody(), QString()); + QTest::ignoreMessage(QtWarningMsg, "plainBody called with room set to nullptr."); + QCOMPARE(EventHandler::plainBody(nullptr, nullptr), QString()); + + QTest::ignoreMessage(QtWarningMsg, "plainBody called with event set to nullptr."); + QCOMPARE(EventHandler::plainBody(room, nullptr), QString()); } void EventHandlerTest::genericBody_data() @@ -253,54 +225,45 @@ void EventHandlerTest::genericBody() QFETCH(int, eventNum); QFETCH(QString, output); - EventHandler eventHandler(room, room->messageEvents().at(eventNum).get()); - - QCOMPARE(eventHandler.getGenericBody(), output); + QCOMPARE(EventHandler::genericBody(room->messageEvents().at(eventNum).get()), output); } void EventHandlerTest::nullGenericBody() { - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getGenericBody called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getGenericBody(), QString()); + QTest::ignoreMessage(QtWarningMsg, "genericBody called with event set to nullptr."); + QCOMPARE(EventHandler::genericBody(nullptr), QString()); } void EventHandlerTest::markdownBody() { - EventHandler eventHandler(room, room->messageEvents().at(0).get()); - - QCOMPARE(eventHandler.getMarkdownBody(), QStringLiteral("This is an example\ntext message")); + QCOMPARE(EventHandler::markdownBody(room->messageEvents().at(0).get()), QStringLiteral("This is an example\ntext message")); } void EventHandlerTest::markdownBodyReply() { - EventHandler eventHandler(room, room->messageEvents().at(5).get()); - - QCOMPARE(eventHandler.getMarkdownBody(), QStringLiteral("reply")); + QCOMPARE(EventHandler::markdownBody(room->messageEvents().at(5).get()), QStringLiteral("reply")); } void EventHandlerTest::subtitle() { - EventHandler eventHandler(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandler.subtitleText(), QStringLiteral("after: This is an example text message")); - - EventHandler eventHandler2(room, room->messageEvents().at(2).get()); - QCOMPARE(eventHandler2.subtitleText(), QStringLiteral("after: This is a highlight @bob:kde.org and this is a link https://kde.org")); + QCOMPARE(EventHandler::subtitleText(room, room->messageEvents().at(0).get()), QStringLiteral("after: This is an example text message")); + QCOMPARE(EventHandler::subtitleText(room, room->messageEvents().at(2).get()), + QStringLiteral("after: This is a highlight @bob:kde.org and this is a link https://kde.org")); } void EventHandlerTest::nullSubtitle() { - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "subtitleText called with m_event set to nullptr."); - QCOMPARE(noEventHandler.subtitleText(), QString()); + QTest::ignoreMessage(QtWarningMsg, "subtitleText called with room set to nullptr."); + QCOMPARE(EventHandler::subtitleText(nullptr, nullptr), QString()); + + QTest::ignoreMessage(QtWarningMsg, "subtitleText called with event set to nullptr."); + QCOMPARE(EventHandler::subtitleText(room, nullptr), QString()); } void EventHandlerTest::mediaInfo() { auto event = room->messageEvents().at(4).get(); - EventHandler eventHandler(room, event); - - auto mediaInfo = eventHandler.getMediaInfo(); + auto mediaInfo = EventHandler::mediaInfo(room, event); auto thumbnailInfo = mediaInfo["tempInfo"_ls].toMap(); QCOMPARE(mediaInfo["source"_ls], room->makeMediaUrl(event->id(), QUrl("mxc://kde.org/1234567"_ls))); @@ -320,53 +283,42 @@ void EventHandlerTest::mediaInfo() void EventHandlerTest::nullMediaInfo() { - QTest::ignoreMessage(QtWarningMsg, "getMediaInfo called with m_room set to nullptr."); - QCOMPARE(emptyHandler.getMediaInfo(), QVariantMap()); + QTest::ignoreMessage(QtWarningMsg, "mediaInfo called with room set to nullptr."); + QCOMPARE(EventHandler::mediaInfo(nullptr, nullptr), QVariantMap()); - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getMediaInfo called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getMediaInfo(), QVariantMap()); + QTest::ignoreMessage(QtWarningMsg, "mediaInfo called with event set to nullptr."); + QCOMPARE(EventHandler::mediaInfo(room, nullptr), QVariantMap()); } void EventHandlerTest::hasReply() { - EventHandler eventHandlerReply(room, room->messageEvents().at(5).get()); - QCOMPARE(eventHandlerReply.hasReply(), true); - - EventHandler eventHandlerNoReply(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandlerNoReply.hasReply(), false); + QCOMPARE(EventHandler::hasReply(room->messageEvents().at(5).get()), true); + QCOMPARE(EventHandler::hasReply(room->messageEvents().at(0).get()), false); } void EventHandlerTest::nullHasReply() { - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "hasReply called with m_event set to nullptr."); - QCOMPARE(noEventHandler.hasReply(), false); + QTest::ignoreMessage(QtWarningMsg, "hasReply called with event set to nullptr."); + QCOMPARE(EventHandler::hasReply(nullptr), false); } void EventHandlerTest::replyId() { - EventHandler eventHandlerReply(room, room->messageEvents().at(5).get()); - QCOMPARE(eventHandlerReply.getReplyId(), QStringLiteral("$153456789:example.org")); - - EventHandler eventHandlerNoReply(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandlerNoReply.getReplyId(), QStringLiteral("")); + QCOMPARE(EventHandler::replyId(room->messageEvents().at(5).get()), QStringLiteral("$153456789:example.org")); + QCOMPARE(EventHandler::replyId(room->messageEvents().at(0).get()), QStringLiteral("")); } void EventHandlerTest::nullReplyId() { - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getReplyId called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getReplyId(), QString()); + QTest::ignoreMessage(QtWarningMsg, "replyId called with event set to nullptr."); + QCOMPARE(EventHandler::replyId(nullptr), QString()); } void EventHandlerTest::replyAuthor() { auto replyEvent = room->messageEvents().at(0).get(); auto replyAuthor = room->member(replyEvent->senderId()); - EventHandler eventHandler(room, room->messageEvents().at(5).get()); - - auto eventHandlerReplyAuthor = eventHandler.getReplyAuthor(); + auto eventHandlerReplyAuthor = EventHandler::replyAuthor(room, room->messageEvents().at(5).get()); QCOMPARE(eventHandlerReplyAuthor.isLocalMember(), replyAuthor.id() == room->localMember().id()); QCOMPARE(eventHandlerReplyAuthor.id(), replyAuthor.id()); @@ -375,121 +327,58 @@ void EventHandlerTest::replyAuthor() QCOMPARE(eventHandlerReplyAuthor.avatarMediaId(), replyAuthor.avatarMediaId()); QCOMPARE(eventHandlerReplyAuthor.color(), replyAuthor.color()); - EventHandler eventHandlerNoAuthor(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandlerNoAuthor.getReplyAuthor(), RoomMember()); + QCOMPARE(EventHandler::replyAuthor(room, room->messageEvents().at(0).get()), RoomMember()); } void EventHandlerTest::nullReplyAuthor() { - QTest::ignoreMessage(QtWarningMsg, "getReplyAuthor called with m_room set to nullptr."); - QCOMPARE(emptyHandler.getReplyAuthor(), RoomMember()); + QTest::ignoreMessage(QtWarningMsg, "replyAuthor called with room set to nullptr."); + QCOMPARE(EventHandler::replyAuthor(nullptr, nullptr), RoomMember()); - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getReplyAuthor called with m_event set to nullptr. Returning empty user."); - QCOMPARE(noEventHandler.getReplyAuthor(), RoomMember()); -} - -void EventHandlerTest::replyBody() -{ - EventHandler eventHandler(room, room->messageEvents().at(5).get()); - - QCOMPARE(eventHandler.getReplyRichBody(), QStringLiteral("This is an example
text message
")); - QCOMPARE(eventHandler.getReplyRichBody(true), QStringLiteral("This is an example text message")); - QCOMPARE(eventHandler.getReplyPlainBody(), QStringLiteral("This is an example\ntext message")); - QCOMPARE(eventHandler.getReplyPlainBody(true), QStringLiteral("This is an example text message")); -} - -void EventHandlerTest::nullReplyBody() -{ - EventHandler noEventHandler(room, nullptr); - - QTest::ignoreMessage(QtWarningMsg, "getReplyRichBody called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getReplyRichBody(), QString()); - - QTest::ignoreMessage(QtWarningMsg, "getReplyPlainBody called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getReplyPlainBody(), QString()); -} - -void EventHandlerTest::replyMediaInfo() -{ - auto event = room->messageEvents().at(6).get(); - auto replyEvent = room->messageEvents().at(4).get(); - EventHandler eventHandler(room, event); - - auto mediaInfo = eventHandler.getReplyMediaInfo(); - auto thumbnailInfo = mediaInfo["tempInfo"_ls].toMap(); - - QCOMPARE(mediaInfo["source"_ls], room->makeMediaUrl(replyEvent->id(), QUrl("mxc://kde.org/1234567"_ls))); - QCOMPARE(mediaInfo["mimeType"_ls], QStringLiteral("video/mp4")); - QCOMPARE(mediaInfo["mimeIcon"_ls], QStringLiteral("video-mp4")); - QCOMPARE(mediaInfo["size"_ls], 62650636); - QCOMPARE(mediaInfo["duration"_ls], 10); - QCOMPARE(mediaInfo["width"_ls], 1920); - QCOMPARE(mediaInfo["height"_ls], 1080); - QCOMPARE(thumbnailInfo["source"_ls], room->makeMediaUrl(replyEvent->id(), QUrl("mxc://kde.org/2234567"_ls))); - QCOMPARE(thumbnailInfo["mimeType"_ls], QStringLiteral("image/jpeg")); - QCOMPARE(thumbnailInfo["mimeIcon"_ls], QStringLiteral("image-jpeg")); - QCOMPARE(thumbnailInfo["size"_ls], 382249); - QCOMPARE(thumbnailInfo["width"_ls], 800); - QCOMPARE(thumbnailInfo["height"_ls], 450); -} - -void EventHandlerTest::nullReplyMediaInfo() -{ - QTest::ignoreMessage(QtWarningMsg, "getReplyMediaInfo called with m_room set to nullptr."); - QCOMPARE(emptyHandler.getReplyMediaInfo(), QVariantMap()); - - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "getReplyMediaInfo called with m_event set to nullptr."); - QCOMPARE(noEventHandler.getReplyMediaInfo(), QVariantMap()); + QTest::ignoreMessage(QtWarningMsg, "replyAuthor called with event set to nullptr. Returning empty user."); + QCOMPARE(EventHandler::replyAuthor(room, nullptr), RoomMember()); } void EventHandlerTest::thread() { - EventHandler eventHandlerNoThread(room, room->messageEvents().at(0).get()); - QCOMPARE(eventHandlerNoThread.isThreaded(), false); - QCOMPARE(eventHandlerNoThread.threadRoot(), QString()); + QCOMPARE(EventHandler::isThreaded(room->messageEvents().at(0).get()), false); + QCOMPARE(EventHandler::threadRoot(room->messageEvents().at(0).get()), QString()); - EventHandler eventHandlerThreadRoot(room, room->messageEvents().at(9).get()); - QCOMPARE(eventHandlerThreadRoot.isThreaded(), true); - QCOMPARE(eventHandlerThreadRoot.threadRoot(), QStringLiteral("$threadroot:example.org")); - QCOMPARE(eventHandlerThreadRoot.getReplyId(), QStringLiteral("$threadroot:example.org")); + QCOMPARE(EventHandler::isThreaded(room->messageEvents().at(9).get()), true); + QCOMPARE(EventHandler::threadRoot(room->messageEvents().at(9).get()), QStringLiteral("$threadroot:example.org")); + QCOMPARE(EventHandler::replyId(room->messageEvents().at(9).get()), QStringLiteral("$threadroot:example.org")); - EventHandler eventHandlerThreadReply(room, room->messageEvents().at(10).get()); - QCOMPARE(eventHandlerThreadReply.isThreaded(), true); - QCOMPARE(eventHandlerThreadReply.threadRoot(), QStringLiteral("$threadroot:example.org")); - QCOMPARE(eventHandlerThreadReply.getReplyId(), QStringLiteral("$threadmessage1:example.org")); + QCOMPARE(EventHandler::isThreaded(room->messageEvents().at(10).get()), true); + QCOMPARE(EventHandler::threadRoot(room->messageEvents().at(10).get()), QStringLiteral("$threadroot:example.org")); + QCOMPARE(EventHandler::replyId(room->messageEvents().at(10).get()), QStringLiteral("$threadmessage1:example.org")); } void EventHandlerTest::nullThread() { - QTest::ignoreMessage(QtWarningMsg, "isThreaded called with m_event set to nullptr."); - QCOMPARE(emptyHandler.isThreaded(), false); + QTest::ignoreMessage(QtWarningMsg, "isThreaded called with event set to nullptr."); + QCOMPARE(EventHandler::isThreaded(nullptr), false); - EventHandler noEventHandler(room, nullptr); - QTest::ignoreMessage(QtWarningMsg, "threadRoot called with m_event set to nullptr."); - QCOMPARE(noEventHandler.threadRoot(), QString()); + QTest::ignoreMessage(QtWarningMsg, "threadRoot called with event set to nullptr."); + QCOMPARE(EventHandler::threadRoot(nullptr), QString()); } void EventHandlerTest::location() { - EventHandler eventHandler(room, room->messageEvents().at(7).get()); - - QCOMPARE(eventHandler.getLatitude(), QStringLiteral("51.7035").toFloat()); - QCOMPARE(eventHandler.getLongitude(), QStringLiteral("-1.14394").toFloat()); - QCOMPARE(eventHandler.getLocationAssetType(), QStringLiteral("m.pin")); + QCOMPARE(EventHandler::latitude(room->messageEvents().at(7).get()), QStringLiteral("51.7035").toFloat()); + QCOMPARE(EventHandler::longitude(room->messageEvents().at(7).get()), QStringLiteral("-1.14394").toFloat()); + QCOMPARE(EventHandler::locationAssetType(room->messageEvents().at(7).get()), QStringLiteral("m.pin")); } void EventHandlerTest::nullLocation() { - QTest::ignoreMessage(QtWarningMsg, "getLatitude called with m_event set to nullptr."); - QCOMPARE(emptyHandler.getLatitude(), -100.0); + QTest::ignoreMessage(QtWarningMsg, "latitude called with event set to nullptr."); + QCOMPARE(EventHandler::latitude(nullptr), -100.0); - QTest::ignoreMessage(QtWarningMsg, "getLongitude called with m_event set to nullptr."); - QCOMPARE(emptyHandler.getLongitude(), -200.0); + QTest::ignoreMessage(QtWarningMsg, "longitude called with event set to nullptr."); + QCOMPARE(EventHandler::longitude(nullptr), -200.0); - QTest::ignoreMessage(QtWarningMsg, "getLocationAssetType called with m_event set to nullptr."); - QCOMPARE(emptyHandler.getLocationAssetType(), QString()); + QTest::ignoreMessage(QtWarningMsg, "locationAssetType called with event set to nullptr."); + QCOMPARE(EventHandler::locationAssetType(nullptr), QString()); } QTEST_MAIN(EventHandlerTest) diff --git a/src/chatbarcache.cpp b/src/chatbarcache.cpp index 95fbd38f9..a0a6981dc 100644 --- a/src/chatbarcache.cpp +++ b/src/chatbarcache.cpp @@ -119,8 +119,7 @@ QString ChatBarCache::relationMessage() const } if (auto event = room->findInTimeline(m_relationId); event != room->historyEdge()) { - EventHandler eventhandler(room, &**event); - return eventhandler.getMarkdownBody(); + return EventHandler::markdownBody(&**event); } return {}; } diff --git a/src/eventhandler.cpp b/src/eventhandler.cpp index e5eb71b63..d62645cf8 100644 --- a/src/eventhandler.cpp +++ b/src/eventhandler.cpp @@ -5,13 +5,16 @@ #include +#include #include #include +#include #include #include #include #include +#include #include #include #include @@ -23,9 +26,6 @@ #include "events/locationbeaconevent.h" #include "events/pollevent.h" #include "events/widgetevent.h" -#include "linkpreviewer.h" -#include "messagecomponenttype.h" -#include "models/reactionmodel.h" #include "neochatconfig.h" #include "neochatroom.h" #include "texthandler.h" @@ -33,68 +33,52 @@ using namespace Quotient; -EventHandler::EventHandler(const NeoChatRoom *room, const RoomEvent *event) - : m_room(room) - , m_event(event) +QString EventHandler::id(const Quotient::RoomEvent *event) { -} - -QString EventHandler::getId() const -{ - if (m_event == nullptr) { - qCWarning(EventHandling) << "getId called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "id called with event set to nullptr."; return {}; } - return !m_event->id().isEmpty() ? m_event->id() : m_event->transactionId(); + return !event->id().isEmpty() ? event->id() : event->transactionId(); } -MessageComponentType::Type EventHandler::messageComponentType() const +QString EventHandler::authorDisplayName(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool isPending) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "messageComponentType called with m_event set to nullptr."; - return MessageComponentType::Other; - } - - return MessageComponentType::typeForEvent(*m_event); -} - -QString EventHandler::getAuthorDisplayName(bool isPending) const -{ - if (m_room == nullptr) { - qCWarning(EventHandling) << "getAuthorDisplayName called with m_room set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "authorDisplayName called with room set to nullptr."; return {}; } - if (m_event == nullptr) { - qCWarning(EventHandling) << "getAuthorDisplayName called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "authorDisplayName called with event set to nullptr."; return {}; } - if (is(*m_event) && !m_event->unsignedJson()[QStringLiteral("prev_content")][QStringLiteral("displayname")].isNull() - && m_event->stateKey() == m_event->senderId()) { - auto previousDisplayName = m_event->unsignedJson()[QStringLiteral("prev_content")][QStringLiteral("displayname")].toString().toHtmlEscaped(); + if (is(*event) && !event->unsignedJson()[QStringLiteral("prev_content")][QStringLiteral("displayname")].isNull() + && event->stateKey() == event->senderId()) { + auto previousDisplayName = event->unsignedJson()[QStringLiteral("prev_content")][QStringLiteral("displayname")].toString().toHtmlEscaped(); if (previousDisplayName.isEmpty()) { - previousDisplayName = m_event->senderId(); + previousDisplayName = event->senderId(); } return previousDisplayName; } else { - const auto author = isPending ? m_room->localMember() : m_room->member(m_event->senderId()); + const auto author = isPending ? room->localMember() : room->member(event->senderId()); return author.htmlSafeDisplayName(); } } -QString EventHandler::singleLineAuthorDisplayname(bool isPending) const +QString EventHandler::singleLineAuthorDisplayname(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool isPending) { - if (m_room == nullptr) { - qCWarning(EventHandling) << "getAuthorDisplayName called with m_room set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "singleLineAuthorDisplayname called with room set to nullptr."; return {}; } - if (m_event == nullptr) { - qCWarning(EventHandling) << "getAuthorDisplayName called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "singleLineAuthorDisplayname called with event set to nullptr."; return {}; } - const auto author = isPending ? m_room->localMember() : m_room->member(m_event->senderId()); + const auto author = isPending ? room->localMember() : room->member(event->senderId()); auto displayName = author.displayName(); displayName.replace(QStringLiteral("
\n"), QStringLiteral(" ")); displayName.replace(QStringLiteral("
"), QStringLiteral(" ")); @@ -105,10 +89,10 @@ QString EventHandler::singleLineAuthorDisplayname(bool isPending) const return displayName; } -QDateTime EventHandler::getTime(bool isPending, QDateTime lastUpdated) const +QDateTime EventHandler::time(const Quotient::RoomEvent *event, bool isPending, QDateTime lastUpdated) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getTime called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "time called with event set to nullptr."; return {}; } if (isPending && lastUpdated == QDateTime()) { @@ -116,24 +100,16 @@ QDateTime EventHandler::getTime(bool isPending, QDateTime lastUpdated) const return {}; } - return isPending ? lastUpdated : m_event->originTimestamp(); + return isPending ? lastUpdated : event->originTimestamp(); } -QString EventHandler::getTimeString(bool relative, QLocale::FormatType format, bool isPending, QDateTime lastUpdated) const +QString EventHandler::timeString(const Quotient::RoomEvent *event, bool relative, QLocale::FormatType format, bool isPending, QDateTime lastUpdated) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getTimeString called with m_event set to nullptr."; - return {}; - } - if (isPending && lastUpdated == QDateTime()) { - qCWarning(EventHandling) << "a value must be provided for lastUpdated for a pending event."; - return {}; - } - - auto ts = getTime(isPending, lastUpdated); + auto ts = time(event, isPending, lastUpdated); if (ts.isValid()) { if (relative) { - return m_format.formatRelativeDate(ts.toLocalTime().date(), format); + KFormat formatter; + return formatter.formatRelativeDate(ts.toLocalTime().date(), format); } else { return QLocale().toString(ts.toLocalTime().time(), format); } @@ -141,41 +117,41 @@ QString EventHandler::getTimeString(bool relative, QLocale::FormatType format, b return {}; } -QString EventHandler::getTimeString(const QString &format, bool isPending, const QDateTime &lastUpdated) +QString EventHandler::timeString(const Quotient::RoomEvent *event, const QString &format, bool isPending, const QDateTime &lastUpdated) { - return getTime(isPending, lastUpdated).toLocalTime().toString(format); + return time(event, isPending, lastUpdated).toLocalTime().toString(format); } -bool EventHandler::isHighlighted() +bool EventHandler::isHighlighted(const NeoChatRoom *room, const Quotient::RoomEvent *event) { - if (m_room == nullptr) { - qCWarning(EventHandling) << "isHighlighted called with m_room set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "isHighlighted called with room set to nullptr."; return false; } - if (m_event == nullptr) { - qCWarning(EventHandling) << "isHighlighted called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "isHighlighted called with event set to nullptr."; return false; } - return !m_room->isDirectChat() && m_room->isEventHighlighted(m_event); + return !room->isDirectChat() && room->isEventHighlighted(event); } -bool EventHandler::isHidden() +bool EventHandler::isHidden(const NeoChatRoom *room, const Quotient::RoomEvent *event) { - if (m_room == nullptr) { - qCWarning(EventHandling) << "isHidden called with m_room set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "isHidden called with room set to nullptr."; return false; } - if (m_event == nullptr) { - qCWarning(EventHandling) << "isHidden called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "isHidden called with event set to nullptr."; return false; } - if (m_event->isStateEvent() && !NeoChatConfig::self()->showStateEvent()) { + if (event->isStateEvent() && !NeoChatConfig::self()->showStateEvent()) { return true; } - if (auto roomMemberEvent = eventCast(m_event)) { + if (auto roomMemberEvent = eventCast(event)) { if ((roomMemberEvent->isJoin() || roomMemberEvent->isLeave()) && !NeoChatConfig::self()->showLeaveJoinEvent()) { return true; } else if (roomMemberEvent->isRename() && !roomMemberEvent->isJoin() && !roomMemberEvent->isLeave() && !NeoChatConfig::self()->showRename()) { @@ -186,33 +162,33 @@ bool EventHandler::isHidden() } } - if (m_event->isStateEvent() && eventCast(m_event)->repeatsState()) { + if (event->isStateEvent() && eventCast(event)->repeatsState()) { return true; } // isReplacement? - if (auto e = eventCast(m_event)) { + if (auto e = eventCast(event)) { if (!e->replacedEvent().isEmpty()) { return true; } } - if (is(*m_event) || is(*m_event)) { + if (is(*event) || is(*event)) { return true; } - if (auto e = eventCast(m_event)) { + if (auto e = eventCast(event)) { if (!e->replacedEvent().isEmpty() && e->replacedEvent() != e->id()) { return true; } } - if (m_room->connection()->isIgnored(m_event->senderId())) { + if (room->connection()->isIgnored(event->senderId())) { return true; } // hide ending live location beacons - if (m_event->isStateEvent() && m_event->matrixType() == "org.matrix.msc3672.beacon_info"_ls && !m_event->contentJson()["live"_ls].toBool()) { + if (event->isStateEvent() && event->matrixType() == "org.matrix.msc3672.beacon_info"_ls && !event->contentJson()["live"_ls].toBool()) { return true; } @@ -251,44 +227,52 @@ QString EventHandler::rawMessageBody(const Quotient::RoomMessageEvent &event) return body; } -QString EventHandler::getRichBody(bool stripNewlines) const +QString EventHandler::richBody(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool stripNewlines) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getRichBody called with m_event set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "richBody called with room set to nullptr."; return {}; } - return getBody(m_event, Qt::RichText, stripNewlines); + if (event == nullptr) { + qCWarning(EventHandling) << "richBody called with event set to nullptr."; + return {}; + } + return getBody(room, event, Qt::RichText, stripNewlines); } -QString EventHandler::getPlainBody(bool stripNewlines) const +QString EventHandler::plainBody(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool stripNewlines) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getPlainBody called with m_event set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "plainBody called with room set to nullptr."; return {}; } - return getBody(m_event, Qt::PlainText, stripNewlines); + if (event == nullptr) { + qCWarning(EventHandling) << "plainBody called with event set to nullptr."; + return {}; + } + return getBody(room, event, Qt::PlainText, stripNewlines); } -QString EventHandler::getMarkdownBody() const +QString EventHandler::markdownBody(const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getMarkdownBody called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "markdownBody called with event set to nullptr."; return {}; } - if (!m_event->is()) { - qCWarning(EventHandling) << "getMarkdownBody called when m_event isn't a RoomMessageEvent."; + if (!event->is()) { + qCWarning(EventHandling) << "markdownBody called when event isn't a RoomMessageEvent."; return {}; } - const auto roomMessageEvent = eventCast(m_event); + const auto roomMessageEvent = eventCast(event); QString plainBody = roomMessageEvent->plainBody(); plainBody.remove(TextRegex::removeReply); return plainBody; } -QString EventHandler::getBody(const Quotient::RoomEvent *event, Qt::TextFormat format, bool stripNewlines) const +QString EventHandler::getBody(const NeoChatRoom *room, const Quotient::RoomEvent *event, Qt::TextFormat format, bool stripNewlines) { if (event->isRedacted()) { auto reason = event->redactedBecause()->reason(); @@ -299,15 +283,15 @@ QString EventHandler::getBody(const Quotient::RoomEvent *event, Qt::TextFormat f return switchOnType( *event, - [this, format, stripNewlines](const RoomMessageEvent &event) { - return getMessageBody(event, format, stripNewlines); + [room, format, stripNewlines](const RoomMessageEvent &event) { + return getMessageBody(room, event, format, stripNewlines); }, [](const StickerEvent &e) { return e.body(); }, - [this, prettyPrint](const RoomMemberEvent &e) { + [room, prettyPrint](const RoomMemberEvent &e) { // FIXME: Rewind to the name that was at the time of this event - auto subjectName = m_room->member(e.userId()).htmlSafeDisplayName(); + auto subjectName = room->member(e.userId()).htmlSafeDisplayName(); if (e.membership() == Membership::Leave) { if (e.prevContent() && e.prevContent()->displayName) { subjectName = sanitized(*e.prevContent()->displayName); @@ -319,7 +303,7 @@ QString EventHandler::getBody(const Quotient::RoomEvent *event, Qt::TextFormat f if (prettyPrint) { subjectName = QStringLiteral("%3") - .arg(e.userId(), m_room->member(e.userId()).color().name(), subjectName); + .arg(e.userId(), room->member(e.userId()).color().name(), subjectName); } // The below code assumes senderName output in AuthorRole @@ -455,7 +439,7 @@ QString EventHandler::getBody(const Quotient::RoomEvent *event, Qt::TextFormat f i18n("Unknown event")); } -QString EventHandler::getMessageBody(const RoomMessageEvent &event, Qt::TextFormat format, bool stripNewlines) const +QString EventHandler::getMessageBody(const NeoChatRoom *room, const RoomMessageEvent &event, Qt::TextFormat format, bool stripNewlines) { TextHandler textHandler; @@ -487,24 +471,24 @@ QString EventHandler::getMessageBody(const RoomMessageEvent &event, Qt::TextForm } if (format == Qt::RichText) { - return textHandler.handleRecieveRichText(inputFormat, m_room, &event, stripNewlines, event.isReplaced()); + return textHandler.handleRecieveRichText(inputFormat, room, &event, stripNewlines, event.isReplaced()); } else { return textHandler.handleRecievePlainText(inputFormat, stripNewlines); } } -QString EventHandler::getGenericBody() const +QString EventHandler::genericBody(const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getGenericBody called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "genericBody called with event set to nullptr."; return {}; } - if (m_event->isRedacted()) { + if (event->isRedacted()) { return i18n("[This message was deleted]"); } return switchOnType( - *m_event, + *event, [](const RoomMessageEvent &e) { Q_UNUSED(e) return i18n("sent a message"); @@ -624,29 +608,33 @@ QString EventHandler::getGenericBody() const i18n("Unknown event")); } -QString EventHandler::subtitleText() const +QString EventHandler::subtitleText(const NeoChatRoom *room, const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "subtitleText called with m_event set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "subtitleText called with room set to nullptr."; return {}; } - return singleLineAuthorDisplayname() + (m_event->isStateEvent() ? QLatin1String(" ") : QLatin1String(": ")) + getPlainBody(true); + if (event == nullptr) { + qCWarning(EventHandling) << "subtitleText called with event set to nullptr."; + return {}; + } + return singleLineAuthorDisplayname(room, event) + (event->isStateEvent() ? QLatin1String(" ") : QLatin1String(": ")) + plainBody(room, event, true); } -QVariantMap EventHandler::getMediaInfo() const +QVariantMap EventHandler::mediaInfo(const NeoChatRoom *room, const Quotient::RoomEvent *event) { - if (m_room == nullptr) { - qCWarning(EventHandling) << "getMediaInfo called with m_room set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "mediaInfo called with room set to nullptr."; return {}; } - if (m_event == nullptr) { - qCWarning(EventHandling) << "getMediaInfo called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "mediaInfo called with event set to nullptr."; return {}; } - return getMediaInfoForEvent(m_event); + return getMediaInfoForEvent(room, event); } -QVariantMap EventHandler::getMediaInfoForEvent(const Quotient::RoomEvent *event) const +QVariantMap EventHandler::getMediaInfoForEvent(const NeoChatRoom *room, const Quotient::RoomEvent *event) { QString eventId = event->id(); @@ -659,7 +647,7 @@ QVariantMap EventHandler::getMediaInfoForEvent(const Quotient::RoomEvent *event) const EventContent::FileInfo *fileInfo; fileInfo = roomMessageEvent->content()->fileInfo(); - QVariantMap mediaInfo = getMediaInfoFromFileInfo(fileInfo, eventId, false, false); + QVariantMap mediaInfo = getMediaInfoFromFileInfo(room, fileInfo, eventId, false, false); // if filename isn't specifically given, it is in body // https://spec.matrix.org/latest/client-server-api/#mfile mediaInfo["filename"_ls] = (fileInfo->originalName.isEmpty()) ? roomMessageEvent->plainBody() : fileInfo->originalName; @@ -671,13 +659,17 @@ QVariantMap EventHandler::getMediaInfoForEvent(const Quotient::RoomEvent *event) auto stickerEvent = eventCast(event); fileInfo = &stickerEvent->image(); - return getMediaInfoFromFileInfo(fileInfo, eventId, false, true); + return getMediaInfoFromFileInfo(room, fileInfo, eventId, false, true); } else { return {}; } } -QVariantMap EventHandler::getMediaInfoFromFileInfo(const EventContent::FileInfo *fileInfo, const QString &eventId, bool isThumbnail, bool isSticker) const +QVariantMap EventHandler::getMediaInfoFromFileInfo(const NeoChatRoom *room, + const EventContent::FileInfo *fileInfo, + const QString &eventId, + bool isThumbnail, + bool isSticker) { QVariantMap mediaInfo; @@ -685,7 +677,7 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const EventContent::FileInfo if (!fileInfo->url().isValid() || fileInfo->url().scheme() != QStringLiteral("mxc") || eventId.isEmpty()) { mediaInfo["source"_ls] = QUrl(); } else { - QUrl source = m_room->makeMediaUrl(eventId, fileInfo->url()); + QUrl source = room->makeMediaUrl(eventId, fileInfo->url()); if (source.isValid()) { mediaInfo["source"_ls] = source; @@ -717,7 +709,7 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const EventContent::FileInfo if (!isThumbnail) { QVariantMap tempInfo; - auto thumbnailInfo = getMediaInfoFromFileInfo(castInfo->thumbnailInfo(), eventId, true); + auto thumbnailInfo = getMediaInfoFromFileInfo(room, castInfo->thumbnailInfo(), eventId, true); if (thumbnailInfo["source"_ls].toUrl().scheme() == "mxc"_ls) { tempInfo = thumbnailInfo; } else { @@ -740,7 +732,7 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const EventContent::FileInfo if (!isThumbnail) { QVariantMap tempInfo; - auto thumbnailInfo = getMediaInfoFromFileInfo(castInfo->thumbnailInfo(), eventId, true); + auto thumbnailInfo = getMediaInfoFromFileInfo(room, castInfo->thumbnailInfo(), eventId, true); if (thumbnailInfo["source"_ls].toUrl().scheme() == "mxc"_ls) { tempInfo = thumbnailInfo; } else { @@ -764,14 +756,14 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const EventContent::FileInfo return mediaInfo; } -bool EventHandler::hasReply(bool showFallbacks) const +bool EventHandler::hasReply(const Quotient::RoomEvent *event, bool showFallbacks) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "hasReply called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "hasReply called with event set to nullptr."; return false; } - const auto relations = m_event->contentPart("m.relates_to"_ls); + const auto relations = event->contentPart("m.relates_to"_ls); if (!relations.isEmpty()) { const bool hasReplyRelation = relations.contains("m.in_reply_to"_ls); bool isFallingBack = relations["is_falling_back"_ls].toBool(); @@ -780,147 +772,73 @@ bool EventHandler::hasReply(bool showFallbacks) const return false; } -QString EventHandler::getReplyId() const +QString EventHandler::replyId(const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getReplyId called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "replyId called with event set to nullptr."; return {}; } - return m_event->contentJson()["m.relates_to"_ls].toObject()["m.in_reply_to"_ls].toObject()["event_id"_ls].toString(); + return event->contentJson()["m.relates_to"_ls].toObject()["m.in_reply_to"_ls].toObject()["event_id"_ls].toString(); } -MessageComponentType::Type EventHandler::replyMessageComponentType() const +Quotient::RoomMember EventHandler::replyAuthor(const NeoChatRoom *room, const Quotient::RoomEvent *event) { - if (m_room == nullptr) { - qCWarning(EventHandling) << "replyMessageComponentType called with m_room set to nullptr."; - return MessageComponentType::Other; - } - if (m_event == nullptr) { - qCWarning(EventHandling) << "replyMessageComponentType called with m_event set to nullptr."; - return MessageComponentType::Other; - } - - auto replyEvent = m_room->getReplyForEvent(*m_event); - if (replyEvent == nullptr) { - return MessageComponentType::Other; - } - return MessageComponentType::typeForEvent(*replyEvent); -} - -Quotient::RoomMember EventHandler::getReplyAuthor() const -{ - if (m_room == nullptr) { - qCWarning(EventHandling) << "getReplyAuthor called with m_room set to nullptr."; + if (room == nullptr) { + qCWarning(EventHandling) << "replyAuthor called with room set to nullptr."; return {}; } - if (m_event == nullptr) { - qCWarning(EventHandling) << "getReplyAuthor called with m_event set to nullptr. Returning empty user."; + if (event == nullptr) { + qCWarning(EventHandling) << "replyAuthor called with event set to nullptr. Returning empty user."; return {}; } - if (auto replyPtr = m_room->getReplyForEvent(*m_event)) { - return m_room->member(replyPtr->senderId()); + if (auto replyPtr = room->getReplyForEvent(*event)) { + return room->member(replyPtr->senderId()); } else { - return m_room->member(QString()); + return room->member(QString()); } } -QString EventHandler::getReplyRichBody(bool stripNewlines) const +bool EventHandler::isThreaded(const Quotient::RoomEvent *event) { - if (m_room == nullptr) { - qCWarning(EventHandling) << "getReplyRichBody called with m_room set to nullptr."; - return {}; - } - if (m_event == nullptr) { - qCWarning(EventHandling) << "getReplyRichBody called with m_event set to nullptr."; - return {}; - } - - auto replyEvent = m_room->getReplyForEvent(*m_event); - if (replyEvent == nullptr) { - return {}; - } - - return getBody(replyEvent, Qt::RichText, stripNewlines); -} - -QString EventHandler::getReplyPlainBody(bool stripNewlines) const -{ - if (m_room == nullptr) { - qCWarning(EventHandling) << "getReplyPlainBody called with m_room set to nullptr."; - return {}; - } - if (m_event == nullptr) { - qCWarning(EventHandling) << "getReplyPlainBody called with m_event set to nullptr."; - return {}; - } - - auto replyEvent = m_room->getReplyForEvent(*m_event); - if (replyEvent == nullptr) { - return {}; - } - - return getBody(replyEvent, Qt::PlainText, stripNewlines); -} - -QVariantMap EventHandler::getReplyMediaInfo() const -{ - if (m_room == nullptr) { - qCWarning(EventHandling) << "getReplyMediaInfo called with m_room set to nullptr."; - return {}; - } - if (m_event == nullptr) { - qCWarning(EventHandling) << "getReplyMediaInfo called with m_event set to nullptr."; - return {}; - } - - auto replyPtr = m_room->getReplyForEvent(*m_event); - if (!replyPtr) { - return {}; - } - return getMediaInfoForEvent(replyPtr); -} - -bool EventHandler::isThreaded() const -{ - if (m_event == nullptr) { - qCWarning(EventHandling) << "isThreaded called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "isThreaded called with event set to nullptr."; return false; } - return (m_event->contentPart("m.relates_to"_ls).contains("rel_type"_ls) - && m_event->contentPart("m.relates_to"_ls)["rel_type"_ls].toString() == "m.thread"_ls) - || (!m_event->unsignedPart("m.relations"_ls).isEmpty() && m_event->unsignedPart("m.relations"_ls).contains("m.thread"_ls)); + return (event->contentPart("m.relates_to"_ls).contains("rel_type"_ls) + && event->contentPart("m.relates_to"_ls)["rel_type"_ls].toString() == "m.thread"_ls) + || (!event->unsignedPart("m.relations"_ls).isEmpty() && event->unsignedPart("m.relations"_ls).contains("m.thread"_ls)); } -QString EventHandler::threadRoot() const +QString EventHandler::threadRoot(const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "threadRoot called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "threadRoot called with event set to nullptr."; return {}; } // Get the thread root ID from m.relates_to if it exists. - if (m_event->contentPart("m.relates_to"_ls).contains("rel_type"_ls) - && m_event->contentPart("m.relates_to"_ls)["rel_type"_ls].toString() == "m.thread"_ls) { - return m_event->contentPart("m.relates_to"_ls)["event_id"_ls].toString(); + if (event->contentPart("m.relates_to"_ls).contains("rel_type"_ls) + && event->contentPart("m.relates_to"_ls)["rel_type"_ls].toString() == "m.thread"_ls) { + return event->contentPart("m.relates_to"_ls)["event_id"_ls].toString(); } // For thread root events they have an m.relations in the unsigned part with a m.thread object. // If so return the event ID as it is the root. - if (!m_event->unsignedPart("m.relations"_ls).isEmpty() && m_event->unsignedPart("m.relations"_ls).contains("m.thread"_ls)) { - return getId(); + if (!event->unsignedPart("m.relations"_ls).isEmpty() && event->unsignedPart("m.relations"_ls).contains("m.thread"_ls)) { + return id(event); } return {}; } -float EventHandler::getLatitude() const +float EventHandler::latitude(const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getLatitude called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "latitude called with event set to nullptr."; return -100.0; } - const auto geoUri = m_event->contentJson()["geo_uri"_ls].toString(); + const auto geoUri = event->contentJson()["geo_uri"_ls].toString(); if (geoUri.isEmpty()) { return -100.0; // latitude runs from -90deg to +90deg so -100 is out of range. } @@ -928,14 +846,14 @@ float EventHandler::getLatitude() const return latitude.toFloat(); } -float EventHandler::getLongitude() const +float EventHandler::longitude(const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getLongitude called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "longitude called with event set to nullptr."; return -200.0; } - const auto geoUri = m_event->contentJson()["geo_uri"_ls].toString(); + const auto geoUri = event->contentJson()["geo_uri"_ls].toString(); if (geoUri.isEmpty()) { return -200.0; // longitude runs from -180deg to +180deg so -200 is out of range. } @@ -943,14 +861,14 @@ float EventHandler::getLongitude() const return latitude.toFloat(); } -QString EventHandler::getLocationAssetType() const +QString EventHandler::locationAssetType(const Quotient::RoomEvent *event) { - if (m_event == nullptr) { - qCWarning(EventHandling) << "getLocationAssetType called with m_event set to nullptr."; + if (event == nullptr) { + qCWarning(EventHandling) << "locationAssetType called with event set to nullptr."; return {}; } - const auto assetType = m_event->contentJson()["org.matrix.msc3488.asset"_ls].toObject()["type"_ls].toString(); + const auto assetType = event->contentJson()["org.matrix.msc3488.asset"_ls].toObject()["type"_ls].toString(); if (assetType.isEmpty()) { return {}; } diff --git a/src/eventhandler.h b/src/eventhandler.h index 6ec089391..0184f7670 100644 --- a/src/eventhandler.h +++ b/src/eventhandler.h @@ -3,24 +3,22 @@ #pragma once -#include - -#include - -#include -#include -#include - -#include "enums/messagecomponenttype.h" +#include +#include +#include namespace Quotient { +namespace EventContent +{ +class FileInfo; +} +class RoomEvent; class RoomMember; +class RoomMessageEvent; } -class LinkPreviewer; class NeoChatRoom; -class ReactionModel; /** * @class EventHandler @@ -38,20 +36,14 @@ class ReactionModel; */ class EventHandler { - Q_GADGET - public: - EventHandler(const NeoChatRoom *room, const Quotient::RoomEvent *event); - /** - * @brief Return the Matrix ID of the event. + * @brief Return the ID of the event. + * + * Returns the transaction ID if the Matrix ID is empty, which may be the case + * for a pending event. */ - QString getId() const; - - /** - * @brief The MessageComponentType to use to visualise the main event content. - */ - MessageComponentType::Type messageComponentType() const; + static QString id(const Quotient::RoomEvent *event); /** * @brief Get the display name of the event author. @@ -64,7 +56,7 @@ public: * @param isPending whether the event is pending as this cannot be derived from * just the event object. */ - QString getAuthorDisplayName(bool isPending = false) const; + static QString authorDisplayName(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool isPending = false); /** * @brief Get the display name of the event author but with any newlines removed. @@ -75,12 +67,12 @@ public: * @param isPending whether the event is pending as this cannot be derived from * just the event object. */ - QString singleLineAuthorDisplayname(bool isPending = false) const; + static QString singleLineAuthorDisplayname(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool isPending = false); /** * @brief Return a QDateTime object for the event timestamp. */ - QDateTime getTime(bool isPending = false, QDateTime lastUpdated = {}) const; + static QDateTime time(const Quotient::RoomEvent *event, bool isPending = false, QDateTime lastUpdated = {}); /** * @brief Return a QString for the event timestamp. @@ -96,16 +88,32 @@ public: * @param lastUpdated the time the event was last updated locally as this cannot be * obtained from the event. */ - QString getTimeString(bool relative, QLocale::FormatType format = QLocale::ShortFormat, bool isPending = false, QDateTime lastUpdated = {}) const; + static QString timeString(const Quotient::RoomEvent *event, + bool relative, + QLocale::FormatType format = QLocale::ShortFormat, + bool isPending = false, + QDateTime lastUpdated = {}); - QString getTimeString(const QString &format, bool isPending = false, const QDateTime &lastUpdated = {}); + /** + * @brief Return a QString for the event timestamp. + * + * This is intended to return a string that is read for display in the UI without + * any further manipulation required. + * + * @param format the format to use as a string. + * @param isPending whether the event is pending as this cannot be derived from + * just the event object. + * @param lastUpdated the time the event was last updated locally as this cannot be + * obtained from the event. + */ + static QString timeString(const Quotient::RoomEvent *event, const QString &format, bool isPending = false, const QDateTime &lastUpdated = {}); /** * @brief Whether the event should be highlighted in the timeline. * * @note Messages in direct chats are never highlighted. */ - bool isHighlighted(); + static bool isHighlighted(const NeoChatRoom *room, const Quotient::RoomEvent *event); /** * @brief Whether the event should be hidden in the timeline. @@ -114,7 +122,7 @@ public: * user has hidden all state events or if the sender has been ignored by the local * user. */ - bool isHidden(); + static bool isHidden(const NeoChatRoom *room, const Quotient::RoomEvent *event); /** * @brief The input format of the body in the message. @@ -146,7 +154,7 @@ public: * * @param stripNewlines whether the output should have new lines in it. */ - QString getRichBody(bool stripNewlines = false) const; + static QString richBody(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool stripNewlines = false); /** * @brief Output a string for the message content ready for display in a plain text field. @@ -162,14 +170,14 @@ public: * * @param stripNewlines whether the output should have new lines in it. */ - QString getPlainBody(bool stripNewlines = false) const; + static QString plainBody(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool stripNewlines = false); /** * @brief Output the original body for the message content, useful for editing the original message. * * The event type must be a room message event. */ - QString getMarkdownBody() const; + static QString markdownBody(const Quotient::RoomEvent *event); /** * @brief Output a generic string for the message content ready for display. @@ -182,9 +190,9 @@ public: * E.g. For a message the text will be: * "sent a message" * - * @sa getRichBody(), getPlainBody() + * @sa richBody(), plainBody() */ - QString getGenericBody() const; + static QString genericBody(const Quotient::RoomEvent *event); /** * @brief Output a string for the event to be used as a RoomList subtitle. @@ -192,7 +200,7 @@ public: * The output includes the username followed by the plain message, all with no * line breaks. */ - QString subtitleText() const; + static QString subtitleText(const NeoChatRoom *room, const Quotient::RoomEvent *event); /** * @brief Return the media info for the event. @@ -210,7 +218,7 @@ public: * - tempInfo - mediaInfo (with the same properties as this except no tempInfo) for a temporary image while the file downloads. * - isSticker - Whether the image is a sticker or not */ - QVariantMap getMediaInfo() const; + static QVariantMap mediaInfo(const NeoChatRoom *room, const Quotient::RoomEvent *event); /** * @brief Whether the event is a reply to another in the timeline. @@ -219,17 +227,12 @@ public: * show the fallback reply. Leave true for non-threaded * timelines. */ - bool hasReply(bool showFallbacks = true) const; + static bool hasReply(const Quotient::RoomEvent *event, bool showFallbacks = true); /** * @brief Return the Matrix ID of the event replied to. */ - QString getReplyId() const; - - /** - * @brief The MessageComponentType to use to visualise the reply content. - */ - MessageComponentType::Type replyMessageComponentType() const; + static QString replyId(const Quotient::RoomEvent *event); /** * @brief Get the author of the event replied to in context of the room. @@ -244,73 +247,21 @@ public: * * @sa Quotient::RoomMember */ - Quotient::RoomMember getReplyAuthor() const; - - /** - * @brief Output a string for the message content of the event replied to ready - * for display in a rich text field. - * - * The output string is dependant upon the event type and the desired output format. - * - * For most messages this is the body content of the message. For media messages - * this will be the caption and for state events it will be a string specific - * to that event with some dynamic details about the event added. - * - * E.g. For a room topic state event the text will be: - * "set the topic to: " - * - * @param stripNewlines whether the output should have new lines in it. - */ - QString getReplyRichBody(bool stripNewlines = false) const; - - /** - * @brief Output a string for the message content of the event replied to ready - * for display in a plain text field. - * - * The output string is dependant upon the event type and the desired output format. - * - * For most messages this is the body content of the message. For media messages - * this will be the caption and for state events it will be a string specific - * to that event with some dynamic details about the event added. - * - * E.g. For a room topic state event the text will be: - * "set the topic to: " - * - * @param stripNewlines whether the output should have new lines in it. - */ - QString getReplyPlainBody(bool stripNewlines = false) const; - - /** - * @brief Return the media info for the event replied to. - * - * An empty QVariantMap will be returned for any event that doesn't have any - * media info. - * - * @return This should consist of the following: - * - source - The mxc URL for the media. - * - mimeType - The MIME type of the media (should be image/xxx for this delegate). - * - mimeIcon - The MIME icon name (should be image-xxx). - * - size - The file size in bytes. - * - width - The width in pixels of the audio media. - * - height - The height in pixels of the audio media. - * - tempInfo - mediaInfo (with the same properties as this except no tempInfo) for a temporary image while the file downloads. - * - isSticker - Whether the image is a sticker or not - */ - QVariantMap getReplyMediaInfo() const; + static Quotient::RoomMember replyAuthor(const NeoChatRoom *room, const Quotient::RoomEvent *event); /** * @brief Whether the message is part of a thread. * * i.e. There is a rel_type of m.thread. */ - bool isThreaded() const; + static bool isThreaded(const Quotient::RoomEvent *event); /** * @brief Return the Matrix ID of the thread's root message. * * Empty if this not part of a thread. */ - QString threadRoot() const; + static QString threadRoot(const Quotient::RoomEvent *event); /** * @brief Return the latitude for the event. @@ -318,7 +269,7 @@ public: * Returns -100.0 if the event doesn't have a location (latitudes are in the * range -90deg to +90deg so -100 is out of range). */ - float getLatitude() const; + static float latitude(const Quotient::RoomEvent *event); /** * @brief Return the longitude for the event. @@ -326,23 +277,21 @@ public: * Returns -200.0 if the event doesn't have a location (latitudes are in the * range -180deg to +180deg so -200 is out of range). */ - float getLongitude() const; + static float longitude(const Quotient::RoomEvent *event); /** * @brief Return the type of location marker for the event. */ - QString getLocationAssetType() const; + static QString locationAssetType(const Quotient::RoomEvent *event); private: - const NeoChatRoom *m_room = nullptr; - const Quotient::RoomEvent *m_event = nullptr; + static QString getBody(const NeoChatRoom *room, const Quotient::RoomEvent *event, Qt::TextFormat format, bool stripNewlines); + static QString getMessageBody(const NeoChatRoom *room, const Quotient::RoomMessageEvent &event, Qt::TextFormat format, bool stripNewlines); - KFormat m_format; - - QString getBody(const Quotient::RoomEvent *event, Qt::TextFormat format, bool stripNewlines) const; - QString getMessageBody(const Quotient::RoomMessageEvent &event, Qt::TextFormat format, bool stripNewlines) const; - - QVariantMap getMediaInfoForEvent(const Quotient::RoomEvent *event) const; - QVariantMap - getMediaInfoFromFileInfo(const Quotient::EventContent::FileInfo *fileInfo, const QString &eventId, bool isThumbnail = false, bool isSticker = false) const; + static QVariantMap getMediaInfoForEvent(const NeoChatRoom *room, const Quotient::RoomEvent *event); + QVariantMap static getMediaInfoFromFileInfo(const NeoChatRoom *room, + const Quotient::EventContent::FileInfo *fileInfo, + const QString &eventId, + bool isThumbnail = false, + bool isSticker = false); }; diff --git a/src/models/messagecontentmodel.cpp b/src/models/messagecontentmodel.cpp index 7d9232465..24633076d 100644 --- a/src/models/messagecontentmodel.cpp +++ b/src/models/messagecontentmodel.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "messagecontentmodel.h" +#include "eventhandler.h" #include "neochatconfig.h" #include @@ -228,7 +229,6 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const return {}; } - EventHandler eventHandler(m_room, m_event.get()); const auto component = m_components[index.row()]; if (role == DisplayRole) { @@ -246,7 +246,7 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const if (!component.content.isEmpty()) { return component.content; } - return eventHandler.getRichBody(); + return EventHandler::richBody(m_room, m_event.get()); } if (role == ComponentTypeRole) { return component.type; @@ -255,7 +255,7 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const return component.attributes; } if (role == EventIdRole) { - return eventHandler.getId(); + return EventHandler::id(m_event.get()); } if (role == TimeRole) { const auto pendingIt = std::find_if(m_room->pendingEvents().cbegin(), m_room->pendingEvents().cend(), [this](const PendingEventItem &pendingEvent) { @@ -263,7 +263,7 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const }); auto lastUpdated = pendingIt == m_room->pendingEvents().cend() ? QDateTime() : pendingIt->lastUpdated(); - return eventHandler.getTime(m_isPending, lastUpdated); + return EventHandler::time(m_event.get(), m_isPending, lastUpdated); } if (role == TimeStringRole) { const auto pendingIt = std::find_if(m_room->pendingEvents().cbegin(), m_room->pendingEvents().cend(), [this](const PendingEventItem &pendingEvent) { @@ -271,13 +271,13 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const }); auto lastUpdated = pendingIt == m_room->pendingEvents().cend() ? QDateTime() : pendingIt->lastUpdated(); - return eventHandler.getTimeString(QStringLiteral("hh:mm"), m_isPending, lastUpdated); + return EventHandler::timeString(m_event.get(), QStringLiteral("hh:mm"), m_isPending, lastUpdated); } if (role == AuthorRole) { return QVariant::fromValue(m_eventSenderObject.get()); } if (role == MediaInfoRole) { - return eventHandler.getMediaInfo(); + return EventHandler::mediaInfo(m_room, m_event.get()); } if (role == FileTransferInfoRole) { return QVariant::fromValue(m_room->cachedFileTransferInfo(m_event.get())); @@ -286,25 +286,22 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const return QVariant::fromValue(m_itineraryModel); } if (role == LatitudeRole) { - return eventHandler.getLatitude(); + return EventHandler::latitude(m_event.get()); } if (role == LongitudeRole) { - return eventHandler.getLongitude(); + return EventHandler::longitude(m_event.get()); } if (role == AssetRole) { - return eventHandler.getLocationAssetType(); + return EventHandler::locationAssetType(m_event.get()); } if (role == PollHandlerRole) { return QVariant::fromValue(m_room->poll(m_eventId)); } - if (role == IsReplyRole) { - return eventHandler.hasReply(); - } if (role == ReplyEventIdRole) { - return eventHandler.getReplyId(); + return EventHandler::replyId(m_event.get()); } if (role == ReplyAuthorRole) { - return QVariant::fromValue(eventHandler.getReplyAuthor()); + return QVariant::fromValue(EventHandler::replyAuthor(m_room, m_event.get())); } if (role == ReplyContentModelRole) { return QVariant::fromValue(m_replyModel); @@ -344,7 +341,6 @@ QHash MessageContentModel::roleNames() const roles[LongitudeRole] = "longitude"; roles[AssetRole] = "asset"; roles[PollHandlerRole] = "pollHandler"; - roles[IsReplyRole] = "isReply"; roles[ReplyEventIdRole] = "replyEventId"; roles[ReplyAuthorRole] = "replyAuthor"; roles[ReplyContentModelRole] = "replyContentModel"; @@ -411,8 +407,7 @@ QList MessageContentModel::messageContentComponents(bool isEdi if (isEditing) { newComponents += MessageComponent{MessageComponentType::Edit, QString(), {}}; } else { - EventHandler eventHandler(m_room, m_event.get()); - newComponents.append(componentsForType(eventHandler.messageComponentType())); + newComponents.append(componentsForType(MessageComponentType::typeForEvent(*m_event.get()))); } if (m_room->urlPreviewEnabled()) { @@ -428,8 +423,7 @@ void MessageContentModel::updateReplyModel() return; } - EventHandler eventHandler(m_room, m_event.get()); - if (!eventHandler.hasReply() || (eventHandler.isThreaded() && NeoChatConfig::self()->threads())) { + if (!EventHandler::hasReply(m_event.get()) || (EventHandler::isThreaded(m_event.get()) && NeoChatConfig::self()->threads())) { if (m_replyModel) { delete m_replyModel; } @@ -440,9 +434,9 @@ void MessageContentModel::updateReplyModel() return; } - const auto replyEvent = m_room->findInTimeline(eventHandler.getReplyId()); + const auto replyEvent = m_room->findInTimeline(EventHandler::replyId(m_event.get())); if (replyEvent == m_room->historyEdge()) { - m_replyModel = new MessageContentModel(m_room, eventHandler.getReplyId(), true, false, this); + m_replyModel = new MessageContentModel(m_room, EventHandler::replyId(m_event.get()), true, false, this); } else { m_replyModel = new MessageContentModel(m_room, replyEvent->get(), true, false, this); } diff --git a/src/models/messagecontentmodel.h b/src/models/messagecontentmodel.h index 857317c95..827c00b3c 100644 --- a/src/models/messagecontentmodel.h +++ b/src/models/messagecontentmodel.h @@ -10,7 +10,6 @@ #include #include "enums/messagecomponenttype.h" -#include "eventhandler.h" #include "itinerarymodel.h" #include "neochatroommember.h" @@ -66,7 +65,6 @@ public: AssetRole, /**< Type of location event, e.g. self pin of the user location. */ PollHandlerRole, /**< The PollHandler for the event, if any. */ - IsReplyRole, /**< Is the message a reply to another event. */ ReplyEventIdRole, /**< The matrix ID of the message that was replied to. */ ReplyAuthorRole, /**< The author of the event that was replied to. */ ReplyContentModelRole, /**< The MessageContentModel for the reply event. */ diff --git a/src/models/messageeventmodel.cpp b/src/models/messageeventmodel.cpp index d55e3d2fa..6d8ace137 100644 --- a/src/models/messageeventmodel.cpp +++ b/src/models/messageeventmodel.cpp @@ -432,15 +432,13 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const const auto pendingIt = m_currentRoom->pendingEvents().crbegin() + std::min(row, timelineBaseIndex()); const auto &evt = isPending ? **pendingIt : **timelineIt; - EventHandler eventHandler(m_currentRoom, &evt); - if (role == Qt::DisplayRole) { if (evt.isRedacted()) { auto reason = evt.redactedBecause()->reason(); return (reason.isEmpty()) ? i18n("[This message was deleted]") : i18n("[This message was deleted: %1]", evt.redactedBecause()->reason()); } - return eventHandler.getRichBody(); + return EventHandler::richBody(m_currentRoom, &evt); } if (role == ContentModelRole) { @@ -457,7 +455,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == GenericDisplayRole) { - return eventHandler.getGenericBody(); + return EventHandler::genericBody(&evt); } if (role == DelegateTypeRole) { @@ -480,7 +478,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == HighlightRole) { - return eventHandler.isHighlighted(); + return EventHandler::isHighlighted(m_currentRoom, &evt); } if (role == SpecialMarksRole) { @@ -493,11 +491,11 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const return pendingIt->deliveryStatus(); } - if (eventHandler.isHidden()) { + if (EventHandler::isHidden(m_currentRoom, &evt)) { return EventStatus::Hidden; } - if (eventHandler.isThreaded() && eventHandler.threadRoot() != eventHandler.getId() && NeoChatConfig::threads()) { + if (EventHandler::isThreaded(&evt) && EventHandler::threadRoot(&evt) != EventHandler::id(&evt) && NeoChatConfig::threads()) { return EventStatus::Hidden; } @@ -505,7 +503,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == EventIdRole) { - return eventHandler.getId(); + return EventHandler::id(&evt); } if (role == ProgressInfoRole) { @@ -521,20 +519,20 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const if (role == TimeRole) { auto lastUpdated = isPending ? pendingIt->lastUpdated() : QDateTime(); - return eventHandler.getTime(isPending, lastUpdated); + return EventHandler::time(&evt, isPending, lastUpdated); } if (role == SectionRole) { auto lastUpdated = isPending ? pendingIt->lastUpdated() : QDateTime(); - return eventHandler.getTimeString(true, QLocale::ShortFormat, isPending, lastUpdated); + return EventHandler::timeString(&evt, true, QLocale::ShortFormat, isPending, lastUpdated); } if (role == IsThreadedRole) { - return eventHandler.isThreaded(); + return EventHandler::isThreaded(&evt); } if (role == ThreadRootRole) { - return eventHandler.threadRoot(); + return EventHandler::threadRoot(&evt); } if (role == ShowSectionRole) { @@ -587,7 +585,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == AuthorDisplayNameRole) { - return eventHandler.getAuthorDisplayName(isPending); + return EventHandler::authorDisplayName(m_currentRoom, &evt, isPending); } if (role == IsRedactedRole) { @@ -599,11 +597,11 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == MediaInfoRole) { - return eventHandler.getMediaInfo(); + return EventHandler::mediaInfo(m_currentRoom, &evt); } if (role == IsEditableRole) { - return eventHandler.messageComponentType() == MessageComponentType::Text && evt.senderId() == m_currentRoom->localMember().id(); + return MessageComponentType::typeForEvent(evt) == MessageComponentType::Text && evt.senderId() == m_currentRoom->localMember().id(); } return {}; @@ -650,9 +648,8 @@ void MessageEventModel::createEventObjects(const Quotient::RoomEvent *event) } } - const auto eventHandler = EventHandler(m_currentRoom, event); - if (eventHandler.isThreaded() && !m_threadModels.contains(eventHandler.threadRoot())) { - m_threadModels[eventHandler.threadRoot()] = QSharedPointer(new ThreadModel(eventHandler.threadRoot(), m_currentRoom)); + if (EventHandler::isThreaded(event) && !m_threadModels.contains(EventHandler::threadRoot(event))) { + m_threadModels[EventHandler::threadRoot(event)] = QSharedPointer(new ThreadModel(EventHandler::threadRoot(event), m_currentRoom)); } // ReadMarkerModel handles updates to add and remove markers, we only need to diff --git a/src/models/notificationsmodel.cpp b/src/models/notificationsmodel.cpp index b3bfed5f6..689ac5d40 100644 --- a/src/models/notificationsmodel.cpp +++ b/src/models/notificationsmodel.cpp @@ -121,12 +121,11 @@ void NotificationsModel::loadData() const auto &authorAvatar = avatar.isValid() && avatar.scheme() == QStringLiteral("mxc") ? avatar : QUrl(); const auto &roomEvent = eventCast(notification.event.get()); - EventHandler eventHandler(dynamic_cast(room), roomEvent); beginInsertRows({}, m_notifications.length(), m_notifications.length()); m_notifications += Notification{ .roomId = notification.roomId, .text = room->member(authorId).htmlSafeDisplayName() + (roomEvent->is() ? QStringLiteral(" ") : QStringLiteral(": ")) - + eventHandler.getPlainBody(true), + + EventHandler::plainBody(dynamic_cast(room), roomEvent, true), .authorName = room->member(authorId).htmlSafeDisplayName(), .authorAvatar = authorAvatar, .eventId = roomEvent->id(), diff --git a/src/models/roomlistmodel.cpp b/src/models/roomlistmodel.cpp index ef5400ae4..03f4a72a1 100644 --- a/src/models/roomlistmodel.cpp +++ b/src/models/roomlistmodel.cpp @@ -245,8 +245,7 @@ QVariant RoomListModel::data(const QModelIndex &index, int role) const if (room->lastEvent() == nullptr || room->lastEventIsSpoiler()) { return QString(); } - EventHandler eventHandler(room, room->lastEvent()); - return eventHandler.subtitleText(); + return EventHandler::subtitleText(room, room->lastEvent()); } if (role == AvatarImageRole) { return room->avatar(128); diff --git a/src/models/roomtreemodel.cpp b/src/models/roomtreemodel.cpp index 31e829ef4..3f7050aef 100644 --- a/src/models/roomtreemodel.cpp +++ b/src/models/roomtreemodel.cpp @@ -357,8 +357,7 @@ QVariant RoomTreeModel::data(const QModelIndex &index, int role) const if (room->lastEvent() == nullptr || room->lastEventIsSpoiler()) { return QString(); } - EventHandler eventHandler(room, room->lastEvent()); - return eventHandler.subtitleText(); + return EventHandler::subtitleText(room, room->lastEvent()); } if (role == AvatarImageRole) { return room->avatar(128); diff --git a/src/models/searchmodel.cpp b/src/models/searchmodel.cpp index 5a45e0d43..4960ce424 100644 --- a/src/models/searchmodel.cpp +++ b/src/models/searchmodel.cpp @@ -89,8 +89,6 @@ QVariant SearchModel::data(const QModelIndex &index, int role) const auto row = index.row(); const auto &event = *m_result->results[row].result; - EventHandler eventHandler(m_room, &event); - switch (role) { case AuthorRole: return QVariant::fromValue(m_memberObjects.at(event.senderId()).get()); @@ -100,7 +98,7 @@ QVariant SearchModel::data(const QModelIndex &index, int role) const } return event.originTimestamp().date() != m_result->results[row - 1].result->originTimestamp().date(); case SectionRole: - return eventHandler.getTimeString(true); + return EventHandler::timeString(&event, true); case ShowReactionsRole: return false; case ShowReadMarkersRole: @@ -108,13 +106,13 @@ QVariant SearchModel::data(const QModelIndex &index, int role) const case IsPendingRole: return false; case HighlightRole: - return eventHandler.isHighlighted(); + return EventHandler::isHighlighted(m_room, &event); case EventIdRole: - return eventHandler.getId(); + return EventHandler::id(&event); case IsThreadedRole: - return eventHandler.isThreaded(); + return EventHandler::isThreaded(&event); case ThreadRootRole: - return eventHandler.threadRoot(); + return EventHandler::threadRoot(&event); case ContentModelRole: { if (!event.isStateEvent()) { return QVariant::fromValue(new MessageContentModel(m_room, &event)); diff --git a/src/models/threadmodel.cpp b/src/models/threadmodel.cpp index 542b0c0c3..0fc3e9f0b 100644 --- a/src/models/threadmodel.cpp +++ b/src/models/threadmodel.cpp @@ -23,8 +23,7 @@ ThreadModel::ThreadModel(const QString &threadRootId, NeoChatRoom *room) connect(room, &Quotient::Room::pendingEventAboutToAdd, this, [this](Quotient::RoomEvent *event) { if (auto roomEvent = eventCast(event)) { - EventHandler eventHandler(dynamic_cast(QObject::parent()), roomEvent); - if (eventHandler.isThreaded() && eventHandler.threadRoot() == m_threadRootId) { + if (EventHandler::isThreaded(roomEvent) && EventHandler::threadRoot(roomEvent) == m_threadRootId) { addNewEvent(event); clearModels(); addModels(); @@ -34,8 +33,7 @@ ThreadModel::ThreadModel(const QString &threadRootId, NeoChatRoom *room) connect(room, &Quotient::Room::aboutToAddNewMessages, this, [this](Quotient::RoomEventsRange events) { for (const auto &event : events) { if (auto roomEvent = eventCast(event)) { - EventHandler eventHandler(dynamic_cast(QObject::parent()), roomEvent); - if (eventHandler.isThreaded() && eventHandler.threadRoot() == m_threadRootId) { + if (EventHandler::isThreaded(roomEvent) && EventHandler::threadRoot(roomEvent) == m_threadRootId) { addNewEvent(roomEvent); } } diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index d3249f176..42facf326 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -549,10 +549,8 @@ void NeoChatRoom::postHtmlMessage(const QString &text, bool isFallingBack = !fallbackId.isEmpty(); QString replyEventId = isFallingBack ? fallbackId : QString(); if (isReply) { - EventHandler eventHandler(this, &**replyIt); - isFallingBack = false; - replyEventId = eventHandler.getId(); + replyEventId = EventHandler::id(replyIt->get()); } // If we are not replying and there is no fallback ID it means a new thread @@ -605,12 +603,10 @@ void NeoChatRoom::postHtmlMessage(const QString &text, if (isReply) { const auto &replyEvt = **replyIt; - EventHandler eventHandler(this, &**replyIt); - // clang-format off QJsonObject json{ {"msgtype"_ls, msgTypeToString(type)}, - {"body"_ls, "> <%1> %2\n\n%3"_ls.arg(replyEvt.senderId(), eventHandler.getPlainBody(), text)}, + {"body"_ls, "> <%1> %2\n\n%3"_ls.arg(replyEvt.senderId(), EventHandler::plainBody(this, &replyEvt), text)}, {"format"_ls, "org.matrix.custom.html"_ls}, {"m.relates_to"_ls, QJsonObject { @@ -622,7 +618,7 @@ void NeoChatRoom::postHtmlMessage(const QString &text, } }, {"formatted_body"_ls, - "
In reply to %4
%5
%6"_ls.arg(id(), replyEventId, replyEvt.senderId(), replyEvt.senderId(), eventHandler.getRichBody(), html) + "
In reply to %4
%5
%6"_ls.arg(id(), replyEventId, replyEvt.senderId(), replyEvt.senderId(), EventHandler::richBody(this, &replyEvt), html) } }; // clang-format on diff --git a/src/roommanager.cpp b/src/roommanager.cpp index a8a2fe223..a847c92e0 100644 --- a/src/roommanager.cpp +++ b/src/roommanager.cpp @@ -6,6 +6,7 @@ #include "chatbarcache.h" #include "controller.h" +#include "eventhandler.h" #include "neochatconfig.h" #include "neochatconnection.h" #include "neochatroom.h" @@ -203,19 +204,23 @@ void RoomManager::viewEventSource(const QString &eventId) void RoomManager::viewEventMenu(const QString &eventId, NeoChatRoom *room, NeochatRoomMember *sender, const QString &selectedText) { const auto &event = **room->findInTimeline(eventId); - const auto eventHandler = EventHandler(room, &event); - if (eventHandler.getMediaInfo().contains("mimeType"_ls)) { + if (EventHandler::mediaInfo(room, &event).contains("mimeType"_ls)) { Q_EMIT showFileMenu(eventId, sender, - eventHandler.messageComponentType(), - eventHandler.getPlainBody(), - eventHandler.getMediaInfo()["mimeType"_ls].toString(), + MessageComponentType::typeForEvent(event), + EventHandler::plainBody(room, &event), + EventHandler::mediaInfo(room, &event)["mimeType"_ls].toString(), room->fileTransferInfo(eventId)); return; } - Q_EMIT showMessageMenu(eventId, sender, eventHandler.messageComponentType(), eventHandler.getPlainBody(), eventHandler.getRichBody(), selectedText); + Q_EMIT showMessageMenu(eventId, + sender, + MessageComponentType::typeForEvent(event), + EventHandler::plainBody(room, &event), + EventHandler::richBody(room, &event), + selectedText); } bool RoomManager::hasOpenRoom() const diff --git a/src/roommanager.h b/src/roommanager.h index 7c642c377..9c521b3e0 100644 --- a/src/roommanager.h +++ b/src/roommanager.h @@ -13,7 +13,6 @@ #include "chatdocumenthandler.h" #include "enums/messagecomponenttype.h" -#include "eventhandler.h" #include "models/mediamessagefiltermodel.h" #include "models/messagefiltermodel.h" #include "models/roomlistmodel.h"