From 08dc05c707c35e53a049a631f2cde8085af4f84e Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 27 Apr 2025 15:52:44 -0400 Subject: [PATCH] Refactor lastEventIsSpoiler, remove lastEvent() call that isn't needed This is only called after we already get an event with lastEvent() so doing it again is useless. Instead, we should refactor it to behave like similar functions (e.g. isEventHighlighted.) --- src/libneochat/models/roomlistmodel.cpp | 2 +- src/libneochat/neochatroom.cpp | 12 +++++------- src/libneochat/neochatroom.h | 4 ++-- src/rooms/models/roomtreemodel.cpp | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/libneochat/models/roomlistmodel.cpp b/src/libneochat/models/roomlistmodel.cpp index e9084c4ab..648275359 100644 --- a/src/libneochat/models/roomlistmodel.cpp +++ b/src/libneochat/models/roomlistmodel.cpp @@ -248,7 +248,7 @@ QVariant RoomListModel::data(const QModelIndex &index, int role) const } if (role == SubtitleTextRole) { const auto lastEvent = room->lastEvent(m_hiddenFilter); - if (lastEvent == nullptr || room->lastEventIsSpoiler()) { + if (lastEvent == nullptr || room->isEventSpoiler(lastEvent)) { return QString(); } return EventHandler::subtitleText(room, lastEvent); diff --git a/src/libneochat/neochatroom.cpp b/src/libneochat/neochatroom.cpp index 60325613a..20389350e 100644 --- a/src/libneochat/neochatroom.cpp +++ b/src/libneochat/neochatroom.cpp @@ -379,14 +379,12 @@ void NeoChatRoom::cacheLastEvent() } } -bool NeoChatRoom::lastEventIsSpoiler() const +bool NeoChatRoom::isEventSpoiler(const RoomEvent *e) const { - if (auto event = lastEvent()) { - if (auto e = eventCast(event)) { - if (e->has() && e->content() && e->mimeType().name() == "text/html"_L1) { - auto htmlBody = e->get()->body; - return htmlBody.contains("data-mx-spoiler"_L1); - } + if (const auto message = eventCast(e)) { + if (message->has() && message->content() && message->mimeType().name() == "text/html"_L1) { + const auto htmlBody = message->get()->body; + return htmlBody.contains("data-mx-spoiler"_L1); } } return false; diff --git a/src/libneochat/neochatroom.h b/src/libneochat/neochatroom.h index ab20ead77..8eff6fef8 100644 --- a/src/libneochat/neochatroom.h +++ b/src/libneochat/neochatroom.h @@ -212,7 +212,7 @@ public: [[nodiscard]] const Quotient::RoomEvent *lastEvent(std::function filter = {}) const; /** - * @brief Convenient way to check if the last event looks like it has spoilers. + * @brief Convenient way to check if the event looks like it has spoilers. * * This does a basic check to see if the message contains a data-mx-spoiler * attribute within the text which makes it likely that the message has a spoiler @@ -222,7 +222,7 @@ public: * * @sa lastEvent() */ - [[nodiscard]] bool lastEventIsSpoiler() const; + [[nodiscard]] bool isEventSpoiler(const Quotient::RoomEvent *e) const; /** * @brief Return the notification count for the room accounting for tags and notification state. diff --git a/src/rooms/models/roomtreemodel.cpp b/src/rooms/models/roomtreemodel.cpp index ad5657c23..03cf48aca 100644 --- a/src/rooms/models/roomtreemodel.cpp +++ b/src/rooms/models/roomtreemodel.cpp @@ -360,7 +360,7 @@ QVariant RoomTreeModel::data(const QModelIndex &index, int role) const return i18nc("@info:label", "%1 invited you", room->member(room->invitingUserId()).displayName()); } const auto lastEvent = room->lastEvent(m_hiddenFilter); - if (lastEvent == nullptr || room->lastEventIsSpoiler()) { + if (lastEvent == nullptr || room->isEventSpoiler(lastEvent)) { return QString(); } return EventHandler::subtitleText(room, lastEvent);