From 157a1c1227e6d73db7b3f09a02aa452a7dfbc655 Mon Sep 17 00:00:00 2001 From: nekobit Date: Wed, 5 Oct 2022 05:04:43 +0000 Subject: [PATCH] Build webfinger fake data FossilOrigin-Name: 6891ce4a310c34f0527227a54c0fce0d1b5b85ebd282ad114579b6002843c776 --- src/protocol/webfinger/webfinger.cpp | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/protocol/webfinger/webfinger.cpp b/src/protocol/webfinger/webfinger.cpp index c8698e6..dfa88bc 100644 --- a/src/protocol/webfinger/webfinger.cpp +++ b/src/protocol/webfinger/webfinger.cpp @@ -16,6 +16,10 @@ * along with this program. If not, see . */ +#include +#include +#include +#include #include #include "http/response.h" #include "webfinger.h" @@ -24,9 +28,59 @@ using namespace Protocol; +namespace +{ + struct Resource { + std::string href; + std::string rel; + std::string type; + std::string tmpl; + }; + rapidjson::Value make_resource(Resource&& res, rapidjson::Document::AllocatorType& a) + { + using Value = rapidjson::Value; + rapidjson::Value resource(rapidjson::kObjectType); + // TODO make helper macro + if (!res.href.empty()) resource.AddMember("href", Value(res.href.c_str(), a).Move(), a); + if (!res.rel.empty()) resource.AddMember("rel", Value(res.rel.c_str(), a).Move(), a); + if (!res.type.empty()) resource.AddMember("type", Value(res.type.c_str(), a).Move(), a); + if (!res.tmpl.empty()) resource.AddMember("template", Value(res.tmpl.c_str(), a).Move(), a); + return resource; + } +} + +// TODO a lot HTTP::Response Route::webfinger(std::any& args, const HTTP::RequestArgs_t& arg) { + rapidjson::Document root(rapidjson::kObjectType); + rapidjson::Document::AllocatorType& a = root.GetAllocator(); + rapidjson::Value links(rapidjson::kArrayType); + + // Add profile page + links.PushBack(make_resource({"https://localhost/users/", + "http://webfinger.net/rel/profile-page", + "text/html", {}}, a), a); + + // Add self page + links.PushBack(make_resource({"https://localhost/users/", + "self", + "application/activity+json", {}}, a), a); + + // Add activity stream + links.PushBack(make_resource({"https://localhost/", + "http://webfinger.net/rel/profile-page", + "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", {}}, a), a); + root.AddMember("links", links.Move(), root.GetAllocator()); + + root.AddMember("subject", "acct:", a); + + // Stringify TODO write helper! + rapidjson::StringBuffer buf; + rapidjson::Writer writer(buf); + root.Accept(writer); + + return { buf.GetString() }; } void Webfinger::init_webfinger(HTTP::Server* server)