Skip to content

Commit

Permalink
Fix user search
Browse files Browse the repository at this point in the history
  • Loading branch information
amirRamirfatahi committed Jan 22, 2025
1 parent 70f9408 commit ebc8538
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
18 changes: 10 additions & 8 deletions src/db/kv/index/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,22 +358,24 @@ pub async fn get_multiple<T: DeserializeOwned + Send + Sync>(
let results: Vec<Option<T>> = if indexed_values.is_empty() {
(0..keys.len()).map(|_| None).collect()
} else {
deserialize_values(indexed_values)
deserialize_values(indexed_values)?
};

Ok(results)
}

// Helper function to deserialize JSON strings to Vec<Option<T>>
fn deserialize_values<T: DeserializeOwned>(values: Vec<Option<String>>) -> Vec<Option<T>> {
fn deserialize_values<T: DeserializeOwned>(
values: Vec<Option<String>>,
) -> Result<Vec<Option<T>>, DynError> {
values
.into_iter()
.map(|opt| {
opt.and_then(|value_str| {
serde_json::from_str::<Vec<T>>(&value_str)
.ok()
.and_then(|vec| vec.into_iter().next())
})
.map(|value_str| match value_str {
Some(value) => {
let value: Vec<T> = serde_json::from_str(&value)?;
Ok(value.into_iter().next())
}
None => Ok(None),
})
.collect()
}
Expand Down
5 changes: 4 additions & 1 deletion src/models/user/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ where
.map_err(serde::de::Error::custom)?;
Ok(Some(urls))
}
_ => Err(serde::de::Error::custom("Expected a string or an array")),
serde_json::Value::Null => Ok(None),
_ => Err(serde::de::Error::custom(
"Expected either a string, an array or null",
)),
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/models/user/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ impl UserSearch {
return Ok(());
}
let mut records_to_delete: Vec<String> = Vec::with_capacity(user_ids.len());
let users = UserDetails::get_by_ids(user_ids)
let keys = user_ids
.iter()
.map(|&id| vec![id])
.collect::<Vec<Vec<&str>>>();
let users = UserDetails::get_from_index(keys.iter().map(|item| item.as_slice()).collect())
.await?
.into_iter()
.flatten()
Expand Down Expand Up @@ -171,7 +175,7 @@ mod tests {
.put_index_json(vec![user_id].as_slice(), None)
.await?;

// Call `put_to_index` with the same `UserDetails` object multiple times
// Call `put_to_index` with the same `UserDetails` object
UserSearch::put_to_index(&[&user_details]).await?;

// Check that the index contains only one record for the user
Expand Down

0 comments on commit ebc8538

Please sign in to comment.