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:51:34 +02:00
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
"Client 14.17.1.1 :Room Previews"
|
2018-02-15 22:06:49 +01:00
|
|
|
};
|
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
static bool
|
2019-06-07 11:48:33 +02:00
|
|
|
append_event(json::stack::array &out,
|
|
|
|
const m::event &event,
|
|
|
|
const m::event::idx &event_idx,
|
2019-06-30 03:51:23 +02:00
|
|
|
const int64_t &room_depth,
|
|
|
|
const m::user::room &);
|
2019-06-07 11:48:33 +02:00
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
static bool
|
|
|
|
get_events_from(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
|
|
|
const m::event::id &room_head,
|
2019-06-07 11:48:33 +02:00
|
|
|
const int64_t &room_depth,
|
2019-03-02 02:54:16 +01:00
|
|
|
json::stack::object &out);
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
get__events(client &client,
|
|
|
|
const resource::request &request);
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
resource
|
|
|
|
events_resource
|
2017-08-23 23:51:34 +02:00
|
|
|
{
|
2017-10-12 05:52:33 +02:00
|
|
|
"/_matrix/client/r0/events",
|
2017-12-12 21:26:39 +01:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
"(14.17.1.1) Room Previews"
|
2017-12-12 21:26:39 +01:00
|
|
|
}
|
2017-08-23 23:51:34 +02:00
|
|
|
};
|
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
conf::item<ircd::milliseconds>
|
|
|
|
timeout_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.events.timeout.max" },
|
|
|
|
{ "default", 15 * 1000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<ircd::milliseconds>
|
|
|
|
timeout_min
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.events.timeout.min" },
|
|
|
|
{ "default", 5 * 1000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<ircd::milliseconds>
|
|
|
|
timeout_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.events.timeout.default" },
|
|
|
|
{ "default", 10 * 1000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
static conf::item<size_t>
|
|
|
|
events_limit
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.events.limit" },
|
|
|
|
{ "default", 32L },
|
|
|
|
};
|
|
|
|
|
|
|
|
static conf::item<size_t>
|
|
|
|
buffer_size
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.events.buffer_size" },
|
|
|
|
{ "default", long(96_KiB) },
|
|
|
|
};
|
|
|
|
|
|
|
|
static conf::item<size_t>
|
|
|
|
flush_hiwat
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.events.flush.hiwat" },
|
|
|
|
{ "default", long(16_KiB) },
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_get
|
|
|
|
{
|
|
|
|
events_resource, "GET", get__events
|
|
|
|
};
|
|
|
|
|
|
|
|
struct waiter
|
|
|
|
{
|
|
|
|
m::user::id user_id;
|
|
|
|
m::room::id room_id;
|
|
|
|
std::string *event;
|
2019-07-10 09:56:02 +02:00
|
|
|
m::event::id::buf *event_id;
|
2019-03-02 02:54:16 +01:00
|
|
|
ctx::dock *dock;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::list<waiter>
|
|
|
|
clients;
|
|
|
|
|
2017-08-18 20:19:13 +02:00
|
|
|
resource::response
|
2018-02-16 21:07:12 +01:00
|
|
|
get__events(client &client,
|
|
|
|
const resource::request &request)
|
2017-08-23 23:51:34 +02:00
|
|
|
{
|
2018-02-25 13:18:22 +01:00
|
|
|
if(!request.query["room_id"])
|
2018-02-16 21:07:12 +01:00
|
|
|
throw m::UNSUPPORTED
|
|
|
|
{
|
|
|
|
"Specify a room_id or use /sync"
|
|
|
|
};
|
|
|
|
|
2018-02-25 13:18:22 +01:00
|
|
|
m::room::id::buf room_id
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
url::decode(room_id, request.query.at("room_id"))
|
2018-02-25 13:18:22 +01:00
|
|
|
};
|
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
m::event::id::buf event_id
|
2018-02-25 13:18:22 +01:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
request.query["from"]?
|
|
|
|
url::decode(event_id, request.query.at("from")):
|
|
|
|
m::head(room_id)
|
2018-02-25 13:18:22 +01:00
|
|
|
};
|
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
const m::room room
|
2018-02-25 13:18:22 +01:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
room_id, event_id
|
2018-02-25 13:18:22 +01:00
|
|
|
};
|
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
if(!room.visible(request.user_id))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not able to view the room at this event."
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response::chunked response
|
2018-02-25 13:18:22 +01:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
client, http::OK, buffer_size
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
json::stack out
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
response.buf, response.flusher(), size_t(flush_hiwat)
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
2017-09-08 11:32:49 +02:00
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
2018-02-25 13:18:22 +01:00
|
|
|
|
2019-06-07 11:48:33 +02:00
|
|
|
const auto &room_top
|
|
|
|
{
|
|
|
|
m::top(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &room_depth
|
|
|
|
{
|
|
|
|
std::get<int64_t>(room_top)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::id &room_head
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
2019-06-07 11:48:33 +02:00
|
|
|
std::get<m::event::id::buf>(room_top)
|
2018-02-16 21:07:12 +01:00
|
|
|
};
|
2017-09-08 11:32:49 +02:00
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
json::stack::member
|
2017-08-23 23:51:34 +02:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
top, "start", event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(event_id && event_id != room_head)
|
|
|
|
{
|
|
|
|
json::stack::checkpoint checkpoint
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
out
|
|
|
|
};
|
|
|
|
|
2019-06-07 11:48:33 +02:00
|
|
|
if(get_events_from(client, request, room_id, event_id, room_head, room_depth, top))
|
2019-03-02 02:54:16 +01:00
|
|
|
return response;
|
|
|
|
|
|
|
|
checkpoint.rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx::dock dock;
|
|
|
|
std::string event;
|
2019-07-10 09:56:02 +02:00
|
|
|
m::event::id::buf eid;
|
2019-03-02 02:54:16 +01:00
|
|
|
const unique_iterator it
|
|
|
|
{
|
2019-07-10 09:56:02 +02:00
|
|
|
clients, clients.emplace(end(clients), waiter{request.user_id, room_id, &event, &eid, &dock})
|
2017-08-18 20:19:13 +02:00
|
|
|
};
|
2019-03-02 02:54:16 +01:00
|
|
|
|
2019-07-12 04:25:01 +02:00
|
|
|
const milliseconds timeout
|
|
|
|
{
|
|
|
|
minmax
|
|
|
|
(
|
|
|
|
request.query.get("timeout", milliseconds(timeout_default)),
|
|
|
|
milliseconds(timeout_min),
|
|
|
|
milliseconds(timeout_max)
|
|
|
|
)
|
|
|
|
};
|
2019-03-02 02:54:16 +01:00
|
|
|
|
2019-07-12 04:25:01 +02:00
|
|
|
dock.wait_for(timeout, [&event, &eid]
|
2019-03-02 02:54:16 +01:00
|
|
|
{
|
2019-07-12 04:25:01 +02:00
|
|
|
return !empty(event) && !empty(eid);
|
2019-03-02 02:54:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if(!event.empty())
|
|
|
|
{
|
2019-06-07 11:48:33 +02:00
|
|
|
const m::event &event_
|
2019-03-02 02:54:16 +01:00
|
|
|
{
|
2019-07-12 04:25:01 +02:00
|
|
|
json::object(event), eid
|
2019-03-02 02:54:16 +01:00
|
|
|
};
|
|
|
|
|
2019-06-07 11:48:33 +02:00
|
|
|
const auto &event_idx
|
2019-03-02 02:54:16 +01:00
|
|
|
{
|
2019-07-12 04:47:28 +02:00
|
|
|
event_.event_id?
|
|
|
|
m::index(event_):
|
|
|
|
0UL
|
2019-03-02 02:54:16 +01:00
|
|
|
};
|
|
|
|
|
2019-06-07 11:48:33 +02:00
|
|
|
const auto &room_depth
|
2019-03-02 02:54:16 +01:00
|
|
|
{
|
2019-06-07 11:48:33 +02:00
|
|
|
m::depth(room_id)
|
2019-03-02 02:54:16 +01:00
|
|
|
};
|
|
|
|
|
2019-06-30 03:51:23 +02:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-06-07 11:48:33 +02:00
|
|
|
json::stack::array chunk
|
2019-03-02 02:54:16 +01:00
|
|
|
{
|
2019-06-07 11:48:33 +02:00
|
|
|
top, "chunk"
|
2019-03-02 02:54:16 +01:00
|
|
|
};
|
2019-06-07 11:48:33 +02:00
|
|
|
|
2019-06-30 03:51:23 +02:00
|
|
|
append_event(chunk, event_, event_idx, room_depth, user_room);
|
2019-03-02 02:54:16 +01:00
|
|
|
}
|
|
|
|
else json::stack::array
|
|
|
|
{
|
|
|
|
top, "chunk"
|
|
|
|
};
|
|
|
|
|
2019-07-10 09:56:02 +02:00
|
|
|
if(eid)
|
2019-03-02 02:54:16 +01:00
|
|
|
json::stack::member
|
|
|
|
{
|
2019-07-10 09:56:02 +02:00
|
|
|
top, "end", eid
|
2019-03-02 02:54:16 +01:00
|
|
|
};
|
|
|
|
else
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "end", room_head
|
|
|
|
};
|
|
|
|
|
|
|
|
return response;
|
2017-08-18 20:19:13 +02:00
|
|
|
}
|
2017-08-23 23:51:34 +02:00
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
static void
|
|
|
|
handle_notify(const m::event &,
|
|
|
|
m::vm::eval &);
|
|
|
|
|
|
|
|
m::hookfn<m::vm::eval &>
|
|
|
|
notified
|
2017-08-23 23:51:34 +02:00
|
|
|
{
|
2019-03-02 02:54:16 +01:00
|
|
|
handle_notify,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.notify" },
|
|
|
|
}
|
2017-08-23 23:51:34 +02:00
|
|
|
};
|
2019-03-02 02:54:16 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
handle_notify(const m::event &event,
|
|
|
|
m::vm::eval &)
|
2019-07-25 01:29:44 +02:00
|
|
|
try
|
2019-03-02 02:54:16 +01:00
|
|
|
{
|
|
|
|
const auto &room_id
|
|
|
|
{
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!room_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(auto &waiter : clients)
|
|
|
|
{
|
|
|
|
if(!waiter.event->empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(waiter.room_id != room_id)
|
|
|
|
continue;
|
|
|
|
|
2019-07-25 02:35:58 +02:00
|
|
|
assert(waiter.event_id);
|
|
|
|
*waiter.event_id = event.event_id?
|
|
|
|
m::event::id::buf{event.event_id}:
|
|
|
|
m::event::id::buf{};
|
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
assert(waiter.event);
|
|
|
|
*waiter.event = json::strung{event};
|
|
|
|
|
|
|
|
assert(waiter.dock);
|
|
|
|
waiter.dock->notify_one();
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 01:29:44 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
m::log, "client/events vm.notify hook :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2019-03-02 02:54:16 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
get_events_from(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
|
|
|
const m::event::id &room_head,
|
2019-06-07 11:48:33 +02:00
|
|
|
const int64_t &room_depth,
|
2019-03-02 02:54:16 +01:00
|
|
|
json::stack::object &out)
|
|
|
|
{
|
2019-06-30 03:51:23 +02:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-03-02 02:54:16 +01:00
|
|
|
m::room::messages it
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
json::stack::array chunk
|
|
|
|
{
|
|
|
|
out, "chunk"
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0), j(0);
|
|
|
|
for(; it && i < size_t(events_limit); --it, ++i)
|
|
|
|
{
|
|
|
|
if(!visible(it.event_id(), request.user_id))
|
|
|
|
continue;
|
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
j += append_event(chunk, *it, it.event_idx(), room_depth, user_room);
|
2019-03-02 02:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!j)
|
|
|
|
return j;
|
|
|
|
|
|
|
|
chunk.~array();
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
out, "end", it? it.event_id() : room_head
|
|
|
|
};
|
|
|
|
|
|
|
|
return j;
|
|
|
|
}
|
2019-06-07 11:48:33 +02:00
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
bool
|
2019-06-07 11:48:33 +02:00
|
|
|
append_event(json::stack::array &out,
|
|
|
|
const m::event &event,
|
|
|
|
const m::event::idx &event_idx,
|
2019-06-30 03:51:23 +02:00
|
|
|
const int64_t &room_depth,
|
|
|
|
const m::user::room &user_room)
|
2019-06-07 11:48:33 +02:00
|
|
|
{
|
|
|
|
m::event_append_opts opts;
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.room_depth = &room_depth;
|
2019-06-30 03:51:23 +02:00
|
|
|
opts.user_room = &user_room;
|
|
|
|
opts.user_id = &user_room.user.user_id;
|
2019-07-16 22:29:15 +02:00
|
|
|
return m::append(out, event, opts);
|
2019-06-07 11:48:33 +02:00
|
|
|
}
|