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 account switch not being saved if no other settings were changed. (#5558)
- Bugfix: Fixed some tooltips not being readable. (#5578) - Bugfix: Fixed some tooltips not being readable. (#5578)
- Bugfix: Fixed log files being locked longer than needed. (#5592) - 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) - 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 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) - 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,6 +79,12 @@ void TwitchBadges::loadTwitchBadges()
break; break;
} }
qCWarning(chatterinoTwitch) << errorMessage; qCWarning(chatterinoTwitch) << errorMessage;
this->loadLocalBadges();
});
}
void TwitchBadges::loadLocalBadges()
{
QFile file(":/twitch-badges.json"); QFile file(":/twitch-badges.json");
if (!file.open(QFile::ReadOnly)) if (!file.open(QFile::ReadOnly))
{ {
@ -92,46 +98,40 @@ void TwitchBadges::loadTwitchBadges()
auto bytes = file.readAll(); auto bytes = file.readAll();
auto doc = QJsonDocument::fromJson(bytes); auto doc = QJsonDocument::fromJson(bytes);
this->parseTwitchBadges(doc.object()); {
const auto &root = doc.object();
this->loaded();
});
}
void TwitchBadges::parseTwitchBadges(QJsonObject root)
{
auto badgeSets = this->badgeSets_.access(); auto badgeSets = this->badgeSets_.access();
auto jsonSets = root.value("badge_sets").toObject(); for (auto setIt = root.begin(); setIt != root.end(); setIt++)
for (auto sIt = jsonSets.begin(); sIt != jsonSets.end(); ++sIt)
{ {
auto key = sIt.key(); auto key = setIt.key();
auto versions = sIt.value().toObject().value("versions").toObject();
for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt) for (auto versionValue : setIt.value().toArray())
{ {
auto versionObj = vIt.value().toObject(); const auto versionObj = versionValue.toObject();
auto id = versionObj["id"].toString();
auto baseImage = versionObj["image"].toString();
auto emote = Emote{ auto emote = Emote{
.name = {""}, .name = {},
.images = .images =
ImageSet{ ImageSet{
Image::fromUrl( Image::fromUrl({baseImage + '1'}, 1,
{versionObj.value("image_url_1x").toString()}, 1,
BADGE_BASE_SIZE), BADGE_BASE_SIZE),
Image::fromUrl( Image::fromUrl({baseImage + '2'}, .5,
{versionObj.value("image_url_2x").toString()}, .5,
BADGE_BASE_SIZE * 2), BADGE_BASE_SIZE * 2),
Image::fromUrl( Image::fromUrl({baseImage + '3'}, .25,
{versionObj.value("image_url_4x").toString()}, .25,
BADGE_BASE_SIZE * 4), BADGE_BASE_SIZE * 4),
}, },
.tooltip = Tooltip{versionObj.value("title").toString()}, .tooltip = Tooltip{versionObj["title"].toString()},
.homePage = Url{versionObj.value("click_url").toString()}, .homePage = Url{versionObj["url"].toString()},
}; };
(*badgeSets)[key][vIt.key()] = std::make_shared<Emote>(emote); (*badgeSets)[key][id] = std::make_shared<Emote>(emote);
} }
} }
}
this->loaded();
} }
void TwitchBadges::loaded() void TwitchBadges::loaded()

View file

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