2018-02-14 12:23:20 -08: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;
|
|
|
|
|
2020-03-06 18:53:00 -08:00
|
|
|
/// 11.20.1.1 - The maximum number of events to return. Default: 10.
|
2018-05-31 03:24:07 -07:00
|
|
|
const size_t
|
|
|
|
default_limit
|
|
|
|
{
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2019-04-18 14:12:18 -07:00
|
|
|
log::log
|
|
|
|
context_log
|
|
|
|
{
|
2019-07-31 19:40:18 -07:00
|
|
|
"m.context"
|
2019-04-18 14:12:18 -07:00
|
|
|
};
|
|
|
|
|
2019-07-16 13:29:15 -07:00
|
|
|
static bool
|
2019-03-09 19:21:44 -08:00
|
|
|
_append(json::stack::array &,
|
|
|
|
const m::event &,
|
|
|
|
const m::event::idx &,
|
2019-03-13 17:03:01 -07:00
|
|
|
const m::user::room &,
|
2019-04-15 12:16:48 -07:00
|
|
|
const int64_t &,
|
2019-03-13 17:03:01 -07:00
|
|
|
const bool &query_txnid = true);
|
2019-03-09 19:21:44 -08:00
|
|
|
|
2019-09-28 16:12:07 -07:00
|
|
|
m::resource::response
|
2018-02-14 12:23:20 -08:00
|
|
|
get__context(client &client,
|
2019-09-28 16:12:07 -07:00
|
|
|
const m::resource::request &request,
|
2018-02-14 12:23:20 -08:00
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2019-03-01 19:01:21 -08:00
|
|
|
if(request.parv.size() < 3)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"event_id path parameter required"
|
|
|
|
};
|
|
|
|
|
2018-02-14 12:23:20 -08:00
|
|
|
m::event::id::buf event_id
|
|
|
|
{
|
2019-03-31 00:34:44 -07:00
|
|
|
url::decode(event_id, request.parv[2])
|
2018-02-14 12:23:20 -08:00
|
|
|
};
|
|
|
|
|
2019-10-10 13:33:43 -07:00
|
|
|
const auto &limit
|
2018-05-31 03:24:07 -07:00
|
|
|
{
|
2019-10-10 13:33:43 -07:00
|
|
|
std::min(request.query.get<size_t>("limit", default_limit), size_t(limit_max))
|
|
|
|
};
|
2018-05-31 03:24:07 -07:00
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
2018-02-14 12:23:20 -08:00
|
|
|
|
2019-08-14 01:01:46 -07:00
|
|
|
if(!visible(room, request.user_id))
|
2018-05-31 04:25:46 -07:00
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted to view the room at this event"
|
|
|
|
};
|
|
|
|
|
2019-07-07 18:04:22 -07:00
|
|
|
// Non-spec param to allow preventing any state from being returned.
|
|
|
|
const bool include_state
|
|
|
|
{
|
|
|
|
request.query.get("state", true)
|
|
|
|
};
|
|
|
|
|
2019-07-07 17:45:41 -07:00
|
|
|
// The standard ?filter= is parsed here. m::filter::get() handles
|
|
|
|
// whether this is a filter_id and conducts a fetch into this buffer;
|
|
|
|
// or inline JSON, and performs URL decoding into this buffer.
|
|
|
|
const std::string filter_json
|
|
|
|
{
|
|
|
|
m::filter::get(request.query["filter"], request.user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room_event_filter filter
|
|
|
|
{
|
|
|
|
filter_json
|
|
|
|
};
|
|
|
|
|
2018-02-14 12:23:20 -08:00
|
|
|
const m::event::fetch event
|
|
|
|
{
|
2019-08-05 17:24:33 -07:00
|
|
|
event_id
|
2018-02-14 12:23:20 -08:00
|
|
|
};
|
|
|
|
|
2019-03-09 19:21:44 -08:00
|
|
|
const m::user::room &user_room
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-04-15 12:16:48 -07:00
|
|
|
const auto room_depth
|
|
|
|
{
|
|
|
|
m::depth(std::nothrow, room_id)
|
|
|
|
};
|
|
|
|
|
2019-09-28 16:12:07 -07:00
|
|
|
m::resource::response::chunked response
|
2018-05-31 03:24:07 -07:00
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
2018-09-04 22:48:23 -07:00
|
|
|
response.buf, response.flusher(), size_t(flush_hiwat)
|
2018-05-31 03:24:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object ret
|
2018-02-14 12:23:20 -08:00
|
|
|
{
|
2018-05-31 03:24:07 -07:00
|
|
|
out
|
|
|
|
};
|
|
|
|
|
2019-04-23 13:42:50 -07:00
|
|
|
// Output the main event first.
|
2018-05-31 03:24:07 -07:00
|
|
|
{
|
2019-04-23 13:42:50 -07:00
|
|
|
json::stack::object _event
|
|
|
|
{
|
|
|
|
ret, "event"
|
|
|
|
};
|
|
|
|
|
2019-08-02 16:56:18 -07:00
|
|
|
// We use m::event::append() to modify/add/remove data for this client.
|
|
|
|
m::event::append::opts opts;
|
2019-04-23 13:42:50 -07:00
|
|
|
opts.event_idx = &event.event_idx;
|
|
|
|
opts.user_id = &user_room.user.user_id;
|
|
|
|
opts.user_room = &user_room;
|
|
|
|
opts.room_depth = &room_depth;
|
2019-08-02 16:56:18 -07:00
|
|
|
m::event::append(_event, event, opts);
|
2019-04-23 13:42:50 -07:00
|
|
|
}
|
2018-05-31 03:24:07 -07:00
|
|
|
|
2019-04-18 14:12:18 -07:00
|
|
|
// Counters for debug messages
|
|
|
|
struct counts
|
|
|
|
{
|
|
|
|
size_t before {0};
|
|
|
|
size_t after {0};
|
|
|
|
size_t state {0};
|
|
|
|
}
|
|
|
|
counts;
|
|
|
|
|
2019-03-31 00:36:56 -07:00
|
|
|
m::event::id::buf start;
|
2018-05-31 03:24:07 -07:00
|
|
|
{
|
2019-03-31 00:36:56 -07:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
ret, "events_before"
|
|
|
|
};
|
|
|
|
|
2019-08-30 14:26:07 -07:00
|
|
|
m::room::events before
|
2018-09-03 02:35:11 -07:00
|
|
|
{
|
2019-08-05 17:24:33 -07:00
|
|
|
room, event_id
|
2018-09-03 02:35:11 -07:00
|
|
|
};
|
|
|
|
|
2018-05-31 03:24:07 -07:00
|
|
|
if(before)
|
|
|
|
--before;
|
|
|
|
|
|
|
|
for(size_t i(0); i < limit && before; --before, ++i)
|
2018-02-14 12:23:20 -08:00
|
|
|
{
|
2018-05-31 03:24:07 -07:00
|
|
|
const m::event &event{*before};
|
2019-07-10 00:26:25 -07:00
|
|
|
start = event.event_id;
|
2018-05-31 04:25:46 -07:00
|
|
|
if(!visible(event, request.user_id))
|
2019-03-04 12:43:14 -08:00
|
|
|
continue;
|
2018-05-31 04:25:46 -07:00
|
|
|
|
2019-07-16 13:29:15 -07:00
|
|
|
counts.before += _append(array, event, before.event_idx(), user_room, room_depth);
|
2018-02-14 12:23:20 -08:00
|
|
|
}
|
2019-03-04 12:43:14 -08:00
|
|
|
|
2019-03-09 18:14:59 -08:00
|
|
|
if(before && limit > 0)
|
2019-03-04 12:43:14 -08:00
|
|
|
--before;
|
|
|
|
|
|
|
|
if(before)
|
2019-09-24 14:24:16 -07:00
|
|
|
start = m::event_id(before.event_idx());
|
2019-03-09 18:14:59 -08:00
|
|
|
else
|
|
|
|
start = {};
|
2018-05-31 03:24:07 -07:00
|
|
|
}
|
|
|
|
|
2019-03-31 00:36:56 -07:00
|
|
|
if(start)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "start", json::value{start}
|
|
|
|
};
|
2018-05-31 03:24:07 -07:00
|
|
|
|
2019-03-31 00:36:56 -07:00
|
|
|
m::event::id::buf end;
|
2018-05-31 03:24:07 -07:00
|
|
|
{
|
2019-03-31 00:36:56 -07:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
ret, "events_after"
|
|
|
|
};
|
|
|
|
|
2019-08-30 14:26:07 -07:00
|
|
|
m::room::events after
|
2018-09-03 02:35:11 -07:00
|
|
|
{
|
2019-08-05 17:24:33 -07:00
|
|
|
room, event_id
|
2018-09-03 02:35:11 -07:00
|
|
|
};
|
|
|
|
|
2018-05-31 03:24:07 -07:00
|
|
|
if(after)
|
|
|
|
++after;
|
|
|
|
|
|
|
|
for(size_t i(0); i < limit && after; ++after, ++i)
|
|
|
|
{
|
|
|
|
const m::event &event{*after};
|
2019-07-10 00:26:25 -07:00
|
|
|
end = event.event_id;
|
2018-05-31 04:25:46 -07:00
|
|
|
if(!visible(event, request.user_id))
|
2019-03-04 12:43:14 -08:00
|
|
|
continue;
|
2018-05-31 04:25:46 -07:00
|
|
|
|
2019-07-16 13:29:15 -07:00
|
|
|
counts.after += _append(array, event, after.event_idx(), user_room, room_depth);
|
2018-05-31 03:24:07 -07:00
|
|
|
}
|
2019-03-04 12:43:14 -08:00
|
|
|
|
2019-03-09 18:14:59 -08:00
|
|
|
if(after && limit > 0)
|
2019-03-04 12:43:14 -08:00
|
|
|
++after;
|
|
|
|
|
|
|
|
if(after)
|
2019-09-24 14:24:16 -07:00
|
|
|
end = m::event_id(after.event_idx());
|
2019-03-09 18:14:59 -08:00
|
|
|
else
|
|
|
|
end = {};
|
2018-05-31 03:24:07 -07:00
|
|
|
}
|
|
|
|
|
2019-03-31 00:36:56 -07:00
|
|
|
if(end)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "end", json::value{end}
|
|
|
|
};
|
2018-05-31 03:24:07 -07:00
|
|
|
|
2019-07-07 18:04:22 -07:00
|
|
|
if(include_state)
|
2018-05-31 03:24:07 -07:00
|
|
|
{
|
2019-03-31 00:36:56 -07:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
ret, "state"
|
|
|
|
};
|
|
|
|
|
2018-09-03 02:35:11 -07:00
|
|
|
const m::room::state state
|
|
|
|
{
|
2019-08-05 17:24:33 -07:00
|
|
|
room
|
2018-09-03 02:35:11 -07:00
|
|
|
};
|
|
|
|
|
2019-07-07 17:45:41 -07:00
|
|
|
// Setup the event::fetch instance outside of the closure to avoid
|
|
|
|
// underlying reconstruction costs for now.
|
|
|
|
m::event::fetch event;
|
|
|
|
|
|
|
|
// Iterate the state.
|
|
|
|
state.for_each([&]
|
|
|
|
(const string_view &type, const string_view &state_key, const m::event::idx &event_idx)
|
2018-05-31 03:24:07 -07:00
|
|
|
{
|
2019-07-07 17:45:41 -07:00
|
|
|
// Conditions to decide if we should skip this state event based
|
|
|
|
// on the lazy-loading spec.
|
|
|
|
const bool lazy_loaded
|
2019-03-11 16:02:48 -07:00
|
|
|
{
|
2019-07-07 17:45:41 -07:00
|
|
|
// The user supplied a filter enabling lazy-loading.
|
|
|
|
json::get<"lazy_load_members"_>(filter) &&
|
|
|
|
|
|
|
|
// The type of this state event is a m.room.member type
|
|
|
|
type == "m.room.member"
|
2019-03-11 16:02:48 -07:00
|
|
|
};
|
|
|
|
|
2019-07-07 17:45:41 -07:00
|
|
|
if(lazy_loaded)
|
|
|
|
return true;
|
|
|
|
|
2020-04-02 19:50:00 -07:00
|
|
|
if(!seek(std::nothrow, event, event_idx))
|
2019-07-07 17:45:41 -07:00
|
|
|
return true;
|
2019-03-11 16:02:48 -07:00
|
|
|
|
2018-09-03 02:24:22 -07:00
|
|
|
if(!visible(event, request.user_id))
|
2019-07-07 17:45:41 -07:00
|
|
|
return true;
|
2018-09-03 02:24:22 -07:00
|
|
|
|
2019-07-16 13:29:15 -07:00
|
|
|
counts.state += _append(array, event, event_idx, user_room, room_depth, false);
|
2019-07-07 17:45:41 -07:00
|
|
|
return true;
|
2018-05-31 03:24:07 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:12:18 -07:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
context_log, "%s %s in %s before:%zu start:%s after:%zu end:%s state:%zu",
|
|
|
|
client.loghead(),
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
counts.before,
|
|
|
|
string_view{start},
|
|
|
|
counts.after,
|
|
|
|
string_view{end},
|
|
|
|
counts.state,
|
|
|
|
};
|
|
|
|
|
2019-06-23 23:09:41 -06:00
|
|
|
return std::move(response);
|
2018-02-14 12:23:20 -08:00
|
|
|
}
|
2019-03-09 19:21:44 -08:00
|
|
|
|
2019-07-16 13:29:15 -07:00
|
|
|
bool
|
2019-03-09 19:21:44 -08:00
|
|
|
_append(json::stack::array &chunk,
|
|
|
|
const m::event &event,
|
|
|
|
const m::event::idx &event_idx,
|
2019-03-13 17:03:01 -07:00
|
|
|
const m::user::room &user_room,
|
2019-04-15 12:16:48 -07:00
|
|
|
const int64_t &room_depth,
|
2019-03-13 17:03:01 -07:00
|
|
|
const bool &query_txnid)
|
2019-03-09 19:21:44 -08:00
|
|
|
{
|
2019-08-02 16:56:18 -07:00
|
|
|
m::event::append::opts opts;
|
2019-03-11 16:02:48 -07:00
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.user_id = &user_room.user.user_id;
|
|
|
|
opts.user_room = &user_room;
|
2019-04-15 12:16:48 -07:00
|
|
|
opts.room_depth = &room_depth;
|
2019-08-02 16:56:18 -07:00
|
|
|
return m::event::append(chunk, event, opts);
|
2019-03-09 19:21:44 -08:00
|
|
|
}
|