improvement: handle limit and set limited in /user_directory/search

next
timokoesters 2020-07-30 11:12:49 +02:00
parent 18dcf44aa4
commit 52b1df6fd2
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
1 changed files with 33 additions and 30 deletions

View File

@ -2262,39 +2262,42 @@ pub fn search_users_route(
db: State<'_, Database>, db: State<'_, Database>,
body: Ruma<search_users::Request>, body: Ruma<search_users::Request>,
) -> ConduitResult<search_users::Response> { ) -> ConduitResult<search_users::Response> {
Ok(search_users::Response { let limit = if let Some(limit) = body.limit {
results: db u64::from(limit)
.users } else {
.iter() 10
.filter_map(|user_id| { } as usize;
// Filter out buggy users (they should not exist, but you never know...)
let user_id = user_id.ok()?;
if db.users.is_deactivated(&user_id).ok()? {
return None;
}
let user = search_users::User { let mut users = db.users.iter().filter_map(|user_id| {
user_id: user_id.clone(), // Filter out buggy users (they should not exist, but you never know...)
display_name: db.users.displayname(&user_id).ok()?, let user_id = user_id.ok()?;
avatar_url: db.users.avatar_url(&user_id).ok()?, if db.users.is_deactivated(&user_id).ok()? {
}; return None;
}
if !user.user_id.to_string().contains(&body.search_term) let user = search_users::User {
&& user user_id: user_id.clone(),
.display_name display_name: db.users.displayname(&user_id).ok()?,
.as_ref() avatar_url: db.users.avatar_url(&user_id).ok()?,
.filter(|name| name.contains(&body.search_term)) };
.is_none()
{
return None;
}
Some(user) if !user.user_id.to_string().contains(&body.search_term)
}) && user
.collect(), .display_name
limited: false, .as_ref()
} .filter(|name| name.contains(&body.search_term))
.into()) .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())
} }
#[cfg_attr( #[cfg_attr(