2017-08-23 23:51:34 +02:00
|
|
|
/*
|
2017-08-23 23:10:28 +02:00
|
|
|
* Copyright (C) 2017 Charybdis Development Team
|
|
|
|
* Copyright (C) 2017 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-08-23 23:51:34 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
struct room
|
|
|
|
:resource
|
|
|
|
{
|
2017-09-25 03:05:42 +02:00
|
|
|
static constexpr const auto base_url
|
|
|
|
{
|
|
|
|
"_matrix/client/r0/rooms/"
|
|
|
|
};
|
|
|
|
|
2017-08-23 23:51:34 +02:00
|
|
|
using resource::resource;
|
|
|
|
}
|
|
|
|
rooms_resource
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-25 03:05:42 +02:00
|
|
|
room::base_url, resource::opts
|
|
|
|
{
|
|
|
|
resource::DIRECTORY,
|
|
|
|
"Rooms (7.0)"
|
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-08-23 23:51:34 +02:00
|
|
|
mapi::header IRCD_MODULE
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-08-18 20:19:13 +02:00
|
|
|
"registers the resource 'client/rooms'"
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_state(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view ¶ms,
|
2017-09-28 03:31:21 +02:00
|
|
|
const m::event::query<> &query)
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
|
|
|
const auto count
|
|
|
|
{
|
2017-09-28 03:31:21 +02:00
|
|
|
m::events::count(query)
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
if(!count)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"No state."
|
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
size_t j(0);
|
|
|
|
json::value ret[count];
|
2017-09-28 03:31:21 +02:00
|
|
|
m::events::for_each(query, [&count, &j, &ret]
|
2017-09-25 03:05:42 +02:00
|
|
|
(const auto &event)
|
|
|
|
{
|
|
|
|
if(j < count)
|
|
|
|
ret[j++] = event;
|
|
|
|
});
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::value
|
|
|
|
{
|
|
|
|
ret, j
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_state(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view ¶ms,
|
2017-09-28 03:31:21 +02:00
|
|
|
const m::room::id &room_id,
|
2017-09-25 03:05:42 +02:00
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
{
|
2017-09-28 03:31:21 +02:00
|
|
|
const m::event::query<m::event::where::equal> query
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-09-28 03:31:21 +02:00
|
|
|
{ "room_id", room_id },
|
|
|
|
{ "type", type },
|
|
|
|
{ "state_key", state_key },
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2017-09-28 03:31:21 +02:00
|
|
|
return get_state(client, request, params, query);
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_state(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view ¶ms,
|
2017-09-28 03:31:21 +02:00
|
|
|
const m::room::id &room_id,
|
2017-09-25 03:05:42 +02:00
|
|
|
const string_view &type)
|
|
|
|
{
|
2017-09-28 03:31:21 +02:00
|
|
|
const m::event::query<m::event::where::equal> query
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-09-28 03:31:21 +02:00
|
|
|
{ "room_id", room_id },
|
|
|
|
{ "type", type }
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2017-09-28 03:31:21 +02:00
|
|
|
return get_state(client, request, params, query);
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_state(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view ¶ms,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
string_view token[4];
|
|
|
|
tokens(params, "/", token);
|
|
|
|
|
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
token[2]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &state_key
|
|
|
|
{
|
|
|
|
token[3]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type && state_key)
|
2017-09-28 03:31:21 +02:00
|
|
|
return get_state(client, request, params, room_id, type, state_key);
|
2017-09-25 03:05:42 +02:00
|
|
|
|
|
|
|
if(type)
|
2017-09-28 03:31:21 +02:00
|
|
|
return get_state(client, request, params, room_id, type);
|
|
|
|
|
|
|
|
const m::event::query<m::event::where::equal> query
|
|
|
|
{
|
|
|
|
{ "room_id", room_id },
|
|
|
|
{ "state_key", "" },
|
|
|
|
};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2017-09-28 03:31:21 +02:00
|
|
|
return get_state(client, request, params, query);
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_context(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view ¶ms,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
2017-10-01 12:11:54 +02:00
|
|
|
token(params, '/', 2)
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
m::event::find(event_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
2017-10-01 12:11:54 +02:00
|
|
|
throw m::NOT_FOUND{"event not found"};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
|
|
|
const auto event
|
|
|
|
{
|
|
|
|
json::string(*it)
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event", event }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_rooms(client &client, const resource::request &request)
|
|
|
|
{
|
|
|
|
const auto params
|
|
|
|
{
|
|
|
|
lstrip(request.head.path, room::base_url)
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view token[2];
|
2017-10-01 12:11:54 +02:00
|
|
|
if(tokens(params, '/', token) != 2)
|
|
|
|
throw m::BAD_REQUEST{"/rooms command required"};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
m::room::id::buf room_id
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-10-01 12:11:54 +02:00
|
|
|
urldecode(token[0], room_id)
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
const string_view &cmd{token[1]};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
|
|
|
if(cmd == "context")
|
|
|
|
return get_context(client, request, params, room_id);
|
|
|
|
|
|
|
|
if(cmd == "state")
|
|
|
|
return get_state(client, request, params, room_id);
|
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
if(cmd == "members")
|
|
|
|
return get_members(client, request, params, room_id);
|
|
|
|
|
|
|
|
if(cmd == "messages")
|
|
|
|
return get_messages(client, request, params, room_id);
|
|
|
|
|
|
|
|
throw m::NOT_FOUND{"/rooms command not found"};
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource::method method_get
|
|
|
|
{
|
|
|
|
rooms_resource, "GET", get_rooms
|
|
|
|
};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
put_send(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view ¶ms,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
string_view token[4];
|
2017-10-01 12:11:54 +02:00
|
|
|
tokens(params, '/', token);
|
2017-09-26 06:42:07 +02:00
|
|
|
|
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
token[2]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &txnid
|
|
|
|
{
|
|
|
|
token[3]
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov event;
|
|
|
|
|
|
|
|
const json::iov::push _type
|
|
|
|
{
|
|
|
|
event, "type", type
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::push _sender
|
|
|
|
{
|
|
|
|
event, "sender", request.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, "content", json::object{request}
|
|
|
|
};
|
|
|
|
|
|
|
|
m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
|
|
|
room.send(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event_id", event_id }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
put_rooms(client &client, const resource::request &request)
|
|
|
|
{
|
|
|
|
const auto params
|
|
|
|
{
|
|
|
|
lstrip(request.head.path, room::base_url)
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view token[2];
|
|
|
|
if(tokens(params, "/", token) != 2)
|
2017-10-01 12:11:54 +02:00
|
|
|
throw m::BAD_REQUEST{"/rooms command required"};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
token[0]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &cmd
|
|
|
|
{
|
|
|
|
token[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "send")
|
|
|
|
return put_send(client, request, params, room_id);
|
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
throw m::NOT_FOUND{"/rooms command not found"};
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource::method method_put
|
|
|
|
{
|
|
|
|
rooms_resource, "PUT", put_rooms,
|
|
|
|
{
|
|
|
|
method_put.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|