2018-02-25 13:07:48 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2019-04-11 15:59:09 +02:00
|
|
|
static void
|
2018-03-01 06:25:25 +01:00
|
|
|
get__initialsync_remote(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-03-02 00:57:28 +01:00
|
|
|
const m::room &room);
|
|
|
|
|
|
|
|
static void
|
|
|
|
get__initialsync_local(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-03-02 00:57:28 +01:00
|
|
|
const m::room &room,
|
|
|
|
const m::user &user,
|
|
|
|
json::stack::object &out);
|
|
|
|
|
|
|
|
static conf::item<size_t>
|
|
|
|
initial_backfill
|
2018-03-01 06:25:25 +01:00
|
|
|
{
|
2019-03-02 00:57:28 +01:00
|
|
|
{ "name", "ircd.client.rooms.initialsync.backfill" },
|
|
|
|
{ "default", 20L },
|
|
|
|
};
|
2018-03-01 06:25:25 +01:00
|
|
|
|
2019-03-02 00:57:28 +01:00
|
|
|
static conf::item<size_t>
|
|
|
|
buffer_size
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.initialsync.buffer_size" },
|
2020-01-24 03:47:23 +01:00
|
|
|
{ "default", long(128_KiB) },
|
2019-03-02 00:57:28 +01:00
|
|
|
};
|
2018-03-01 06:25:25 +01:00
|
|
|
|
2019-03-02 00:57:28 +01:00
|
|
|
static conf::item<size_t>
|
|
|
|
flush_hiwat
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.initialsync.flush.hiwat" },
|
|
|
|
{ "default", long(32_KiB) },
|
|
|
|
};
|
2018-03-01 06:25:25 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-25 13:07:48 +01:00
|
|
|
get__initialsync(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2018-02-25 13:07:48 +01:00
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2019-03-02 00:57:28 +01:00
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!exists(room))
|
2018-03-01 06:25:25 +01:00
|
|
|
{
|
2019-03-02 00:57:28 +01:00
|
|
|
if(!my(room))
|
2019-04-11 15:59:09 +02:00
|
|
|
get__initialsync_remote(client, request, room);
|
|
|
|
else
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"room_id '%s' does not exist.", string_view{room_id}
|
|
|
|
};
|
2018-03-01 06:25:25 +01:00
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2018-03-01 06:25:25 +01:00
|
|
|
{
|
2019-03-02 00:57:28 +01:00
|
|
|
client, http::OK, buffer_size
|
2018-03-01 06:25:25 +01:00
|
|
|
};
|
|
|
|
|
2019-03-02 00:57:28 +01:00
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher(), size_t(flush_hiwat)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
get__initialsync_local(client, request, room, request.user_id, top);
|
2019-06-24 07:09:41 +02:00
|
|
|
return std::move(response);
|
2019-03-02 00:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
get__initialsync_local(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-03-02 00:57:28 +01:00
|
|
|
const m::room &room,
|
|
|
|
const m::user &user,
|
|
|
|
json::stack::object &out)
|
|
|
|
{
|
2019-06-30 03:39:53 +02:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto room_depth
|
|
|
|
{
|
|
|
|
m::depth(std::nothrow, room)
|
|
|
|
};
|
|
|
|
|
2019-07-25 23:02:25 +02:00
|
|
|
char membership_buf[m::room::MEMBERSHIP_MAX_SIZE];
|
2019-03-02 00:57:28 +01:00
|
|
|
json::stack::member
|
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
out, "membership", membership(membership_buf, room, request.user_id)
|
2019-03-02 00:57:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
out, "visibility", m::rooms::summary::has(room)? "public"_sv: "private"_sv
|
2019-03-02 00:57:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room_account_data room_account_data
|
|
|
|
{
|
|
|
|
user, room
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array account_data
|
|
|
|
{
|
|
|
|
out, "account_data"
|
|
|
|
};
|
|
|
|
|
|
|
|
room_account_data.for_each([&account_data]
|
|
|
|
(const string_view &type, const json::object &content)
|
|
|
|
{
|
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
account_data
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "type", type
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "content", content
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2019-03-06 02:10:22 +01:00
|
|
|
|
|
|
|
const m::user::room_tags room_tags
|
|
|
|
{
|
|
|
|
user, room
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object tag
|
|
|
|
{
|
|
|
|
account_data
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
tag, "type", "m.tag"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object tag_content
|
|
|
|
{
|
|
|
|
tag, "content"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object tags
|
|
|
|
{
|
|
|
|
tag_content, "tags"
|
|
|
|
};
|
|
|
|
|
|
|
|
room_tags.for_each([&tags]
|
|
|
|
(const string_view &type, const json::object &content)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
tags, type, content
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
tags.~object();
|
|
|
|
tag_content.~object();
|
|
|
|
tag.~object();
|
2019-03-02 00:57:28 +01:00
|
|
|
account_data.~array();
|
|
|
|
|
|
|
|
json::stack::array state
|
2018-03-01 06:25:25 +01:00
|
|
|
{
|
2019-03-02 00:57:28 +01:00
|
|
|
out, "state"
|
2018-03-01 06:25:25 +01:00
|
|
|
};
|
|
|
|
|
2020-04-15 23:52:46 +02:00
|
|
|
m::event::fetch state_event;
|
|
|
|
m::room::state{room}.for_each([&]
|
|
|
|
(const auto &type, const auto &state_key, const m::event::idx &event_idx)
|
2018-03-01 06:25:25 +01:00
|
|
|
{
|
2020-04-15 23:52:46 +02:00
|
|
|
if(!seek(std::nothrow, state_event, event_idx))
|
2019-03-02 00:57:28 +01:00
|
|
|
return true;
|
|
|
|
|
2020-04-15 23:52:46 +02:00
|
|
|
if(!visible(state_event, user.user_id))
|
2019-09-24 23:24:16 +02:00
|
|
|
return true;
|
|
|
|
|
2019-08-03 01:56:18 +02:00
|
|
|
m::event::append::opts opts;
|
2020-04-15 23:52:46 +02:00
|
|
|
opts.event_idx = &event_idx;
|
2019-06-30 03:39:53 +02:00
|
|
|
opts.user_id = &user.user_id;
|
|
|
|
opts.user_room = &user_room;
|
|
|
|
opts.room_depth = &room_depth;
|
|
|
|
opts.query_txnid = false;
|
2020-04-15 23:52:46 +02:00
|
|
|
m::event::append(state, state_event, opts);
|
2019-03-02 00:57:28 +01:00
|
|
|
return true;
|
2020-04-15 23:52:46 +02:00
|
|
|
});
|
2019-03-02 00:57:28 +01:00
|
|
|
state.~array();
|
|
|
|
|
2019-08-30 23:26:07 +02:00
|
|
|
m::room::events it{room};
|
2019-03-02 00:57:28 +01:00
|
|
|
json::stack::object messages
|
|
|
|
{
|
|
|
|
out, "messages"
|
2018-03-01 06:25:25 +01:00
|
|
|
};
|
|
|
|
|
2019-03-02 00:57:28 +01:00
|
|
|
if(it)
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-09-24 23:24:16 +02:00
|
|
|
messages, "start", m::event_id(it.event_idx())
|
2019-03-02 00:57:28 +01:00
|
|
|
};
|
|
|
|
|
2020-04-15 23:58:51 +02:00
|
|
|
const size_t limit
|
|
|
|
{
|
|
|
|
std::min(request.query.get("limit", size_t(initial_backfill)), 256UL)
|
|
|
|
};
|
|
|
|
|
2019-03-02 00:57:28 +01:00
|
|
|
// seek down first to give events in chronological order.
|
2020-04-15 23:58:51 +02:00
|
|
|
for(size_t i(0); it && i <= limit; --it, ++i);
|
|
|
|
|
2019-03-02 00:57:28 +01:00
|
|
|
if(it)
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-09-24 23:24:16 +02:00
|
|
|
messages, "end", m::event_id(it.event_idx())
|
2019-03-02 00:57:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array chunk
|
2018-03-01 06:25:25 +01:00
|
|
|
{
|
2019-03-02 00:57:28 +01:00
|
|
|
messages, "chunk"
|
2018-03-01 06:25:25 +01:00
|
|
|
};
|
2018-02-25 13:07:48 +01:00
|
|
|
|
2019-03-02 00:57:28 +01:00
|
|
|
for(; it; ++it)
|
2018-02-25 13:07:48 +01:00
|
|
|
{
|
2019-03-12 00:02:48 +01:00
|
|
|
const auto &event_idx(it.event_idx());
|
2019-09-24 23:24:16 +02:00
|
|
|
const m::event &event(*it);
|
|
|
|
if(!visible(event, user.user_id))
|
|
|
|
continue;
|
2019-03-02 00:57:28 +01:00
|
|
|
|
2019-08-03 01:56:18 +02:00
|
|
|
m::event::append::opts opts;
|
2019-03-12 00:02:48 +01:00
|
|
|
opts.event_idx = &event_idx;
|
2019-06-30 03:39:53 +02:00
|
|
|
opts.user_id = &user.user_id;
|
|
|
|
opts.user_room = &user_room;
|
|
|
|
opts.room_depth = &room_depth;
|
|
|
|
opts.query_txnid = true;
|
2019-08-03 01:56:18 +02:00
|
|
|
m::event::append(chunk, event, opts);
|
2019-03-02 00:57:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:59:09 +02:00
|
|
|
void
|
2019-03-02 00:57:28 +01:00
|
|
|
get__initialsync_remote(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-03-02 00:57:28 +01:00
|
|
|
const m::room &room)
|
|
|
|
{
|
2019-07-16 06:22:43 +02:00
|
|
|
const m::room::origins origins{room};
|
|
|
|
|
|
|
|
char server_buf[384];
|
|
|
|
const string_view server
|
2019-03-02 00:57:28 +01:00
|
|
|
{
|
2019-07-16 06:22:43 +02:00
|
|
|
origins.random(server_buf, [](const string_view &origin)
|
|
|
|
{
|
|
|
|
return !my_host(origin);
|
|
|
|
})
|
2018-02-25 13:07:48 +01:00
|
|
|
};
|
2019-04-11 15:59:09 +02:00
|
|
|
|
2020-03-08 02:15:58 +01:00
|
|
|
const auto remote
|
2019-04-11 15:59:09 +02:00
|
|
|
{
|
2019-07-16 06:22:43 +02:00
|
|
|
server?: room.room_id.host()
|
2019-04-11 15:59:09 +02:00
|
|
|
};
|
|
|
|
|
2019-07-16 06:22:43 +02:00
|
|
|
const auto head
|
|
|
|
{
|
2020-10-15 06:05:21 +02:00
|
|
|
m::room::head::fetch(room, remote, request.user_id)
|
2019-07-16 06:22:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
m::room room_{room};
|
|
|
|
room_.event_id = head;
|
2019-07-27 00:39:23 +02:00
|
|
|
//m::fetch::state_ids(room_);
|
2018-02-25 13:07:48 +01:00
|
|
|
}
|