Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: mini reorganization of FfzEmotes.cpp #5117

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 120 additions & 118 deletions src/providers/ffz/FfzEmotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,147 +10,151 @@
#include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Settings.hpp"

namespace chatterino {
namespace {

const QString CHANNEL_HAS_NO_EMOTES(
"This channel has no FrankerFaceZ channel emotes.");

Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
{
auto emote = urls[emoteScale];
if (emote.isUndefined() || emote.isNull())
{
return {""};
}
using namespace chatterino;

assert(emote.isString());
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
const auto &LOG = chatterinoFfzemotes;

return parseFfzUrl(emote.toString());
}
const QString CHANNEL_HAS_NO_EMOTES(
"This channel has no FrankerFaceZ channel emotes.");

void fillInEmoteData(const QJsonObject &urls, const EmoteName &name,
const QString &tooltip, Emote &emoteData)
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
{
auto emote = urls[emoteScale];
if (emote.isUndefined() || emote.isNull())
{
auto url1x = getEmoteLink(urls, "1");
auto url2x = getEmoteLink(urls, "2");
auto url3x = getEmoteLink(urls, "4");

//, code, tooltip
emoteData.name = name;
emoteData.images =
ImageSet{Image::fromUrl(url1x, 1),
url2x.string.isEmpty() ? Image::getEmpty()
: Image::fromUrl(url2x, 0.5),
url3x.string.isEmpty() ? Image::getEmpty()
: Image::fromUrl(url3x, 0.25)};
emoteData.tooltip = {tooltip};
return {""};
}

EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
assert(emote.isString());

return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
}
return parseFfzUrl(emote.toString());
}

void fillInEmoteData(const QJsonObject &urls, const EmoteName &name,
const QString &tooltip, Emote &emoteData)
{
auto url1x = getEmoteLink(urls, "1");
auto url2x = getEmoteLink(urls, "2");
auto url3x = getEmoteLink(urls, "4");

//, code, tooltip
emoteData.name = name;
emoteData.images = ImageSet{
Image::fromUrl(url1x, 1),
url2x.string.isEmpty() ? Image::getEmpty() : Image::fromUrl(url2x, 0.5),
url3x.string.isEmpty() ? Image::getEmpty()
: Image::fromUrl(url3x, 0.25)};
emoteData.tooltip = {tooltip};
}

EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;

return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
}

