// 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, "", 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 += "
" +
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()) + "") + "
";
}
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.") + "
" +
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.") + "
";
}
// 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", "
";
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 += "