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;
|
|
|
|
|
2020-03-07 03:53:00 +01:00
|
|
|
/// 11.20.1.1 - The maximum number of events to return. Default: 10.
|
2018-05-31 12:24:07 +02: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 23:12:18 +02:00
|
|
|
log::log
|
|
|
|
context_log
|
|
|
|
{
|
2019-08-01 04:40:18 +02:00
|
|
|
"m.context"
|
2019-04-18 23:12:18 +02:00
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-14 21:23:20 +01:00
|
|
|
get__context(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2018-02-14 21:23:20 +01:00
|
|
|
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-31 09:34:44 +02:00
|
|
|
url::decode(event_id, request.parv[2])
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2023-01-16 03:10:36 +01:00
|
|
|
const auto limit
|
2018-05-31 12:24:07 +02:00
|
|
|
{
|
2019-10-10 22:33:43 +02:00
|
|
|
std::min(request.query.get<size_t>("limit", default_limit), size_t(limit_max))
|
|
|
|
};
|
2018-05-31 12:24:07 +02:00
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!visible(room, request.user_id))
|
2018-05-31 13:25:46 +02:00
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted to view the room at this event"
|
|
|
|
};
|
|
|
|
|
2019-07-08 03:04:22 +02:00
|
|
|
// Non-spec param to allow preventing any state from being returned.
|
|
|
|
const bool include_state
|
|
|
|
{
|
|
|
|
request.query.get("state", true)
|
|
|
|
};
|
|
|
|
|
2019-07-08 02:45:41 +02: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 21:23:20 +01:00
|
|
|
const m::event::fetch event
|
|
|
|
{
|
2019-08-06 02:24:33 +02:00
|
|
|
event_id
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2019-03-10 04:21:44 +01:00
|
|
|
const m::user::room &user_room
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-04-15 21:16:48 +02:00
|
|
|
const auto room_depth
|
|
|
|
{
|
|
|
|
m::depth(std::nothrow, room_id)
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2018-05-31 12:24:07 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-04-23 22:42:50 +02:00
|
|
|
// Output the main event first.
|
2018-05-31 12:24:07 +02:00
|
|
|
{
|
2019-04-23 22:42:50 +02:00
|
|
|
json::stack::object _event
|
|
|
|
{
|
|
|
|
ret, "event"
|
|
|
|
};
|
|
|
|
|
2019-08-03 01:56:18 +02:00
|
|
|
// We use m::event::append() to modify/add/remove data for this client.
|
2022-07-02 07:18:58 +02:00
|
|
|
m::event::append
|
|
|
|
{
|
|
|
|
_event, event,
|
|
|
|
{
|
|
|
|
.event_idx = &event.event_idx,
|
|
|
|
.user_id = &user_room.user.user_id,
|
|
|
|
.user_room = &user_room,
|
|
|
|
.room_depth = &room_depth,
|
|
|
|
}
|
|
|
|
};
|
2019-04-23 22:42:50 +02:00
|
|
|
}
|
2018-05-31 12:24:07 +02:00
|
|
|
|
2019-04-18 23:12:18 +02:00
|
|
|
// Counters for debug messages
|
|
|
|
struct counts
|
|
|
|
{
|
|
|
|
size_t before {0};
|
|
|
|
size_t after {0};
|
|
|
|
size_t state {0};
|
|
|
|
}
|
|
|
|
counts;
|
|
|
|
|
2019-03-31 09:36:56 +02:00
|
|
|
m::event::id::buf start;
|
2018-05-31 12:24:07 +02:00
|
|
|
{
|
2019-03-31 09:36:56 +02:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
ret, "events_before"
|
|
|
|
};
|
|
|
|
|
2019-08-30 23:26:07 +02:00
|
|
|
m::room::events before
|
2018-09-03 11:35:11 +02:00
|
|
|
{
|
2019-08-06 02:24:33 +02:00
|
|
|
room, event_id
|
2018-09-03 11:35:11 +02:00
|
|
|
};
|
|
|
|
|
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-07-10 09:26:25 +02:00
|
|
|
start = event.event_id;
|
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
|
|
|
|
2022-07-02 07:45:34 +02:00
|
|
|
const auto event_idx(before.event_idx());
|
|
|
|
counts.before += m::event::append
|
|
|
|
{
|
|
|
|
array, event,
|
|
|
|
{
|
|
|
|
.event_idx = &event_idx,
|
|
|
|
.user_id = &user_room.user.user_id,
|
|
|
|
.user_room = &user_room,
|
|
|
|
.room_depth = &room_depth,
|
|
|
|
.query_txnid = true,
|
|
|
|
}
|
|
|
|
};
|
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)
|
2019-09-24 23:24:16 +02:00
|
|
|
start = m::event_id(before.event_idx());
|
2019-03-10 03:14:59 +01:00
|
|
|
else
|
|
|
|
start = {};
|
2018-05-31 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 09:36:56 +02:00
|
|
|
if(start)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "start", json::value{start}
|
|
|
|
};
|
2018-05-31 12:24:07 +02:00
|
|
|
|
2019-03-31 09:36:56 +02:00
|
|
|
m::event::id::buf end;
|
2018-05-31 12:24:07 +02:00
|
|
|
{
|
2019-03-31 09:36:56 +02:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
ret, "events_after"
|
|
|
|
};
|
|
|
|
|
2019-08-30 23:26:07 +02:00
|
|
|
m::room::events after
|
2018-09-03 11:35:11 +02:00
|
|
|
{
|
2019-08-06 02:24:33 +02:00
|
|
|
room, event_id
|
2018-09-03 11:35:11 +02:00
|
|
|
};
|
|
|
|
|
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-07-10 09:26:25 +02:00
|
|
|
end = event.event_id;
|
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
|
|
|
|
2022-07-02 07:45:34 +02:00
|
|
|
const auto event_idx(after.event_idx());
|
|
|
|
counts.after += m::event::append
|
|
|
|
{
|
|
|
|
array, event,
|
|
|
|
{
|
|
|
|
.event_idx = &event_idx,
|
|
|
|
.user_id = &user_room.user.user_id,
|
|
|
|
.user_room = &user_room,
|
|
|
|
.room_depth = &room_depth,
|
|
|
|
.query_txnid = true,
|
|
|
|
}
|
|
|
|
};
|
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)
|
2019-09-24 23:24:16 +02:00
|
|
|
end = m::event_id(after.event_idx());
|
2019-03-10 03:14:59 +01:00
|
|
|
else
|
|
|
|
end = {};
|
2018-05-31 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 09:36:56 +02:00
|
|
|
if(end)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "end", json::value{end}
|
|
|
|
};
|
2018-05-31 12:24:07 +02:00
|
|
|
|
2019-07-08 03:04:22 +02:00
|
|
|
if(include_state)
|
2018-05-31 12:24:07 +02:00
|
|
|
{
|
2019-03-31 09:36:56 +02:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
ret, "state"
|
|
|
|
};
|
|
|
|
|
2018-09-03 11:35:11 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
2019-08-06 02:24:33 +02:00
|
|
|
room
|
2018-09-03 11:35:11 +02:00
|
|
|
};
|
|
|
|
|
2019-07-08 02:45:41 +02: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 12:24:07 +02:00
|
|
|
{
|
2019-07-08 02:45:41 +02:00
|
|
|
// Conditions to decide if we should skip this state event based
|
|
|
|
// on the lazy-loading spec.
|
|
|
|
const bool lazy_loaded
|
2019-03-12 00:02:48 +01:00
|
|
|
{
|
2019-07-08 02:45:41 +02: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-12 00:02:48 +01:00
|
|
|
};
|
|
|
|
|
2019-07-08 02:45:41 +02:00
|
|
|
if(lazy_loaded)
|
|
|
|
return true;
|
|
|
|
|
2020-04-03 04:50:00 +02:00
|
|
|
if(!seek(std::nothrow, event, event_idx))
|
2019-07-08 02:45:41 +02:00
|
|
|
return true;
|
2019-03-12 00:02:48 +01:00
|
|
|
|
2018-09-03 11:24:22 +02:00
|
|
|
if(!visible(event, request.user_id))
|
2019-07-08 02:45:41 +02:00
|
|
|
return true;
|
2018-09-03 11:24:22 +02:00
|
|
|
|
2022-07-02 07:45:34 +02:00
|
|
|
counts.state += m::event::append
|
|
|
|
{
|
|
|
|
array, event,
|
|
|
|
{
|
|
|
|
.event_idx = &event_idx,
|
|
|
|
.user_id = &user_room.user.user_id,
|
|
|
|
.user_room = &user_room,
|
|
|
|
.room_depth = &room_depth,
|
|
|
|
.query_txnid = false,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-08 02:45:41 +02:00
|
|
|
return true;
|
2018-05-31 12:24:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-18 23:12:18 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
context_log, "%s %s in %s before:%zu start:%s after:%zu end:%s state:%zu",
|
2023-02-09 19:12:11 +01:00
|
|
|
loghead(client),
|
2019-04-18 23:12:18 +02:00
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
counts.before,
|
|
|
|
string_view{start},
|
|
|
|
counts.after,
|
|
|
|
string_view{end},
|
|
|
|
counts.state,
|
|
|
|
};
|
|
|
|
|
2022-06-25 22:47:43 +02:00
|
|
|
return response;
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|