Guard against missing user_webpage and social

This commit is contained in:
blankie 2023-04-08 15:48:36 +07:00
parent 00df5327f0
commit 1a9c9d1a53
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 14 additions and 13 deletions

View File

@ -53,15 +53,14 @@ void from_json(const nlohmann::json& j, User& user) {
user.profile_pictures.thumbnails.push_back(profile_img.at("main").get<std::string>());
user.profile_pictures.original = get_original_profile_picture(user.profile_pictures.thumbnails.back());
std::string user_webpage = j.at("user_webpage").get<std::string>();
if (!user_webpage.empty()) {
user.links.push_back({"Webpage", std::move(user_webpage)});
if (j.contains("user_webpage")) {
std::string user_webpage = j.at("user_webpage").get<std::string>();
if (!user_webpage.empty()) {
user.links.push_back({"Webpage", std::move(user_webpage)});
}
}
auto add_social_as_needed = [&](const char* key, const char* public_name) {
nlohmann::json social = j.at("social");
if (!social.is_object()) {
return;
}
nlohmann::json social = j["social"];
if (!social.contains(key)) {
return;
}
@ -69,12 +68,14 @@ void from_json(const nlohmann::json& j, User& user) {
std::string url = social[key].at("url").get<std::string>();
user.links.push_back({public_name, std::move(url)});
};
add_social_as_needed("twitter", "Twitter");
add_social_as_needed("instagram", "Instagram");
add_social_as_needed("tumblr", "Tumblr");
add_social_as_needed("facebook", "Facebook");
add_social_as_needed("circlems", "Circle.ms");
add_social_as_needed("pawoo", "Pawoo");
if (j.contains("social") && j["social"].is_object()) {
add_social_as_needed("twitter", "Twitter");
add_social_as_needed("instagram", "Instagram");
add_social_as_needed("tumblr", "Tumblr");
add_social_as_needed("facebook", "Facebook");
add_social_as_needed("circlems", "Circle.ms");
add_social_as_needed("pawoo", "Pawoo");
}
}
static std::regex resolution_path_regex("/c/(\\d+x\\d+)(.+)");