// SPDX-License-Identifier: GPL-3.0-only namespace {std::string news_body_markup(const std::string &text){auto value=esc(text);size_t at=0;while((at=value.find('\n',at))!=std::string::npos){value.replace(at,1,"
");at+=5;}return value;}} // The picture of a message: the crest of the club it is about, the flag for the national side, // else one of four drawn signs: a letter, the doctor's case, a coin, a newspaper. std::string App::render_news_art(const Json &message, double size) { const auto kind = message.at("kind").get(); const auto sign = [&](const char *name, const std::string &body) { return picture(renderer_.diagram(std::string("news-sign-") + name, "" + body + "", 192), size * .95, size * .95); }; const auto has = [&](std::initializer_list words) { const auto text = message.value("title", std::string()) + " " + message.value("body", std::string()); return std::any_of(words.begin(), words.end(), [&](const char *word) { return text.find(word) != std::string::npos; }); }; static const std::string letter = ""; static const std::string doctor = ""; static const std::string coin = ""; static const std::string paper = ""; std::string art; 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") art = crest(world_->find("clubs", message.value("scope", Json::object()).value("id", std::string())), size * .69); // a report of a match shows both clubs: the one the report is about and its opponent if (kind == "fixture" && message.contains("scope") && message.contains("article")) { const auto &article = message.at("article"); const Json *data = article.is_object() && article.contains("event") && article.at("event").is_object() && article.at("event").contains("data") ? &article.at("event").at("data") : nullptr; const auto *own = world_->find("clubs", message.at("scope").value("id", std::string())); const auto *other = data && data->is_object() && data->contains("opponent") && data->at("opponent").is_object() ? world_->find("clubs", data->at("opponent").value("id", std::string())) : nullptr; if (own && other) art = "" + crest(own, size * .46) + crest(other, size * .46) + ""; } if (art.empty()) { // what a letter is about decides its sign: an injury, money, a report of a match if (has({"verletz", "Verletz", "fällt", "Arzt", "Prellung", "prellung", "Zerrung", "riss"})) art = sign("doctor", doctor); else if (kind == "finance" || has({"€", "Prämie", "prämie", "Werbe", "Sponsor", "Zinsen", "Kredit"})) art = sign("coin", coin); else if (kind == "fixture" || message.value("category", std::string()) == "Bericht") art = sign("paper", paper); else art = sign("letter", letter); } return "
" + art + "
"; } // The messages of a folder as rows of the page: a dot while unread, the sender's picture, date // and kind, the headline as a link, a line of the text, and the two things one does with a // message. The controls replace this list alone while the player types a search. std::string App::render_news_feed(const Json &data) { const std::string ink = theme_ == "midnight" ? "#d2d6dc" : theme_ == "matchday" ? "#3a4830" : "#474238"; const auto icon = [&](const char *name, const char *path) { return picture(renderer_.diagram(std::string("news-icon-") + name + "-" + theme_, "", 72), 36, 36); }; std::string out; for (const auto &m : data.at("items")) { const auto id = m.at("id").get(), name = m.at("title").get(); const bool unread = m.at("read").is_boolean() && !m.at("read").get(), archived = m.at("folder") == "archive" || (engine_mode_ && m.at("folder") == "trash"); const auto category = m.at("category").get(); out += "
" + render_news_art(m, 100) + "
" + esc(m.at("display_date").get()) + "" + esc(category) + "" + (unread ? "Neu" : "") + (data.value("fictional", false) ? "Demo" : "") + "
" + btn("news-open", "" + esc(name) + "", "pg-link nw-title", "id=\"news-read-" + id + "\" data-id=\"" + id + "\" aria-label=\"Nachricht lesen: " + esc(name) + "\"") + // a one-line message is its own headline; the excerpt would only repeat it (m.at("body").get() == name ? std::string() : "" + esc(m.at("body").get()) + "") + "
" + pg_button("news-move", icon(archived ? "restore" : "archive", archived ? "M3 10l4-4v3h10v2H7v3z" : "M3 4h14v4H3zM4 8v8h12V8M8 11h4"), "round", "id=\"news-move-" + id + "\" data-id=\"" + id + "\" aria-label=\"" + std::string(archived ? "Ins Postfach verschieben: " : "Archivieren: ") + esc(name) + "\" title=\"" + std::string(archived ? "Ins Postfach verschieben" : "Archivieren") + "\" " + std::string(engine_mode_ && m.value("mutable", false) == false ? "disabled" : "")) + pg_button("news-delete", icon("trash", "M4 6h12M8 6V4h4v2M6 6l1 10h6l1-10"), "round", "id=\"news-delete-" + id + "\" data-id=\"" + id + "\" aria-label=\"Löschen: " + esc(name) + "\" title=\"Löschen\" " + std::string(engine_mode_ && (m.value("mutable", false) == false || m.at("folder") == "trash") ? "disabled" : "")) + "
"; } if (out.empty()) { const bool query = !news_view_.value("query", "").empty(), archive = news_view_.value("folder", "inbox") == "archive"; out = "
" + std::string(query ? "Keine Nachrichten gefunden" : archive ? "Dein Archiv ist noch leer" : "Alles auf dem neuesten Stand") + "" + std::string(query ? "Versuche einen anderen Suchbegriff." : archive ? "Archivierte Meldungen bleiben hier zum Nachlesen." : "Hier erscheinen neue Meldungen für dein Team.") + "
"; } return out; } std::string App::render_news_detail(const Json &message) { const auto name = message.at("title").get(), category = message.at("category").get(); const bool archived = message.at("folder") == "archive" || (engine_mode_ && message.at("folder") == "trash"); return "

