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"
|
|
|
|
|
2018-02-23 05:16:12 +01:00
|
|
|
using namespace ircd::m;
|
2018-02-14 21:23:20 +01:00
|
|
|
using namespace ircd;
|
|
|
|
|
2021-02-02 16:11:29 +01:00
|
|
|
static bool
|
|
|
|
append_event(const m::resource::request &request,
|
|
|
|
json::stack::array &,
|
|
|
|
const m::event::idx &event_idx);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-04-22 22:21:55 +02:00
|
|
|
get__state(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-04-22 22:21:55 +02:00
|
|
|
const m::room::state &state);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-03-14 22:25:37 +01:00
|
|
|
put__state(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2018-02-23 05:16:12 +01:00
|
|
|
const room::id &room_id)
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2019-03-14 22:25:37 +01:00
|
|
|
if(request.parv.size() < 3)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"'type' path parameter required."
|
|
|
|
};
|
|
|
|
|
2019-02-18 21:25:00 +01:00
|
|
|
char type_buf[m::event::TYPE_MAX_SIZE];
|
2018-02-14 21:23:20 +01:00
|
|
|
const string_view &type
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(type_buf, request.parv[2])
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2019-02-18 21:25:00 +01:00
|
|
|
char skey_buf[m::event::STATE_KEY_MAX_SIZE];
|
2018-02-14 21:23:20 +01:00
|
|
|
const string_view &state_key
|
|
|
|
{
|
2019-08-18 16:21:27 +02:00
|
|
|
request.parv.size() > 3?
|
|
|
|
url::decode(skey_buf, request.parv[3]):
|
|
|
|
string_view{""} // "" is significant
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
const json::object &content
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2019-03-14 22:25:37 +01:00
|
|
|
request.content
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
const auto event_id
|
|
|
|
{
|
2019-08-26 04:28:06 +02:00
|
|
|
m::send(room_id, request.user_id, type, state_key, content)
|
2019-03-14 22:25:37 +01:00
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2019-03-14 22:25:37 +01:00
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event_id", event_id }
|
|
|
|
}
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|
2018-02-16 21:43:30 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-03-14 22:25:37 +01:00
|
|
|
get__state(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2018-02-23 05:16:12 +01:00
|
|
|
const room::id &room_id)
|
2018-02-16 21:43:30 +01:00
|
|
|
{
|
2019-02-18 21:25:00 +01:00
|
|
|
char type_buf[m::event::TYPE_MAX_SIZE];
|
2018-02-16 21:43:30 +01:00
|
|
|
const string_view &type
|
|
|
|
{
|
2019-08-18 16:21:27 +02:00
|
|
|
request.parv.size() > 2?
|
|
|
|
url::decode(type_buf, request.parv[2]):
|
|
|
|
string_view{}
|
2018-02-16 21:43:30 +01:00
|
|
|
};
|
|
|
|
|
2019-02-18 21:25:00 +01:00
|
|
|
char skey_buf[m::event::STATE_KEY_MAX_SIZE];
|
2018-02-16 21:43:30 +01:00
|
|
|
const string_view &state_key
|
|
|
|
{
|
2019-08-18 16:21:27 +02:00
|
|
|
request.parv.size() > 3?
|
|
|
|
url::decode(skey_buf, request.parv[3]):
|
|
|
|
string_view{}
|
2018-02-16 21:43:30 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
// (non-standard) Allow an event_id to be passed in the query string
|
|
|
|
// for reference framing.
|
|
|
|
m::event::id::buf event_id;
|
|
|
|
if(request.query["event_id"])
|
|
|
|
url::decode(event_id, request.query["event_id"]);
|
2018-02-16 21:43:30 +01:00
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
const m::room room
|
2018-02-16 21:43:30 +01:00
|
|
|
{
|
2019-03-14 22:25:37 +01:00
|
|
|
room_id, event_id
|
2018-02-16 21:43:30 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
if(!exists(room))
|
|
|
|
throw m::NOT_FOUND
|
2018-02-16 21:43:30 +01:00
|
|
|
{
|
2019-03-14 22:25:37 +01:00
|
|
|
"Room %s not found.",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
2018-02-23 05:16:12 +01:00
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!visible(room, request.user_id))
|
2019-03-14 22:25:37 +01:00
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted to view %s",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2019-08-06 02:24:33 +02:00
|
|
|
room
|
2018-02-23 05:16:12 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
if(!type)
|
|
|
|
return get__state(client, request, state);
|
2018-02-23 05:16:12 +01:00
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
const m::event::fetch event
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2019-08-06 02:24:33 +02:00
|
|
|
state.get(type, state_key)
|
2019-03-14 22:25:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!visible(event, request.user_id))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted to view this state in %s",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
2018-02-23 05:16:12 +01:00
|
|
|
|
2021-02-02 16:23:53 +01:00
|
|
|
const json::object content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2021-02-02 16:23:53 +01:00
|
|
|
client, content
|
2018-02-23 05:16:12 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-23 05:16:12 +01:00
|
|
|
get__state(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-03-14 22:25:37 +01:00
|
|
|
const m::room::state &state)
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2019-03-14 22:25:37 +01:00
|
|
|
client, http::OK
|
2018-02-23 05:16:12 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
json::stack out
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2019-03-14 22:25:37 +01:00
|
|
|
response.buf, response.flusher()
|
2018-02-23 05:16:12 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
json::stack::array top
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2019-03-14 22:25:37 +01:00
|
|
|
out
|
2018-02-23 05:16:12 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 22:25:37 +01:00
|
|
|
state.for_each([&request, &top]
|
2021-02-02 16:11:29 +01:00
|
|
|
(const m::event::idx &event_idx)
|
2018-02-23 05:16:12 +01:00
|
|
|
{
|
2021-02-02 16:11:29 +01:00
|
|
|
append_event(request, top, event_idx);
|
2019-03-14 22:25:37 +01:00
|
|
|
});
|
|
|
|
|
2019-06-24 07:09:41 +02:00
|
|
|
return std::move(response);
|
2018-02-23 05:16:12 +01:00
|
|
|
}
|
2021-02-02 16:11:29 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
append_event(const m::resource::request &request,
|
|
|
|
json::stack::array &array,
|
|
|
|
const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
std::nothrow, event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!event.valid))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!visible(event, request.user_id))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m::event::append::opts opts;
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.user_id = &request.user_id;
|
|
|
|
opts.query_redacted = false;
|
|
|
|
opts.query_prev_state = false;
|
|
|
|
opts.query_txnid = false;
|
|
|
|
m::event::append
|
|
|
|
{
|
|
|
|
array, event, opts
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|