conduit/src/client_server/user_directory.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

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;
#[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))]
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()?,
};
if !user
.user_id
.to_string()
.to_lowercase()
.contains(&body.search_term.to_lowercase())
2020-07-30 16:14:47 +00:00
&& user
.display_name
.as_ref()
.filter(|name| {
name.to_lowercase()
.contains(&body.search_term.to_lowercase())
})
2020-07-30 16:14:47 +00:00
.is_none()
{
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())
}