Add cached emotes fallback for failed provider fetches (#6125)

This commit is contained in:
Elias A.
2025-04-12 13:56:04 +03:00
committed by GitHub
parent b59124702c
commit 7c8274ed56
12 changed files with 198 additions and 41 deletions
+52
View File
@@ -1,12 +1,19 @@
#include "util/Helpers.hpp"
#include "Application.hpp"
#include "common/QLogging.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "singletons/Paths.hpp"
#include <QDateTime>
#include <QDirIterator>
#include <QJsonObject>
#include <QLocale>
#include <QLoggingCategory>
#include <QRegularExpression>
#include <QStringBuilder>
#include <QStringView>
#include <QThreadPool>
#include <QTimeZone>
#include <QUuid>
@@ -386,4 +393,49 @@ void removeLastQS(QString &str)
#endif
}
void writeProviderEmotesCache(const QString &id, const QString &provider,
const QByteArray &bytes)
{
QThreadPool::globalInstance()->start([bytes, id, provider]() {
auto cacheKey = id % "." % provider;
QFile responseCache(getApp()->getPaths().cacheFilePath(cacheKey));
if (responseCache.open(QIODevice::WriteOnly))
{
qCDebug(chatterinoCache)
<< "Saved json response " << id << "." << provider;
responseCache.write(qCompress(bytes));
}
});
}
bool readProviderEmotesCache(const QString &id, const QString &provider,
const std::function<void(QJsonDocument)> &callback)
{
auto cacheKey = id % "." % provider;
QFile responseCache(getApp()->getPaths().cacheFilePath(cacheKey));
if (responseCache.open(QIODevice::ReadOnly))
{
QJsonParseError parseError;
auto doc = QJsonDocument::fromJson(qUncompress(responseCache.readAll()),
&parseError);
if (parseError.error != QJsonParseError::NoError)
{
qCWarning(chatterinoCache)
<< "Emote cache " << id << "." << provider
<< " parsing failed: " << parseError.errorString();
}
qCDebug(chatterinoCache)
<< "Loaded emote cache: " << id << "." << provider;
callback(doc);
return true;
}
// If the API call fails, we need to know if loading cached emotes was successful
return false;
}
} // namespace chatterino