// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/news_journal.hpp" #include #include namespace bmh::modern_ui { namespace { bool str(const Json &v, size_t maximum, bool required = true) { return v.is_string() && v.get_ref().size() <= maximum && v.get_ref().find('\0') == std::string::npos && (!required || v.get_ref().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().find_first_not_of( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst" "uvwxyz0123456789:_./-") == std::string::npos; } bool row_id(const Json &v) { return str(v, 64) && v.get_ref().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 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().size() != 16 || s.at("fingerprint") .get_ref() .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()); 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