/* This file is part of FediBlock-backend. * Copyright © 2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ #include "json.hpp" #include "fs-compat.hpp" #include "types.hpp" #include namespace FediBlock::json { string to_json(const entry_type &entry) { const string filename{fs::path(entry.screenshot_filepath).filename()}; // clang-format off const nlohmann::json json{{"instance", entry.instance}, {"tags", entry.tags}, {"receipts", entry.receipts}, {"description", entry.description}, {"screenshot", filename}, {"report_time", entry.report_time}}; // clang-format on return json.dump(4); } string pull_request_body(string_view branch, const entry_type &entry) { const nlohmann::json json{{"base", "main"}, {"head", branch.data()}, {"title", "From web: " + entry.instance}, {"body", entry.description}}; return json.dump(); } entry_type from_json(const string_view json_string) { const auto json{nlohmann::json::parse(json_string.data())}; entry_type entry; entry.description = json[0].at("description").get(); entry.instance = json[0].at("instance").get(); entry.receipts = json[0].at("receipts").get>(); try { entry.screenshot_filepath = json[0].at("screenshot").get(); } #if NLOHMANN_JSON_VERSION_MAJOR >= 3 catch (const nlohmann::detail::out_of_range &) // Ignore missing screenshot. #else catch (const std::out_of_range &) #endif {} entry.tags = json[0].at("tags").get>(); try { entry.report_time = json[0].at("report_time").get(); } // TODO: this is only necessary for compatibility with old test data. Remove // before going live. #if NLOHMANN_JSON_VERSION_MAJOR >= 3 catch (const nlohmann::detail::out_of_range &) #else catch (const std::out_of_range &) #endif { entry.report_time = "1970-01-01T00:00:00"; } return entry; } } // namespace FediBlock::json