fix: properly display global badges for anon users (#5599)

This commit is contained in:
nerix 2024-09-28 12:12:13 +02:00 committed by GitHub
parent c4df31a226
commit edf18a7a0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 76 additions and 45 deletions

View file

@ -49,6 +49,7 @@
- Bugfix: Fixed account switch not being saved if no other settings were changed. (#5558)
- Bugfix: Fixed some tooltips not being readable. (#5578)
- Bugfix: Fixed log files being locked longer than needed. (#5592)
- Bugfix: Fixed global badges not showing in anonymous mode. (#5599)
- Bugfix: Fixed grammar in the user highlight page. (#5602)
- Dev: Update Windows build from Qt 6.5.0 to Qt 6.7.1. (#5420)
- Dev: Update vcpkg build Qt from 6.5.0 to 6.7.0, boost from 1.83.0 to 1.85.0, openssl from 3.1.3 to 3.3.0. (#5422)

File diff suppressed because one or more lines are too long

28
scripts/update-badges.py Normal file
View file

@ -0,0 +1,28 @@
import sys
import json
from pathlib import Path
def trim_version(version):
base_url = version["image_url_1x"].removesuffix("1")
assert version["image_url_2x"] == base_url + "2"
assert version["image_url_4x"] == base_url + "3"
v = {
"id": version["id"],
"title": version["title"],
"image": base_url,
}
if version["click_url"]:
v["url"] = version["click_url"]
return v
raw = sys.stdin.read()
assert len(raw) > 0, "Response from Helix' chat/badges/global needs to be piped"
base = json.loads(raw)["data"]
out = {set["set_id"]: [trim_version(v) for v in set["versions"]] for set in base}
with open(
Path(__file__).parent.parent / "resources" / "twitch-badges.json", mode="w"
) as f:
f.write(json.dumps(out, separators=(",", ":")))

View file

@ -79,59 +79,59 @@ void TwitchBadges::loadTwitchBadges()
break;
}
qCWarning(chatterinoTwitch) << errorMessage;
QFile file(":/twitch-badges.json");
if (!file.open(QFile::ReadOnly))
{
// Despite erroring out, we still want to reach the same point
// Loaded should still be set to true to not build up an endless queue, and the quuee should still be flushed.
qCWarning(chatterinoTwitch)
<< "Error loading Twitch Badges from the local backup file";
this->loaded();
return;
}
auto bytes = file.readAll();
auto doc = QJsonDocument::fromJson(bytes);
this->parseTwitchBadges(doc.object());
this->loaded();
this->loadLocalBadges();
});
}
void TwitchBadges::parseTwitchBadges(QJsonObject root)
void TwitchBadges::loadLocalBadges()
{
auto badgeSets = this->badgeSets_.access();
auto jsonSets = root.value("badge_sets").toObject();
for (auto sIt = jsonSets.begin(); sIt != jsonSets.end(); ++sIt)
QFile file(":/twitch-badges.json");
if (!file.open(QFile::ReadOnly))
{
auto key = sIt.key();
auto versions = sIt.value().toObject().value("versions").toObject();
// Despite erroring out, we still want to reach the same point
// Loaded should still be set to true to not build up an endless queue, and the quuee should still be flushed.
qCWarning(chatterinoTwitch)
<< "Error loading Twitch Badges from the local backup file";
this->loaded();
return;
}
auto bytes = file.readAll();
auto doc = QJsonDocument::fromJson(bytes);
for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt)
{
const auto &root = doc.object();
auto badgeSets = this->badgeSets_.access();
for (auto setIt = root.begin(); setIt != root.end(); setIt++)
{
auto versionObj = vIt.value().toObject();
auto emote = Emote{
.name = {""},
.images =
ImageSet{
Image::fromUrl(
{versionObj.value("image_url_1x").toString()}, 1,
BADGE_BASE_SIZE),
Image::fromUrl(
{versionObj.value("image_url_2x").toString()}, .5,
BADGE_BASE_SIZE * 2),
Image::fromUrl(
{versionObj.value("image_url_4x").toString()}, .25,
BADGE_BASE_SIZE * 4),
},
.tooltip = Tooltip{versionObj.value("title").toString()},
.homePage = Url{versionObj.value("click_url").toString()},
};
auto key = setIt.key();
(*badgeSets)[key][vIt.key()] = std::make_shared<Emote>(emote);
for (auto versionValue : setIt.value().toArray())
{
const auto versionObj = versionValue.toObject();
auto id = versionObj["id"].toString();
auto baseImage = versionObj["image"].toString();
auto emote = Emote{
.name = {},
.images =
ImageSet{
Image::fromUrl({baseImage + '1'}, 1,
BADGE_BASE_SIZE),
Image::fromUrl({baseImage + '2'}, .5,
BADGE_BASE_SIZE * 2),
Image::fromUrl({baseImage + '3'}, .25,
BADGE_BASE_SIZE * 4),
},
.tooltip = Tooltip{versionObj["title"].toString()},
.homePage = Url{versionObj["url"].toString()},
};
(*badgeSets)[key][id] = std::make_shared<Emote>(emote);
}
}
}
this->loaded();
}
void TwitchBadges::loaded()

View file

@ -46,7 +46,9 @@ public:
void loadTwitchBadges();
private:
void parseTwitchBadges(QJsonObject root);
/// Loads the badges shipped with Chatterino (twitch-badges.json)
void loadLocalBadges();
void loaded();
void loadEmoteImage(const QString &name, const ImagePtr &image,
BadgeIconCallback &&callback);