" + esc(name) + "

" + render_news_art(message, 144) + "
" + esc(category) + "" + esc(message.at("display_date").get()) + "

" + (engine_mode_ ? news_body_markup(message.at("body").get()) : esc(message.at("body").get())) + "

" + std::string(engine_mode_ ? (message.value("source", std::string()) == "mail" ? "Aus dem Spielpostfach" : "Bericht aus dem Almanach") : "Fiktive Meldung für den Oberflächen-Prototyp.") + "
" + pg_button("news-detail-move", archived ? "Ins Postfach" : "Archivieren", "", "id=\"news-detail-move\" " + std::string(engine_mode_ && message.value("mutable", false) == false ? "disabled" : "")) + pg_button("news-detail-close", "Schließen", "primary", "id=\"news-detail-close\"") + "
"; } // The inbox as a page of the notebook: whose it is, the folders as tabs, the search, the // messages, and at the foot what there is and the way back from the last move. std::string App::render_news() { const auto s = engine_mode_?Snapshot{}:snap(); if (!engine_mode_ && (news_world_ != s.world || news_source_ != source_.get())) { reset_news(); news_world_ = s.world; news_source_ = source_.get(); } const auto data = news_data(news_view_.value("folder","inbox"),news_view_.value("query","")); if (!data.value("ok", false)) return window("Nachrichten", "
Das Nachrichtenpostfach ist nicht verfügbar.
"); const auto folder = news_view_.value("folder", "inbox"); std::string content = "
" + (data.value("scope", Json::object()).value("kind", std::string()) == "national" ? picture(renderer_.diagram("news-header-national", ui_nation_flag_svg("de"), 176), 104, 70) : crest(world_->find("clubs", data.value("club", std::string())), 104)) + "
" + esc(data.at("club_name").get()) + "" + esc(data.at("date").get()) + "
"; for (const auto &f : (engine_mode_ ? std::vector{"inbox", "archive", "trash"} : std::vector{"inbox", "archive"})) content += btn("news-folder", "" + std::string(f == "inbox" ? "Postfach" : f == "archive" ? "Archiv" : "Papierkorb") + " · " + std::to_string(data.at("counts").at(f).get()) + "", folder == f ? "on" : "", "id=\"news-tab-" + f + "\" data-id=\"" + f + "\" role=\"tab\" aria-controls=\"news-feed\" aria-selected=\"" + (folder == f ? "true" : "false") + "\" tabindex=\"" + (folder == f ? "0" : "-1") + "\""); content += "
" + pg_button("news-read-all", "Alle gelesen", "", "id=\"news-read-all\" " + std::string(data.at("can_read").get() ? "" : "disabled")) + "
" + render_news_feed(data) + "
"; if (!news_dialog_.empty()) { const auto all = news_data(folder,""); const auto &items = all.at("items"); const auto found = std::find_if(items.begin(), items.end(), [&](const auto &m) { return m.at("id") == news_dialog_; }); if (found != items.end()) content += render_news_detail(*found); else news_dialog_.clear(); } return window("Nachrichten", content); } std::string App::render_news_widget(const Json &object) { const auto data = news_data("inbox", ""); const auto raw_items = engine_mode_ ? engine_news_value_.at("items") : snap().state.at("news").at("items"); if (!data.value("ok", false)) return ""; const bool compact_preview = object.value("w", 472) <= 472 && object.value("h", 472) <= 472; const int limit = compact_preview ? 2 : 3; std::string out = btn(engine_mode_ ? "engine-open" : "open:news", "Postfach · " + std::to_string(data.at(engine_mode_ ? "shown_unread" : "unread").get()) + " ungelesen" + glyph("arrow-diagonal", 26) + "", "news-widget-heading", "id=\"desktop-news-open\" data-id=\"news\" data-interactive=\"true\" " "aria-label=\"Nachrichten öffnen\""); out += "
"; int count = 0; // Gold's widget uses source order, independently of the open tablet's folder, // search and sorted list. Resolve templates through the same read projection. for (const auto &raw : raw_items) { if (raw.at("folder") != "inbox") continue; const auto &shown = data.at("items"); const auto found = std::find_if(shown.begin(), shown.end(), [&](const auto &m) { return m.at("id") == raw.at("id"); }); if (found == shown.end()) continue; out += "
at("read") == false ? " unread" : "") + "\">
" + esc(found->at("display_date").get()) + "" + esc(found->at("title").get()) + "
"; if (++count == limit) break; } return out + "
"; }