Render typed bilingual news and preserve historical journal scope (by Codex)
This commit is contained in:
1 parent
1eb5e99847
commit
950d79d529
16 files changed
+6858
-81
No files matched your search
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp demo_career.cpp demo_national_ui.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_live.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.cpp demo_settings.cpp ui_preferences.cpp demo_widgets.cpp demo_files.cpp demo_opening.cpp)
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp demo_career.cpp demo_national_ui.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_live.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.cpp news_catalog.cpp news_journal.cpp demo_settings.cpp ui_preferences.cpp demo_widgets.cpp demo_files.cpp demo_opening.cpp)
|
||||
target_include_directories(bmh_modern_ui_core PUBLIC ../..)
|
||||
target_link_libraries(bmh_modern_ui_core PUBLIC bmh_asset_io)
|
||||
add_library(bmh_modern_ui_mods STATIC mod_library.cpp)
|
||||
@@ -38,7 +38,7 @@ endif()
|
||||
set(modern_assets "${CMAKE_BINARY_DIR}/assets/modern")
|
||||
FetchContent_GetProperties(rmlui)
|
||||
file(MAKE_DIRECTORY "${modern_assets}")
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss reports.rcss report_details.rcss report_pickers.rcss indoor.rcss planning.rcss almanac.rcss finance.rcss bank.rcss contracts.rcss amount.rcss environment.rcss advertising.rcss toto.rcss lineup.rcss live.rcss transfer.rcss career_reports.rcss rankings.rcss stadium.rcss statistics.rcss news.rcss settings.rcss widgets.rcss files.rcss front.rcss entry.rcss opening.rcss select_menu.rcss career.rcss national.rcss)
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss reports.rcss report_details.rcss report_pickers.rcss indoor.rcss planning.rcss almanac.rcss finance.rcss bank.rcss contracts.rcss amount.rcss environment.rcss advertising.rcss toto.rcss lineup.rcss live.rcss transfer.rcss career_reports.rcss rankings.rcss stadium.rcss statistics.rcss news.rcss news-catalog.json settings.rcss widgets.rcss files.rcss front.rcss entry.rcss opening.rcss select_menu.rcss career.rcss national.rcss)
|
||||
configure_file("${CMAKE_SOURCE_DIR}/assets/modern/${asset}" "${modern_assets}/${asset}" COPYONLY)
|
||||
endforeach()
|
||||
|
||||
@@ -132,3 +132,10 @@ foreach(component statistics news settings desktop files entry opening career li
|
||||
endif()
|
||||
add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
|
||||
endforeach()
|
||||
|
||||
add_executable(modern_news_catalog_tests ../../../tests/modern_news_catalog_tests.cpp)
|
||||
target_link_libraries(modern_news_catalog_tests PRIVATE bmh_modern_ui_core)
|
||||
if(MSVC)
|
||||
target_compile_options(modern_news_catalog_tests PRIVATE /utf-8)
|
||||
endif()
|
||||
add_test(NAME modern_news_catalog_tests COMMAND modern_news_catalog_tests "${CMAKE_SOURCE_DIR}/assets/modern/news-catalog.json" "${CMAKE_SOURCE_DIR}/tests/fixtures/modern_news_catalog_reference.json")
|
||||
+85
-27
@@ -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
|
||||
@@ -3,9 +3,10 @@
|
||||
#include "modern/ui/catalog.hpp"
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
// Presentation-only journal. Templates remain persisted, then resolve against
|
||||
// the current snapshot's first manager/club. No catalogue or engine mutation.
|
||||
// Presentation-only journal with historical text/owner snapshots. Unknown v1
|
||||
// ownership is retained separately, never inferred from the current manager.
|
||||
Json demo_news(const Catalog &, const Json &full_state);
|
||||
Json demo_news_scope(const Json &, const Catalog * = nullptr);
|
||||
bool valid_demo_news(const Json &full_state);
|
||||
bool migrate_demo_news(Json &full_state, const Catalog &);
|
||||
// read{id}, read-all{folder}, move{id,folder}, undo{patch:[{id,before,after}]}.
|
||||
@@ -18,6 +19,7 @@ int demo_news_unread(const Json &full_state);
|
||||
// date. Query and folder belong to the view and never enter a checkpoint.
|
||||
Json demo_news_view(const Catalog &, const Json &full_state,
|
||||
const std::string &folder = "inbox",
|
||||
const std::string &query = {});
|
||||
const std::string &query = {},
|
||||
const std::string &language = "de");
|
||||
std::string news_display_date(const std::string &);
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -320,6 +320,7 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
next["edits"]["setup"]=options;
|
||||
next["career"]=demo_career_defaults(next.at("managers"),options);
|
||||
next["career_views"]=Json::object();
|
||||
next["news"] = demo_news(*catalog_, next);
|
||||
} else if (next.contains("career")) {
|
||||
const int active=next["career"].value("active_portrait",0);
|
||||
if (std::none_of(next["managers"].begin(),next["managers"].end(),[&](const auto &m){return m.value("portrait",0)==active;}))
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,66 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include "modern/ui/catalog.hpp"
|
||||
#include <optional>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
enum class NewsLanguage { de, en };
|
||||
enum class NewsScopeKind { club, national, global };
|
||||
struct NewsEntity {
|
||||
std::string id, name;
|
||||
std::optional<std::pair<std::string, std::string>> localized_names;
|
||||
Json json() const;
|
||||
};
|
||||
struct NewsScope {
|
||||
NewsScopeKind kind = NewsScopeKind::club;
|
||||
NewsEntity team;
|
||||
Json json() const;
|
||||
};
|
||||
// Always two decimal places, unlike the general DTO's explicit minorUnits.
|
||||
// No implicit conversion from DTO/native integer amounts is provided.
|
||||
struct NewsMinorMoney {
|
||||
int64_t minor = 0;
|
||||
std::string currency;
|
||||
Json json() const;
|
||||
};
|
||||
std::optional<NewsMinorMoney> news_money_from_dto(const Json &,
|
||||
std::string &error);
|
||||
struct NewsEvent {
|
||||
std::string id, type, date;
|
||||
NewsScope scope;
|
||||
Json data =
|
||||
Json::object(); // Every field is validated against registry types.
|
||||
std::optional<std::string> variant;
|
||||
bool demo = false;
|
||||
Json json() const;
|
||||
};
|
||||
struct NewsRenderContext {
|
||||
NewsLanguage language = NewsLanguage::de;
|
||||
std::optional<NewsEntity> manager;
|
||||
std::optional<std::string> as_of;
|
||||
Json json() const;
|
||||
};
|
||||
// Pure authored-text renderer. Names come from the normalized world/adapter;
|
||||
// UTF-8 is checked and preserved. This module adds no Unicode runtime library.
|
||||
class NewsCatalog {
|
||||
public:
|
||||
explicit NewsCatalog(Json catalogue);
|
||||
static NewsCatalog load(const fs::path &);
|
||||
Json registry() const;
|
||||
const Json &data() const { return catalogue_; }
|
||||
Json from_event(const NewsEvent &, const NewsRenderContext & = {}) const;
|
||||
Json from_event(const Json &, const Json &context = Json::object()) const;
|
||||
Json validate_article(const Json &) const;
|
||||
Json example(const std::string &template_id,
|
||||
const Json &options = Json::object()) const;
|
||||
Json from_almanac(const Json &, const Json &) const;
|
||||
|
||||
private:
|
||||
Json catalogue_;
|
||||
};
|
||||
// Stable canonical facts/dedupe identity, matching the HTML event contract.
|
||||
std::string news_canonical(const Json &);
|
||||
std::string news_hash(const std::string &);
|
||||
bool news_iso_date(const std::string &);
|
||||
bool news_scope_matches(const Json &article_scope, const Json ¤t_scope);
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -52,9 +52,10 @@ void App::news_after_render() {
|
||||
}
|
||||
void App::news_update_feed() {
|
||||
const auto s = snap();
|
||||
const auto data =
|
||||
demo_news_view(*s.world, s.state, news_view_.value("folder", "inbox"),
|
||||
news_view_.value("query", ""));
|
||||
const auto data = demo_news_view(
|
||||
*s.world, s.state, news_view_.value("folder", "inbox"),
|
||||
news_view_.value("query", ""),
|
||||
ui_preferences_.at("display").value("language", std::string("de")));
|
||||
if (!data.value("ok", false))
|
||||
return;
|
||||
if (auto *feed = element("news-feed")) {
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/news_journal.hpp"
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
bool str(const Json &v, size_t maximum, bool required = true) {
|
||||
return v.is_string() && v.get_ref<const std::string &>().size() <= maximum &&
|
||||
v.get_ref<const std::string &>().find('\0') == std::string::npos &&
|
||||
(!required || v.get_ref<const std::string &>().find_first_not_of(
|
||||
" \t\r\n") != std::string::npos);
|
||||
}
|
||||
bool id(const Json &v, size_t maximum = 140) {
|
||||
return str(v, maximum) && v.get_ref<const std::string &>().find_first_not_of(
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst"
|
||||
"uvwxyz0123456789:_./-") == std::string::npos;
|
||||
}
|
||||
bool row_id(const Json &v) {
|
||||
return str(v, 64) &&
|
||||
v.get_ref<const std::string &>().find_first_not_of(
|
||||
"abcdefghijklmnopqrstuvwxyz0123456789-") == std::string::npos;
|
||||
}
|
||||
bool scope(const Json &v) {
|
||||
return v.is_object() && v.contains("id") && v.contains("name") &&
|
||||
v.contains("kind") && id(v.at("id")) && str(v.at("name"), 480) &&
|
||||
(v.at("kind") == "club" || v.at("kind") == "national" ||
|
||||
v.at("kind") == "global");
|
||||
}
|
||||
bool category(const Json &v) {
|
||||
return v == "club" || v == "fixture" || v == "transfer" || v == "finance" ||
|
||||
v == "training" || v == "stadium" || v == "national" ||
|
||||
v == "indoor" || v == "career";
|
||||
}
|
||||
bool location(const Json &v) {
|
||||
return v == "inbox" || v == "archive" || v == "trash";
|
||||
}
|
||||
bool legacy_row(const Json &v) {
|
||||
return v.is_object() && v.size() == 7 && row_id(v.at("id")) &&
|
||||
str(v.at("date"), 10) && news_iso_date(v.at("date")) &&
|
||||
category(v.at("kind")) && str(v.at("title"), 640) &&
|
||||
str(v.at("body"), 4800, false) && v.at("read").is_boolean() &&
|
||||
location(v.at("folder"));
|
||||
}
|
||||
bool valid_owner(const Json &v) {
|
||||
return v.is_null() ||
|
||||
(v.is_object() && v.contains("id") && v.contains("name") &&
|
||||
id(v.at("id")) && str(v.at("name"), 480));
|
||||
}
|
||||
bool bound_row(const Json &v) {
|
||||
if (!v.is_object() || (v.size() != 9 && v.size() != 10) ||
|
||||
!row_id(v.at("id")) || !str(v.at("date"), 10) ||
|
||||
!news_iso_date(v.at("date")) || !category(v.at("kind")) ||
|
||||
!str(v.at("title"), v.contains("article") ? 960 : 160) ||
|
||||
!str(v.at("body"), v.contains("article") ? 8800 : 1200, false) ||
|
||||
!v.at("read").is_boolean() || !location(v.at("folder")) ||
|
||||
!scope(v.at("scope")) || !valid_owner(v.at("owner")))
|
||||
return false;
|
||||
const auto kind = v.at("scope").at("kind");
|
||||
if ((v.at("kind") == "finance" || v.at("kind") == "transfer" ||
|
||||
v.at("kind") == "stadium" || v.at("kind") == "indoor") &&
|
||||
kind != "club")
|
||||
return false;
|
||||
if (v.at("kind") == "national" && kind != "national")
|
||||
return false;
|
||||
if (kind == "global" && v.at("kind") != "career")
|
||||
return false;
|
||||
if (v.contains("article")) {
|
||||
const auto &a = v.at("article");
|
||||
if (!a.is_object() || a.at("schema") != "bmh-news-article@1" ||
|
||||
a.at("id") != v.at("id") || a.at("date") != v.at("date") ||
|
||||
a.at("kind") != v.at("kind") || a.at("headline") != v.at("title") ||
|
||||
a.at("body") != v.at("body") ||
|
||||
news_canonical(a.at("scope")) != news_canonical(v.at("scope")))
|
||||
return false;
|
||||
const auto key = news_canonical(Json::array(
|
||||
{a.at("scope").at("kind"), a.at("scope").at("id"), a.at("eventId")}));
|
||||
if (!a.at("event").is_object() ||
|
||||
a.at("event").at("id") != a.at("eventId") ||
|
||||
a.at("event").at("type") != a.at("eventType") ||
|
||||
a.at("event").at("date") != a.at("date") ||
|
||||
news_canonical(a.at("event").at("scope")) !=
|
||||
news_canonical(a.at("scope")))
|
||||
return false;
|
||||
if (a.at("dedupeKey") != key || a.at("id") != "evt-" + news_hash(key) ||
|
||||
a.at("fingerprint") != news_hash(news_canonical(a.at("event"))))
|
||||
return false;
|
||||
if (a.at("language") != "de" && a.at("language") != "en")
|
||||
return false;
|
||||
for (auto language : {"de", "en"})
|
||||
if (!str(a.at("translations").at(language).at("headline"), 960) ||
|
||||
!str(a.at("translations").at(language).at("body"), 8800, false))
|
||||
return false;
|
||||
const std::string language = a.at("language");
|
||||
if (a.at("translations").at(language).at("headline") != v.at("title") ||
|
||||
a.at("translations").at(language).at("body") != v.at("body"))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::string resolve(const std::string &source, const std::string &club,
|
||||
const std::string &manager) {
|
||||
std::string out;
|
||||
size_t at = 0;
|
||||
while (at < source.size()) {
|
||||
const auto mark = source.find("{{", at);
|
||||
if (mark == source.npos) {
|
||||
out += source.substr(at);
|
||||
break;
|
||||
}
|
||||
out += source.substr(
|
||||
at, mark - at); // Inserted entity text is never expanded a second time.
|
||||
if (source.compare(mark, 8, "{{club}}") == 0) {
|
||||
out += club;
|
||||
at = mark + 8;
|
||||
} else if (source.compare(mark, 11, "{{manager}}") == 0) {
|
||||
out += manager;
|
||||
at = mark + 11;
|
||||
} else {
|
||||
out += source.substr(mark, 2);
|
||||
at = mark + 2;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Json failure(const char *code) {
|
||||
return {{"ok", false},
|
||||
{"error", {{"code", code}, {"fields", Json::array()}}}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json empty_news_journal() {
|
||||
return {{"version", 2},
|
||||
{"items", Json::array()},
|
||||
{"unbound", Json::array()},
|
||||
{"seen", Json::array()}};
|
||||
}
|
||||
bool valid_news_journal(const Json &v) {
|
||||
try {
|
||||
if (!v.is_object() || v.size() != 4 || v.at("version") != 2 ||
|
||||
!v.at("items").is_array() || v.at("items").size() > 500 ||
|
||||
!v.at("unbound").is_array() || v.at("unbound").size() > 100 ||
|
||||
!v.at("seen").is_array() || v.at("seen").size() > 2000)
|
||||
return false;
|
||||
std::set<std::string> ids, ledger_ids, keys;
|
||||
for (const auto &m : v.at("unbound"))
|
||||
if (!legacy_row(m) || !ids.insert(m.at("id")).second)
|
||||
return false;
|
||||
for (const auto &m : v.at("items"))
|
||||
if (!bound_row(m) || !ids.insert(m.at("id")).second)
|
||||
return false;
|
||||
for (const auto &s : v.at("seen")) {
|
||||
if (!s.is_object() || s.size() != 3 || !str(s.at("key"), 500) ||
|
||||
!str(s.at("id"), 64) ||
|
||||
(!str(s.at("fingerprint"), 16) ||
|
||||
s.at("fingerprint").get_ref<const std::string &>().size() != 16 ||
|
||||
s.at("fingerprint")
|
||||
.get_ref<const std::string &>()
|
||||
.find_first_not_of("0123456789abcdef") !=
|
||||
std::string::npos) ||
|
||||
!keys.insert(s.at("key")).second ||
|
||||
!ledger_ids.insert(s.at("id")).second)
|
||||
return false;
|
||||
const auto key = Json::parse(s.at("key").get<std::string>());
|
||||
if (!key.is_array() || key.size() != 3 ||
|
||||
(key[0] != "club" && key[0] != "national" && key[0] != "global") ||
|
||||
!id(key[1]) || !id(key[2]) || s.at("key") != news_canonical(key) ||
|
||||
s.at("id") != "evt-" + news_hash(s.at("key")))
|
||||
return false;
|
||||
}
|
||||
for (const auto &m : v.at("items"))
|
||||
if (m.contains("article")) {
|
||||
const auto &a = m.at("article");
|
||||
const auto found = std::find_if(
|
||||
v.at("seen").begin(), v.at("seen").end(),
|
||||
[&](const auto &s) { return s.at("key") == a.at("dedupeKey"); });
|
||||
if (found == v.at("seen").end() || found->at("id") != a.at("id") ||
|
||||
found->at("fingerprint") != a.at("fingerprint"))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool migrate_news_journal(Json &v) {
|
||||
try {
|
||||
if (valid_news_journal(v))
|
||||
return true;
|
||||
if (!v.is_object() || v.size() != 2 || v.at("version") != 1 ||
|
||||
!v.at("items").is_array() || v.at("items").size() > 100)
|
||||
return false;
|
||||
auto proposed = empty_news_journal();
|
||||
proposed["unbound"] = v.at("items");
|
||||
if (!valid_news_journal(proposed))
|
||||
return false;
|
||||
v = std::move(proposed);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Json bind_created_news(const Json &legacy_items, const Json &owner_scope,
|
||||
const Json &manager) {
|
||||
if (!scope(owner_scope) || !valid_owner(manager) || manager.is_null() ||
|
||||
!legacy_items.is_array())
|
||||
throw editor::Error("invalid_news_creation_context");
|
||||
auto out = empty_news_journal();
|
||||
for (const auto &m : legacy_items) {
|
||||
if (!legacy_row(m))
|
||||
throw editor::Error("invalid_news_creation_item");
|
||||
auto bound = m;
|
||||
bound["title"] =
|
||||
resolve(m.at("title"), owner_scope.at("name"), manager.at("name"));
|
||||
bound["body"] =
|
||||
resolve(m.at("body"), owner_scope.at("name"), manager.at("name"));
|
||||
bound["scope"] = owner_scope;
|
||||
bound["owner"] = manager;
|
||||
out["items"].push_back(std::move(bound));
|
||||
}
|
||||
if (!valid_news_journal(out))
|
||||
throw editor::Error("invalid_news_creation");
|
||||
return out;
|
||||
}
|
||||
bool news_row_visible(const Json &row, const Json ¤t_scope) {
|
||||
try {
|
||||
return row.is_object() && row.contains("scope") && scope(row.at("scope")) &&
|
||||
news_scope_matches(row.at("scope"), current_scope);
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Json news_row_text(const Json &row, const std::string &language) {
|
||||
if (row.contains("article")) {
|
||||
const auto &texts =
|
||||
row.at("article").at("translations").at(language == "en" ? "en" : "de");
|
||||
return {{"title", texts.at("headline")}, {"body", texts.at("body")}};
|
||||
}
|
||||
return {{"title", row.at("title")}, {"body", row.at("body")}};
|
||||
}
|
||||
Json publish_news_article(Json &journal, const NewsCatalog &catalogue,
|
||||
const Json &article, const Json &owner) {
|
||||
if (!valid_news_journal(journal) || !valid_owner(owner))
|
||||
return failure("invalid_news_state");
|
||||
const auto checked = catalogue.validate_article(article);
|
||||
if (!checked.value("ok", false))
|
||||
return checked;
|
||||
const auto &key = article.at("dedupeKey");
|
||||
for (const auto &old : journal.at("seen"))
|
||||
if (old.at("key") == key)
|
||||
return old.at("fingerprint") == article.at("fingerprint")
|
||||
? Json{{"ok", true}, {"id", old.at("id")}, {"deduped", true}}
|
||||
: failure("news_event_id_conflict");
|
||||
for (const auto &m : journal.at("items"))
|
||||
if (m.at("id") == article.at("id"))
|
||||
return failure("news_article_id_collision");
|
||||
for (const auto &s : journal.at("seen"))
|
||||
if (s.at("id") == article.at("id"))
|
||||
return failure("news_article_id_collision");
|
||||
if (journal.at("items").size() >= 500 || journal.at("seen").size() >= 2000)
|
||||
return failure("news_capacity");
|
||||
auto next = journal;
|
||||
next["items"].insert(next["items"].begin(),
|
||||
Json{{"id", article.at("id")},
|
||||
{"date", article.at("date")},
|
||||
{"kind", article.at("kind")},
|
||||
{"title", article.at("headline")},
|
||||
{"body", article.at("body")},
|
||||
{"read", false},
|
||||
{"folder", "inbox"},
|
||||
{"scope", article.at("scope")},
|
||||
{"owner", owner},
|
||||
{"article", article}});
|
||||
next["seen"].push_back({{"key", key},
|
||||
{"id", article.at("id")},
|
||||
{"fingerprint", article.at("fingerprint")}});
|
||||
if (!valid_news_journal(next))
|
||||
return failure("invalid_news_state");
|
||||
journal = std::move(next);
|
||||
return {{"ok", true}, {"id", article.at("id")}, {"deduped", false}};
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -0,0 +1,20 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include "modern/ui/news_catalog.hpp"
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
// Version2 keeps immutable facts, mutable read/folder flags and a durable
|
||||
// ledger. Unknown historical owners are retained separately, never attributed
|
||||
// to the person/team that happens to open an old checkpoint.
|
||||
Json empty_news_journal();
|
||||
bool valid_news_journal(const Json &);
|
||||
bool migrate_news_journal(Json &);
|
||||
Json bind_created_news(const Json &legacy_items, const Json &scope,
|
||||
const Json &manager);
|
||||
bool news_row_visible(const Json &row, const Json &scope);
|
||||
Json news_row_text(const Json &row, const std::string &language = "de");
|
||||
// The caller supplies an explicitly validated catalogue/event result, not a
|
||||
// guessed engine fallback. Duplicate delivery is a completely write-free read.
|
||||
Json publish_news_article(Json &journal, const NewsCatalog &,
|
||||
const Json &article, const Json &owner = Json());
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -2,9 +2,19 @@
|
||||
std::string App::render_news_art(const Json &message, double size) {
|
||||
const auto kind = message.at("kind").get<std::string>();
|
||||
std::string art;
|
||||
if (kind == "club")
|
||||
art = crest(club(snap()), size * .69);
|
||||
else {
|
||||
if (kind == "national" ||
|
||||
(message.contains("scope") &&
|
||||
message.at("scope").value("kind", "") == "national"))
|
||||
art = picture(
|
||||
renderer_.diagram("news-national-flag", ui_nation_flag_svg("de"), 176),
|
||||
size * .69, size * .46);
|
||||
else if (kind == "club" || kind == "career") {
|
||||
const auto id =
|
||||
message.value("scope", Json::object()).value("id", std::string());
|
||||
art = crest(snap().world->find("clubs", id), size * .69);
|
||||
if (art.empty())
|
||||
art = glyph("world", size * .55);
|
||||
} else {
|
||||
const auto key =
|
||||
kind == "fixture" ? "button.league"
|
||||
: kind == "transfer" ? "button.contracts"
|
||||
@@ -29,13 +39,17 @@ std::string App::render_news_feed(const Json &data) {
|
||||
(unread ? " is-unread" : "") + "\">";
|
||||
if (unread)
|
||||
out += "<span class=\"news-unread-dot\"></span>";
|
||||
out += render_news_art(m) +
|
||||
"<div class=\"news-message-copy\"><div "
|
||||
"class=\"news-message-meta\"><span>" +
|
||||
esc(m.at("display_date").get<std::string>()) +
|
||||
"</span><span class=\"news-category\">" +
|
||||
esc(m.at("category").get<std::string>()) + "</span>" +
|
||||
(unread ? "<span class=\"news-new\">Neu</span>" : "") + "</div>";
|
||||
out +=
|
||||
render_news_art(m) +
|
||||
"<div class=\"news-message-copy\"><div "
|
||||
"class=\"news-message-meta\"><span>" +
|
||||
esc(m.at("display_date").get<std::string>()) +
|
||||
"</span><span class=\"news-category\">" +
|
||||
esc(m.at("category").get<std::string>()) + "</span>" +
|
||||
(unread ? "<span class=\"news-new\">Neu</span>" : "") +
|
||||
(data.value("fictional", false) ? "<span class=\"news-new\">Demo</span>"
|
||||
: "") +
|
||||
"</div>";
|
||||
out += btn("news-open", esc(name), "news-message-title",
|
||||
"id=\"news-read-" + id + "\" data-id=\"" + id +
|
||||
"\" aria-label=\"Nachricht lesen: " + esc(name) + "\"");
|
||||
@@ -70,10 +84,10 @@ std::string App::render_news_feed(const Json &data) {
|
||||
: archive ? "Dein Archiv ist noch leer"
|
||||
: "Alles auf dem neuesten Stand") +
|
||||
"</h3><p>" +
|
||||
std::string(
|
||||
query ? "Versuche einen anderen Suchbegriff."
|
||||
: archive ? "Archivierte Meldungen bleiben hier zum Nachlesen."
|
||||
: "Hier erscheinen neue Meldungen aus deinem Verein.") +
|
||||
std::string(query ? "Versuche einen anderen Suchbegriff."
|
||||
: archive
|
||||
? "Archivierte Meldungen bleiben hier zum Nachlesen."
|
||||
: "Hier erscheinen neue Meldungen für dein Team.") +
|
||||
"</p></div>";
|
||||
}
|
||||
return out;
|
||||
@@ -107,9 +121,10 @@ std::string App::render_news() {
|
||||
news_world_ = s.world;
|
||||
news_source_ = source_.get();
|
||||
}
|
||||
const auto data =
|
||||
demo_news_view(*s.world, s.state, news_view_.value("folder", "inbox"),
|
||||
news_view_.value("query", ""));
|
||||
const auto data = demo_news_view(
|
||||
*s.world, s.state, news_view_.value("folder", "inbox"),
|
||||
news_view_.value("query", ""),
|
||||
ui_preferences_.at("display").value("language", std::string("de")));
|
||||
if (!data.value("ok", false))
|
||||
return window("Nachrichten",
|
||||
"<p>Das Nachrichtenpostfach ist nicht verfügbar.</p>");
|
||||
@@ -118,11 +133,18 @@ std::string App::render_news() {
|
||||
"<div id=\"news-underlay\" class=\"news-layout\"><div "
|
||||
"class=\"news-masthead\"><div class=\"news-brand\"><span "
|
||||
"class=\"news-wordmark\">kicker</span><div><p>DEIN "
|
||||
"VEREINSJOURNAL</p><h2>Nachrichten</h2></div></div><div "
|
||||
"SPIELJOURNAL</p><h2>Nachrichten</h2></div></div><div "
|
||||
"class=\"news-owner\"><div><strong>" +
|
||||
esc(data.at("club_name").get<std::string>()) + "</strong><p>" +
|
||||
esc(data.at("date").get<std::string>()) + "</p></div>" +
|
||||
crest(club(s), 88) + "</div></div>";
|
||||
(data.value("scope", Json::object()).value("kind", std::string()) ==
|
||||
"national"
|
||||
? picture(renderer_.diagram("news-header-national",
|
||||
ui_nation_flag_svg("de"), 176),
|
||||
88, 59)
|
||||
: crest(s.world->find("clubs", data.value("club", std::string())),
|
||||
88)) +
|
||||
"</div></div>";
|
||||
content += "<div class=\"news-navigation\"><div class=\"news-tabs\" "
|
||||
"role=\"tablist\" aria-label=\"Postfach und Archiv\">";
|
||||
for (const auto &f : {std::string("inbox"), std::string("archive")})
|
||||
@@ -160,19 +182,25 @@ std::string App::render_news() {
|
||||
" · " + std::to_string(data.at("shown_unread").get<int>()) +
|
||||
" ungelesen"
|
||||
: feedback);
|
||||
content +=
|
||||
"</p>" +
|
||||
btn("news-undo",
|
||||
glyph("undo", 32) + "<span>Rückgängig</span>",
|
||||
"news-undo",
|
||||
"id=\"news-undo\" style=\"display:" +
|
||||
std::string(news_undo_.is_array() && !news_undo_.empty()
|
||||
? "flex"
|
||||
: "none") +
|
||||
";\"") +
|
||||
"</div></div>";
|
||||
if (data.value("unbound_count", size_t(0)))
|
||||
content +=
|
||||
esc(" · " + std::to_string(data.at("unbound_count").get<size_t>()) +
|
||||
(data.at("unbound_count").get<size_t>() == 1
|
||||
? " Altbestand ohne Besitzerbeleg unverändert erhalten"
|
||||
: " Altbestände ohne Besitzerbeleg unverändert erhalten"));
|
||||
content += "</p>" +
|
||||
btn("news-undo", glyph("undo", 32) + "<span>Rückgängig</span>",
|
||||
"news-undo",
|
||||
"id=\"news-undo\" style=\"display:" +
|
||||
std::string(news_undo_.is_array() && !news_undo_.empty()
|
||||
? "flex"
|
||||
: "none") +
|
||||
";\"") +
|
||||
"</div></div>";
|
||||
if (!news_dialog_.empty()) {
|
||||
const auto all = demo_news_view(*s.world, s.state, folder, "");
|
||||
const auto all = demo_news_view(
|
||||
*s.world, s.state, folder, "",
|
||||
ui_preferences_.at("display").value("language", std::string("de")));
|
||||
const auto &items = all.at("items");
|
||||
const auto found =
|
||||
std::find_if(items.begin(), items.end(),
|
||||
@@ -188,14 +216,16 @@ std::string App::render_news() {
|
||||
"id=\"window-close\" aria-label=\"Fenster schließen\"") +
|
||||
"<h1>Nachrichten</h1></div><div class=\"content\">" + content +
|
||||
"</div><div class=\"news-screen-footer\"><span>Fiktive "
|
||||
"Vereinsmeldungen · Modern UI "
|
||||
"Meldungen · Modern UI "
|
||||
"Demo</span><span>OpenBMH</span></div><div "
|
||||
"class=\"news-homebar\"></div></div>";
|
||||
}
|
||||
|
||||
std::string App::render_news_widget(const Json &object) {
|
||||
const auto s = snap();
|
||||
const auto data = demo_news_view(*s.world, s.state, "inbox", "");
|
||||
const auto data = demo_news_view(
|
||||
*s.world, s.state, "inbox", "",
|
||||
ui_preferences_.at("display").value("language", std::string("de")));
|
||||
if (!data.value("ok", false))
|
||||
return "";
|
||||
const bool compact_preview =
|
||||
|
||||
Reference in new issue
Block a user