Support Animated FFZ Emotes and Authors for Global Emotes (#4434)

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2023-03-18 12:22:40 +01:00
committed by GitHub
parent 93a9e41d31
commit d7206a2203
2 changed files with 70 additions and 82 deletions
+1
View File
@@ -2,6 +2,7 @@
## Unversioned ## Unversioned
- Minor: Added support for FrankerFaceZ animated emotes. (#4434)
- Dev: Only log debug messages when NDEBUG is not defined. (#4442) - Dev: Only log debug messages when NDEBUG is not defined. (#4442)
- Dev: Cleaned up theme related code. (#4450) - Dev: Cleaned up theme related code. (#4450)
+69 -82
View File
@@ -19,7 +19,7 @@ namespace {
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale) Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
{ {
auto emote = urls.value(emoteScale); auto emote = urls[emoteScale];
if (emote.isUndefined() || emote.isNull()) if (emote.isUndefined() || emote.isNull())
{ {
return {""}; return {""};
@@ -47,6 +47,7 @@ namespace {
: 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;
@@ -54,25 +55,57 @@ namespace {
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id); return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
} }
std::pair<Outcome, EmoteMap> parseGlobalEmotes(
const QJsonObject &jsonRoot, const EmoteMap &currentEmotes) void parseEmoteSetInto(const QJsonObject &emoteSet, const QString &kind,
EmoteMap &map)
{
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())
{
// 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)
{ {
// 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.value("default_sets").toArray(); auto jsonDefaultSets = jsonRoot["default_sets"].toArray();
for (auto jsonDefaultSet : jsonDefaultSets) for (auto jsonDefaultSet : jsonDefaultSets)
{ {
defaultSets.insert(jsonDefaultSet.toInt()); defaultSets.insert(jsonDefaultSet.toInt());
} }
auto jsonSets = jsonRoot.value("sets").toObject();
auto emotes = EmoteMap(); auto emotes = EmoteMap();
for (auto jsonSet : jsonSets) for (const auto emoteSetRef : jsonRoot["sets"].toObject())
{ {
auto jsonSetObject = jsonSet.toObject(); const auto emoteSet = emoteSetRef.toObject();
const auto emoteSetID = jsonSetObject.value("id").toInt(); auto emoteSetID = emoteSet["id"].toInt();
if (defaultSets.find(emoteSetID) == defaultSets.end()) if (!defaultSets.contains(emoteSetID))
{ {
qCDebug(chatterinoFfzemotes) qCDebug(chatterinoFfzemotes)
<< "Skipping global emote set" << emoteSetID << "Skipping global emote set" << emoteSetID
@@ -80,35 +113,14 @@ namespace {
continue; continue;
} }
auto jsonEmotes = jsonSetObject.value("emoticons").toArray(); parseEmoteSetInto(emoteSet, "Global", emotes);
for (auto jsonEmoteValue : jsonEmotes)
{
auto jsonEmote = jsonEmoteValue.toObject();
auto name = EmoteName{jsonEmote.value("name").toString()};
auto id =
EmoteId{QString::number(jsonEmote.value("id").toInt())};
auto urls = jsonEmote.value("urls").toObject();
auto emote = Emote();
fillInEmoteData(urls, name,
name.string + "<br>Global FFZ Emote", emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};
emotes[name] =
cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
}
} }
return {Success, std::move(emotes)}; return emotes;
} }
boost::optional<EmotePtr> parseAuthorityBadge(const QJsonObject &badgeUrls, boost::optional<EmotePtr> parseAuthorityBadge(const QJsonObject &badgeUrls,
const QString tooltip) const QString &tooltip)
{ {
boost::optional<EmotePtr> authorityBadge; boost::optional<EmotePtr> authorityBadge;
@@ -140,40 +152,11 @@ namespace {
EmoteMap parseChannelEmotes(const QJsonObject &jsonRoot) EmoteMap parseChannelEmotes(const QJsonObject &jsonRoot)
{ {
auto jsonSets = jsonRoot.value("sets").toObject();
auto emotes = EmoteMap(); auto emotes = EmoteMap();
for (auto jsonSet : jsonSets) for (const auto emoteSetRef : jsonRoot["sets"].toObject())
{ {
auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray(); parseEmoteSetInto(emoteSetRef.toObject(), "Channel", emotes);
for (auto _jsonEmote : jsonEmotes)
{
auto jsonEmote = _jsonEmote.toObject();
// margins
auto id =
EmoteId{QString::number(jsonEmote.value("id").toInt())};
auto name = EmoteName{jsonEmote.value("name").toString()};
auto author = EmoteAuthor{jsonEmote.value("owner")
.toObject()
.value("display_name")
.toString()};
auto urls = jsonEmote.value("urls").toObject();
Emote emote;
fillInEmoteData(urls, name,
QString("%1<br>Channel FFZ Emote<br>By: %2")
.arg(name.string)
.arg(author.string),
emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};
emotes[name] = cachedOrMake(std::move(emote), id);
}
} }
return emotes; return emotes;
@@ -195,7 +178,9 @@ boost::optional<EmotePtr> FfzEmotes::emote(const EmoteName &name) const
auto emotes = this->global_.get(); auto emotes = this->global_.get();
auto it = emotes->find(name); auto it = emotes->find(name);
if (it != emotes->end()) if (it != emotes->end())
{
return it->second; return it->second;
}
return boost::none; return boost::none;
} }
@@ -213,41 +198,38 @@ void FfzEmotes::loadEmotes()
.timeout(30000) .timeout(30000)
.onSuccess([this](auto result) -> Outcome { .onSuccess([this](auto result) -> Outcome {
auto emotes = this->emotes(); auto parsedSet = parseGlobalEmotes(result.parseJson());
auto pair = parseGlobalEmotes(result.parseJson(), *emotes); this->global_.set(std::make_shared<EmoteMap>(std::move(parsedSet)));
if (pair.first)
this->global_.set( return Success;
std::make_shared<EmoteMap>(std::move(pair.second)));
return pair.first;
}) })
.execute(); .execute();
} }
void FfzEmotes::loadChannel( void FfzEmotes::loadChannel(
std::weak_ptr<Channel> channel, const QString &channelId, std::weak_ptr<Channel> channel, const QString &channelID,
std::function<void(EmoteMap &&)> emoteCallback, std::function<void(EmoteMap &&)> emoteCallback,
std::function<void(boost::optional<EmotePtr>)> modBadgeCallback, std::function<void(boost::optional<EmotePtr>)> modBadgeCallback,
std::function<void(boost::optional<EmotePtr>)> vipBadgeCallback, std::function<void(boost::optional<EmotePtr>)> vipBadgeCallback,
bool manualRefresh) bool manualRefresh)
{ {
qCDebug(chatterinoFfzemotes) qCDebug(chatterinoFfzemotes)
<< "[FFZEmotes] 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)
.timeout(20000) .timeout(20000)
.onSuccess([emoteCallback = std::move(emoteCallback), .onSuccess([emoteCallback = std::move(emoteCallback),
modBadgeCallback = std::move(modBadgeCallback), modBadgeCallback = std::move(modBadgeCallback),
vipBadgeCallback = std::move(vipBadgeCallback), channel, vipBadgeCallback = std::move(vipBadgeCallback), channel,
manualRefresh](auto result) -> Outcome { manualRefresh](const auto &result) -> Outcome {
auto json = result.parseJson(); const auto json = result.parseJson();
auto emoteMap = parseChannelEmotes(json); auto emoteMap = parseChannelEmotes(json);
auto modBadge = parseAuthorityBadge( auto modBadge = parseAuthorityBadge(
json.value("room").toObject().value("mod_urls").toObject(), json["room"]["mod_urls"].toObject(), "Moderator");
"Moderator");
auto vipBadge = parseAuthorityBadge( auto vipBadge = parseAuthorityBadge(
json.value("room").toObject().value("vip_badge").toObject(), json["room"]["vip_badge"].toObject(), "VIP");
"VIP");
bool hasEmotes = !emoteMap.empty(); bool hasEmotes = !emoteMap.empty();
@@ -270,22 +252,27 @@ void FfzEmotes::loadChannel(
return Success; return Success;
}) })
.onError([channelId, channel, manualRefresh](NetworkResult result) { .onError([channelID, channel, manualRefresh](const auto &result) {
auto shared = channel.lock(); auto shared = channel.lock();
if (!shared) if (!shared)
{
return; return;
}
if (result.status() == 404) if (result.status() == 404)
{ {
// User does not have any FFZ emotes // User does not have any FFZ emotes
if (manualRefresh) if (manualRefresh)
{
shared->addMessage( shared->addMessage(
makeSystemMessage(CHANNEL_HAS_NO_EMOTES)); makeSystemMessage(CHANNEL_HAS_NO_EMOTES));
}
} }
else if (result.status() == NetworkResult::timedoutStatus) else if (result.status() == NetworkResult::timedoutStatus)
{ {
// TODO: Auto retry in case of a timeout, with a delay // TODO: Auto retry in case of a timeout, with a delay
qCWarning(chatterinoFfzemotes) qCWarning(chatterinoFfzemotes)
<< "Fetching FFZ emotes for channel" << channelId << "Fetching FFZ emotes for channel" << channelID
<< "failed due to timeout"; << "failed due to timeout";
shared->addMessage( shared->addMessage(
makeSystemMessage("Failed to fetch FrankerFaceZ channel " makeSystemMessage("Failed to fetch FrankerFaceZ channel "
@@ -294,7 +281,7 @@ void FfzEmotes::loadChannel(
else else
{ {
qCWarning(chatterinoFfzemotes) qCWarning(chatterinoFfzemotes)
<< "Error fetching FFZ emotes for channel" << channelId << "Error fetching FFZ emotes for channel" << channelID
<< ", error" << result.status(); << ", error" << result.status();
shared->addMessage( shared->addMessage(
makeSystemMessage("Failed to fetch FrankerFaceZ channel " makeSystemMessage("Failed to fetch FrankerFaceZ channel "