Render typed bilingual news and preserve historical journal scope (by Codex)

This commit is contained in:
Codex authored and Codex committed 2026-10-27 05:46:07 +01:00
1 parent 1eb5e99847
commit 950d79d529
16 files changed
+6858 -81

No files matched your search

+85 -27
View File
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "modern/ui/demo_news.hpp"
#include "modern/ui/demo_career.hpp"
#include "modern/ui/news_journal.hpp"
#include <algorithm>
#include <array>
#include <cctype>
@@ -69,6 +70,12 @@ std::string news_category(const std::string &kind) {
return "Training";
if (kind == "stadium")
return "Stadion";
if (kind == "national")
return "Nationalteam";
if (kind == "indoor")
return "Halle";
if (kind == "career")
return "Karriere";
return {};
}
std::string news_replace(std::string text, const std::string &key,
@@ -99,6 +106,33 @@ bool news_valid_pair(const Json &v) {
}
} // namespace
Json demo_news_scope(const Json &state, const Catalog *world) {
try {
if (state.value("career", Json::object()).value("ended", false) ||
!state.contains("managers") || state.at("managers").empty())
return Json();
const auto &m = demo_career_manager(state);
const auto role = state.value("career", Json::object())
.value("role", state.value("edits", Json::object())
.value("setup", Json::object())
.value("league", true)
? "club"
: "national");
if (role == "national")
return {{"kind", "national"},
{"id", "national-de"},
{"name", "Deutschland"},
{"names", {{"de", "Deutschland"}, {"en", "Germany"}}}};
if (role != "club" || !m.contains("club") || !m.at("club").is_string())
return Json();
const auto id = m.at("club").get<std::string>();
const auto *c = world ? world->find("clubs", id) : nullptr;
return {{"kind", "club"}, {"id", id}, {"name", c ? world->name(*c) : id}};
} catch (...) {
return Json();
}
}
std::string news_display_date(const std::string &s) {
return news_date(s)
? s.substr(8, 2) + "." + s.substr(5, 2) + "." + s.substr(0, 4)
@@ -183,11 +217,22 @@ Json demo_news(const Catalog &world, const Json &state) {
{"read", f.read},
{"folder", f.folder}});
}
return {{"version", 1}, {"items", items}};
const auto scope = demo_news_scope(state, &world);
// These old authored fixtures are specifically club stories. Do not invent
// national equivalents or republish club finances under a national header.
if (scope.is_null() || scope.at("kind") != "club")
return empty_news_journal();
const auto &manager = demo_career_manager(state);
return bind_created_news(
items, scope,
{{"id", "portrait:" + std::to_string(manager.value("portrait", 0))},
{"name", manager.value("name", std::string())}});
}
bool valid_demo_news(const Json &state) {
try {
const auto &news = state.at("news");
if (news.value("version", 0) == 2)
return valid_news_journal(news);
if (!news_keys(news, {"version", "items"}) ||
!news.at("version").is_number_integer() || news.at("version") != 1 ||
!news.at("items").is_array() || news.at("items").size() > 100)
@@ -216,8 +261,15 @@ bool migrate_demo_news(Json &state, const Catalog &world) {
try {
if (!state.is_object())
return false;
if (state.contains("news"))
return valid_demo_news(state);
if (state.contains("news")) {
if (!valid_demo_news(state))
return false;
auto proposed = state.at("news");
if (!migrate_news_journal(proposed))
return false;
state["news"] = std::move(proposed);
return true;
}
auto next = state;
next["news"] = demo_news(world, state);
if (!valid_demo_news(next))
@@ -229,7 +281,7 @@ bool migrate_demo_news(Json &state, const Catalog &world) {
}
}
std::string edit_demo_news(Json &state, const Json &action, const Catalog &) {
if (!valid_demo_news(state))
if (!valid_demo_news(state) || state.at("news").at("version") != 2)
return "invalid_news_state";
try {
if (!action.is_object() || !action.contains("kind") ||
@@ -237,13 +289,15 @@ std::string edit_demo_news(Json &state, const Json &action, const Catalog &) {
return "invalid_news_action";
const auto kind = action.at("kind").get<std::string>();
auto next = state;
const auto scope = demo_news_scope(state);
auto &items = next["news"]["items"];
if (kind == "read-all") {
if (!news_keys(action, {"kind", "folder"}) ||
!news_folder(action.at("folder"), false))
return "invalid_news_action";
for (auto &item : items)
if (item.at("folder") == action.at("folder"))
if (item.at("folder") == action.at("folder") &&
news_row_visible(item, scope))
item["read"] = true;
} else if (kind == "read" || kind == "move") {
if (!(kind == "read" ? news_keys(action, {"kind", "id"})
@@ -252,7 +306,7 @@ std::string edit_demo_news(Json &state, const Json &action, const Catalog &) {
(kind == "move" && !news_folder(action.at("folder"))))
return "invalid_news_action";
auto found = std::find_if(items.begin(), items.end(), [&](const auto &m) {
return m.at("id") == action.at("id");
return m.at("id") == action.at("id") && news_row_visible(m, scope);
});
if (found == items.end() || found->at("folder") == "trash")
return "news_not_found";
@@ -277,7 +331,7 @@ std::string edit_demo_news(Json &state, const Json &action, const Catalog &) {
return "invalid_news_action";
auto found =
std::find_if(items.begin(), items.end(), [&](const auto &m) {
return m.at("id") == p.at("id");
return m.at("id") == p.at("id") && news_row_visible(m, scope);
});
if (found == items.end() || news_pair(*found) != p.at("after"))
return "news_undo_conflict";
@@ -299,41 +353,41 @@ int demo_news_unread(const Json &state) {
return 0;
int count = 0;
for (const auto &m : state.at("news").at("items"))
if (m.at("folder") == "inbox" && !m.at("read").get<bool>())
if (m.at("folder") == "inbox" && !m.at("read").get<bool>() &&
news_row_visible(m, demo_news_scope(state)))
++count;
return count;
}
Json demo_news_view(const Catalog &world, const Json &state,
const std::string &folder, const std::string &query) {
const std::string &folder, const std::string &query,
const std::string &language) {
if (!valid_demo_news(state) || (folder != "inbox" && folder != "archive") ||
query.size() > 120)
query.size() > 120 || (language != "de" && language != "en"))
return {{"ok", false}, {"error", "invalid_news_view"}};
std::string manager, club_id, club_name;
if (state.contains("managers") && state.at("managers").is_array() &&
!state.at("managers").empty()) {
const auto &m = demo_career_manager(state);
manager = m.value("name", "");
club_id = m.value("club", "");
if (const auto *c = world.find("clubs", club_id))
club_name = world.name(*c);
}
const auto resolve = [&](const std::string &v) {
return news_replace(news_replace(v, "{{club}}", club_name), "{{manager}}",
manager);
};
const auto scope = demo_news_scope(state, &world);
const auto &person =
state.contains("managers") && !state.at("managers").empty()
? demo_career_manager(state)
: Json::object();
const auto manager = person.value("name", std::string()),
club_id = scope.is_object() ? scope.value("id", std::string())
: std::string(),
club_name = scope.is_object() ? scope.value("name", std::string())
: std::string();
Json shown = Json::array(), counts = {{"inbox", 0}, {"archive", 0}};
bool can_read = false;
for (auto m : state.at("news").at("items")) {
const auto location = m.at("folder").get<std::string>();
if (location == "trash")
if (location == "trash" || !news_row_visible(m, scope))
continue;
counts[location] = counts.at(location).get<int>() + 1;
if (location != folder)
continue;
if (!m.at("read").get<bool>())
can_read = true;
m["title"] = resolve(m.at("title").get<std::string>());
m["body"] = resolve(m.at("body").get<std::string>());
const auto rendered = news_row_text(m, language);
m["title"] = rendered.at("title");
m["body"] = rendered.at("body");
m["category"] = news_category(m.at("kind").get<std::string>());
m["display_date"] = news_display_date(m.at("date").get<std::string>());
const auto haystack = news_fold(m.at("title").get<std::string>() + " " +
@@ -362,6 +416,10 @@ Json demo_news_view(const Catalog &world, const Json &state,
{"club", club_id},
{"club_name", club_name},
{"manager", manager},
{"date", news_display_date(state.value("date", std::string()))}};
{"date", news_display_date(state.value("date", std::string()))},
{"scope", scope},
{"unbound_count", state.at("news").value("version", 1) == 2
? state.at("news").at("unbound").size()
: state.at("news").at("items").size()}};
}
} // namespace bmh::modern_ui