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;
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
static void
|
|
|
|
relations_chunk(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
|
|
|
const string_view &rel_type,
|
|
|
|
const string_view &type,
|
|
|
|
json::stack::array &chunk);
|
|
|
|
|
2019-08-14 18:28:05 +02:00
|
|
|
resource::response
|
|
|
|
get__relations(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
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])
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!m::exists(event_id))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Cannot get relations about %s which is not found.",
|
|
|
|
string_view{event_id}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
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"
|
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
relations_chunk(client, request, room_id, event_id, rel_type, type, chunk);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
relations_chunk(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
|
|
|
const string_view &rel_type,
|
|
|
|
const string_view &type,
|
|
|
|
json::stack::array &chunk)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto append{[&chunk, &request]
|
|
|
|
(const auto &event_idx, const m::event &event)
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
m::event::append::opts opts;
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.user_id = &request.user_id;
|
|
|
|
opts.query_txnid = false;
|
|
|
|
m::event::append
|
|
|
|
{
|
|
|
|
chunk, event, opts
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
m::event::idx event_idx
|
|
|
|
{
|
|
|
|
index(event_id, std::nothrow)
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
m::event::fetch event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow
|
|
|
|
};
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
const auto each_ref{[&event, &append, &rel_type, &request]
|
2019-08-14 18:28:05 +02:00
|
|
|
(const m::event::idx &event_idx, const m::dbs::ref &)
|
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
if(!seek(event, event_idx, std::nothrow))
|
2019-08-14 18:28:05 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
const json::object &m_relates_to
|
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
at<"content"_>(event).get("m.relates_to")
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
const json::string &_rel_type
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
|
|
|
m_relates_to.get("rel_type")
|
|
|
|
};
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
if(_rel_type != rel_type)
|
2019-08-14 18:28:05 +02:00
|
|
|
return true;
|
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
if(!visible(event, request.user_id))
|
|
|
|
return true;
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
append(event_idx, event);
|
|
|
|
return true;
|
|
|
|
}};
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
if(!event.valid)
|
|
|
|
return;
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
if(!visible(event, request.user_id))
|
|
|
|
return;
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
// Send the original event
|
|
|
|
append(event_idx, event);
|
2019-08-14 18:28:05 +02:00
|
|
|
|
2019-08-15 07:09:39 +02:00
|
|
|
// Send all the referencees
|
|
|
|
const m::event::refs refs{event_idx};
|
|
|
|
refs.for_each(m::dbs::ref::M_RELATES, each_ref);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
2019-08-14 18:28:05 +02:00
|
|
|
{
|
2019-08-15 07:09:39 +02:00
|
|
|
m::log, "relations in %s for %s rel_type:%s type:%s by %s :%s",
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{event_id},
|
|
|
|
rel_type,
|
|
|
|
type,
|
|
|
|
string_view{request.user_id},
|
|
|
|
e.what(),
|
2019-08-14 18:28:05 +02:00
|
|
|
};
|
|
|
|
}
|