coyote/routes/status.cpp

72 lines
2.1 KiB
C++
Raw Normal View History

2023-11-23 06:05:17 +00:00
#include "routes.h"
2023-11-24 01:09:49 +00:00
#include "../lxb_wrapper.h"
2023-11-23 06:05:17 +00:00
#include "../servehelper.h"
#include "../client.h"
#include "../models.h"
2023-11-24 01:09:49 +00:00
static inline std::string make_title(const Post& post);
2023-11-23 06:05:17 +00:00
void status_route(const httplib::Request& req, httplib::Response& res) {
std::string server = req.matches.str(1);
std::string id = req.matches.str(2);
std::optional<Post> post;
2023-11-23 23:46:47 +00:00
PostContext context;
2023-11-23 06:05:17 +00:00
try {
post = mastodon_client.get_post(server, id);
2023-11-23 23:46:47 +00:00
if (post) {
context = mastodon_client.get_post_context(server, id);
}
2023-11-23 06:05:17 +00:00
} catch (const std::exception& e) {
res.status = 500;
serve_error(req, res, "500: Internal server error", "Failed to fetch post information", e.what());
return;
}
if (!post) {
res.status = 404;
serve_error(req, res, "404: Post not found");
return;
}
2023-11-23 08:49:27 +00:00
if (post->reblog) {
serve_redirect(req, res, get_origin(req) + '/' + server + "/@" + post->reblog->account.acct(false) + '/' + post->reblog->id, true);
return;
}
2023-11-23 23:46:47 +00:00
Element body("body");
body.nodes.reserve(context.ancestors.size() * 2 + 1 + context.descendants.size() * 2);
for (const Post& i : context.ancestors) {
body.nodes.push_back(serialize_post(req, server, i));
body.nodes.push_back(Element("hr"));
}
body.nodes.push_back(serialize_post(req, server, *post, false, true));
for (const Post& i : context.descendants) {
body.nodes.push_back(Element("hr"));
body.nodes.push_back(serialize_post(req, server, i));
}
2023-11-24 01:09:49 +00:00
serve(req, res, make_title(*post), std::move(body));
}
static inline std::string make_title(const Post& post) {
LXB::HTML::Document document(post.content.str);
size_t content_len;
const char* content = reinterpret_cast<const char*>(lxb_dom_node_text_content(document.body(), &content_len));
std::string title = post.account.display_name + " (" + post.account.acct() + "): ";
if (content_len) {
title.append(content, content_len > 50 ? 50 : content_len);
if (content_len > 50) {
title += "";
}
} else {
title += "Post";
}
return title;
2023-11-23 06:05:17 +00:00
}