Merge pull request 'improvement: handle limit and set limited in /user_directory/search' (#169) from user-dir-limit into master

Reviewed-on: https://git.koesters.xyz/timo/conduit/pulls/169
next
Timo Kösters 2020-07-30 15:19:21 +02:00
commit 100e3d6d6d
1 changed files with 33 additions and 30 deletions

View File

@ -2262,11 +2262,13 @@ pub fn search_users_route(
db: State<'_, Database>,
body: Ruma<search_users::Request>,
) -> ConduitResult<search_users::Response> {
Ok(search_users::Response {
results: db
.users
.iter()
.filter_map(|user_id| {
let limit = if let Some(limit) = body.limit {
u64::from(limit)
} else {
10
} as usize;
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()?;
if db.users.is_deactivated(&user_id).ok()? {
@ -2290,11 +2292,12 @@ pub fn search_users_route(
}
Some(user)
})
.collect(),
limited: false,
}
.into())
});
let results = users.by_ref().take(limit).collect();
let limited = users.next().is_some();
Ok(search_users::Response { results, limited }.into())
}
#[cfg_attr(