// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/demo_news.hpp" #include "modern/ui/engine_bridge.hpp" #include "modern/ui/news_catalog.hpp" #include #include namespace bmh::modern_ui { namespace { void check(bool yes, const char *text) { if (!yes) throw editor::Error(text); } std::string text(const Json &v) { return v.is_string() ? v.get() : v.is_object() ? v.value("text", std::string()) : std::string(); } std::string fold(std::string s) { for (size_t i = 0; i < s.size(); ++i) { if (s[i] >= 'A' && s[i] <= 'Z') s[i] = char(s[i] - 'A' + 'a'); else if (static_cast(s[i]) == 0xc3 && i + 1 < s.size()) { auto &n = s[++i]; const auto c = static_cast(n); if (c >= 0x80 && c <= 0x9e && c != 0x97) n = char(c + 32); } } return s; } } // namespace NewsCatalog engine_news_catalogue(const NewsCatalog &base) { auto data = base.data(); data["version"] = 2; const auto add = [&](const std::string &type, const std::string &id, bool result) { Json p = {{"competition", {{"type", "entity"}}}, {"team", {{"type", "entity"}, {"source", "scope"}}}}; if (result) { p["opponent"] = {{"type", "entity"}}; p["score"] = {{"type", "score"}}; } data["templates"].push_back( {{"id", id}, {"event", type}, {"variant", "recorded"}, {"kind", "fixture"}, {"scopes", {"club", "national"}}, {"tone", result ? "neutral" : "positive"}, {"parameters", p}, {"guards", Json::array()}, {"de", {{"headline", result ? "Ergebnis in {{competition}}" : "{{team}} gewinnt {{competition}}"}, {"sentences", result ? Json::array({"{{team}} und {{opponent}} treffen in " "{{competition}} aufeinander.", "Das Ergebnis lautet {{score}}."}) : Json::array( {"{{team}} hat {{competition}} gewonnen.", "Der Wettbewerb endet mit {{team}} als Sieger."})}}}, {"en", {{"headline", result ? "Result in {{competition}}" : "{{team}} win {{competition}}"}, {"sentences", result ? Json::array({"{{team}} meet {{opponent}} in {{competition}}.", "The recorded result is {{score}}."}) : Json::array( {"{{team}} have won {{competition}}.", "The competition ends with {{team}} as winners."})}}}, {"fixture", Json::object()}}); }; add("cup.round_result", "engine-cup-round-recorded", true); add("cup.final_result", "engine-cup-final-recorded", true); add("cup.won", "engine-cup-title-recorded", false); return NewsCatalog(std::move(data)); } Json engine_news_projection(const Json &bundle, const Json &read, const NewsCatalog &catalogue, const std::string &language) { validate_engine_desk(bundle); const auto &meta = bundle.at("desktop").at("meta"); check(read.value("messageType", "") == "snapshot" && read.at("data").value("kind", "") == "news", "Nachrichten sind nicht verfügbar."); for (const auto *key : {"contractVersion", "careerId", "worldId", "worldRevision", "stateRevision", "scope"}) check(read.at("meta").at(key) == meta.at(key), "Das Nachrichtenpostfach hat sich geändert."); check(language == "de" || language == "en", "Ungültige Nachrichtensprache."); const auto &d = read.at("data"), &desk = bundle.at("desktop").at("data"); const auto owner = meta.at("scope").at("managerId"); const auto &commands = meta.at("scope").at("allowedCommands"); const bool can_change = std::find(commands.begin(), commands.end(), Json("news.update")) != commands.end(); Json items = Json::array(), unsupported = Json::array(); std::set ids; const Json scope = { {"kind", meta.at("scope").at("role") == "national" ? "national" : "club"}, {"id", meta.at("scope").at("teamId")}, {"name", text(desk.at("team").at("name"))}}; for (const auto &message : d.at("messages")) { const auto id = message.at("id").get(); check(!id.empty() && ids.insert(id).second && message.at("ownerManagerId") == owner, "Fremde oder doppelte Postfachnachricht."); const auto folder = message.at("folder").get(); check(folder == "inbox" || folder == "archive" || folder == "trash", "Unbekannter Nachrichtenordner."); std::string body; for (const auto &p : message.at("paragraphs")) { if (!body.empty()) body += "\n\n"; body += text(p); } items.push_back({{"id", id}, {"date", message.at("date")}, {"display_date", news_display_date(message.at("date"))}, {"title", text(message.at("subject"))}, {"body", body}, {"kind", "mail"}, {"category", "Postfach"}, {"read", message.at("read")}, {"folder", folder}, {"scope", scope}, {"mutable", can_change}, {"source", "mail"}}); } for (const auto &event : d.at("events")) { const auto id = event.at("id").get(); check(!id.empty() && ids.insert(id).second, "Doppelte Pressekennung."); Json normalized = event; if (event.at("type") == "cup.round_result" || event.at("type") == "cup.final_result" || event.at("type") == "cup.won") normalized["variant"] = "recorded"; if (normalized.value("variant", Json()).is_null()) normalized.erase("variant"); Json parameters = Json::object(); for (const auto &t : catalogue.data().at("templates")) if (t.at("event") == event.at("type")) for (const auto &[key, spec] : t.at("parameters").items()) parameters[key] = spec; Json data = Json::object(); for (const auto &[key, value] : event.at("data").items()) if (key != "team" && parameters.contains(key) && !value.is_null()) { if (parameters.at(key).at("type") == "money") { std::string error; const auto amount = news_money_from_dto(value, error); if (amount) data[key] = amount->json(); else data[key] = value; } else data[key] = value; } normalized["data"] = data; const auto rendered = catalogue.from_event( normalized, {{"language", language}, {"asOf", meta.at("date")}, {"manager", {{"id", owner}, {"name", text(desk.at("manager").at("name"))}}}}); if (!rendered.value("ok", false)) { unsupported.push_back( {{"id", id}, {"type", event.at("type")}, {"error", rendered}}); continue; } const auto &article = rendered.at("article"); const bool marked = event.contains("read") && event.at("read").is_boolean() && event.contains("folder"); items.push_back({{"id", id}, {"date", article.at("date")}, {"display_date", news_display_date(article.at("date"))}, {"title", article.at("headline")}, {"body", article.at("body")}, {"kind", article.at("kind")}, {"category", "Bericht"}, {"read", marked ? event.at("read") : Json()}, {"folder", marked ? event.at("folder") : Json("inbox")}, {"scope", article.at("scope")}, {"mutable", marked && can_change}, {"source", "press"}, {"article", article}}); } std::stable_sort( items.begin(), items.end(), [](const auto &a, const auto &b) { return a.at("date") > b.at("date"); }); return {{"ok", true}, {"fictional", false}, {"items", items}, {"unsupported", unsupported}, {"club", meta.at("scope").at("teamId")}, {"club_name", text(desk.at("team").at("name"))}, {"manager", text(desk.at("manager").at("name"))}, {"date", news_display_date(meta.at("date"))}, {"scope", scope}}; } Json engine_news_view(const Json &value, const std::string &folder, const std::string &query) { check(folder == "inbox" || folder == "archive" || folder == "trash", "Ungültiger Nachrichtenordner."); check(query.size() <= 480, "Suchbegriff ist zu lang."); auto out = value; Json shown = Json::array(), counts = {{"inbox", 0}, {"archive", 0}, {"trash", 0}}; int unread = 0; bool can_read = false; for (const auto &row : value.at("items")) { const auto where = row.at("folder").get(); check(counts.contains(where), "Unbekannter Nachrichtenordner."); counts[where] = counts.at(where).get() + 1; if (where != folder) continue; const bool is_unread = row.at("read").is_boolean() && row.at("read") == false; can_read |= is_unread && row.at("mutable") == true; if (!query.empty() && fold(row.at("title").get() + " " + row.at("body").get() + " " + row.at("display_date").get()) .find(fold(query)) == std::string::npos) continue; shown.push_back(row); unread += is_unread; } out["items"] = shown; out["counts"] = counts; out["can_read"] = can_read; out["shown_unread"] = unread; return out; } Json engine_news_command(const Json &bundle, const Json &value, const std::string &id, const std::string &action, const std::string &request) { validate_engine_desk(bundle); const auto &meta = bundle.at("desktop").at("meta"); check(!request.empty(), "Anfragekennung fehlt."); const auto &commands = meta.at("scope").at("allowedCommands"); check(std::find(commands.begin(), commands.end(), Json("news.update")) != commands.end(), "Dieses Postfach ist schreibgeschützt."); check(action == "read" || action == "unread" || action == "archive" || action == "trash" || action == "restore", "Unbekannte Nachrichtenaktion."); const auto found = std::find_if(value.at("items").begin(), value.at("items").end(), [&](const auto &m) { return m.at("id") == id; }); check(found != value.at("items").end() && found->at("mutable") == true, "Diese Nachricht kann nicht geändert werden."); return {{"messageType", "command"}, {"requestId", request}, {"careerId", meta.at("careerId")}, {"expectedRevision", meta.at("stateRevision")}, {"owner", {{"managerId", meta.at("scope").at("managerId")}}}, {"kind", "news.update"}, {"payload", {{"messageId", id}, {"action", action}}}}; } } // namespace bmh::modern_ui