Add search predicates for badges and sub tiers (#4013)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
xel86
2022-10-01 08:30:29 -04:00
committed by GitHub
parent b5241670ae
commit bfcc9ff7a4
7 changed files with 191 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
#include "messages/search/BadgePredicate.hpp"
#include "util/Qt.hpp"
namespace chatterino {
BadgePredicate::BadgePredicate(const QStringList &badges)
{
// Check if any comma-seperated values were passed and transform those
for (const auto &entry : badges)
{
for (const auto &badge : entry.split(',', Qt::SkipEmptyParts))
{
// convert short form name of certain badges to formal name
if (badge.compare("mod", Qt::CaseInsensitive) == 0)
{
this->badges_ << "moderator";
}
else if (badge.compare("sub", Qt::CaseInsensitive) == 0)
{
this->badges_ << "subscriber";
}
else if (badge.compare("prime", Qt::CaseInsensitive) == 0)
{
this->badges_ << "premium";
}
else
{
this->badges_ << badge;
}
}
}
}
bool BadgePredicate::appliesTo(const Message &message)
{
for (const Badge &badge : message.badges)
{
if (badges_.contains(badge.key_, Qt::CaseInsensitive))
{
return true;
}
}
return false;
}
} // namespace chatterino
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include "messages/search/MessagePredicate.hpp"
namespace chatterino {
/**
* @brief MessagePredicate checking for the badges of a message.
*
* This predicate will only allow messages that are sent by a list of users,
* specified by their user names, who have a badge specified (i.e 'staff').
*/
class BadgePredicate : public MessagePredicate
{
public:
/**
* @brief Create an BadgePredicate with a list of badges to search for.
*
* @param badges a list of badges that a message should contain
*/
BadgePredicate(const QStringList &badges);
/**
* @brief Checks whether the message contains any of the badges passed
* in the constructor.
*
* @param message the message to check
* @return true if the message contains a badge listed in the specified badges,
* false otherwise
*/
bool appliesTo(const Message &message) override;
private:
/// Holds the badges that will be searched for
QStringList badges_;
};
} // namespace chatterino
+35
View File
@@ -0,0 +1,35 @@
#include "messages/search/SubtierPredicate.hpp"
#include "util/Qt.hpp"
namespace chatterino {
SubtierPredicate::SubtierPredicate(const QStringList &subtiers)
{
// Check if any comma-seperated values were passed and transform those
for (const auto &entry : subtiers)
{
for (const auto &subtier : entry.split(',', Qt::SkipEmptyParts))
{
this->subtiers_ << subtier;
}
}
}
bool SubtierPredicate::appliesTo(const Message &message)
{
for (const Badge &badge : message.badges)
{
if (badge.key_ == "subscriber")
{
const auto &subTier =
badge.value_.length() > 3 ? badge.value_.at(0) : '1';
return subtiers_.contains(subTier);
}
}
return false;
}
} // namespace chatterino
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include "messages/search/MessagePredicate.hpp"
namespace chatterino {
/**
* @brief MessagePredicate checking for the badges of a message.
*
* This predicate will only allow messages that are sent by a subscribed user
* who has a specified subtier (i.e. 1,2,3..)
*/
class SubtierPredicate : public MessagePredicate
{
public:
/**
* @brief Create an SubtierPredicate with a list of subtiers to search for.
*
* @param subtiers a list of subtiers that a message should contain
*/
SubtierPredicate(const QStringList &subtiers);
/**
* @brief Checks whether the message contains any of the subtiers passed
* in the constructor.
*
* @param message the message to check
* @return true if the message contains a subtier listed in the specified subtiers,
* false otherwise
*/
bool appliesTo(const Message &message) override;
private:
/// Holds the subtiers that will be searched for
QStringList subtiers_;
};
} // namespace chatterino