0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-17 23:40:57 +01:00

ircd:Ⓜ️:v1: Add key query request.

This commit is contained in:
Jason Volk 2018-05-07 14:46:53 -07:00
parent 213826d06c
commit 9821d7f333
3 changed files with 141 additions and 0 deletions

45
include/ircd/m/v1/key.h Normal file
View file

@ -0,0 +1,45 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_M_V1_KEY_H
namespace ircd::m::v1
{
struct key;
};
struct ircd::m::v1::key
:server::request
{
struct opts;
explicit operator json::array() const
{
const json::object object{in.content};
const json::array &server_keys{object.get("server_keys")};
return server_keys;
}
using server_key = std::pair<string_view, string_view>; // server_name, key_id
key(const vector_view<const server_key> &, const mutable_buffer &, opts);
key() = default;
};
struct ircd::m::v1::key::opts
{
net::hostport remote;
m::request request;
server::out out;
server::in in;
const struct server::request::opts *sopts {nullptr};
bool dynamic {false};
};

View file

@ -18,6 +18,7 @@ namespace ircd::m::v1
}
#include "version.h"
#include "key.h"
#include "query.h"
#include "user.h"
#include "make_join.h"

View file

@ -927,6 +927,101 @@ ircd::m::v1::query::query(const string_view &type,
{
}
///////////////////////////////////////////////////////////////////////////////
//
// v1/key.h
//
namespace ircd::m::v1
{
static const_buffer
_make_server_keys(const vector_view<const key::server_key> &,
const mutable_buffer &);
}
ircd::m::v1::key::key(const vector_view<const server_key> &keys,
const mutable_buffer &buf_,
opts opts)
:server::request{[&]
{
assert(!!opts.remote);
if(!defined(json::get<"origin"_>(opts.request)))
json::get<"origin"_>(opts.request) = my_host();
if(!defined(json::get<"destination"_>(opts.request)))
json::get<"destination"_>(opts.request) = host(opts.remote);
if(defined(json::get<"content"_>(opts.request)))
opts.out.content = json::get<"content"_>(opts.request);
if(!defined(json::get<"content"_>(opts.request)))
json::get<"content"_>(opts.request) = json::object{opts.out.content};
if(!defined(json::get<"uri"_>(opts.request)))
json::get<"uri"_>(opts.request) = "/_matrix/key/v2/query";
json::get<"method"_>(opts.request) = "POST";
window_buffer buf{buf_};
if(!defined(json::get<"content"_>(opts.request)))
{
buf([&keys](const mutable_buffer &buf)
{
return _make_server_keys(keys, buf);
});
json::get<"content"_>(opts.request) = json::object{buf.completed()};
opts.out.content = json::get<"content"_>(opts.request);
}
opts.out.head = opts.request(buf);
if(!size(opts.in))
{
opts.in.head = buf + size(opts.out.head);
opts.in.content = opts.dynamic?
mutable_buffer{}: // server::request will allocate new mem
opts.in.head; // server::request will auto partition
}
return server::request
{
opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts
};
}()}
{
}
static ircd::const_buffer
ircd::m::v1::_make_server_keys(const vector_view<const key::server_key> &keys,
const mutable_buffer &buf)
{
json::stack out{buf};
{
json::stack::object top{out};
json::stack::member server_keys{top, "server_keys"};
for(const auto &sk : keys)
{
json::stack::object object{server_keys};
json::stack::member server_name{object, sk.first};
{
json::stack::object object{server_name};
json::stack::member key_name{object, sk.second};
{
json::stack::object object{key_name};
json::stack::member mvut
{
object, "minimum_valid_until_ts", json::value{0L}
};
}
}
}
}
return out.completed();
}
///////////////////////////////////////////////////////////////////////////////
//
// v1/version.h