2019-08-14 18:28:05 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 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;
|
|
|
|
|
2022-08-18 00:32:49 +02:00
|
|
|
static void
|
|
|
|
relations_chunk_append(client &client,
|
|
|
|
const m::resource::request &request,
|
|
|
|
const m::event::idx &event_idx,
|
2022-08-18 01:16:09 +02:00
|
|
|
m::event::fetch &event,
|
2022-08-18 00:32:49 +02:00
|
|
|
json::stack::array &chunk);
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
static void
|
|
|
|
relations_chunk(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-08-15 07:09:39 +02:00
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
2022-08-18 00:32:49 +02:00
|
|
|
const m::event::idx &event_idx,
|
2019-08-15 07:09:39 +02:00
|
|
|
const string_view &rel_type,
|
|
|
|
json::stack::array &chunk);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-08-14 18:28:05 +02:00
|
|
|
get__relations(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-08-14 18:28:05 +02:00
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
if(!m::exists(room_id))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
"Cannot find relations in %s which is not found.",
|
2019-08-14 18:28:05 +02:00
|
|
|
string_view{room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 3)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"event_id path parameter required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
url::decode(event_id, request.parv[2])
|
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
// Get the rel_type path parameter.
|
|
|
|
// Note: Not requiring path parameter; empty will mean all types.
|
|
|
|
char rel_type_buf[m::event::TYPE_MAX_SIZE];
|
|
|
|
if((false) && request.parv.size() < 4)
|
2019-08-14 18:28:05 +02:00
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
"relation rel_type path parameter required"
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
const string_view &rel_type
|
|
|
|
{
|
|
|
|
url::decode(rel_type_buf, request.parv[3])
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get the alleged type path parameter.
|
|
|
|
// Note: Not requiring this path parameter either; it is not clear yet
|
|
|
|
// what this parameter actually means.
|
|
|
|
char type_buf[m::event::TYPE_MAX_SIZE];
|
|
|
|
if((false) && request.parv.size() < 5)
|
2019-08-14 18:28:05 +02:00
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
"relation ?type? path parameter required"
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
const string_view &type
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
url::decode(type_buf, request.parv[4])
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
2022-08-18 00:32:49 +02:00
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
m::index(event_id)
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
client, http::OK
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
json::stack out
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
response.buf, response.flusher()
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
json::stack::object top
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
out
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array chunk
|
|
|
|
{
|
|
|
|
top, "chunk"
|
|
|
|
};
|
|
|
|
|
2022-08-18 00:32:49 +02:00
|
|
|
relations_chunk
|
|
|
|
(
|
|
|
|
client, request, room_id, event_id, event_idx, rel_type, chunk
|
|
|
|
);
|
|
|
|
|
2022-06-25 22:47:43 +02:00
|
|
|
return response;
|
2019-08-15 07:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
relations_chunk(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-08-15 07:09:39 +02:00
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
2022-08-18 00:32:49 +02:00
|
|
|
const m::event::idx &event_idx,
|
2019-08-15 07:09:39 +02:00
|
|
|
const string_view &rel_type,
|
|
|
|
json::stack::array &chunk)
|
|
|
|
try
|
|
|
|
{
|
2022-08-18 00:32:49 +02:00
|
|
|
const m::relates relates
|
2019-08-15 07:09:39 +02:00
|
|
|
{
|
2022-08-18 00:32:49 +02:00
|
|
|
// Find relations to this event
|
|
|
|
.refs = event_idx,
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2022-08-18 00:32:49 +02:00
|
|
|
// Relations may require the same sender as event_idx.
|
|
|
|
.match_sender = rel_type == "m.replace"
|
2019-08-15 07:09:39 +02:00
|
|
|
};
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2022-08-18 00:32:49 +02:00
|
|
|
m::event::fetch event;
|
2022-08-18 01:16:09 +02:00
|
|
|
relates.rfor_each(rel_type, [&client, &request, &event, &chunk]
|
2022-08-18 00:32:49 +02:00
|
|
|
(const m::event::idx &event_idx, const json::object &, const m::relates_to &)
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2022-08-18 00:32:49 +02:00
|
|
|
relations_chunk_append(client, request, event_idx, event, chunk);
|
|
|
|
});
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2022-08-18 01:16:09 +02:00
|
|
|
relations_chunk_append(client, request, event_idx, event, chunk);
|
2019-08-15 07:09:39 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2022-08-18 00:32:49 +02:00
|
|
|
m::log, "relations in %s for %s rel_type:%s by %s :%s",
|
2019-08-15 07:09:39 +02:00
|
|
|
string_view{room_id},
|
|
|
|
string_view{event_id},
|
|
|
|
rel_type,
|
|
|
|
string_view{request.user_id},
|
|
|
|
e.what(),
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
}
|
2022-08-18 00:32:49 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
relations_chunk_append(client &client,
|
|
|
|
const m::resource::request &request,
|
|
|
|
const m::event::idx &event_idx,
|
2022-08-18 01:16:09 +02:00
|
|
|
m::event::fetch &event,
|
2022-08-18 00:32:49 +02:00
|
|
|
json::stack::array &chunk)
|
|
|
|
|
|
|
|
{
|
2022-08-18 01:16:09 +02:00
|
|
|
if(unlikely(!seek(std::nothrow, event, event_idx)))
|
|
|
|
return;
|
|
|
|
|
2022-08-18 00:32:49 +02:00
|
|
|
if(!visible(event, request.user_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m::event::append
|
|
|
|
{
|
|
|
|
chunk, event,
|
|
|
|
{
|
|
|
|
.event_idx = &event_idx,
|
|
|
|
.user_id = &request.user_id,
|
|
|
|
.query_txnid = false,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|