This repository has been archived on 2021-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
backend/src/json.cpp

91 lines
3.0 KiB
C++
Raw Normal View History

2020-06-29 06:10:15 +02:00
/* This file is part of FediBlock-backend.
* Copyright © 2020, 2021 tastytea <tastytea@tastytea.de>
2020-06-29 06:10:15 +02:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "json.hpp"
#include "fs-compat.hpp"
2020-07-02 07:13:57 +02:00
#include "types.hpp"
2020-06-29 06:10:15 +02:00
#include <nlohmann/json.hpp>
2020-07-01 20:51:35 +02:00
namespace FediBlock::json
2020-06-29 06:10:15 +02:00
{
2020-07-02 07:13:57 +02:00
string to_json(const entry_type &entry)
2020-06-29 06:10:15 +02:00
{
vector<string> filenames;
for (const auto &screenshot : entry.screenshot_filepaths)
{
filenames.push_back(fs::path(screenshot).filename());
}
2020-07-05 11:38:37 +02:00
// clang-format off
2020-06-29 07:07:11 +02:00
const nlohmann::json json{{"instance", entry.instance},
{"tags", entry.tags},
{"receipts", entry.receipts},
2020-06-29 22:37:09 +02:00
{"description", entry.description},
{"screenshots", filenames},
{"report_time", entry.report_time},
{"unreachable", entry.unreachable}};
2020-07-05 11:38:37 +02:00
// clang-format on
2020-06-29 06:10:15 +02:00
return json.dump(4);
}
2020-07-02 07:13:57 +02:00
string pull_request_body(string_view branch, const entry_type &entry)
2020-07-02 00:18:40 +02:00
{
const nlohmann::json json{{"base", "main"},
{"head", branch.data()},
2020-07-02 00:18:40 +02:00
{"title", "From web: " + entry.instance},
{"body", entry.description}};
return json.dump();
}
2020-07-02 08:09:45 +02:00
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<string>();
entry.instance = json[0].at("instance").get<string>();
entry.receipts = json[0].at("receipts").get<vector<string>>();
try
{
if (json[0].count("screenshot") == 1)
{
// Compatibility for old entries.
entry.screenshot_filepaths.push_back(
json[0].at("screenshot").get<string>());
}
else
{
entry.screenshot_filepaths = json[0]
.at("screenshots")
.get<vector<string>>();
}
2020-07-02 08:09:45 +02:00
}
2020-07-02 11:04:18 +02:00
#if NLOHMANN_JSON_VERSION_MAJOR >= 3
2020-07-02 08:09:45 +02:00
catch (const nlohmann::detail::out_of_range &) // Ignore missing screenshot.
2020-07-02 11:04:18 +02:00
#else
catch (const std::out_of_range &)
#endif
2020-07-02 08:09:45 +02:00
{}
entry.tags = json[0].at("tags").get<vector<string>>();
2020-10-15 06:02:16 +02:00
entry.report_time = json[0].at("report_time").get<string>();
entry.unreachable = json[0].value("unreachable", false);
2020-07-02 08:09:45 +02:00
return entry;
}
2020-07-01 20:51:35 +02:00
} // namespace FediBlock::json