Files
chatterino2/src/providers/seventv/SeventvBadges.cpp
nerix 33fa3e0a97 Use New 7TV Cosmetics System (#4512)
* feat(seventv): use new cosmetics system

* chore: add changelog entry

* fix: old `clang-format`

* fix: small suggestions pt1

* refactor: add 7tv api wrapper

* fix: small clang-tidy things

* fix: remove unused constants

* fix: old clangtidy

* refactor: rename

* fix: increase interval to 60s

* fix: newline

* fix: Twitch

* docs: add comment

* fix: remove v2 badges endpoint

* fix: deadlock

This is actually really sad.

* fix: remove api entry

* fix: old clang-format

* Sort functions in SeventvBadges.hpp/cpp

* Remove unused vector include

* Add comments to SeventvBadges.hpp functions

* Rename `addBadge` to `registerBadge`

* fix: cleanup eventloop

* ci(test): add timeout

---------

Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
2023-07-29 09:49:44 +00:00

81 lines
2.0 KiB
C++

#include "providers/seventv/SeventvBadges.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
#include "providers/seventv/SeventvAPI.hpp"
#include "providers/seventv/SeventvEmotes.hpp"
#include <QJsonArray>
#include <QUrl>
#include <QUrlQuery>
#include <map>
namespace chatterino {
boost::optional<EmotePtr> SeventvBadges::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 boost::none;
}
void SeventvBadges::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 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),
.tooltip = Tooltip{badgeJson["tooltip"].toString()},
.homePage = Url{},
.id = EmoteId{badgeID},
};
if (emote.images.getImage1()->isEmpty())
{
return; // Bad images
}
this->knownBadges_[badgeID] =
std::make_shared<const Emote>(std::move(emote));
}
} // namespace chatterino