2023-04-07 15:06:31 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "../routes.h"
|
2023-04-07 15:13:25 +00:00
|
|
|
#include "../../numberhelper.h"
|
2023-04-07 15:06:31 +00:00
|
|
|
#include "../../servehelper.h"
|
|
|
|
#include "../../pixivclient.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
2023-04-09 08:29:47 +00:00
|
|
|
static Element generate_pager(const Illusts& illusts, size_t page, bool first_selector);
|
2023-04-09 05:55:26 +00:00
|
|
|
static inline Element generate_content(const httplib::Request& req, const Config& config, const Illusts& illusts);
|
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;
|
2023-04-08 16:27:45 +00:00
|
|
|
Illusts illusts;
|
2023-04-07 15:06:31 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
user = pixiv_client.get_user(user_id);
|
2023-04-08 16:27:45 +00:00
|
|
|
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", {
|
|
|
|
generate_user_header(std::move(user), config),
|
2023-04-09 08:29:47 +00:00
|
|
|
generate_pager(illusts, page, true),
|
2023-04-07 15:06:31 +00:00
|
|
|
Element("br"),
|
2023-04-09 05:55:26 +00:00
|
|
|
generate_content(req, config, illusts),
|
2023-04-09 08:29:47 +00:00
|
|
|
generate_pager(illusts, page, false)
|
2023-04-07 15:06:31 +00:00
|
|
|
});
|
|
|
|
serve(req, res, config, user.display_name + " illustrations", std::move(body));
|
|
|
|
}
|
|
|
|
|
2023-04-09 08:29:47 +00:00
|
|
|
static Element generate_pager(const Illusts& illusts, size_t page, bool first_selector) {
|
|
|
|
auto link = [](size_t new_page, const char* text, bool add_link) {
|
|
|
|
using namespace std::string_literals;
|
2023-04-07 15:06:31 +00:00
|
|
|
|
|
|
|
Element b("b");
|
2023-04-09 08:29:47 +00:00
|
|
|
std::string href = "?p="s + std::to_string(new_page) + "#pageselector";
|
|
|
|
|
2023-04-07 15:06:31 +00:00
|
|
|
if (add_link) {
|
|
|
|
b.nodes.push_back(Element("a", {{"href", std::move(href)}}, {text}));
|
|
|
|
} else {
|
|
|
|
b.nodes.push_back(text);
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
};
|
2023-04-09 08:29:47 +00:00
|
|
|
|
|
|
|
Element div("div", {{"class", "center"}}, {
|
|
|
|
link(1, "First", page != 0), " ",
|
|
|
|
link(page, "Prev", page != 0), " ",
|
2023-04-08 16:27:45 +00:00
|
|
|
std::to_string(page + 1), "/", std::to_string(illusts.total_pages), " ",
|
2023-04-09 08:29:47 +00:00
|
|
|
link(page + 2, "Next", page + 1 < illusts.total_pages), " ",
|
|
|
|
link(illusts.total_pages, "Last", page + 1 < illusts.total_pages)
|
2023-04-07 15:06:31 +00:00
|
|
|
});
|
2023-04-09 08:29:47 +00:00
|
|
|
if (first_selector) {
|
|
|
|
div.attributes.push_back({"id", "pageselector"});
|
|
|
|
}
|
|
|
|
return div;
|
2023-04-07 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 05:55:26 +00:00
|
|
|
static inline Element generate_content(const httplib::Request& req, const Config& config, const Illusts& illusts) {
|
|
|
|
Element div("div", {{"class", "userillustrations"}}, {});
|
2023-04-07 15:06:31 +00:00
|
|
|
|
2023-04-09 05:55:26 +00:00
|
|
|
div.nodes.reserve(illusts.illusts.size());
|
2023-04-08 16:27:45 +00:00
|
|
|
for (const Illust& i : illusts.illusts) {
|
2023-04-09 05:55:26 +00:00
|
|
|
std::string illust_url = get_origin(req, config) + "/artworks/" + std::to_string(i.illust_id);
|
|
|
|
std::string image_url = proxy_image_url(config, i.images[0].thumbnail_or_original());
|
|
|
|
|
|
|
|
div.nodes.push_back(Element("a", {{"href", {std::move(illust_url)}}}, {
|
|
|
|
Element("img", {{"loading", "lazy"}, {"src", std::move(image_url)}}, {}),
|
|
|
|
Element("p", {i.title})
|
2023-04-07 15:06:31 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-04-09 05:55:26 +00:00
|
|
|
return div;
|
2023-04-07 15:06:31 +00:00
|
|
|
}
|