pixwhile/routes/users/illustrations.cpp

68 lines
3.0 KiB
C++
Raw Normal View History

2023-04-07 15:06:31 +00:00
#include <utility>
#include "../routes.h"
#include "../../numberhelper.h"
2023-04-07 15:06:31 +00:00
#include "../../servehelper.h"
#include "../../pixivclient.h"
#include "common.h"
static inline Nodes generate_ogp_nodes(const httplib::Request& req, const Config& config, const User& user, uint64_t page);
2023-06-04 07:09:03 +00:00
2023-04-07 15:06:31 +00:00
void user_illustrations_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) {
uint64_t user_id = to_ull(req.matches[1].str());
uint64_t page = req.has_param("p") ? to_ull(req.get_param_value("p")) - 1 : 0;
User user;
Illusts illusts;
2023-04-07 15:06:31 +00:00
try {
user = pixiv_client.get_user(user_id);
illusts = pixiv_client.get_illusts(user_id, page);
2023-04-07 15:06:31 +00:00
} catch (const PixivException& e) {
if (e.status == 404) {
res.status = 404;
serve_error(req, res, config, "404: User not found", e.what());
} else {
res.status = 500;
serve_error(req, res, config, "500: Internal server error", "Failed to fetch user information", e.what());
}
return;
} catch (const std::exception& e) {
res.status = 500;
serve_error(req, res, config, "500: Internal server error", "Failed to fetch user information", e.what());
return;
}
Element body("body", {
2023-06-07 09:16:03 +00:00
generate_user_header(std::move(user), req, config),
2023-04-28 09:00:21 +00:00
generate_illusts_pager(req, config, illusts, page, "illusts")
2023-04-07 15:06:31 +00:00
});
serve(req, res, config, user.display_name + "'s illustrations", std::move(body), generate_ogp_nodes(req, config, user, page));
2023-06-04 07:09:03 +00:00
}
static inline Nodes generate_ogp_nodes(const httplib::Request& req, const Config& config, const User& user, uint64_t page) {
using namespace std::string_literals;
std::string url = get_origin(req, config) + "/users/" + std::to_string(user.user_id) + "/illustrations";
if (page != 0) {
url += "?p="s + std::to_string(page + 1);
}
2023-06-04 07:09:03 +00:00
Nodes nodes({
Element("meta", {{"property", "og:title"}, {"content", user.display_name + " (@" + user.username + ')'}}, {}),
Element("meta", {{"property", "og:type"}, {"content", user.ogp_image ? "photo" : "website"}}, {}),
Element("meta", {{"property", "og:site_name"}, {"content", "Pixwhile"}}, {}),
Element("meta", {{"property", "og:url"}, {"content", std::move(url)}}, {})
2023-06-04 07:09:03 +00:00
});
if (user.ogp_image) {
2023-06-07 09:16:03 +00:00
nodes.push_back(Element("meta", {{"property", "og:image"}, {"content", proxy_image_url(req, config, *user.ogp_image)}}, {}));
2023-06-04 07:09:03 +00:00
} else {
const Image& image = user.profile_pictures.thumbnail_or_original();
2023-06-07 09:16:03 +00:00
nodes.push_back(Element("meta", {{"property", "og:image"}, {"content", proxy_image_url(req, config, image.url)}}, {}));
2023-06-04 07:09:03 +00:00
if (image.size) {
nodes.push_back(Element("meta", {{"property", "og:image:width"}, {"content", std::to_string(image.size->first)}}, {}));
nodes.push_back(Element("meta", {{"property", "og:image:height"}, {"content", std::to_string(image.size->second)}}, {}));
}
}
return nodes;
2023-04-07 15:06:31 +00:00
}