refactor: mini reorganization of FfzEmotes.cpp (#5117)

This commit is contained in:
pajlada 2024-01-21 12:28:20 +01:00 committed by GitHub
parent 075a7c5af0
commit 65d3e73c5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,14 +10,18 @@
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
namespace chatterino {
namespace { namespace {
const QString CHANNEL_HAS_NO_EMOTES( using namespace chatterino;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
const auto &LOG = chatterinoFfzemotes;
const QString CHANNEL_HAS_NO_EMOTES(
"This channel has no FrankerFaceZ channel emotes."); "This channel has no FrankerFaceZ channel emotes.");
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale) Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
{ {
auto emote = urls[emoteScale]; auto emote = urls[emoteScale];
if (emote.isUndefined() || emote.isNull()) if (emote.isUndefined() || emote.isNull())
{ {
@ -27,37 +31,36 @@ namespace {
assert(emote.isString()); assert(emote.isString());
return parseFfzUrl(emote.toString()); return parseFfzUrl(emote.toString());
} }
void fillInEmoteData(const QJsonObject &urls, const EmoteName &name, void fillInEmoteData(const QJsonObject &urls, const EmoteName &name,
const QString &tooltip, Emote &emoteData) const QString &tooltip, Emote &emoteData)
{ {
auto url1x = getEmoteLink(urls, "1"); auto url1x = getEmoteLink(urls, "1");
auto url2x = getEmoteLink(urls, "2"); auto url2x = getEmoteLink(urls, "2");
auto url3x = getEmoteLink(urls, "4"); auto url3x = getEmoteLink(urls, "4");
//, code, tooltip //, code, tooltip
emoteData.name = name; emoteData.name = name;
emoteData.images = emoteData.images = ImageSet{
ImageSet{Image::fromUrl(url1x, 1), Image::fromUrl(url1x, 1),
url2x.string.isEmpty() ? Image::getEmpty() url2x.string.isEmpty() ? Image::getEmpty() : Image::fromUrl(url2x, 0.5),
: Image::fromUrl(url2x, 0.5),
url3x.string.isEmpty() ? Image::getEmpty() url3x.string.isEmpty() ? Image::getEmpty()
: Image::fromUrl(url3x, 0.25)}; : Image::fromUrl(url3x, 0.25)};
emoteData.tooltip = {tooltip}; emoteData.tooltip = {tooltip};
} }
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id) EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{ {
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache; static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex; static std::mutex mutex;
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id); return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
} }
void parseEmoteSetInto(const QJsonObject &emoteSet, const QString &kind, void parseEmoteSetInto(const QJsonObject &emoteSet, const QString &kind,
EmoteMap &map) EmoteMap &map)
{ {
for (const auto emoteRef : emoteSet["emoticons"].toArray()) for (const auto emoteRef : emoteSet["emoticons"].toArray())
{ {
const auto emoteJson = emoteRef.toObject(); const auto emoteJson = emoteRef.toObject();
@ -86,10 +89,10 @@ namespace {
map[name] = cachedOrMake(std::move(emote), id); map[name] = cachedOrMake(std::move(emote), id);
} }
} }
EmoteMap parseGlobalEmotes(const QJsonObject &jsonRoot) EmoteMap parseGlobalEmotes(const QJsonObject &jsonRoot)
{ {
// Load default sets from the `default_sets` object // Load default sets from the `default_sets` object
std::unordered_set<int> defaultSets{}; std::unordered_set<int> defaultSets{};
auto jsonDefaultSets = jsonRoot["default_sets"].toArray(); auto jsonDefaultSets = jsonRoot["default_sets"].toArray();
@ -106,8 +109,7 @@ namespace {
auto emoteSetID = emoteSet["id"].toInt(); auto emoteSetID = emoteSet["id"].toInt();
if (!defaultSets.contains(emoteSetID)) if (!defaultSets.contains(emoteSetID))
{ {
qCDebug(chatterinoFfzemotes) qCDebug(LOG) << "Skipping global emote set" << emoteSetID
<< "Skipping global emote set" << emoteSetID
<< "as it's not part of the default sets"; << "as it's not part of the default sets";
continue; continue;
} }
@ -116,11 +118,11 @@ namespace {
} }
return emotes; return emotes;
} }
std::optional<EmotePtr> parseAuthorityBadge(const QJsonObject &badgeUrls, std::optional<EmotePtr> parseAuthorityBadge(const QJsonObject &badgeUrls,
const QString &tooltip) const QString &tooltip)
{ {
std::optional<EmotePtr> authorityBadge; std::optional<EmotePtr> authorityBadge;
if (!badgeUrls.isEmpty()) if (!badgeUrls.isEmpty())
@ -140,17 +142,19 @@ namespace {
}; };
authorityBadge = std::make_shared<Emote>(Emote{ authorityBadge = std::make_shared<Emote>(Emote{
{""}, .name = {""},
authorityBadgeImageSet, .images = authorityBadgeImageSet,
Tooltip{tooltip}, .tooltip = Tooltip{tooltip},
authorityBadge1x, .homePage = authorityBadge1x,
}); });
} }
return authorityBadge; return authorityBadge;
} }
} // namespace } // namespace
namespace chatterino {
using namespace ffz::detail; using namespace ffz::detail;
EmoteMap ffz::detail::parseChannelEmotes(const QJsonObject &jsonRoot) EmoteMap ffz::detail::parseChannelEmotes(const QJsonObject &jsonRoot)
@ -218,8 +222,7 @@ void FfzEmotes::loadChannel(
std::function<void(std::optional<EmotePtr>)> vipBadgeCallback, std::function<void(std::optional<EmotePtr>)> vipBadgeCallback,
bool manualRefresh) bool manualRefresh)
{ {
qCDebug(chatterinoFfzemotes) qCDebug(LOG) << "Reload FFZ Channel Emotes for channel" << channelID;
<< "[FFZEmotes] Reload FFZ Channel Emotes for channel" << channelID;
NetworkRequest("https://api.frankerfacez.com/v1/room/id/" + channelID) NetworkRequest("https://api.frankerfacez.com/v1/room/id/" + channelID)
@ -275,9 +278,8 @@ void FfzEmotes::loadChannel(
{ {
// TODO: Auto retry in case of a timeout, with a delay // TODO: Auto retry in case of a timeout, with a delay
auto errorString = result.formatError(); auto errorString = result.formatError();
qCWarning(chatterinoFfzemotes) qCWarning(LOG) << "Error fetching FFZ emotes for channel"
<< "Error fetching FFZ emotes for channel" << channelID << channelID << ", error" << errorString;
<< ", error" << errorString;
shared->addMessage(makeSystemMessage( shared->addMessage(makeSystemMessage(
QStringLiteral("Failed to fetch FrankerFaceZ channel " QStringLiteral("Failed to fetch FrankerFaceZ channel "
"emotes. (Error: %1)") "emotes. (Error: %1)")