pixwhile/routes/users/common.cpp

61 lines
2.2 KiB
C++
Raw Normal View History

2023-04-05 16:36:20 +00:00
#include "common.h"
#include "../../config.h"
#include "../../servehelper.h"
#include "../../pixivclient.h"
static inline Element generate_user_links(const User& user);
static std::string thumbnail_or_original(const Images& images);
static std::string original_or_thumbnail(const Images& images);
Element generate_user_header(const User& user, const Config& config) {
Element header("header");
if (user.cover_images) {
2023-04-06 12:24:09 +00:00
std::string cover_original = proxy_image_url(config, original_or_thumbnail(*user.cover_images));
std::string cover_thumbnail = proxy_image_url(config, thumbnail_or_original(*user.cover_images));
header.nodes.push_back(Element("a", {{"href", std::move(cover_original)}}, {
Element("img", {{"class", "cover"}, {"src", std::move(cover_thumbnail)}}, {})
2023-04-05 16:36:20 +00:00
}));
}
2023-04-06 12:24:09 +00:00
std::string profile_picture_original = proxy_image_url(config, original_or_thumbnail(user.profile_pictures));
std::string profile_picture_thumbnail = proxy_image_url(config, thumbnail_or_original(user.profile_pictures));
2023-04-05 16:36:20 +00:00
header.nodes.push_back(Element("div", {{"class", "usermetadata"}}, {
2023-04-06 12:24:09 +00:00
Element("a", {{"href", std::move(profile_picture_original)}}, {
Element("img", {{"class", "profilepicture"}, {"src", std::move(profile_picture_thumbnail)}}, {})
2023-04-05 16:36:20 +00:00
}),
Element("div", {
Element("p", {Element("b", {user.display_name}), " (@", user.username, ")"}),
generate_user_links(user)
})
}));
return header;
}
static inline Element generate_user_links(const User& user) {
Element p("p");
for (const auto &[name, url] : user.links) {
if (!p.nodes.empty()) {
p.nodes.push_back(", ");
}
p.nodes.push_back(Element("a", {{"href", url}}, {name}));
}
return p;
}
static std::string thumbnail_or_original(const Images& images) {
if (!images.thumbnails.empty()) {
return images.thumbnails.back();
}
return images.original.value_or("");
}
static std::string original_or_thumbnail(const Images& images) {
if (images.original) {
return *images.original;
}
if (!images.thumbnails.empty()) {
return images.thumbnails.back();
}
return "";
}