Build webfinger fake data

FossilOrigin-Name: 6891ce4a310c34f0527227a54c0fce0d1b5b85ebd282ad114579b6002843c776
This commit is contained in:
nekobit 2022-10-05 05:04:43 +00:00
parent ace295b683
commit 157a1c1227

View file

@ -16,6 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <utility>
#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<rapidjson::StringBuffer> writer(buf);
root.Accept(writer);
return { buf.GetString() };
}
void Webfinger::init_webfinger(HTTP::Server* server)