feat: show BTTV Pro badges (#6625)

Reviewed-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2025-12-07 15:15:58 +01:00
committed by GitHub
parent ab8e0751b1
commit 0aff6fbe83
31 changed files with 394 additions and 82 deletions
+7 -51
View File
@@ -4,74 +4,30 @@
#include "messages/Image.hpp"
#include "providers/seventv/SeventvEmotes.hpp"
#include <QJsonArray>
#include <QUrl>
#include <QUrlQuery>
namespace chatterino {
std::optional<EmotePtr> SeventvBadges::getBadge(const UserId &id) const
QString SeventvBadges::idForBadge(const QJsonObject &badgeJson) 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;
return badgeJson["id"].toString();
}
void SeventvBadges::assignBadgeToUser(const QString &badgeID,
const UserId &userID)
EmotePtr SeventvBadges::createBadge(const QString &id,
const QJsonObject &badgeJson) const
{
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 SeventvBadges::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);
}
}
void SeventvBadges::registerBadge(const QJsonObject &badgeJson)
{
const auto badgeID = badgeJson["id"].toString();
const std::unique_lock lock(this->mutex_);
if (this->knownBadges_.find(badgeID) != this->knownBadges_.end())
{
return;
}
auto emote = Emote{
.name = EmoteName{},
.images = SeventvEmotes::createImageSet(badgeJson, true),
.tooltip = Tooltip{badgeJson["tooltip"].toString()},
.homePage = Url{},
.id = EmoteId{badgeID},
.id = EmoteId{id},
};
if (emote.images.getImage1()->isEmpty())
{
return; // Bad images
return nullptr; // Bad images
}
this->knownBadges_[badgeID] =
std::make_shared<const Emote>(std::move(emote));
return std::make_shared<const Emote>(std::move(emote));
}
} // namespace chatterino
+7 -26
View File
@@ -1,44 +1,25 @@
#pragma once
#include "common/Aliases.hpp"
#include "util/QStringHash.hpp"
#include "util/BadgeRegistry.hpp"
#include <QJsonObject>
#include <memory>
#include <optional>
#include <shared_mutex>
#include <unordered_map>
namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class SeventvBadges
class SeventvBadges : public BadgeRegistry
{
public:
// Return the badge, if any, that is assigned to the user
std::optional<EmotePtr> getBadge(const UserId &id) const;
SeventvBadges() = default;
// 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
void registerBadge(const QJsonObject &badgeJson);
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_;
protected:
QString idForBadge(const QJsonObject &badgeJson) const override;
EmotePtr createBadge(const QString &id,
const QJsonObject &badgeJson) const override;
};
} // namespace chatterino