2018-02-20 02:21:42 +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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client 8.1 :User Directory"
|
|
|
|
};
|
|
|
|
|
|
|
|
resource
|
|
|
|
search_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/user_directory/search",
|
|
|
|
{
|
|
|
|
"(8.1) User directory search",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
post__search(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
2019-06-05 10:12:55 +02:00
|
|
|
const json::string &search_term
|
2018-02-20 02:21:42 +01:00
|
|
|
{
|
2019-06-05 10:12:55 +02:00
|
|
|
request.at("search_term")
|
2018-02-20 02:21:42 +01:00
|
|
|
};
|
|
|
|
|
2018-02-24 04:36:40 +01:00
|
|
|
const ushort &limit
|
2018-02-20 02:21:42 +01:00
|
|
|
{
|
|
|
|
request.get<ushort>("limit", 10)
|
|
|
|
};
|
|
|
|
|
2018-02-24 04:36:40 +01:00
|
|
|
// Search term in this endpoint comes in as-is from Riot. Our query
|
|
|
|
// is a lower_bound of a user_id, so we have to prefix the '@'.
|
2019-06-05 10:12:55 +02:00
|
|
|
char qbuf[256] {'@', '\0'};
|
2018-02-24 04:36:40 +01:00
|
|
|
const string_view &query
|
|
|
|
{
|
|
|
|
!startswith(search_term, '@')?
|
2019-06-05 10:12:55 +02:00
|
|
|
string_view{strlcat{qbuf, search_term}}:
|
|
|
|
string_view{search_term}
|
2018-02-24 04:36:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
bool limited{true};
|
2018-02-20 02:21:42 +01:00
|
|
|
std::vector<json::value> results;
|
2019-06-05 10:12:55 +02:00
|
|
|
const m::room::state &users{m::user::users};
|
2018-09-26 01:02:19 +02:00
|
|
|
users.for_each("ircd.user", query, [&results, &limit, &limited]
|
2018-02-24 04:36:40 +01:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
results.emplace_back(json::members
|
|
|
|
{
|
|
|
|
{ "user_id", at<"state_key"_>(event) },
|
|
|
|
});
|
|
|
|
|
|
|
|
limited = results.size() >= limit;
|
2018-09-26 01:02:19 +02:00
|
|
|
return !limited; // returns true to continue
|
2018-02-24 04:36:40 +01:00
|
|
|
});
|
2018-02-20 02:21:42 +01:00
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "limited", limited },
|
|
|
|
{ "results", { results.data(), results.size() } },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
search_post
|
|
|
|
{
|
|
|
|
search_resource, "POST", post__search,
|
|
|
|
{
|
|
|
|
search_post.REQUIRES_AUTH |
|
|
|
|
search_post.RATE_LIMITED
|
|
|
|
}
|
|
|
|
};
|