0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

ircd:Ⓜ️ Elaborate v1 federation requests in directory.

This commit is contained in:
Jason Volk 2018-01-22 03:32:15 -08:00
parent 755a6184ce
commit c79539ebf3
9 changed files with 573 additions and 2 deletions

View file

@ -69,6 +69,7 @@ namespace ircd
#include "room.h"
#include "request.h"
#include "session.h"
#include "v1/v1.h"
#include "io.h"
#include "vm.h"
#include "user.h"

View file

@ -0,0 +1,44 @@
// 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_BACKFILL_H
namespace ircd::m::v1
{
struct backfill;
};
struct ircd::m::v1::backfill
:server::request
{
struct opts;
static const opts default_opts;
explicit operator json::object() const
{
return json::object{in.content};
}
backfill(const room::id &, const mutable_buffer &, opts);
backfill(const room::id &, const mutable_buffer &);
};
struct ircd::m::v1::backfill::opts
{
net::hostport remote;
string_view event_id;
size_t limit {64};
m::request request;
server::out out;
server::in in;
const struct server::request::opts *sopts {nullptr};
};

49
include/ircd/m/v1/event.h Normal file
View file

@ -0,0 +1,49 @@
// 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_EVENT_H
namespace ircd::m::v1
{
struct event;
};
struct ircd::m::v1::event
:server::request
{
struct opts;
static const opts default_opts;
explicit operator json::object() const
{
const json::object object{in.content};
const json::array pdus{object.at("pdus")};
return pdus.at(0);
}
explicit operator m::event() const
{
return json::object{*this};
}
event(const m::event::id &, const mutable_buffer &, opts);
event(const m::event::id &, const mutable_buffer &);
};
struct ircd::m::v1::event::opts
{
net::hostport remote;
m::request request;
server::out out;
server::in in;
const struct server::request::opts *sopts {nullptr};
};

View file

@ -0,0 +1,42 @@
// 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_MAKE_JOIN_H
namespace ircd::m::v1
{
struct make_join;
};
struct ircd::m::v1::make_join
:server::request
{
struct opts;
static const opts default_opts;
operator json::object() const
{
return json::object{in.content};
}
make_join(const room::id &, const user::id &, const mutable_buffer &, opts);
make_join(const room::id &, const user::id &, const mutable_buffer &);
};
struct ircd::m::v1::make_join::opts
{
net::hostport remote;
m::request request;
server::out out;
server::in in;
const struct server::request::opts *sopts {nullptr};
};

43
include/ircd/m/v1/state.h Normal file
View file

@ -0,0 +1,43 @@
// 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_STATE_H
namespace ircd::m::v1
{
struct state;
};
struct ircd::m::v1::state
:server::request
{
struct opts;
static const opts default_opts;
explicit operator json::object() const
{
return json::object{in.content};
}
state(const room::id &, const mutable_buffer &, opts);
state(const room::id &, const mutable_buffer &);
};
struct ircd::m::v1::state::opts
{
net::hostport remote;
string_view event_id;
m::request request;
server::out out;
server::in in;
const struct server::request::opts *sopts {nullptr};
};

17
include/ircd/m/v1/v1.h Normal file
View file

@ -0,0 +1,17 @@
// 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_H
#include "make_join.h"
#include "event.h"
#include "state.h"
#include "backfill.h"

View file

@ -77,6 +77,7 @@ libircd_la_SOURCES = \
logger.cc \
m/event.cc \
m/id.cc \
m/v1.cc \
m/io.cc \
m/keys.cc \
m/request.cc \

View file

@ -45,12 +45,17 @@ ircd::json::object
ircd::m::io::get(const id::event &event_id,
const mutable_buffer &buf)
{
event::fetch tab
v1::event request
{
event_id, buf
};
return acquire(tab);
request.wait();
return json::object
{
request
};
}
void

369
ircd/m/v1.cc Normal file
View file

