2018-02-04 03:22:01 +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.
|
2017-08-23 23:10:28 +02:00
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client 7.5 :Public Rooms"
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:53:36 +02:00
|
|
|
resource
|
|
|
|
publicrooms_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/publicRooms",
|
|
|
|
{
|
|
|
|
"(7.5) Lists the public rooms on the server. "
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-05 07:08:37 +02:00
|
|
|
conf::item<size_t>
|
|
|
|
flush_hiwat
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.publicrooms.flush.hiwat" },
|
|
|
|
{ "default", 16384L },
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:53:36 +02:00
|
|
|
static resource::response
|
2018-10-25 02:11:03 +02:00
|
|
|
get__publicrooms(client &,
|
|
|
|
const resource::request &);
|
2018-04-22 08:53:36 +02:00
|
|
|
|
2018-10-25 02:11:03 +02:00
|
|
|
resource::method
|
|
|
|
post_method
|
|
|
|
{
|
|
|
|
publicrooms_resource, "POST", get__publicrooms
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
get_method
|
|
|
|
{
|
|
|
|
publicrooms_resource, "GET", get__publicrooms
|
|
|
|
};
|
2018-04-22 08:53:36 +02:00
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
resource::response
|
2018-10-25 02:11:03 +02:00
|
|
|
get__publicrooms(client &client,
|
|
|
|
const resource::request &request)
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-09-05 07:08:37 +02:00
|
|
|
const string_view &since
|
|
|
|
{
|
2018-10-25 02:11:03 +02:00
|
|
|
request.has("since")?
|
|
|
|
unquote(request["since"]):
|
|
|
|
request.query["since"]
|
2018-09-05 07:08:37 +02:00
|
|
|
};
|
|
|
|
|
2018-10-25 01:57:41 +02:00
|
|
|
if(since && !valid(m::id::ROOM, since))
|
|
|
|
throw m::BAD_REQUEST
|
|
|
|
{
|
|
|
|
"Invalid since token for this server."
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:53:36 +02:00
|
|
|
const auto &server
|
|
|
|
{
|
|
|
|
request.query["server"]
|
|
|
|
};
|
|
|
|
|
2018-09-05 07:08:37 +02:00
|
|
|
const json::object &filter
|
2018-04-22 08:53:36 +02:00
|
|
|
{
|
2018-09-05 07:08:37 +02:00
|
|
|
request["filter"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &search_term
|
|
|
|
{
|
|
|
|
unquote(filter["generic_search_term"])
|
2018-04-22 08:53:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t limit
|
|
|
|
{
|
2018-10-25 02:11:03 +02:00
|
|
|
request.has("limit")?
|
|
|
|
uint8_t(request.at<ushort>("limit")):
|
|
|
|
uint8_t(request.query.get<ushort>("limit", 16U))
|
2018-04-22 08:53:36 +02:00
|
|
|
};
|
|
|
|
|
2018-09-05 07:08:37 +02:00
|
|
|
const bool include_all_networks
|
|
|
|
{
|
|
|
|
request.get<bool>("include_all_networks", false)
|
|
|
|
};
|
2018-02-22 00:39:54 +01:00
|
|
|
|
2018-09-05 07:08:37 +02:00
|
|
|
resource::response::chunked response
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-09-05 07:08:37 +02:00
|
|
|
client, http::OK
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2018-09-05 07:08:37 +02:00
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher(), size_t(flush_hiwat)
|
|
|
|
};
|
2018-04-22 08:53:36 +02:00
|
|
|
|
2018-09-05 07:31:08 +02:00
|
|
|
size_t count{0};
|
2018-09-05 07:08:37 +02:00
|
|
|
m::room::id::buf prev_batch_buf;
|
|
|
|
m::room::id::buf next_batch_buf;
|
|
|
|
json::stack::object top{out};
|
|
|
|
{
|
|
|
|
json::stack::member chunk_m{top, "chunk"};
|
|
|
|
json::stack::array chunk{chunk_m};
|
2018-10-25 01:57:41 +02:00
|
|
|
|
|
|
|
//TODO: better keying for search terms
|
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
since?
|
|
|
|
since:
|
|
|
|
server?
|
|
|
|
server:
|
|
|
|
search_term?
|
|
|
|
search_term:
|
|
|
|
my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
m::rooms::for_each_public(key, [&]
|
2018-10-24 23:15:36 +02:00
|
|
|
(const m::room::id &room_id)
|
2018-09-05 07:08:37 +02:00
|
|
|
{
|
|
|
|
json::stack::object obj{chunk};
|
2018-10-24 23:01:21 +02:00
|
|
|
m::rooms::summary_chunk(room_id, obj);
|
2018-09-05 07:08:37 +02:00
|
|
|
|
2018-09-05 07:31:08 +02:00
|
|
|
if(!count && !empty(since))
|
|
|
|
prev_batch_buf = room_id; //TODO: ???
|
2018-09-05 07:08:37 +02:00
|
|
|
|
2018-09-05 07:31:08 +02:00
|
|
|
next_batch_buf = room_id;
|
2018-10-24 23:01:21 +02:00
|
|
|
return ++count < limit;
|
2018-09-05 07:08:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
json::stack::member
|
2018-04-22 08:53:36 +02:00
|
|
|
{
|
2018-09-05 07:08:37 +02:00
|
|
|
top, "total_room_count_estimate", json::value
|
|
|
|
{
|
2018-10-25 01:57:41 +02:00
|
|
|
ssize_t(m::rooms::count_public(server))
|
2018-09-05 07:08:37 +02:00
|
|
|
}
|
2018-04-22 08:53:36 +02:00
|
|
|
};
|
|
|
|
|
2018-09-05 07:31:08 +02:00
|
|
|
if(prev_batch_buf)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "prev_batch", prev_batch_buf
|
|
|
|
};
|
2018-04-22 08:53:36 +02:00
|
|
|
|
2018-09-05 07:31:08 +02:00
|
|
|
if(count >= limit)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "next_batch", next_batch_buf
|
|
|
|
};
|
2018-04-22 08:53:36 +02:00
|
|
|
|
2018-10-24 23:22:10 +02:00
|
|
|
return response;
|
2018-09-05 07:08:37 +02:00
|
|
|
}
|