2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, ConduitResult, Ruma};
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::api::client::r0::user_directory::search_users;
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::post;
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `POST /_matrix/client/r0/user_directory/search`
|
|
|
|
///
|
|
|
|
/// Searches all known users for a match.
|
|
|
|
///
|
|
|
|
/// - TODO: Hide users that are not in any public rooms?
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/user_directory/search", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn search_users_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<search_users::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<search_users::Response> {
|
2020-08-12 21:32:39 +00:00
|
|
|
let limit = u64::from(body.limit) as usize;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let mut users = db.users.iter().filter_map(|user_id| {
|
|
|
|
// Filter out buggy users (they should not exist, but you never know...)
|
|
|
|
let user_id = user_id.ok()?;
|
|
|
|
|
|
|
|
let user = search_users::User {
|
|
|
|
user_id: user_id.clone(),
|
|
|
|
display_name: db.users.displayname(&user_id).ok()?,
|
|
|
|
avatar_url: db.users.avatar_url(&user_id).ok()?,
|
|
|
|
};
|
|
|
|
|
2021-07-14 10:31:38 +00:00
|
|
|
let user_id_matches = user
|
2021-06-12 16:40:33 +00:00
|
|
|
.user_id
|
|
|
|
.to_string()
|
|
|
|
.to_lowercase()
|
2021-07-14 10:31:38 +00:00
|
|
|
.contains(&body.search_term.to_lowercase());
|
|
|
|
|
|
|
|
let user_displayname_matches = user
|
|
|
|
.display_name
|
|
|
|
.as_ref()
|
|
|
|
.filter(|name| {
|
|
|
|
name.to_lowercase()
|
|
|
|
.contains(&body.search_term.to_lowercase())
|
|
|
|
})
|
|
|
|
.is_some();
|
|
|
|
|
|
|
|
if !user_id_matches && !user_displayname_matches {
|
2020-07-30 16:14:47 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(user)
|
|
|
|
});
|
|
|
|
|
|
|
|
let results = users.by_ref().take(limit).collect();
|
|
|
|
let limited = users.next().is_some();
|
|
|
|
|
|
|
|
Ok(search_users::Response { results, limited }.into())
|
|
|
|
}
|