2023-11-22 08:39:24 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <regex>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <system_error>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
#include "models.h"
|
|
|
|
#include "numberhelper.h"
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
static time_t parse_rfc3339(const std::string& str);
|
|
|
|
|
|
|
|
|
|
|
|
void from_json(const json& j, Emoji& emoji) {
|
|
|
|
j.at("shortcode").get_to(emoji.shortcode);
|
|
|
|
j.at("url").get_to(emoji.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void from_json(const json& j, AccountField& field) {
|
|
|
|
j.at("name").get_to(field.name);
|
|
|
|
field.value_html = j.at("value").get<std::string>();
|
|
|
|
if (j.at("verified_at").is_null()) {
|
|
|
|
field.verified_at = -1;
|
|
|
|
} else {
|
|
|
|
field.verified_at = parse_rfc3339(j["verified_at"].get_ref<const std::string&>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::regex host_regex(R"EOF(https?://([a-z0-9-.]+)/.+)EOF", std::regex::ECMAScript | std::regex::icase);
|
|
|
|
void from_json(const json& j, Account& account) {
|
|
|
|
using namespace std::string_literals;
|
|
|
|
|
|
|
|
j.at("id").get_to(account.id);
|
|
|
|
j.at("username").get_to(account.username);
|
|
|
|
j.at("display_name").get_to(account.display_name);
|
|
|
|
account.created_at = parse_rfc3339(j.at("created_at").get_ref<const std::string&>());
|
|
|
|
account.note_html = j.at("note").get<std::string>();
|
|
|
|
j.at("avatar").get_to(account.avatar);
|
2023-11-23 08:49:27 +00:00
|
|
|
j.at("avatar_static").get_to(account.avatar_static);
|
2023-11-22 08:39:24 +00:00
|
|
|
j.at("header").get_to(account.header);
|
|
|
|
j.at("followers_count").get_to(account.followers_count);
|
|
|
|
j.at("following_count").get_to(account.following_count);
|
|
|
|
j.at("statuses_count").get_to(account.statuses_count);
|
|
|
|
j.at("emojis").get_to(account.emojis);
|
|
|
|
j.at("fields").get_to(account.fields);
|
|
|
|
|
|
|
|
std::smatch sm;
|
|
|
|
const std::string& url = j.at("url").get_ref<const std::string&>();
|
|
|
|
if (!std::regex_match(url, sm, host_regex)) {
|
|
|
|
throw std::runtime_error("failed to find host in url: "s + url);
|
|
|
|
}
|
2023-11-23 06:05:17 +00:00
|
|
|
account.server = sm.str(1);
|
|
|
|
}
|
|
|
|
|
2023-11-23 12:20:49 +00:00
|
|
|
void from_json(const json& j, Media& media) {
|
|
|
|
j.at("type").get_to(media.type);
|
|
|
|
j.at("url").get_to(media.url);
|
|
|
|
if (!j.at("preview_url").is_null()) {
|
|
|
|
media.preview_url = j["preview_url"].get<std::string>();
|
|
|
|
}
|
|
|
|
if (!j.at("remote_url").is_null()) {
|
|
|
|
media.remote_url = j["remote_url"].get<std::string>();
|
|
|
|
}
|
|
|
|
if (!j.at("description").is_null()) {
|
|
|
|
media.description = j["description"].get<std::string>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-23 06:05:17 +00:00
|
|
|
void from_json(const json& j, Post& post) {
|
|
|
|
j.at("id").get_to(post.id);
|
|
|
|
post.created_at = parse_rfc3339(j.at("created_at").get_ref<const std::string&>());
|
|
|
|
if (!j.at("in_reply_to_id").is_null()) {
|
|
|
|
post.in_reply_to_id = j["in_reply_to_id"].get<std::string>();
|
|
|
|
}
|
|
|
|
if (!j.at("in_reply_to_account_id").is_null()) {
|
|
|
|
post.in_reply_to_account_id = j["in_reply_to_account_id"].get<std::string>();
|
|
|
|
}
|
|
|
|
j.at("sensitive").get_to(post.sensitive);
|
|
|
|
j.at("spoiler_text").get_to(post.spoiler_text);
|
|
|
|
j.at("replies_count").get_to(post.replies_count);
|
|
|
|
j.at("reblogs_count").get_to(post.reblogs_count);
|
|
|
|
j.at("favourites_count").get_to(post.favorites_count);
|
|
|
|
if (!j.at("edited_at").is_null()) {
|
|
|
|
post.edited_at = parse_rfc3339(j["edited_at"].get_ref<const std::string&>());
|
|
|
|
} else {
|
|
|
|
post.edited_at = -1;
|
|
|
|
}
|
|
|
|
post.content = j.at("content").get<std::string>();
|
|
|
|
if (!j.at("reblog").is_null()) {
|
|
|
|
post.reblog = std::make_unique<Post>();
|
|
|
|
from_json(j["reblog"].get<json>(), *post.reblog.get());
|
|
|
|
}
|
|
|
|
j.at("account").get_to(post.account);
|
2023-11-23 12:20:49 +00:00
|
|
|
j.at("media_attachments").get_to(post.media_attachments);
|
2023-11-23 06:05:17 +00:00
|
|
|
j.at("emojis").get_to(post.emojis);
|
2023-11-22 08:39:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-23 23:46:47 +00:00
|
|
|
void from_json(const json& j, PostContext& context) {
|
|
|
|
j.at("ancestors").get_to(context.ancestors);
|
|
|
|
j.at("descendants").get_to(context.descendants);
|
|
|
|
}
|
|
|
|
|
2023-11-22 08:39:24 +00:00
|
|
|
|
|
|
|
static std::regex rfc3339_re(R"EOF((\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:(Z)|([+-]\d{2}):(\d{2})))EOF", std::regex::ECMAScript | std::regex::icase);
|
|
|
|
time_t parse_rfc3339(const std::string& str) {
|
|
|
|
using namespace std::string_literals;
|
|
|
|
|
|
|
|
std::smatch sm;
|
|
|
|
if (!std::regex_match(str, sm, rfc3339_re)) {
|
|
|
|
throw std::invalid_argument("unknown date format: "s + str);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tm tm = {
|
|
|
|
.tm_sec = to_int(sm.str(6)),
|
|
|
|
.tm_min = to_int(sm.str(5)),
|
|
|
|
.tm_hour = to_int(sm.str(4)),
|
|
|
|
|
|
|
|
.tm_mday = to_int(sm.str(3)),
|
|
|
|
.tm_mon = to_int(sm.str(2)) - 1,
|
|
|
|
.tm_year = to_int(sm.str(1)) - 1900,
|
|
|
|
|
|
|
|
.tm_isdst = -1,
|
|
|
|
.tm_gmtoff = !sm.str(7).empty() ? 0 : to_int(sm.str(8)) * 60 * 60 + to_int(sm.str(9)) * 60,
|
|
|
|
};
|
|
|
|
time_t time = mktime(&tm);
|
|
|
|
if (time == -1) {
|
|
|
|
throw std::system_error(errno, std::generic_category(), "mktime()");
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|