refactor: simplify uses of getMessageSnapshot (#6607)
Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -53,6 +53,7 @@
|
|||||||
- Dev: Check Lua unwinding and version in tests. (#6586)
|
- Dev: Check Lua unwinding and version in tests. (#6586)
|
||||||
- Dev: Added method to get the last N messages of a channel. (#6602, #6604)
|
- Dev: Added method to get the last N messages of a channel. (#6602, #6604)
|
||||||
- Dev: Unwrapped `LimitedQueueSnapshot` to `std::vector`. (#6606)
|
- Dev: Unwrapped `LimitedQueueSnapshot` to `std::vector`. (#6606)
|
||||||
|
- Dev: Simplified uses of `getMessageSnapshot`. (#6607)
|
||||||
|
|
||||||
## 2.5.4
|
## 2.5.4
|
||||||
|
|
||||||
|
|||||||
+12
-6
@@ -81,6 +81,16 @@ std::vector<MessagePtr> Channel::getMessageSnapshot(size_t nItems) const
|
|||||||
return this->messages_.lastN(nItems);
|
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,
|
void Channel::addMessage(MessagePtr message, MessageContext context,
|
||||||
std::optional<MessageFlags> overridingFlags)
|
std::optional<MessageFlags> overridingFlags)
|
||||||
{
|
{
|
||||||
@@ -146,19 +156,15 @@ void Channel::addOrReplaceClearChat(MessagePtr message, const QDateTime &now)
|
|||||||
|
|
||||||
void Channel::disableAllMessages()
|
void Channel::disableAllMessages()
|
||||||
{
|
{
|
||||||
std::vector<MessagePtr> snapshot = this->getMessageSnapshot();
|
for (const auto &message : this->getMessageSnapshot())
|
||||||
int snapshotLength = snapshot.size();
|
|
||||||
for (int i = 0; i < snapshotLength; i++)
|
|
||||||
{
|
{
|
||||||
const auto &message = snapshot[i];
|
|
||||||
if (message->flags.hasAny({MessageFlag::System, MessageFlag::Timeout,
|
if (message->flags.hasAny({MessageFlag::System, MessageFlag::Timeout,
|
||||||
MessageFlag::Whisper}))
|
MessageFlag::Whisper}))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FOURTF: disabled for now
|
message->flags.set(MessageFlag::Disabled);
|
||||||
const_cast<Message *>(message.get())->flags.set(MessageFlag::Disabled);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,9 +79,14 @@ public:
|
|||||||
virtual const QString &getLocalizedName() const;
|
virtual const QString &getLocalizedName() const;
|
||||||
bool isTwitchChannel() const;
|
bool isTwitchChannel() const;
|
||||||
virtual bool isEmpty() const;
|
virtual bool isEmpty() const;
|
||||||
|
|
||||||
std::vector<MessagePtr> getMessageSnapshot() const;
|
std::vector<MessagePtr> getMessageSnapshot() const;
|
||||||
std::vector<MessagePtr> getMessageSnapshot(size_t nItems) const;
|
std::vector<MessagePtr> 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
|
// MESSAGES
|
||||||
// overridingFlags can be filled in with flags that should be used instead
|
// 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
|
// of the message's flags. This is useful in case a flag is specific to a
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
#include "providers/twitch/TwitchChannel.hpp"
|
#include "providers/twitch/TwitchChannel.hpp"
|
||||||
#include "util/Twitch.hpp"
|
#include "util/Twitch.hpp"
|
||||||
|
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
namespace chatterino::commands {
|
namespace chatterino::commands {
|
||||||
|
|
||||||
QString sendReply(const CommandContext &ctx)
|
QString sendReply(const CommandContext &ctx)
|
||||||
@@ -32,9 +34,8 @@ QString sendReply(const CommandContext &ctx)
|
|||||||
stripChannelName(username);
|
stripChannelName(username);
|
||||||
|
|
||||||
auto snapshot = ctx.twitchChannel->getMessageSnapshot();
|
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)
|
if (msg->loginName.compare(username, Qt::CaseInsensitive) == 0)
|
||||||
{
|
{
|
||||||
// found most recent message by user
|
// found most recent message by user
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
namespace ranges = std::ranges;
|
namespace ranges = std::ranges;
|
||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
@@ -154,19 +156,10 @@ void NotificationController::notifyTwitchChannelLive(
|
|||||||
void NotificationController::notifyTwitchChannelOffline(const QString &id) const
|
void NotificationController::notifyTwitchChannelOffline(const QString &id) const
|
||||||
{
|
{
|
||||||
// "delete" old 'CHANNEL is live' message
|
// "delete" old 'CHANNEL is live' message
|
||||||
const std::vector<MessagePtr> snapshot =
|
auto snapshot =
|
||||||
getApp()->getTwitch()->getLiveChannel()->getMessageSnapshot(200);
|
getApp()->getTwitch()->getLiveChannel()->getMessageSnapshot(200);
|
||||||
|
for (const auto &s : snapshot | std::views::reverse)
|
||||||
// Guard against empty snapshot to prevent underflow and invalid access
|
|
||||||
if (snapshot.size() == 0)
|
|
||||||
{
|
{
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = snapshot.size() - 1; i >= 0; --i)
|
|
||||||
{
|
|
||||||
const auto &s = snapshot[i];
|
|
||||||
|
|
||||||
if (s->id == id)
|
if (s->id == id)
|
||||||
{
|
{
|
||||||
s->flags.set(MessageFlag::Disabled);
|
s->flags.set(MessageFlag::Disabled);
|
||||||
|
|||||||
@@ -479,15 +479,14 @@ void TwitchIrcServer::onReadConnected(IrcConnection *connection)
|
|||||||
|
|
||||||
for (const auto &chan : activeChannels)
|
for (const auto &chan : activeChannels)
|
||||||
{
|
{
|
||||||
std::vector<MessagePtr> snapshot = chan->getMessageSnapshot();
|
MessagePtr last = chan->getLastMessage();
|
||||||
|
|
||||||
bool replaceMessage =
|
bool replaceMessage =
|
||||||
snapshot.size() > 0 && snapshot[snapshot.size() - 1]->flags.has(
|
last && last->flags.has(MessageFlag::DisconnectedMessage);
|
||||||
MessageFlag::DisconnectedMessage);
|
|
||||||
|
|
||||||
if (replaceMessage)
|
if (replaceMessage)
|
||||||
{
|
{
|
||||||
chan->replaceMessage(snapshot[snapshot.size() - 1], reconnected);
|
chan->replaceMessage(last, reconnected);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -483,7 +483,7 @@ void EmotePopup::reloadEmotes()
|
|||||||
"7TV");
|
"7TV");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subChannel->getMessageSnapshot().size() == 0)
|
if (!subChannel->hasMessages())
|
||||||
{
|
{
|
||||||
MessageBuilder builder;
|
MessageBuilder builder;
|
||||||
builder->flags.set(MessageFlag::Centered);
|
builder->flags.set(MessageFlag::Centered);
|
||||||
|
|||||||
@@ -121,10 +121,8 @@ ChannelPtr filterMessages(const QString &userName, ChannelPtr channel)
|
|||||||
std::make_shared<Channel>(channel->getName(), Channel::Type::None);
|
std::make_shared<Channel>(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))
|
if (checkMessageUserName(userName, message))
|
||||||
{
|
{
|
||||||
channelPtr->addMessage(message, MessageContext::Repost);
|
channelPtr->addMessage(message, MessageContext::Repost);
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ std::vector<MessagePtr> SearchPopup::buildSnapshot()
|
|||||||
ChannelView &sharedView = channel.get();
|
ChannelView &sharedView = channel.get();
|
||||||
|
|
||||||
const FilterSetPtr filterSet = sharedView.getFilterSet();
|
const FilterSetPtr filterSet = sharedView.getFilterSet();
|
||||||
const std::vector<MessagePtr> &snapshot =
|
std::vector<MessagePtr> snapshot =
|
||||||
sharedView.channel()->getMessageSnapshot();
|
sharedView.channel()->getMessageSnapshot();
|
||||||
|
|
||||||
for (const auto &message : snapshot)
|
for (const auto &message : snapshot)
|
||||||
|
|||||||
Reference in New Issue
Block a user