2018-02-04 03:22:01 +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.
|
2017-08-23 23:10:28 +02:00
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
resource user_resource
|
|
|
|
{
|
2017-10-12 05:52:33 +02:00
|
|
|
"/_matrix/client/r0/user/",
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-12-12 21:26:39 +01:00
|
|
|
"User resource",
|
|
|
|
resource::DIRECTORY,
|
2017-08-23 23:10:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
2018-02-12 00:33:10 +01:00
|
|
|
get_filter(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::user::id &user_id)
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-02-12 04:45:31 +01:00
|
|
|
char filter_id_buf[64];
|
|
|
|
const auto filter_id
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2018-02-12 04:45:31 +01:00
|
|
|
url::decode(request.parv[2], filter_id_buf)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-02-12 04:45:31 +01:00
|
|
|
m::filter::get(user_id, filter_id, [&client]
|
|
|
|
(const json::object &filter)
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2018-02-12 04:45:31 +01:00
|
|
|
resource::response
|
|
|
|
{
|
|
|
|
client, filter
|
|
|
|
};
|
|
|
|
});
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-02-12 04:45:31 +01:00
|
|
|
// Responded from closure.
|
|
|
|
return {};
|
2017-08-23 23:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// (5.2) Uploads a new filter definition to the homeserver. Returns a filter ID that
|
|
|
|
// may be used in future requests to restrict which events are returned to the client.
|
|
|
|
resource::response
|
2018-02-12 00:33:10 +01:00
|
|
|
post_filter(client &client,
|
|
|
|
const resource::request::object<const m::filter> &request,
|
|
|
|
const m::user::id &user_id)
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-10-01 12:11:54 +02:00
|
|
|
// (5.2) Required. The id of the user uploading the filter. The access
|
|
|
|
// token must be authorized to make requests for this user id.
|
2017-12-12 21:33:14 +01:00
|
|
|
if(user_id != request.user_id)
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"Trying to post a filter for `%s' but you are `%s'",
|
|
|
|
user_id,
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
// (5.2) List of event fields to include. If this list is absent then all fields are
|
|
|
|
// included. The entries may include '.' charaters to indicate sub-fields. So
|
|
|
|
// ['content.body'] will include the 'body' field of the 'content' object. A literal '.'
|
|
|
|
// character in a field name may be escaped using a '\'. A server may include more
|
|
|
|
// fields than were requested.
|
2017-09-25 03:05:42 +02:00
|
|
|
const auto &event_fields
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"event_fields"_>(request)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
// (5.2) The format to use for events. 'client' will return the events in a format suitable
|
|
|
|
// for clients. 'federation' will return the raw event as receieved over federation.
|
|
|
|
// The default is 'client'. One of: ["client", "federation"]
|
2017-09-25 03:05:42 +02:00
|
|
|
const auto &event_format
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"event_format"_>(request)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
// (5.2) The user account data that isn't associated with rooms to include.
|
2017-09-25 03:05:42 +02:00
|
|
|
const auto &account_data
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"account_data"_>(request)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-10-01 12:11:54 +02:00
|
|
|
// (5.2) Filters to be applied to room data.
|
2017-09-25 03:05:42 +02:00
|
|
|
const auto &room
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"room"_>(request)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
const auto &state
|
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"state"_>(room)
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &presence
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
|
|
|
// (5.2) The presence updates to include.
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"presence"_>(request)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2018-02-12 04:45:31 +01:00
|
|
|
char filter_id_buf[64];
|
2017-09-25 03:05:42 +02:00
|
|
|
const auto filter_id
|
|
|
|
{
|
2018-02-12 04:45:31 +01:00
|
|
|
m::filter::set(filter_id_buf, user_id, request.body)
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
2017-08-23 23:10:28 +02:00
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
2017-09-08 17:15:14 +02:00
|
|
|
client, http::CREATED,
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-25 03:05:42 +02:00
|
|
|
{ "filter_id", filter_id }
|
2017-08-23 23:10:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-12 00:33:10 +01:00
|
|
|
resource::response
|
|
|
|
put_account_data(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
std::cout << "put account data: " << user_id << " " << request.content << std::endl;
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_user(client &client, const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::MULTIPLE_CHOICES, "M_NOT_FOUND", "user id required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &cmd
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "filter")
|
|
|
|
return get_filter(client, request, user_id);
|
|
|
|
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"/user command not found"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method get_method
|
|
|
|
{
|
|
|
|
user_resource, "GET", get_user,
|
|
|
|
{
|
|
|
|
get_method.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
post_user(client &client, resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::MULTIPLE_CHOICES, "M_NOT_FOUND", "user id required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &cmd
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "filter")
|
|
|
|
return post_filter(client, request, user_id);
|
|
|
|
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"/user command not found"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-12 05:52:33 +02:00
|
|
|
resource::method post_method
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-02-12 00:33:10 +01:00
|
|
|
user_resource, "POST", post_user,
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-10-12 05:52:33 +02:00
|
|
|
post_method.REQUIRES_AUTH
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2018-02-12 00:33:10 +01:00
|
|
|
resource::response
|
|
|
|
put_user(client &client, const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::MULTIPLE_CHOICES, "M_NOT_FOUND", "user id required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &cmd
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "account_data")
|
|
|
|
return put_account_data(client, request, user_id);
|
|
|
|
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"/user command not found"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method put_method
|
|
|
|
{
|
|
|
|
user_resource, "PUT", put_user,
|
|
|
|
{
|
|
|
|
put_method.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
mapi::header IRCD_MODULE
|
|
|
|
{
|
|
|
|
"registers the resource 'client/user' to handle requests"
|
|
|
|
};
|