@ -0,0 +1,369 @@
// 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.
#include <ircd/m/m.h>
///////////////////////////////////////////////////////////////////////////////
//
// v1/backfill.h
//
decltype(ircd::m::v1::backfill::default_opts)
ircd::m::v1::backfill::default_opts
{};
ircd::m::v1::backfill::backfill(const room::id &room_id,
const mutable_buffer &buf)
:backfill
{
room_id, buf, default_opts
}
{
}
ircd::m::v1::backfill::backfill(const room::id &room_id,
const mutable_buffer &buf,
opts opts)
:server::request{[&]
{
if(!opts.event_id)
{
thread_local m::event::id::buf event_id;
v1::make_join request
{
room_id, m::me.user_id, buf
};
request.wait();
const json::object proto
{
request.in.content
};
const json::array prev_events
{
proto.at({"event", "prev_events"})
};
const json::array prev_event
{
prev_events.at(0)
};
event_id = unquote(prev_event.at(0));
opts.event_id = event_id;
}
if(!opts.remote)
opts.remote = room_id.host();
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)))
{
thread_local char urlbuf[1024], ridbuf[768], eidbuf[768];
json::get<"uri"_>(opts.request) =
{
fmt::sprintf
{
urlbuf, "/_matrix/federation/v1/backfill/%s/?limit=%zu&v=%s",
url::encode(room_id, ridbuf),
opts.limit,
url::encode(opts.event_id, eidbuf),
}
};
}
json::get<"method"_>(opts.request) = "GET";
opts.out.head = opts.request(buf);
if(!size(opts.in))
{
const auto in_max
{
std::max(ssize_t(size(buf) - size(opts.out.head)), ssize_t(0))
};
assert(in_max >= ssize_t(size(buf) / 2));
opts.in.head = { data(buf) + size(opts.out.head), size_t(in_max) };
opts.in.content = opts.in.head;
}
return server::request
{
opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts
};
}()}
{
}
///////////////////////////////////////////////////////////////////////////////
//
// v1/state.h
//
decltype(ircd::m::v1::state::default_opts)
ircd::m::v1::state::default_opts
{};
ircd::m::v1::state::state(const room::id &room_id,
const mutable_buffer &buf)
:state
{
room_id, buf, default_opts
}
{
}
ircd::m::v1::state::state(const room::id &room_id,
const mutable_buffer &buf,
opts opts)
:server::request{[&]
{
if(!opts.event_id)
{
thread_local m::event::id::buf event_id;
v1::make_join request
{
room_id, m::me.user_id, buf
};
request.wait();
const json::object proto
{
request.in.content
};
const json::array prev_events
{
proto.at({"event", "prev_events"})
};
const json::array prev_event
{
prev_events.at(0)
};
event_id = unquote(prev_event.at(0));
opts.event_id = event_id;
}
if(!opts.remote)
opts.remote = room_id.host();
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)))
{
thread_local char urlbuf[1024], ridbuf[768], eidbuf[768];
json::get<"uri"_>(opts.request) =
{
fmt::sprintf
{
urlbuf, "/_matrix/federation/v1/state/%s/?event_id=%s",
url::encode(room_id, ridbuf),
url::encode(opts.event_id, eidbuf),
}
};
}
json::get<"method"_>(opts.request) = "GET";
opts.out.head = opts.request(buf);
if(!size(opts.in))
{
const auto in_max
{
std::max(ssize_t(size(buf) - size(opts.out.head)), ssize_t(0))
};
assert(in_max >= ssize_t(size(buf) / 2));
opts.in.head = { data(buf) + size(opts.out.head), size_t(in_max) };
opts.in.content = opts.in.head;
}
return server::request
{
opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts
};
}()}
{
}
///////////////////////////////////////////////////////////////////////////////
//
// v1/event.h
//
decltype(ircd::m::v1::event::default_opts)
ircd::m::v1::event::default_opts
{};
ircd::m::v1::event::event(const m::event::id &event_id,
const mutable_buffer &buf)
:event
{
event_id, buf, default_opts
}
{
}
ircd::m::v1::event::event(const m::event::id &event_id,
const mutable_buffer &buf,
opts opts)
:server::request{[&]
{
if(!opts.remote)
opts.remote = event_id.host();
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)))
{
thread_local char urlbuf[1024], eidbuf[768];
json::get<"uri"_>(opts.request) =
{
fmt::sprintf
{
urlbuf, "/_matrix/federation/v1/event/%s/",
url::encode(event_id, eidbuf),
}
};
}
json::get<"method"_>(opts.request) = "GET";
opts.out.head = opts.request(buf);
if(!size(opts.in))
{
const auto in_max
{
std::max(ssize_t(size(buf) - size(opts.out.head)), ssize_t(0))
};
assert(in_max >= ssize_t(size(buf) / 2));
opts.in.head = { data(buf) + size(opts.out.head), size_t(in_max) };
opts.in.content = opts.in.head;
}
return server::request
{
opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts
};
}()}
{
}
///////////////////////////////////////////////////////////////////////////////
//
// v1/make_join.h
//
decltype(ircd::m::v1::make_join::default_opts)
ircd::m::v1::make_join::default_opts
{};
ircd::m::v1::make_join::make_join(const room::id &room_id,
const user::id &user_id,
const mutable_buffer &buf)
:make_join
{
room_id, user_id, buf, default_opts
}
{
}
ircd::m::v1::make_join::make_join(const room::id &room_id,
const user::id &user_id,
const mutable_buffer &buf,
opts opts)
:server::request{[&]
{
if(!opts.remote)
opts.remote = room_id.host();
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)))
{
thread_local char urlbuf[2048], ridbuf[768], uidbuf[768];
json::get<"uri"_>(opts.request) =
{
fmt::sprintf
{
urlbuf, "/_matrix/federation/v1/make_join/%s/%s",
url::encode(room_id, ridbuf),
url::encode(user_id, uidbuf)
}
};
}
json::get<"method"_>(opts.request) = "GET";
opts.out.head = opts.request(buf);
if(!size(opts.in))
{
const auto in_max
{
std::max(ssize_t(size(buf) - size(opts.out.head)), ssize_t(0))
};
assert(in_max >= ssize_t(size(buf) / 2));
opts.in.head = { data(buf) + size(opts.out.head), size_t(in_max) };
opts.in.content = opts.in.head;
}
return server::request
{
opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts
};
}()}
{
}