From 57202de5e8f0dc7e45373fb929eaebe4d3c6b278 Mon Sep 17 00:00:00 2001 From: blankie Date: Wed, 29 Nov 2023 15:31:26 +1100 Subject: [PATCH] Fix specifically getting posts from Akkoma instances social.kernel.org --- client.cpp | 12 ++++++------ client.h | 4 ++-- main.cpp | 4 ++-- routes/tags.cpp | 5 ----- routes/user.cpp | 5 ----- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/client.cpp b/client.cpp index f231e09..144d91e 100644 --- a/client.cpp +++ b/client.cpp @@ -121,7 +121,7 @@ std::vector MastodonClient::get_posts(const std::string& host, const std:: query += '&'; } query += "max_id="; - query += std::move(*max_id); + query += url_encode(std::move(*max_id)); } std::string url = "https://"s + host + "/api/v1/accounts/" + account_id + "/statuses"; @@ -138,11 +138,11 @@ std::vector MastodonClient::get_posts(const std::string& host, const std:: return posts; } -std::optional MastodonClient::get_post(const std::string& host, const std::string& id) { +std::optional MastodonClient::get_post(const std::string& host, std::string id) { using namespace std::string_literals; try { - Post post = this->_send_request(std::nullopt, "https://"s + host + "/api/v1/statuses/" + id); + Post post = this->_send_request(std::nullopt, "https://"s + host + "/api/v1/statuses/" + url_encode(std::move(id))); handle_post_server(post, host); return post; } catch (const MastodonException& e) { @@ -154,10 +154,10 @@ std::optional MastodonClient::get_post(const std::string& host, const std: } } -PostContext MastodonClient::get_post_context(const std::string& host, const std::string& id) { +PostContext MastodonClient::get_post_context(const std::string& host, std::string id) { using namespace std::string_literals; - PostContext context = this->_send_request(std::nullopt, "https://"s + host + "/api/v1/statuses/" + id + "/context"); + PostContext context = this->_send_request(std::nullopt, "https://"s + host + "/api/v1/statuses/" + url_encode(std::move(id)) + "/context"); for (Post& post : context.ancestors) { handle_post_server(post, host); @@ -175,7 +175,7 @@ std::vector MastodonClient::get_tag_timeline(const std::string& host, cons std::string url = "https://"s + host + "/api/v1/timelines/tag/" + url_encode(tag); if (max_id) { url += "?max_id="; - url += std::move(*max_id); + url += url_encode(std::move(*max_id)); } std::vector posts = this->_send_request(std::nullopt, url); diff --git a/client.h b/client.h index 1b04f79..da1928b 100644 --- a/client.h +++ b/client.h @@ -68,8 +68,8 @@ public: std::vector get_pinned_posts(std::string host, const std::string& account_id); std::vector get_posts(const std::string& host, const std::string& account_id, PostSortingMethod sorting_method, std::optional max_id); - std::optional get_post(const std::string& host, const std::string& id); - PostContext get_post_context(const std::string& host, const std::string& id); + std::optional get_post(const std::string& host, std::string id); + PostContext get_post_context(const std::string& host, std::string id); std::vector get_tag_timeline(const std::string& host, const std::string& tag, std::optional max_id); diff --git a/main.cpp b/main.cpp index f38c7bb..fc60f0e 100644 --- a/main.cpp +++ b/main.cpp @@ -48,8 +48,8 @@ int main(int argc, char** argv) { serve_redirect(req, res, get_origin(req) + '/' + req.matches.str(1) + "/@" + req.matches.str(2) + req.matches.str(3), true); }); - server.Get("/(" DOMAIN_RE ")/@" ACCT_RE "/(\\d+)", status_route); - server.Get("/(" DOMAIN_RE ")/users/(" ACCT_RE ")/statuses/(\\d+)", [](const httplib::Request& req, httplib::Response& res) { + server.Get("/(" DOMAIN_RE ")/@" ACCT_RE "/([a-zA-Z0-9]+)", status_route); + server.Get("/(" DOMAIN_RE ")/users/(" ACCT_RE ")/statuses/([a-zA-Z0-9]+)", [](const httplib::Request& req, httplib::Response& res) { serve_redirect(req, res, get_origin(req) + '/' + req.matches.str(1) + "/@" + req.matches.str(2) + '/' + req.matches.str(3), true); }); diff --git a/routes/tags.cpp b/routes/tags.cpp index be90ab0..44162ed 100644 --- a/routes/tags.cpp +++ b/routes/tags.cpp @@ -11,11 +11,6 @@ void tags_route(const httplib::Request& req, httplib::Response& res) { std::optional 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; - } } std::vector posts; diff --git a/routes/user.cpp b/routes/user.cpp index 4701efe..7cb80b7 100644 --- a/routes/user.cpp +++ b/routes/user.cpp @@ -22,11 +22,6 @@ void user_route(const httplib::Request& req, httplib::Response& res) { std::optional 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; - } } std::optional account;