Add cached emotes fallback for failed provider fetches (#6125)
This commit is contained in:
@@ -11,8 +11,11 @@
|
||||
#include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp"
|
||||
#include "providers/twitch/TwitchChannel.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "util/Helpers.hpp"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QLoggingCategory>
|
||||
#include <QStringLiteral>
|
||||
#include <QThread>
|
||||
|
||||
namespace {
|
||||
@@ -218,9 +221,19 @@ void BttvEmotes::loadEmotes()
|
||||
return;
|
||||
}
|
||||
|
||||
readProviderEmotesCache("global", "betterttv", [this](const auto &jsonDoc) {
|
||||
auto emotes = this->global_.get();
|
||||
auto pair = parseGlobalEmotes(jsonDoc.array(), *emotes);
|
||||
if (pair.first)
|
||||
{
|
||||
this->setEmotes(std::make_shared<EmoteMap>(std::move(pair.second)));
|
||||
}
|
||||
});
|
||||
|
||||
NetworkRequest(QString(globalEmoteApiUrl))
|
||||
.timeout(30000)
|
||||
.onSuccess([this](auto result) {
|
||||
writeProviderEmotesCache("global", "betterttv", result.getData());
|
||||
auto emotes = this->global_.get();
|
||||
auto pair = parseGlobalEmotes(result.parseJsonArray(), *emotes);
|
||||
if (pair.first)
|
||||
@@ -229,6 +242,10 @@ void BttvEmotes::loadEmotes()
|
||||
std::make_shared<EmoteMap>(std::move(pair.second)));
|
||||
}
|
||||
})
|
||||
.onError([](auto result) {
|
||||
qCWarning(chatterinoBttv) << "Failed to fetch global BTTV emotes. "
|
||||
<< result.formatError();
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
@@ -241,15 +258,16 @@ void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
|
||||
const QString &channelId,
|
||||
const QString &channelDisplayName,
|
||||
std::function<void(EmoteMap &&)> callback,
|
||||
bool manualRefresh)
|
||||
bool manualRefresh, bool cacheHit)
|
||||
{
|
||||
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelId)
|
||||
.timeout(20000)
|
||||
.onSuccess([callback = std::move(callback), channel, channelDisplayName,
|
||||
manualRefresh](auto result) {
|
||||
.onSuccess([callback = std::move(callback), channel, channelId,
|
||||
channelDisplayName, manualRefresh](auto result) {
|
||||
auto emotes =
|
||||
parseChannelEmotes(result.parseJson(), channelDisplayName);
|
||||
bool hasEmotes = !emotes.empty();
|
||||
writeProviderEmotesCache(channelId, "betterttv", result.getData());
|
||||
callback(std::move(emotes));
|
||||
|
||||
if (auto shared = channel.lock(); manualRefresh)
|
||||
@@ -265,7 +283,7 @@ void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
|
||||
}
|
||||
}
|
||||
})
|
||||
.onError([channelId, channel, manualRefresh](auto result) {
|
||||
.onError([channelId, channel, manualRefresh, cacheHit](auto result) {
|
||||
auto shared = channel.lock();
|
||||
if (!shared)
|
||||
{
|
||||
@@ -291,6 +309,11 @@ void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
|
||||
QStringLiteral("Failed to fetch BetterTTV channel "
|
||||
"emotes. (Error: %1)")
|
||||
.arg(errorString));
|
||||
if (cacheHit)
|
||||
{
|
||||
shared->addSystemMessage(
|
||||
"Using cached BetterTTV emotes as fallback.");
|
||||
}
|
||||
}
|
||||
})
|
||||
.execute();
|
||||
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
const QString &channelId,
|
||||
const QString &channelDisplayName,
|
||||
std::function<void(EmoteMap &&)> callback,
|
||||
bool manualRefresh);
|
||||
bool manualRefresh, bool cacheHit);
|
||||
|
||||
/**
|
||||
* Adds an emote to the `channelEmoteMap`.
|
||||
|
||||
Reference in New Issue
Block a user