refactor: load Twitch emotes from Helix (#5239)

This commit is contained in:
nerix
2024-09-01 11:22:54 +02:00
committed by GitHub
parent 03b0e4881f
commit 820aa12af6
40 changed files with 1251 additions and 528 deletions
+61
View File
@@ -1,10 +1,15 @@
#include "providers/twitch/TwitchEmotes.hpp"
#include "common/Literals.hpp"
#include "common/QLogging.hpp"
#include "common/UniqueAccess.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "util/QStringHash.hpp"
#include <QStringBuilder>
namespace {
using namespace chatterino;
@@ -399,6 +404,22 @@ qreal getEmote3xScaleFactor(const EmoteId &id)
namespace chatterino {
using namespace literals;
QString TwitchEmoteSet::title() const
{
if (!this->owner || this->owner->name.isEmpty())
{
return "Twitch";
}
if (this->isBits)
{
return this->owner->name + " (Bits)";
}
return this->owner->name;
}
QString TwitchEmotes::cleanUpEmoteCode(const QString &dirtyEmoteCode)
{
auto cleanCode = dirtyEmoteCode;
@@ -453,4 +474,44 @@ EmotePtr TwitchEmotes::getOrCreateEmote(const EmoteId &id,
return shared;
}
TwitchEmoteSetMeta getTwitchEmoteSetMeta(const HelixChannelEmote &emote)
{
// follower emotes are treated as sub emotes
// A sub emote must have an owner or an emote-set id (otherwise it's a
// global like "BopBop")
bool isSub =
(emote.type == u"subscriptions" || emote.type == u"follower") &&
!(emote.ownerID.isEmpty() && emote.setID.isEmpty());
bool isBits = emote.type == u"bitstier";
bool isSubLike = isSub || isBits;
// A lot of emotes don't have their emote-set-id set, so we create a
// virtual emote set that groups emotes by the owner.
// Additionally, a lot of emote sets are small, so they're grouped together as globals.
auto actualSetID = [&]() -> QString {
if (!isSub && !isBits)
{
return u"x-c2-globals"_s;
}
if (!emote.setID.isEmpty())
{
return emote.setID;
}
if (isSub)
{
return TWITCH_SUB_EMOTE_SET_PREFIX % emote.ownerID;
}
// isBits
return TWITCH_BIT_EMOTE_SET_PREFIX % emote.ownerID;
}();
return {
.setID = actualSetID,
.isBits = isBits,
.isSubLike = isSubLike,
};
}
} // namespace chatterino