void parseEmoteSetInto(const QJsonObject &emoteSet, const QString &kind,
EmoteMap &map)
void parseEmoteSetInto(const QJsonObject &emoteSet, const QString &kind,
EmoteMap &map)
{
for (const auto emoteRef : emoteSet["emoticons"].toArray())
{
for (const auto emoteRef : emoteSet["emoticons"].toArray())
const auto emoteJson = emoteRef.toObject();

// margins
auto id = EmoteId{QString::number(emoteJson["id"].toInt())};
auto name = EmoteName{emoteJson["name"].toString()};
auto author =
EmoteAuthor{emoteJson["owner"]["display_name"].toString()};
auto urls = emoteJson["urls"].toObject();
if (emoteJson["animated"].isObject())
{
const auto emoteJson = emoteRef.toObject();

// margins
auto id = EmoteId{QString::number(emoteJson["id"].toInt())};
auto name = EmoteName{emoteJson["name"].toString()};
auto author =
EmoteAuthor{emoteJson["owner"]["display_name"].toString()};
auto urls = emoteJson["urls"].toObject();
if (emoteJson["animated"].isObject())
{
// prefer animated images if available
urls = emoteJson["animated"].toObject();
}

Emote emote;
fillInEmoteData(urls, name,
QString("%1<br>%2 FFZ Emote<br>By: %3")
.arg(name.string, kind, author.string),
emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};

map[name] = cachedOrMake(std::move(emote), id);
// prefer animated images if available
urls = emoteJson["animated"].toObject();
}

Emote emote;
fillInEmoteData(urls, name,
QString("%1<br>%2 FFZ Emote<br>By: %3")
.arg(name.string, kind, author.string),
emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};

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
std::unordered_set<int> defaultSets{};
auto jsonDefaultSets = jsonRoot["default_sets"].toArray();
for (auto jsonDefaultSet : jsonDefaultSets)
{
// Load default sets from the `default_sets` object
std::unordered_set<int> defaultSets{};
auto jsonDefaultSets = jsonRoot["default_sets"].toArray();
for (auto jsonDefaultSet : jsonDefaultSets)
{
defaultSets.insert(jsonDefaultSet.toInt());
}
defaultSets.insert(jsonDefaultSet.toInt());
}

auto emotes = EmoteMap();
auto emotes = EmoteMap();

for (const auto emoteSetRef : jsonRoot["sets"].toObject())
for (const auto emoteSetRef : jsonRoot["sets"].toObject())
{
const auto emoteSet = emoteSetRef.toObject();
auto emoteSetID = emoteSet["id"].toInt();
if (!defaultSets.contains(emoteSetID))
{
const auto emoteSet = emoteSetRef.toObject();
auto emoteSetID = emoteSet["id"].toInt();
if (!defaultSets.contains(emoteSetID))
{
qCDebug(chatterinoFfzemotes)
<< "Skipping global emote set" << emoteSetID
<< "as it's not part of the default sets";
continue;
}

parseEmoteSetInto(emoteSet, "Global", emotes);
qCDebug(LOG) << "Skipping global emote set" << emoteSetID
<< "as it's not part of the default sets";
continue;
}

return emotes;
parseEmoteSetInto(emoteSet, "Global", emotes);
}

std::optional<EmotePtr> parseAuthorityBadge(const QJsonObject &badgeUrls,
const QString &tooltip)
{
std::optional<EmotePtr> authorityBadge;
return emotes;
}

if (!badgeUrls.isEmpty())
{
auto authorityBadge1x = getEmoteLink(badgeUrls, "1");
auto authorityBadge2x = getEmoteLink(badgeUrls, "2");
auto authorityBadge3x = getEmoteLink(badgeUrls, "4");

auto authorityBadgeImageSet = ImageSet{
Image::fromUrl(authorityBadge1x, 1),
authorityBadge2x.string.isEmpty()
? Image::getEmpty()
: Image::fromUrl(authorityBadge2x, 0.5),
authorityBadge3x.string.isEmpty()
? Image::getEmpty()
: Image::fromUrl(authorityBadge3x, 0.25),
};

authorityBadge = std::make_shared<Emote>(Emote{
{""},
authorityBadgeImageSet,
Tooltip{tooltip},
authorityBadge1x,
});
}
return authorityBadge;
std::optional<EmotePtr> parseAuthorityBadge(const QJsonObject &badgeUrls,
const QString &tooltip)
{
std::optional<EmotePtr> authorityBadge;

if (!badgeUrls.isEmpty())
{
auto authorityBadge1x = getEmoteLink(badgeUrls, "1");
auto authorityBadge2x = getEmoteLink(badgeUrls, "2");
auto authorityBadge3x = getEmoteLink(badgeUrls, "4");

auto authorityBadgeImageSet = ImageSet{
Image::fromUrl(authorityBadge1x, 1),
authorityBadge2x.string.isEmpty()
? Image::getEmpty()
: Image::fromUrl(authorityBadge2x, 0.5),
authorityBadge3x.string.isEmpty()
? Image::getEmpty()
: Image::fromUrl(authorityBadge3x, 0.25),
};

authorityBadge = std::make_shared<Emote>(Emote{
.name = {""},
.images = authorityBadgeImageSet,
.tooltip = Tooltip{tooltip},
.homePage = authorityBadge1x,
});
}
return authorityBadge;
}

} // namespace

namespace chatterino {

using namespace ffz::detail;

EmoteMap ffz::detail::parseChannelEmotes(const QJsonObject &jsonRoot)
Expand Down Expand Up @@ -218,8 +222,7 @@ void FfzEmotes::loadChannel(
std::function<void(std::optional<EmotePtr>)> vipBadgeCallback,
bool manualRefresh)
{
qCDebug(chatterinoFfzemotes)
<< "[FFZEmotes] Reload FFZ Channel Emotes for channel" << channelID;
qCDebug(LOG) << "Reload FFZ Channel Emotes for channel" << channelID;

NetworkRequest("https://api.frankerfacez.com/v1/room/id/" + channelID)

Expand Down Expand Up @@ -275,9 +278,8 @@ void FfzEmotes::loadChannel(
{
// TODO: Auto retry in case of a timeout, with a delay
auto errorString = result.formatError();
qCWarning(chatterinoFfzemotes)
<< "Error fetching FFZ emotes for channel" << channelID
<< ", error" << errorString;
qCWarning(LOG) << "Error fetching FFZ emotes for channel"
<< channelID << ", error" << errorString;
shared->addMessage(makeSystemMessage(
QStringLiteral("Failed to fetch FrankerFaceZ channel "
"emotes. (Error: %1)")
Expand Down
Loading