coyote/routes/user.cpp

160 lines
7.0 KiB
C++
Raw Normal View History

2023-11-22 08:39:24 +00:00
#include "routes.h"
#include "../servehelper.h"
#include "../client.h"
#include "../models.h"
2023-11-23 06:05:17 +00:00
#include "../timeutils.h"
2023-11-22 08:39:24 +00:00
static const char* sorting_method_names[3] = {"Posts", "Posts and replies", "Media"};
static const char* sorting_method_suffixes[3] = {"", "/with_replies", "/media"};
static inline PostSortingMethod get_sorting_method(const std::string& method);
2023-11-23 06:05:17 +00:00
static inline Element user_header(const httplib::Request& req, const std::string& server, const Account& account, PostSortingMethod sorting_method);
2023-11-23 00:16:56 +00:00
static inline Element user_link_field(const httplib::Request& req, const Account& account, const AccountField& field);
2023-11-23 06:05:17 +00:00
static inline Element sorting_method_link(const httplib::Request& req, const std::string& server, const Account& account, PostSortingMethod current_method, PostSortingMethod new_method);
2023-11-22 08:39:24 +00:00
void user_route(const httplib::Request& req, httplib::Response& res) {
2023-11-23 08:49:27 +00:00
using namespace std::string_literals;
2023-11-22 08:39:24 +00:00
std::string server = req.matches.str(1);
std::string username = req.matches.str(2);
PostSortingMethod sorting_method = get_sorting_method(req.matches.str(3));
2023-11-23 08:49:27 +00:00
std::optional<std::string> max_id;
if (req.has_param("max_id")) {
max_id = req.get_param_value("max_id");
if (max_id->empty() || max_id->find_first_not_of("0123456789") != std::string::npos) {
res.status = 400;
serve_error(req, res, "400: Bad Request", "Invalid max_id query paramter");
return;
}
}
2023-11-22 08:39:24 +00:00
std::optional<Account> account;
2023-11-23 13:43:55 +00:00
std::vector<Post> pinned_posts, posts;
2023-11-22 08:39:24 +00:00
try {
account = mastodon_client.get_account_by_username(server, username);
2023-11-23 08:49:27 +00:00
if (account) {
2023-11-23 13:43:55 +00:00
if (sorting_method == PostSortingMethod::Posts && !max_id) {
pinned_posts = mastodon_client.get_pinned_posts(server, account->id);
}
posts = mastodon_client.get_posts(server, account->id, sorting_method, max_id);
2023-11-23 08:49:27 +00:00
}
2023-11-22 08:39:24 +00:00
} catch (const std::exception& e) {
res.status = 500;
serve_error(req, res, "500: Internal server error", "Failed to fetch user information", e.what());
return;
}
if (!account) {
res.status = 404;
serve_error(req, res, "404: User not found");
return;
}
Element body("body", {
2023-11-23 06:05:17 +00:00
user_header(req, server, *account, sorting_method),
2023-11-22 08:39:24 +00:00
});
2023-11-23 13:43:55 +00:00
body.nodes.reserve(body.nodes.size() + 2 * (pinned_posts.size() + posts.size()) + 1);
for (const Post& post : pinned_posts) {
body.nodes.push_back(serialize_post(req, server, post, true));
body.nodes.push_back(Element("hr"));
}
2023-11-23 08:49:27 +00:00
for (const Post& post : posts) {
body.nodes.push_back(serialize_post(req, server, post));
body.nodes.push_back(Element("hr"));
}
2023-11-23 13:43:55 +00:00
2023-11-23 08:49:27 +00:00
if (!posts.empty()) {
body.nodes.push_back(Element("a", {{"class", "user_page-more_posts"}, {"href", "?max_id="s + posts[posts.size() - 1].id + "#user_posts_nav"}}, {"See more"}));
2023-11-23 13:43:55 +00:00
} else if (max_id) {
2023-11-23 08:49:27 +00:00
body.nodes.push_back(Element("p", {{"class", "user_page-more_posts"}}, {"There are no more posts"}));
}
2023-11-23 12:20:49 +00:00
serve(req, res, account->display_name + " (@" + account->acct() + ')', std::move(body));
2023-11-22 08:39:24 +00:00
}
static inline PostSortingMethod get_sorting_method(const std::string& method) {
for (size_t i = 0; i < sizeof(sorting_method_suffixes) / sizeof(sorting_method_suffixes[0]); i++) {
if (method == sorting_method_suffixes[i]) {
return static_cast<PostSortingMethod>(i);
}
}
__builtin_unreachable();
}
2023-11-23 06:05:17 +00:00
static inline Element user_header(const httplib::Request& req, const std::string& server, const Account& account, PostSortingMethod sorting_method) {
2023-11-22 08:39:24 +00:00
Element user_links("table", {{"class", "user_page-user_links"}}, {});
user_links.nodes.reserve(account.fields.size());
for (const AccountField& i : account.fields) {
2023-11-23 00:16:56 +00:00
user_links.nodes.push_back(user_link_field(req, account, i));
2023-11-22 08:39:24 +00:00
}
2023-11-23 23:55:02 +00:00
Node view_on_original = !account.same_server
? Element("span", {" (", Element("a", {{"href", get_origin(req) + '/' + account.server + "/@" + account.username}}, {"View on original instance"}), ")"})
: Node("");
2023-11-22 08:39:24 +00:00
Element header("header", {
Element("a", {{"href", account.header}}, {
Element("img", {{"class", "user_page-header"}, {"alt", "User header"}, {"src", account.header}}, {}),
}),
Element("div", {{"class", "user_page-main_header"}}, {
2023-11-22 08:39:24 +00:00
Element("a", {{"href", account.avatar}}, {
Element("img", {{"class", "user_page-profile"}, {"alt", "User profile picture"}, {"src", account.avatar}}, {}),
2023-11-22 08:39:24 +00:00
}),
Element("span", {
2023-11-24 03:57:12 +00:00
Element("b", {preprocess_html(req, account.emojis, account.display_name)}), " (@", account.acct(), ")", view_on_original,
2023-11-22 08:39:24 +00:00
Element("br"),
2023-11-23 06:05:17 +00:00
Element("br"), Element("b", {"Joined: "}), short_time(account.created_at),
2023-11-22 08:39:24 +00:00
Element("br"),
Element("b", {std::to_string(account.statuses_count)}), " Posts", " / ",
Element("b", {std::to_string(account.following_count)}), " Following", " / ",
// https://chaosfem.tw/@Terra
Element("b", {account.followers_count >= 0 ? std::to_string(account.followers_count) : "???"}), " Followers",
2023-11-22 08:39:24 +00:00
}),
}),
Element("div", {{"class", "user_page-user_description"}}, {
2023-11-23 06:05:17 +00:00
Element("div", {{"class", "user_page-user_bio"}}, {preprocess_html(req, account.server, account.emojis, account.note_html)}),
2023-11-22 08:39:24 +00:00
std::move(user_links),
}),
2023-11-23 08:49:27 +00:00
Element("nav", {{"class", "user_page-user_posts_nav"}, {"id", "user_posts_nav"}}, {
2023-11-23 06:05:17 +00:00
sorting_method_link(req, server, account, sorting_method, PostSortingMethod::Posts),
sorting_method_link(req, server, account, sorting_method, PostSortingMethod::PostsAndReplies),
2023-11-23 12:20:49 +00:00
sorting_method_link(req, server, account, sorting_method, PostSortingMethod::MediaOnly),
2023-11-22 08:39:24 +00:00
}),
});
return header;
}
2023-11-23 00:16:56 +00:00
static inline Element user_link_field(const httplib::Request& req, const Account& account, const AccountField& field) {
2023-11-22 08:39:24 +00:00
using namespace std::string_literals;
Element tr("tr", {
Element("th", {preprocess_html(req, account.emojis, field.name)}),
2023-11-23 06:05:17 +00:00
Element("td", {preprocess_html(req, account.server, account.emojis, field.value_html)}),
2023-11-22 08:39:24 +00:00
});
if (field.verified_at >= 0) {
2023-11-23 06:05:17 +00:00
tr.attributes = {{"class", "verified"}, {"title", "Verified at "s + full_time(field.verified_at)}};
2023-11-22 08:39:24 +00:00
}
return tr;
}
2023-11-23 06:05:17 +00:00
static inline Element sorting_method_link(const httplib::Request& req, const std::string& server, const Account& account, PostSortingMethod current_method, PostSortingMethod new_method) {
2023-11-22 08:39:24 +00:00
const char* method_name = sorting_method_names[new_method];
2023-11-23 08:49:27 +00:00
Element a("a", {{"href", get_origin(req) + '/' + server + "/@" + account.acct(false) + sorting_method_suffixes[new_method] + "#user_posts_nav"}}, {
method_name,
});
2023-11-22 08:39:24 +00:00
if (current_method == new_method) {
2023-11-23 08:49:27 +00:00
a.attributes.push_back({"class", "selected"});
2023-11-22 08:39:24 +00:00
}
2023-11-23 08:49:27 +00:00
return a;
2023-11-22 08:39:24 +00:00
}