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 },
|
|
|
|
};
|
|
|
|
|
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-07-16 22:29:15 +02:00
|
|
|
static bool
|
2019-03-10 04:21:44 +01:00
|
|
|
_append(json::stack::array &,
|
|
|
|
const m::event &,
|
|
|
|
const m::event::idx &,
|
2019-03-14 01:03:01 +01:00
|
|
|
const m::user::room &,
|
2019-04-15 21:16:48 +02:00
|
|
|
const int64_t &,
|
2019-03-14 01:03:01 +01:00
|
|
|
const bool &query_txnid = true);
|
2019-03-10 04:21:44 +01:00
|
|
|
|
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-31 09:34:44 +02:00
|
|
|
url::decode(event_id, request.parv[2])
|
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
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
m::event::append::opts opts;
|
2019-04-23 22:42:50 +02: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-03 01:56:18 +02:00
|
|
|
m::event::append(_event, event, opts);
|
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
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
counts.before += _append(array, event, before.event_idx(), user_room, room_depth);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
counts.after += _append(array, event, after.event_idx(), user_room, room_depth);
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(!seek(event, event_idx, std::nothrow))
|
|
|
|
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
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
counts.state += _append(array, event, event_idx, user_room, room_depth, 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",
|
|
|
|
client.loghead(),
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
counts.before,
|
|
|
|
string_view{start},
|
|
|
|
counts.after,
|
|
|
|
string_view{end},
|
|
|
|
counts.state,
|
|
|
|
};
|
|
|
|
|
2019-06-24 07:09:41 +02:00
|
|
|
return std::move(response);
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|
2019-03-10 04:21:44 +01:00
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
bool
|
2019-03-10 04:21:44 +01:00
|
|
|
_append(json::stack::array &chunk,
|
|
|
|
const m::event &event,
|
|
|
|
const m::event::idx &event_idx,
|
2019-03-14 01:03:01 +01:00
|
|
|
const m::user::room &user_room,
|
2019-04-15 21:16:48 +02:00
|
|
|
const int64_t &room_depth,
|
2019-03-14 01:03:01 +01:00
|
|
|
const bool &query_txnid)
|
2019-03-10 04:21:44 +01:00
|
|
|
{
|
2019-08-03 01:56:18 +02:00
|
|
|
m::event::append::opts opts;
|
2019-03-12 00:02:48 +01:00
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.user_id = &user_room.user.user_id;
|
|
|
|
opts.user_room = &user_room;
|
2019-04-15 21:16:48 +02:00
|
|
|
opts.room_depth = &room_depth;
|
2019-08-03 01:56:18 +02:00
|
|
|
return m::event::append(chunk, event, opts);
|
2019-03-10 04:21:44 +01:00
|
|
|
}
|