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;
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
get__state(client &client,
|
|
|
|
const resource::request &request,
|
2018-02-23 05:16:12 +01:00
|
|
|
const room::id &room_id,
|
|
|
|
const string_view &event_id);
|
2018-02-14 21:23:20 +01:00
|
|
|
|
|
|
|
static resource::response
|
|
|
|
get__state(client &client,
|
|
|
|
const resource::request &request,
|
2018-02-23 05:16:12 +01:00
|
|
|
const room::id &room_id,
|
2018-02-14 21:23:20 +01:00
|
|
|
const string_view &event_id,
|
2018-02-23 05:16:12 +01:00
|
|
|
const string_view &type);
|
2018-02-14 21:23:20 +01:00
|
|
|
|
|
|
|
static resource::response
|
|
|
|
get__state(client &client,
|
|
|
|
const resource::request &request,
|
2018-02-23 05:16:12 +01:00
|
|
|
const room::id &room_id,
|
2018-02-14 21:23:20 +01:00
|
|
|
const string_view &event_id,
|
|
|
|
const string_view &type,
|
2018-02-23 05:16:12 +01:00
|
|
|
const string_view &state_key);
|
2018-02-14 21:23:20 +01:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__state(client &client,
|
|
|
|
const 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-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
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(skey_buf, request.parv[3])
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// (non-standard) Allow an event_id to be passed in the query string
|
|
|
|
// for reference framing.
|
2019-02-18 21:25:00 +01:00
|
|
|
char evid_buf[m::id::MAX_SIZE];
|
2018-02-14 21:23:20 +01:00
|
|
|
const string_view &event_id
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(evid_buf, request.query["event_id"])
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(type && state_key)
|
|
|
|
return get__state(client, request, room_id, event_id, type, state_key);
|
|
|
|
|
|
|
|
if(type)
|
|
|
|
return get__state(client, request, room_id, event_id, type);
|
|
|
|
|
|
|
|
return get__state(client, request, room_id, event_id);
|
|
|
|
}
|
2018-02-16 21:43:30 +01:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
put__state(client &client,
|
|
|
|
const 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
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(type_buf, request.parv[2])
|
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
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(skey_buf, request.parv[3])
|
2018-02-16 21:43:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
request.content
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
2018-02-23 05:16:12 +01:00
|
|
|
send(room_id, request.user_id, type, state_key, content)
|
2018-02-16 21:43:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event_id", event_id }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-02-23 05:16:12 +01:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__state(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const room::id &room_id,
|
|
|
|
const string_view &event_id)
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room{room_id, event_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<json::value> ret;
|
|
|
|
ret.reserve(32);
|
|
|
|
|
|
|
|
state.for_each([&ret]
|
|
|
|
(const event &event)
|
|
|
|
{
|
|
|
|
ret.emplace_back(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::value
|
|
|
|
{
|
|
|
|
ret.data(), ret.size()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__state(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const room::id &room_id,
|
|
|
|
const string_view &event_id,
|
|
|
|
const string_view &type)
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room{room_id, event_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<json::value> ret;
|
|
|
|
ret.reserve(32);
|
|
|
|
state.for_each(type, [&ret]
|
|
|
|
(const event &event)
|
|
|
|
{
|
|
|
|
ret.emplace_back(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::value
|
|
|
|
{
|
|
|
|
ret.data(), ret.size()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__state(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const room::id &room_id,
|
|
|
|
const string_view &event_id,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room{room_id, event_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::array<json::value, 1> ret;
|
|
|
|
const bool i{state.get(std::nothrow, type, state_key, [&ret]
|
|
|
|
(const event &event)
|
|
|
|
{
|
|
|
|
ret[0] = event;
|
|
|
|
})};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::value
|
|
|
|
{
|
|
|
|
ret.data(), i
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|