coyote/routes/status.cpp

49 lines
1.5 KiB
C++
Raw Normal View History

2023-11-23 06:05:17 +00:00
#include "routes.h"
#include "../servehelper.h"
#include "../client.h"
#include "../models.h"
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-23 06:05:17 +00:00
serve(req, res, "", std::move(body));
}