// SPDX-License-Identifier: GPL-3.0-only Json App::news_data(const std::string &folder, const std::string &query) const { if (engine_mode_) return engine_news_view(engine_news_value_, folder, query); const auto s = snap(); return demo_news_view( *s.world, s.state, folder, query, ui_preferences_.at("display").value("language", std::string("de"))); } void App::engine_news_sync() { const auto &meta = engine_data_.at("desktop").at("meta"); const bool same = engine_news_context_.is_object() && engine_news_context_.value("careerId", Json()) == meta.at("careerId") && engine_news_context_.at("scope").at("managerId") == meta.at("scope").at("managerId"); if (!same) { reset_news(); engine_news_undo_ = Json(); } if (engine_news_undo_.is_object() && engine_news_undo_.at("context") != meta) engine_news_undo_ = Json(); engine_news_context_ = meta; engine_news_value_ = Json(); engine_news_error_.clear(); try { if (!engine_news_catalog_) engine_news_catalog_ = std::make_unique(engine_news_catalogue( NewsCatalog::load(assets_ / "news-catalog.json"))); engine_news_value_ = engine_news_projection( engine_data_, engine_data_.at("news"), *engine_news_catalog_, ui_preferences_.at("display").value("language", std::string("de"))); } catch (const std::exception &e) { engine_news_error_ = e.what(); } if (!engine_news_operation_.empty()) { if (engine_response_.value("messageType", "") == "command_result") { if (engine_news_pending_undo_.is_object()) { engine_news_undo_ = engine_news_pending_undo_; engine_news_undo_["context"] = meta; } news_view_["feedback"] = engine_response_.value("outcome", "") == "no_change" ? "Unverändert." : "Postfach aktualisiert."; } else news_view_["feedback"] = engine_notice_; engine_news_operation_.clear(); engine_news_pending_undo_ = Json(); } } void App::engine_news_send(const std::vector &requests, const std::string &operation) { if (requests.empty() || engine_busy_ || !engine_fresh_) return; auto link = engine_link_; engine_news_operation_ = operation; engine_start( "Postfach wird aktualisiert …", [link, requests] { Json response; for (auto request : requests) { const auto b = link->snapshot(); request["expectedRevision"] = b.at("desktop").at("meta").at("stateRevision"); response = link->command(request); if (response.value("messageType", "") != "command_result") break; } return EngineReply{link, link->snapshot(), response, nullptr}; }, true); } bool App::engine_news_action(const std::string &action, Rml::Element *target) { if (!action.starts_with("news-")) return false; if (menu_ != "news" || engine_busy_ || !engine_fresh_ || !engine_news_value_.is_object()) return true; const auto id = target ? target->GetAttribute("data-id", "") : std::string(); if (action == "news-detail-close") { news_dialog_.clear(); focus_after_render_ = news_return_focus_; refresh(); return true; } if (!news_dialog_.empty() && action != "news-detail-move") return true; if (action == "news-folder") { if (id == "inbox" || id == "archive" || id == "trash") { news_view_["folder"] = id; news_scroll_ = 0; focus_after_render_ = target->GetId(); refresh(); } return true; } if (action == "news-read-all") { std::vector requests; for (const auto &m : engine_news_value_.at("items")) if (m.at("folder") == news_view_.value("folder", std::string("inbox")) && m.at("read") == false && m.at("mutable") == true) requests.push_back(engine_news_command(engine_data_, engine_news_value_, m.at("id"), "read", editor::new_id("request:ui-"))); engine_news_undo_ = Json(); engine_news_send(requests, "read-all"); return true; } if (action == "news-undo") { if (!engine_news_undo_.is_object() || engine_news_undo_.at("context") != engine_news_context_) return true; const auto undo = engine_news_undo_; engine_news_undo_ = Json(); engine_news_send({engine_news_command( engine_data_, engine_news_value_, undo.at("id"), undo.at("folder") == "archive" ? "archive" : "restore", editor::new_id("request:ui-"))}, "undo"); return true; } const auto requested = action == "news-detail-move" ? news_dialog_ : id; const auto &items = engine_news_value_.at("items"); const auto found = std::find_if(items.begin(), items.end(), [&](const auto &m) { return m.at("id") == requested; }); if (found == items.end()) return true; if (action == "news-open") { news_dialog_ = requested; news_return_focus_ = "news-read-" + requested; focus_after_render_ = "news-detail-close"; if (found->at("mutable") == true && found->at("read") == false) { engine_news_undo_ = Json(); engine_news_send( {engine_news_command(engine_data_, engine_news_value_, requested, "read", editor::new_id("request:ui-"))}, "read"); } refresh(); return true; } if (found->at("mutable") != true) return true; if (action == "news-delete" || action == "news-move" || action == "news-detail-move") { const auto folder = found->at("folder").get(); const auto kind = action == "news-delete" ? "trash" : folder == "archive" || folder == "trash" ? "restore" : "archive"; engine_news_pending_undo_ = {{"id", requested}, {"folder", folder}}; news_dialog_.clear(); focus_after_render_ = "news-tab-" + news_view_.value("folder", std::string("inbox")); engine_news_send( {engine_news_command(engine_data_, engine_news_value_, requested, kind, editor::new_id("request:ui-"))}, kind); return true; } return true; }