2018-02-14 21:23:20 +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.
|
|
|
|
|
|
|
|
#include "rooms.h"
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-05-31 12:24:07 +02:00
|
|
|
const size_t
|
|
|
|
default_limit
|
|
|
|
{
|
|
|
|
// 11.20.1.1 - The maximum number of events to return. Default: 10.
|
|
|
|
10
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<size_t>
|
|
|
|
limit_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.context.limit.max" },
|
|
|
|
{ "default", 128L },
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<size_t>
|
|
|
|
flush_hiwat
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.context.flush.hiwat" },
|
|
|
|
{ "default", 16384L },
|
|
|
|
};
|
|
|
|
|
2018-09-03 11:35:11 +02:00
|
|
|
const m::event::fetch::opts
|
|
|
|
default_fetch_opts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content",
|
|
|
|
"depth",
|
|
|
|
"event_id",
|
|
|
|
"origin_server_ts",
|
|
|
|
"redacts",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"state_key",
|
|
|
|
"type",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-10 04:21:44 +01:00
|
|
|
static void
|
|
|
|
_append(json::stack::array &,
|
|
|
|
const m::event &,
|
|
|
|
const m::event::idx &,
|
|
|
|
const m::user::room &);
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
resource::response
|
|
|
|
get__context(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2019-03-02 04:01:21 +01:00
|
|
|
if(request.parv.size() < 3)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"event_id path parameter required"
|
|
|
|
};
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
m::event::id::buf event_id
|
|
|
|
{
|
2019-03-02 04:01:21 +01:00
|
|
|
m::sigil(request.parv[2]) == m::id::EVENT?
|
|
|
|
url::decode(event_id, request.parv[2]):
|
|
|
|
m::head(room_id)
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2018-05-31 12:24:07 +02:00
|
|
|
const auto limit{[&request]
|
|
|
|
{
|
|
|
|
auto ret(request.query.get<size_t>("limit", default_limit));
|
|
|
|
return std::min(ret, size_t(limit_max));
|
|
|
|
}()};
|
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2018-05-31 13:25:46 +02:00
|
|
|
if(!room.visible(request.user_id))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted to view the room at this event"
|
|
|
|
};
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
event_id
|
|
|
|
};
|
|
|
|
|
2019-03-10 04:21:44 +01:00
|
|
|
const m::user::room &user_room
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2018-05-31 12:24:07 +02:00
|
|
|
resource::response::chunked response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
2018-09-05 07:48:23 +02:00
|
|
|
response.buf, response.flusher(), size_t(flush_hiwat)
|
2018-05-31 12:24:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object ret
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-05-31 12:24:07 +02:00
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "event", event
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::id::buf start{event_id};
|
|
|
|
{
|
|
|
|
json::stack::member member{ret, "events_before"};
|
|
|
|
json::stack::array array{member};
|
2018-09-03 11:35:11 +02:00
|
|
|
m::room::messages before
|
|
|
|
{
|
|
|
|
room, event_id, &default_fetch_opts
|
|
|
|
};
|
|
|
|
|
2018-05-31 12:24:07 +02:00
|
|
|
if(before)
|
|
|
|
--before;
|
|
|
|
|
|
|
|
for(size_t i(0); i < limit && before; --before, ++i)
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-05-31 12:24:07 +02:00
|
|
|
const m::event &event{*before};
|
2019-03-04 21:43:14 +01:00
|
|
|
start = at<"event_id"_>(event);
|
2018-05-31 13:25:46 +02:00
|
|
|
if(!visible(event, request.user_id))
|
2019-03-04 21:43:14 +01:00
|
|
|
continue;
|
2018-05-31 13:25:46 +02:00
|
|
|
|
2019-03-10 04:21:44 +01:00
|
|
|
_append(array, event, before.event_idx(), user_room);
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|
2019-03-04 21:43:14 +01:00
|
|
|
|
2019-03-10 03:14:59 +01:00
|
|
|
if(before && limit > 0)
|
2019-03-04 21:43:14 +01:00
|
|
|
--before;
|
|
|
|
|
|
|
|
if(before)
|
|
|
|
start = before.event_id();
|
2019-03-10 03:14:59 +01:00
|
|
|
else
|
|
|
|
start = {};
|
2018-05-31 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "start", json::value{start}
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
2018-05-31 12:24:07 +02:00
|
|
|
|
|
|
|
m::event::id::buf end{event_id};
|
|
|
|
{
|
|
|
|
json::stack::member member{ret, "events_after"};
|
|
|
|
json::stack::array array{member};
|
2018-09-03 11:35:11 +02:00
|
|
|
m::room::messages after
|
|
|
|
{
|
|
|
|
room, event_id, &default_fetch_opts
|
|
|
|
};
|
|
|
|
|
2018-05-31 12:24:07 +02:00
|
|
|
if(after)
|
|
|
|
++after;
|
|
|
|
|
|
|
|
for(size_t i(0); i < limit && after; ++after, ++i)
|
|
|
|
{
|
|
|
|
const m::event &event{*after};
|
2019-03-04 21:43:14 +01:00
|
|
|
end = at<"event_id"_>(event);
|
2018-05-31 13:25:46 +02:00
|
|
|
if(!visible(event, request.user_id))
|
2019-03-04 21:43:14 +01:00
|
|
|
continue;
|
2018-05-31 13:25:46 +02:00
|
|
|
|
2019-03-10 04:21:44 +01:00
|
|
|
_append(array, event, after.event_idx(), user_room);
|
2018-05-31 12:24:07 +02:00
|
|
|
}
|
2019-03-04 21:43:14 +01:00
|
|
|
|
2019-03-10 03:14:59 +01:00
|
|
|
if(after && limit > 0)
|
2019-03-04 21:43:14 +01:00
|
|
|
++after;
|
|
|
|
|
|
|
|
if(after)
|
|
|
|
end = after.event_id();
|
2019-03-10 03:14:59 +01:00
|
|
|
else
|
|
|
|
end = {};
|
2018-05-31 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "end", json::value{end}
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
json::stack::member member{ret, "state"};
|
|
|
|
json::stack::array array{member};
|
2018-09-03 11:35:11 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room, &default_fetch_opts
|
|
|
|
};
|
|
|
|
|
2019-03-12 00:02:48 +01:00
|
|
|
state.for_each([&array, &request, &user_room]
|
|
|
|
(const m::event::idx &event_idx)
|
2018-05-31 12:24:07 +02:00
|
|
|
{
|
2019-03-12 00:02:48 +01:00
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
|
|
|
return;
|
|
|
|
|
2018-09-03 11:24:22 +02:00
|
|
|
if(!visible(event, request.user_id))
|
|
|
|
return;
|
|
|
|
|
2019-03-12 00:02:48 +01:00
|
|
|
_append(array, event, event_idx, user_room);
|
2018-05-31 12:24:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|
2019-03-10 04:21:44 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
_append(json::stack::array &chunk,
|
|
|
|
const m::event &event,
|
|
|
|
const m::event::idx &event_idx,
|
|
|
|
const m::user::room &user_room)
|
|
|
|
{
|
2019-03-12 00:02:48 +01:00
|
|
|
m::event_append_opts opts;
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.user_id = &user_room.user.user_id;
|
|
|
|
opts.user_room = &user_room;
|
|
|
|
m::append(chunk, event, opts);
|
2019-03-10 04:21:44 +01:00
|
|
|
}
|