From 041674b7f67eae3197176a6e950b6565123d4ec9 Mon Sep 17 00:00:00 2001 From: nerix Date: Sat, 29 Nov 2025 12:40:06 +0100 Subject: [PATCH] refactor: simplify uses of `getMessageSnapshot` (#6607) Reviewed-by: pajlada --- CHANGELOG.md | 1 + src/common/Channel.cpp | 18 ++++++++++++------ src/common/Channel.hpp | 5 +++++ .../commands/builtin/twitch/SendReply.cpp | 5 +++-- .../notifications/NotificationController.cpp | 15 ++++----------- src/providers/twitch/TwitchIrcServer.cpp | 7 +++---- src/widgets/dialogs/EmotePopup.cpp | 2 +- src/widgets/dialogs/UserInfoPopup.cpp | 4 +--- src/widgets/helper/SearchPopup.cpp | 2 +- 9 files changed, 31 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8cb3bd8..ae990699 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ - Dev: Check Lua unwinding and version in tests. (#6586) - Dev: Added method to get the last N messages of a channel. (#6602, #6604) - Dev: Unwrapped `LimitedQueueSnapshot` to `std::vector`. (#6606) +- Dev: Simplified uses of `getMessageSnapshot`. (#6607) ## 2.5.4 diff --git a/src/common/Channel.cpp b/src/common/Channel.cpp index ab8f891f..602d2348 100644 --- a/src/common/Channel.cpp +++ b/src/common/Channel.cpp @@ -81,6 +81,16 @@ std::vector Channel::getMessageSnapshot(size_t nItems) const return this->messages_.lastN(nItems); } +MessagePtr Channel::getLastMessage() const +{ + auto last = this->messages_.last(); + if (last) + { + return *std::move(last); + } + return nullptr; +} + void Channel::addMessage(MessagePtr message, MessageContext context, std::optional overridingFlags) { @@ -146,19 +156,15 @@ void Channel::addOrReplaceClearChat(MessagePtr message, const QDateTime &now) void Channel::disableAllMessages() { - std::vector snapshot = this->getMessageSnapshot(); - int snapshotLength = snapshot.size(); - for (int i = 0; i < snapshotLength; i++) + for (const auto &message : this->getMessageSnapshot()) { - const auto &message = snapshot[i]; if (message->flags.hasAny({MessageFlag::System, MessageFlag::Timeout, MessageFlag::Whisper})) { continue; } - // FOURTF: disabled for now - const_cast(message.get())->flags.set(MessageFlag::Disabled); + message->flags.set(MessageFlag::Disabled); } } diff --git a/src/common/Channel.hpp b/src/common/Channel.hpp index 8cad8c59..e2fcfef8 100644 --- a/src/common/Channel.hpp +++ b/src/common/Channel.hpp @@ -79,9 +79,14 @@ public: virtual const QString &getLocalizedName() const; bool isTwitchChannel() const; virtual bool isEmpty() const; + std::vector getMessageSnapshot() const; std::vector getMessageSnapshot(size_t nItems) const; + /// Returns the last message (the one at the bottom). If the channel has no + /// messages, this will return an empty shared pointer. + MessagePtr getLastMessage() const; + // MESSAGES // overridingFlags can be filled in with flags that should be used instead // of the message's flags. This is useful in case a flag is specific to a diff --git a/src/controllers/commands/builtin/twitch/SendReply.cpp b/src/controllers/commands/builtin/twitch/SendReply.cpp index 80deb015..b5da5441 100644 --- a/src/controllers/commands/builtin/twitch/SendReply.cpp +++ b/src/controllers/commands/builtin/twitch/SendReply.cpp @@ -6,6 +6,8 @@ #include "providers/twitch/TwitchChannel.hpp" #include "util/Twitch.hpp" +#include + namespace chatterino::commands { QString sendReply(const CommandContext &ctx) @@ -32,9 +34,8 @@ QString sendReply(const CommandContext &ctx) stripChannelName(username); auto snapshot = ctx.twitchChannel->getMessageSnapshot(); - for (auto it = snapshot.rbegin(); it != snapshot.rend(); ++it) + for (const auto &msg : snapshot | std::views::reverse) { - const auto &msg = *it; if (msg->loginName.compare(username, Qt::CaseInsensitive) == 0) { // found most recent message by user diff --git a/src/controllers/notifications/NotificationController.cpp b/src/controllers/notifications/NotificationController.cpp index 866be087..497b9161 100644 --- a/src/controllers/notifications/NotificationController.cpp +++ b/src/controllers/notifications/NotificationController.cpp @@ -16,6 +16,8 @@ #include +#include + namespace ranges = std::ranges; namespace chatterino { @@ -154,19 +156,10 @@ void NotificationController::notifyTwitchChannelLive( void NotificationController::notifyTwitchChannelOffline(const QString &id) const { // "delete" old 'CHANNEL is live' message - const std::vector snapshot = + auto snapshot = getApp()->getTwitch()->getLiveChannel()->getMessageSnapshot(200); - - // Guard against empty snapshot to prevent underflow and invalid access - if (snapshot.size() == 0) + for (const auto &s : snapshot | std::views::reverse) { - return; - } - - for (size_t i = snapshot.size() - 1; i >= 0; --i) - { - const auto &s = snapshot[i]; - if (s->id == id) { s->flags.set(MessageFlag::Disabled); diff --git a/src/providers/twitch/TwitchIrcServer.cpp b/src/providers/twitch/TwitchIrcServer.cpp index 53a73911..e7590905 100644 --- a/src/providers/twitch/TwitchIrcServer.cpp +++ b/src/providers/twitch/TwitchIrcServer.cpp @@ -479,15 +479,14 @@ void TwitchIrcServer::onReadConnected(IrcConnection *connection) for (const auto &chan : activeChannels) { - std::vector snapshot = chan->getMessageSnapshot(); + MessagePtr last = chan->getLastMessage(); bool replaceMessage = - snapshot.size() > 0 && snapshot[snapshot.size() - 1]->flags.has( - MessageFlag::DisconnectedMessage); + last && last->flags.has(MessageFlag::DisconnectedMessage); if (replaceMessage) { - chan->replaceMessage(snapshot[snapshot.size() - 1], reconnected); + chan->replaceMessage(last, reconnected); } else { diff --git a/src/widgets/dialogs/EmotePopup.cpp b/src/widgets/dialogs/EmotePopup.cpp index 3ef91640..a8812ad5 100644 --- a/src/widgets/dialogs/EmotePopup.cpp +++ b/src/widgets/dialogs/EmotePopup.cpp @@ -483,7 +483,7 @@ void EmotePopup::reloadEmotes() "7TV"); } - if (subChannel->getMessageSnapshot().size() == 0) + if (!subChannel->hasMessages()) { MessageBuilder builder; builder->flags.set(MessageFlag::Centered); diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index f3efa86b..3a695586 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -121,10 +121,8 @@ ChannelPtr filterMessages(const QString &userName, ChannelPtr channel) std::make_shared(channel->getName(), Channel::Type::None); } - for (size_t i = 0; i < snapshot.size(); i++) + for (const auto &message : snapshot) { - MessagePtr message = snapshot[i]; - if (checkMessageUserName(userName, message)) { channelPtr->addMessage(message, MessageContext::Repost); diff --git a/src/widgets/helper/SearchPopup.cpp b/src/widgets/helper/SearchPopup.cpp index 57f0d488..ab59d30b 100644 --- a/src/widgets/helper/SearchPopup.cpp +++ b/src/widgets/helper/SearchPopup.cpp @@ -248,7 +248,7 @@ std::vector SearchPopup::buildSnapshot() ChannelView &sharedView = channel.get(); const FilterSetPtr filterSet = sharedView.getFilterSet(); - const std::vector &snapshot = + std::vector snapshot = sharedView.channel()->getMessageSnapshot(); for (const auto &message : snapshot)