Add cached emotes fallback for failed provider fetches (#6125)
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "providers/ffz/FfzUtil.hpp"
|
||||
#include "providers/twitch/TwitchChannel.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "util/Helpers.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -237,15 +238,26 @@ void FfzEmotes::loadEmotes()
|
||||
return;
|
||||
}
|
||||
|
||||
readProviderEmotesCache("global", "frankerfacez", [this](auto jsonDoc) {
|
||||
auto parsedSet = parseGlobalEmotes(jsonDoc.object());
|
||||
this->setEmotes(std::make_shared<EmoteMap>(std::move(parsedSet)));
|
||||
});
|
||||
|
||||
QString url("https://api.frankerfacez.com/v1/set/global");
|
||||
|
||||
NetworkRequest(url)
|
||||
|
||||
.timeout(30000)
|
||||
.onSuccess([this](auto result) {
|
||||
writeProviderEmotesCache("global", "frankerfacez",
|
||||
result.getData());
|
||||
auto parsedSet = parseGlobalEmotes(result.parseJson());
|
||||
this->setEmotes(std::make_shared<EmoteMap>(std::move(parsedSet)));
|
||||
})
|
||||
.onError([](auto result) {
|
||||
qCWarning(chatterinoFfzemotes)
|
||||
<< "Failed to fetch global FFZ emotes. "
|
||||
<< result.formatError();
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
@@ -260,7 +272,7 @@ void FfzEmotes::loadChannel(
|
||||
std::function<void(std::optional<EmotePtr>)> modBadgeCallback,
|
||||
std::function<void(std::optional<EmotePtr>)> vipBadgeCallback,
|
||||
std::function<void(FfzChannelBadgeMap &&)> channelBadgesCallback,
|
||||
bool manualRefresh)
|
||||
bool manualRefresh, bool cacheHit)
|
||||
{
|
||||
qCDebug(LOG) << "Reload FFZ Channel Emotes for channel" << channelID;
|
||||
|
||||
@@ -271,7 +283,9 @@ void FfzEmotes::loadChannel(
|
||||
modBadgeCallback = std::move(modBadgeCallback),
|
||||
vipBadgeCallback = std::move(vipBadgeCallback),
|
||||
channelBadgesCallback = std::move(channelBadgesCallback),
|
||||
channel, manualRefresh](const auto &result) {
|
||||
channel, channelID, manualRefresh](const auto &result) {
|
||||
writeProviderEmotesCache(channelID, "frankerfacez",
|
||||
result.getData());
|
||||
const auto json = result.parseJson();
|
||||
|
||||
auto emoteMap = parseChannelEmotes(json);
|
||||
@@ -301,33 +315,39 @@ void FfzEmotes::loadChannel(
|
||||
}
|
||||
}
|
||||
})
|
||||
.onError([channelID, channel, manualRefresh](const auto &result) {
|
||||
auto shared = channel.lock();
|
||||
if (!shared)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.status() == 404)
|
||||
{
|
||||
// User does not have any FFZ emotes
|
||||
if (manualRefresh)
|
||||
.onError(
|
||||
[channelID, channel, manualRefresh, cacheHit](const auto &result) {
|
||||
auto shared = channel.lock();
|
||||
if (!shared)
|
||||
{
|
||||
shared->addSystemMessage(CHANNEL_HAS_NO_EMOTES);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Auto retry in case of a timeout, with a delay
|
||||
auto errorString = result.formatError();
|
||||
qCWarning(LOG) << "Error fetching FFZ emotes for channel"
|
||||
<< channelID << ", error" << errorString;
|
||||
shared->addSystemMessage(
|
||||
QStringLiteral("Failed to fetch FrankerFaceZ channel "
|
||||
"emotes. (Error: %1)")
|
||||
.arg(errorString));
|
||||
}
|
||||
})
|
||||
|
||||
if (result.status() == 404)
|
||||
{
|
||||
// User does not have any FFZ emotes
|
||||
if (manualRefresh)
|
||||
{
|
||||
shared->addSystemMessage(CHANNEL_HAS_NO_EMOTES);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Auto retry in case of a timeout, with a delay
|
||||
auto errorString = result.formatError();
|
||||
qCWarning(LOG) << "Error fetching FFZ emotes for channel"
|
||||
<< channelID << ", error" << errorString;
|
||||
shared->addSystemMessage(
|
||||
QStringLiteral("Failed to fetch FrankerFaceZ channel "
|
||||
"emotes. (Error: %1)")
|
||||
.arg(errorString));
|
||||
if (cacheHit)
|
||||
{
|
||||
shared->addSystemMessage(
|
||||
"Using cached FrankerFaceZ emotes as fallback.");
|
||||
}
|
||||
}
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
std::function<void(std::optional<EmotePtr>)> modBadgeCallback,
|
||||
std::function<void(std::optional<EmotePtr>)> vipBadgeCallback,
|
||||
std::function<void(FfzChannelBadgeMap &&)> channelBadgesCallback,
|
||||
bool manualRefresh);
|
||||
bool manualRefresh, bool cacheHit);
|
||||
|
||||
private:
|
||||
Atomic<std::shared_ptr<const EmoteMap>> global_;
|
||||
|
||||
Reference in New Issue
Block a user