0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-09 05:29:00 +02:00

ircd:Ⓜ️:fed: Move head fetch convenience to room::head interface.

This commit is contained in:
Jason Volk 2020-10-14 21:05:21 -07:00
parent 888c62c156
commit 3f6d90c7a8
5 changed files with 84 additions and 87 deletions

View file

@ -40,9 +40,6 @@ namespace ircd::m::fed
net::hostport matrix_service(net::hostport remote) noexcept;
string_view server(const mutable_buffer &out, const string_view &name, const well_known::opts & = {});
id::event::buf fetch_head(const id::room &room_id, const string_view &remote, const id::user &);
id::event::buf fetch_head(const id::room &room_id, const string_view &remote);
// Observers
bool errant(const string_view &server_name);
bool linked(const string_view &server_name);

View file

@ -43,6 +43,8 @@ struct ircd::m::room::head
struct generate;
using closure = std::function<bool (const event::idx &, const event::id &)>;
static conf::item<milliseconds> fetch_timeout;
m::room room;
bool for_each(const closure &) const;
@ -54,6 +56,8 @@ struct ircd::m::room::head
:room{room}
{}
static event::id::buf fetch(const id &, const string_view &remote, const id::user &);
static event::id::buf fetch(const id &, const string_view &remote);
static void modify(const event::id &, const db::op &, const bool &);
static size_t rebuild(const head &);
static size_t reset(const head &);

View file

@ -406,7 +406,7 @@ ircd::m::fed::backfill::backfill(const room::id &room_id,
m::event::id::buf event_id_buf;
if(!opts.event_id)
{
event_id_buf = fetch_head(room_id, opts.remote);
event_id_buf = m::room::head::fetch(room_id, opts.remote);
opts.event_id = event_id_buf;
}
@ -1639,85 +1639,3 @@ ircd::m::fed::server(const mutable_buffer &buf,
return target;
}
//
// fetch_head util
//
ircd::conf::item<ircd::milliseconds>
fetch_head_timeout
{
{ "name", "ircd.m.v1.fetch_head.timeout" },
{ "default", 30 * 1000L },
};
ircd::m::event::id::buf
ircd::m::fed::fetch_head(const id::room &room_id,
const string_view &remote)
{
const m::room room
{
room_id
};
// When no user_id is supplied and the room exists locally we attempt
// to find the user_id of one of our users with membership in the room.
// This satisfies synapse's requirements for whether we have access
// to the response. If user_id remains blank then make_join will later
// generate a random one from our host as well.
m::user::id::buf user_id
{
any_user(room, my_host(), "join")
};
// Make another attempt to find an invited user because that carries some
// value (this query is not as fast as querying join memberships).
if(!user_id)
user_id = any_user(room, my_host(), "invite");
return fetch_head(room_id, remote, user_id);
}
ircd::m::event::id::buf
ircd::m::fed::fetch_head(const id::room &room_id,
const string_view &remote,
const id::user &user_id)
{
const unique_buffer<mutable_buffer> buf
{
32_KiB
};
make_join::opts opts;
opts.remote = remote;
opts.dynamic = false;
make_join request
{
room_id, user_id, buf, std::move(opts)
};
request.wait(milliseconds(fetch_head_timeout));
request.get();
const json::object proto
{
request.in.content
};
const json::object event
{
proto.at("event")
};
const m::event::prev prev
{
event
};
const auto &prev_event_id
{
prev.prev_event(0)
};
return prev_event_id;
}

View file

@ -8,6 +8,84 @@
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
decltype(ircd::m::room::head::fetch_timeout)
ircd::m::room::head::fetch_timeout
{
{ "name", "ircd.m.room.head.fetch.timeout" },
{ "default", 30 * 1000L },
};
ircd::m::event::id::buf
ircd::m::room::head::fetch(const id &room_id,
const string_view &remote)
{
const m::room room
{
room_id
};
// When no user_id is supplied and the room exists locally we attempt
// to find the user_id of one of our users with membership in the room.
// This satisfies synapse's requirements for whether we have access
// to the response. If user_id remains blank then make_join will later
// generate a random one from our host as well.
m::user::id::buf user_id
{
any_user(room, my_host(), "join")
};
// Make another attempt to find an invited user because that carries some
// value (this query is not as fast as querying join memberships).
if(!user_id)
user_id = any_user(room, my_host(), "invite");
return fetch(room_id, remote, user_id);
}
ircd::m::event::id::buf
ircd::m::room::head::fetch(const id &room_id,
const string_view &remote,
const user::id &user_id)
{
const unique_buffer<mutable_buffer> buf
{
16_KiB
};
fed::make_join::opts opts;
opts.remote = remote;
opts.dynamic = false;
fed::make_join request
{
room_id, user_id, buf, std::move(opts)
};
request.wait(milliseconds(fetch_timeout));
request.get();
const json::object proto
{
request.in.content
};
const json::object event
{
proto.at("event")
};
const m::event::prev prev
{
event
};
const auto &prev_event_id
{
prev.prev_event(0)
};
return prev_event_id;
}
namespace ircd::m
{
static void append_v1(json::stack::array &, const event::id &);

View file

@ -280,7 +280,7 @@ get__initialsync_remote(client &client,
const auto head
{
m::fed::fetch_head(room, remote, request.user_id)
m::room::head::fetch(room, remote, request.user_id)
};
m::room room_{room};