feat: add ability to limit message snapshot size (#6602)
Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -51,6 +51,7 @@
|
||||
- Dev: Mock headers are now added as a header set if supported by CMake. (#6561)
|
||||
- Dev: Set settings directory to temporary one used in tests. (#6584)
|
||||
- Dev: Check Lua unwinding and version in tests. (#6586)
|
||||
- Dev: Added method to get the last N messages of a channel. (#6602)
|
||||
|
||||
## 2.5.4
|
||||
|
||||
|
||||
@@ -71,11 +71,17 @@ bool Channel::hasMessages() const
|
||||
return !this->messages_.empty();
|
||||
}
|
||||
|
||||
LimitedQueueSnapshot<MessagePtr> Channel::getMessageSnapshot()
|
||||
LimitedQueueSnapshot<MessagePtr> Channel::getMessageSnapshot() const
|
||||
{
|
||||
return this->messages_.getSnapshot();
|
||||
}
|
||||
|
||||
LimitedQueueSnapshot<MessagePtr> Channel::getMessageSnapshot(
|
||||
size_t nItems) const
|
||||
{
|
||||
return this->messages_.lastN(nItems);
|
||||
}
|
||||
|
||||
void Channel::addMessage(MessagePtr message, MessageContext context,
|
||||
std::optional<MessageFlags> overridingFlags)
|
||||
{
|
||||
@@ -117,7 +123,7 @@ void Channel::addSystemMessage(const QString &contents)
|
||||
void Channel::addOrReplaceTimeout(MessagePtr message, const QDateTime &now)
|
||||
{
|
||||
addOrReplaceChannelTimeout(
|
||||
this->getMessageSnapshot(), std::move(message), now,
|
||||
this->getMessageSnapshot(20), std::move(message), now,
|
||||
[this](auto /*idx*/, auto msg, auto replacement) {
|
||||
this->replaceMessage(msg, replacement);
|
||||
},
|
||||
@@ -130,7 +136,7 @@ void Channel::addOrReplaceTimeout(MessagePtr message, const QDateTime &now)
|
||||
void Channel::addOrReplaceClearChat(MessagePtr message, const QDateTime &now)
|
||||
{
|
||||
addOrReplaceChannelClear(
|
||||
this->getMessageSnapshot(), std::move(message), now,
|
||||
this->getMessageSnapshot(20), std::move(message), now,
|
||||
[this](auto /*idx*/, auto msg, auto replacement) {
|
||||
this->replaceMessage(msg, replacement);
|
||||
},
|
||||
|
||||
@@ -79,7 +79,8 @@ public:
|
||||
virtual const QString &getLocalizedName() const;
|
||||
bool isTwitchChannel() const;
|
||||
virtual bool isEmpty() const;
|
||||
LimitedQueueSnapshot<MessagePtr> getMessageSnapshot();
|
||||
LimitedQueueSnapshot<MessagePtr> getMessageSnapshot() const;
|
||||
LimitedQueueSnapshot<MessagePtr> getMessageSnapshot(size_t nItems) const;
|
||||
|
||||
// MESSAGES
|
||||
// overridingFlags can be filled in with flags that should be used instead
|
||||
|
||||
@@ -154,13 +154,9 @@ void NotificationController::notifyTwitchChannelLive(
|
||||
void NotificationController::notifyTwitchChannelOffline(const QString &id) const
|
||||
{
|
||||
// "delete" old 'CHANNEL is live' message
|
||||
LimitedQueueSnapshot<MessagePtr> snapshot =
|
||||
getApp()->getTwitch()->getLiveChannel()->getMessageSnapshot();
|
||||
int snapshotLength = static_cast<int>(snapshot.size());
|
||||
|
||||
int end = std::max(0, snapshotLength - 200);
|
||||
|
||||
for (int i = snapshotLength - 1; i >= end; --i)
|
||||
const LimitedQueueSnapshot<MessagePtr> snapshot =
|
||||
getApp()->getTwitch()->getLiveChannel()->getMessageSnapshot(200);
|
||||
for (size_t i = snapshot.size() - 1; i >= 0; --i)
|
||||
{
|
||||
const auto &s = snapshot[i];
|
||||
|
||||
|
||||
@@ -324,6 +324,18 @@ public:
|
||||
return LimitedQueueSnapshot<T>(this->buffer_);
|
||||
}
|
||||
|
||||
[[nodiscard]] LimitedQueueSnapshot<T> lastN(size_t nItems) const
|
||||
{
|
||||
std::shared_lock lock(this->mutex_);
|
||||
return LimitedQueueSnapshot<T>::lastN(this->buffer_, nItems);
|
||||
}
|
||||
|
||||
[[nodiscard]] LimitedQueueSnapshot<T> firstN(size_t nItems) const
|
||||
{
|
||||
std::shared_lock lock(this->mutex_);
|
||||
return LimitedQueueSnapshot<T>::firstN(this->buffer_, nItems);
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,24 @@ private:
|
||||
{
|
||||
}
|
||||
|
||||
static LimitedQueueSnapshot lastN(const boost::circular_buffer<T> &buf,
|
||||
size_t n)
|
||||
{
|
||||
return {buf.end() - std::min(n, buf.size()), buf.end()};
|
||||
}
|
||||
|
||||
static LimitedQueueSnapshot firstN(const boost::circular_buffer<T> &buf,
|
||||
size_t n)
|
||||
{
|
||||
return {buf.begin(), buf.begin() + std::min(n, buf.size())};
|
||||
}
|
||||
|
||||
template <typename It>
|
||||
LimitedQueueSnapshot(It begin, It end)
|
||||
: buffer_(std::move(begin), std::move(end))
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
LimitedQueueSnapshot() = default;
|
||||
|
||||
|
||||
@@ -264,3 +264,51 @@ TEST(LimitedQueue, Find)
|
||||
})
|
||||
.has_value());
|
||||
}
|
||||
|
||||
TEST(LimitedQueue, LastN)
|
||||
{
|
||||
LimitedQueue<int> queue(10);
|
||||
queue.pushBack(1);
|
||||
queue.pushBack(2);
|
||||
queue.pushBack(3);
|
||||
queue.pushBack(4);
|
||||
queue.pushBack(5);
|
||||
queue.pushBack(6);
|
||||
|
||||
SNAPSHOT_EQUALS(queue.lastN(0), {}, "no item");
|
||||
SNAPSHOT_EQUALS(queue.lastN(1), {6}, "one item");
|
||||
SNAPSHOT_EQUALS(queue.lastN(2), {5, 6}, "two items");
|
||||
SNAPSHOT_EQUALS(queue.lastN(6), {1, 2, 3, 4, 5, 6}, "all items");
|
||||
SNAPSHOT_EQUALS(queue.lastN(7), {1, 2, 3, 4, 5, 6}, "all items");
|
||||
SNAPSHOT_EQUALS(queue.lastN(12), {1, 2, 3, 4, 5, 6}, "all items");
|
||||
|
||||
LimitedQueue<int> empty(10);
|
||||
SNAPSHOT_EQUALS(empty.lastN(0), {}, "empty");
|
||||
SNAPSHOT_EQUALS(empty.lastN(1), {}, "empty");
|
||||
SNAPSHOT_EQUALS(empty.lastN(2), {}, "empty");
|
||||
SNAPSHOT_EQUALS(empty.lastN(6), {}, "empty");
|
||||
}
|
||||
|
||||
TEST(LimitedQueue, FirstN)
|
||||
{
|
||||
LimitedQueue<int> queue(10);
|
||||
queue.pushBack(1);
|
||||
queue.pushBack(2);
|
||||
queue.pushBack(3);
|
||||
queue.pushBack(4);
|
||||
queue.pushBack(5);
|
||||
queue.pushBack(6);
|
||||
|
||||
SNAPSHOT_EQUALS(queue.firstN(0), {}, "no item");
|
||||
SNAPSHOT_EQUALS(queue.firstN(1), {1}, "one item");
|
||||
SNAPSHOT_EQUALS(queue.firstN(2), {1, 2}, "two items");
|
||||
SNAPSHOT_EQUALS(queue.firstN(6), {1, 2, 3, 4, 5, 6}, "all items");
|
||||
SNAPSHOT_EQUALS(queue.firstN(7), {1, 2, 3, 4, 5, 6}, "all items");
|
||||
SNAPSHOT_EQUALS(queue.firstN(12), {1, 2, 3, 4, 5, 6}, "all items");
|
||||
|
||||
LimitedQueue<int> empty(10);
|
||||
SNAPSHOT_EQUALS(empty.firstN(0), {}, "empty");
|
||||
SNAPSHOT_EQUALS(empty.firstN(1), {}, "empty");
|
||||
SNAPSHOT_EQUALS(empty.firstN(2), {}, "empty");
|
||||
SNAPSHOT_EQUALS(empty.firstN(6), {}, "empty");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user