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.)
This commit is contained in:
Joshua Goins
2025-04-27 15:52:44 -04:00
committed by Tobias Fella
parent 8817ca7f2b
commit 08dc05c707
4 changed files with 9 additions and 11 deletions

View File

@@ -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);

View File

@@ -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<const RoomMessageEvent>(event)) {
if (e->has<EventContent::TextContent>() && e->content() && e->mimeType().name() == "text/html"_L1) {
auto htmlBody = e->get<EventContent::TextContent>()->body;
return htmlBody.contains("data-mx-spoiler"_L1);
}
if (const auto message = eventCast<const RoomMessageEvent>(e)) {
if (message->has<EventContent::TextContent>() && message->content() && message->mimeType().name() == "text/html"_L1) {
const auto htmlBody = message->get<EventContent::TextContent>()->body;
return htmlBody.contains("data-mx-spoiler"_L1);
}
}
return false;

View File

@@ -212,7 +212,7 @@ public:
[[nodiscard]] const Quotient::RoomEvent *lastEvent(std::function<bool(const Quotient::RoomEvent *)> 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.

View File

@@ -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);