feat: show BTTV Pro badges (#6625)
Reviewed-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#include "util/BadgeRegistry.hpp"
|
||||
|
||||
#include "messages/Emote.hpp"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
std::optional<EmotePtr> BadgeRegistry::getBadge(const UserId &id) const
|
||||
{
|
||||
std::shared_lock lock(this->mutex_);
|
||||
|
||||
auto it = this->badgeMap_.find(id.string);
|
||||
if (it != this->badgeMap_.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void BadgeRegistry::assignBadgeToUser(const QString &badgeID,
|
||||
const UserId &userID)
|
||||
{
|
||||
const std::unique_lock lock(this->mutex_);
|
||||
|
||||
const auto badgeIt = this->knownBadges_.find(badgeID);
|
||||
if (badgeIt != this->knownBadges_.end())
|
||||
{
|
||||
this->badgeMap_[userID.string] = badgeIt->second;
|
||||
}
|
||||
}
|
||||
|
||||
void BadgeRegistry::clearBadgeFromUser(const QString &badgeID,
|
||||
const UserId &userID)
|
||||
{
|
||||
const std::unique_lock lock(this->mutex_);
|
||||
|
||||
const auto it = this->badgeMap_.find(userID.string);
|
||||
if (it != this->badgeMap_.end() && it->second->id.string == badgeID)
|
||||
{
|
||||
this->badgeMap_.erase(userID.string);
|
||||
}
|
||||
}
|
||||
|
||||
QString BadgeRegistry::registerBadge(const QJsonObject &badgeJson)
|
||||
{
|
||||
const auto badgeID = this->idForBadge(badgeJson);
|
||||
|
||||
const std::unique_lock lock(this->mutex_);
|
||||
|
||||
if (this->knownBadges_.contains(badgeID))
|
||||
{
|
||||
return badgeID;
|
||||
}
|
||||
|
||||
auto emote = this->createBadge(badgeID, badgeJson);
|
||||
if (!emote)
|
||||
{
|
||||
return badgeID;
|
||||
}
|
||||
|
||||
this->knownBadges_[badgeID] = std::move(emote);
|
||||
return badgeID;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/Aliases.hpp"
|
||||
|
||||
#include <QJsonObject>
|
||||
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
struct Emote;
|
||||
using EmotePtr = std::shared_ptr<const Emote>;
|
||||
|
||||
class BadgeRegistry
|
||||
{
|
||||
public:
|
||||
virtual ~BadgeRegistry() = default;
|
||||
|
||||
/// Return the badge, if any, that is assigned to the user
|
||||
std::optional<EmotePtr> getBadge(const UserId &id) const;
|
||||
|
||||
/// Assign the given badge to the user
|
||||
void assignBadgeToUser(const QString &badgeID, const UserId &userID);
|
||||
|
||||
/// Remove the given badge from the user
|
||||
void clearBadgeFromUser(const QString &badgeID, const UserId &userID);
|
||||
|
||||
/// Register a new known badge
|
||||
/// The json object will contain all information about the badge, like its ID & its images
|
||||
/// @returns The badge's ID
|
||||
QString registerBadge(const QJsonObject &badgeJson);
|
||||
|
||||
protected:
|
||||
BadgeRegistry() = default;
|
||||
|
||||
virtual QString idForBadge(const QJsonObject &badgeJson) const = 0;
|
||||
virtual EmotePtr createBadge(const QString &id,
|
||||
const QJsonObject &badgeJson) const = 0;
|
||||
|
||||
private:
|
||||
/// Mutex for both `badgeMap_` and `knownBadges_`
|
||||
mutable std::shared_mutex mutex_;
|
||||
|
||||
/// user-id => badge
|
||||
std::unordered_map<QString, EmotePtr> badgeMap_;
|
||||
/// badge-id => badge
|
||||
std::unordered_map<QString, EmotePtr> knownBadges_;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
Reference in New Issue
Block a user