Make lastEvent() take a filter function

Make `lastEvent()` take a filter function so we can remove any dependence on `NeochatConfig` from `NeoChatRoom`.
This commit is contained in:
James Graham
2025-04-04 09:27:04 +00:00
parent 88eb2223c5
commit 913b4ea923
4 changed files with 49 additions and 20 deletions

View File

@@ -3,7 +3,10 @@
#include "roomlistmodel.h"
#include <Quotient/events/roommemberevent.h>
#include "eventhandler.h"
#include "neochatconfig.h"
#include "neochatconnection.h"
#include "neochatroom.h"
#include "roommanager.h"
@@ -239,10 +242,26 @@ QVariant RoomListModel::data(const QModelIndex &index, int role) const
return QVariant::fromValue(room);
}
if (role == SubtitleTextRole) {
if (room->lastEvent() == nullptr || room->lastEventIsSpoiler()) {
const auto lastEvent = room->lastEvent([](const RoomEvent *event) -> bool {
if (event->isStateEvent() && !NeoChatConfig::showStateEvent()) {
return true;
}
if (auto roomMemberEvent = eventCast<const RoomMemberEvent>(event)) {
if ((roomMemberEvent->isJoin() || roomMemberEvent->isLeave()) && !NeoChatConfig::showLeaveJoinEvent()) {
return true;
} else if (roomMemberEvent->isRename() && !roomMemberEvent->isJoin() && !roomMemberEvent->isLeave() && !NeoChatConfig::showRename()) {
return true;
} else if (roomMemberEvent->isAvatarUpdate() && !roomMemberEvent->isJoin() && !roomMemberEvent->isLeave()
&& !NeoChatConfig::showAvatarUpdate()) {
return true;
}
}
return false;
});
if (lastEvent == nullptr || room->lastEventIsSpoiler()) {
return QString();
}
return EventHandler::subtitleText(room, room->lastEvent());
return EventHandler::subtitleText(room, lastEvent);
}
if (role == AvatarImageRole) {
return room->avatar(128);

View File

@@ -3,9 +3,11 @@
#include "roomtreemodel.h"
#include <Quotient/events/roommemberevent.h>
#include <Quotient/room.h>
#include "eventhandler.h"
#include "neochatconfig.h"
#include "neochatconnection.h"
#include "neochatroomtype.h"
#include "spacehierarchycache.h"
@@ -354,10 +356,26 @@ QVariant RoomTreeModel::data(const QModelIndex &index, int role) const
}
return i18nc("@info:label", "%1 invited you", room->member(room->invitingUserId()).displayName());
}
if (room->lastEvent() == nullptr || room->lastEventIsSpoiler()) {
const auto lastEvent = room->lastEvent([](const RoomEvent *event) -> bool {
if (event->isStateEvent() && !NeoChatConfig::showStateEvent()) {
return true;
}
if (auto roomMemberEvent = eventCast<const RoomMemberEvent>(event)) {
if ((roomMemberEvent->isJoin() || roomMemberEvent->isLeave()) && !NeoChatConfig::showLeaveJoinEvent()) {
return true;
} else if (roomMemberEvent->isRename() && !roomMemberEvent->isJoin() && !roomMemberEvent->isLeave() && !NeoChatConfig::showRename()) {
return true;
} else if (roomMemberEvent->isAvatarUpdate() && !roomMemberEvent->isJoin() && !roomMemberEvent->isLeave()
&& !NeoChatConfig::showAvatarUpdate()) {
return true;
}
}
return false;
});
if (lastEvent == nullptr || room->lastEventIsSpoiler()) {
return QString();
}
return EventHandler::subtitleText(room, room->lastEvent());
return EventHandler::subtitleText(room, lastEvent);
}
if (role == AvatarImageRole) {
return room->avatar(128);

View File

@@ -42,7 +42,6 @@
#include "eventhandler.h"
#include "events/pollevent.h"
#include "filetransferpseudojob.h"
#include "neochatconfig.h"
#include "neochatconnection.h"
#include "neochatroommember.h"
#include "roomlastmessageprovider.h"
@@ -314,11 +313,17 @@ void NeoChatRoom::sendTypingNotification(bool isTyping)
connection()->callApi<SetTypingJob>(BackgroundRequest, localMember().id(), id(), isTyping, 10000);
}
const RoomEvent *NeoChatRoom::lastEvent() const
const RoomEvent *NeoChatRoom::lastEvent(std::function<bool(const RoomEvent *)> filter) const
{
for (auto timelineItem = messageEvents().rbegin(); timelineItem < messageEvents().rend(); timelineItem++) {
const RoomEvent *event = timelineItem->get();
if (filter) {
if (filter(event)) {
continue;
}
}
if (is<RedactionEvent>(*event) || is<ReactionEvent>(*event)) {
continue;
}
@@ -326,19 +331,6 @@ const RoomEvent *NeoChatRoom::lastEvent() const
continue;
}
if (event->isStateEvent() && !NeoChatConfig::showStateEvent()) {
continue;
}
if (auto roomMemberEvent = eventCast<const RoomMemberEvent>(event)) {
if ((roomMemberEvent->isJoin() || roomMemberEvent->isLeave()) && !NeoChatConfig::showLeaveJoinEvent()) {
continue;
} else if (roomMemberEvent->isRename() && !roomMemberEvent->isJoin() && !roomMemberEvent->isLeave() && !NeoChatConfig::showRename()) {
continue;
} else if (roomMemberEvent->isAvatarUpdate() && !roomMemberEvent->isJoin() && !roomMemberEvent->isLeave() && !NeoChatConfig::showAvatarUpdate()) {
continue;
}
}
if (event->isStateEvent() && static_cast<const StateEvent &>(*event).repeatsState()) {
continue;
}

View File

@@ -211,7 +211,7 @@ public:
* @warning This function can return an empty pointer if the room does not have
* any RoomMessageEvents loaded.
*/
[[nodiscard]] const Quotient::RoomEvent *lastEvent() const;
[[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.