-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add post_replies source in user stream #239
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR looks good. There is a few things we can improve.
This PR still needs at least 1 more test and it would be very nice if we also add a benchmark for this new stream of users.
src/models/user/stream.rs
Outdated
let post_id = post_id.unwrap(); | ||
let author_id = author_id.unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs proper handling instead of unwrap()
let post_id = post_id.ok_or_else(|| {
anyhow!("Post ID should be provided for user streams with source 'post_replies'")
})?;
let author_id = author_id.ok_or_else(|| {
anyhow!("Author ID should be provided for user streams with source 'post_replies'")
})?;
src/models/user/stream.rs
Outdated
let unique_user_ids: HashSet<String> = replies | ||
.map(|replies| { | ||
replies | ||
.into_iter() | ||
.map(|reply| reply.0.split(":").next().unwrap().to_string()) | ||
.collect::<Vec<String>>() | ||
}) | ||
.into_iter() | ||
.flatten() | ||
.collect(); | ||
Some(unique_user_ids.into_iter().collect()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid unwrap()
Untested, maybe something like.
let unique_user_ids: HashSet<String> = replies
.map(|replies_batch| {
replies_batch
.into_iter()
.filter_map(|(raw_id, _some_other_data)| {
// Split on ":" and take the first piece; avoid `unwrap()`
raw_id.split(':').next().map(ToString::to_string)
})
.collect::<Vec<String>>()
})
.flatten()
.collect();
src/models/user/stream.rs
Outdated
UserStreamSource::PostReplies => { | ||
let post_id = post_id.unwrap(); | ||
let author_id = author_id.unwrap(); | ||
let key_parts = [ | ||
&POST_REPLIES_PER_POST_KEY_PARTS[..], | ||
&[author_id.as_str(), post_id.as_str()], | ||
] | ||
.concat(); | ||
let replies = PostStream::try_from_index_sorted_set( | ||
&key_parts, | ||
None, | ||
None, | ||
None, | ||
None, | ||
SortOrder::Descending, | ||
None, | ||
) | ||
.await?; | ||
let unique_user_ids: HashSet<String> = replies | ||
.map(|replies| { | ||
replies | ||
.into_iter() | ||
.map(|reply| reply.0.split(":").next().unwrap().to_string()) | ||
.collect::<Vec<String>>() | ||
}) | ||
.into_iter() | ||
.flatten() | ||
.collect(); | ||
Some(unique_user_ids.into_iter().collect()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too much logic inside of this arm of the match statement.
We should do the same as for the UserStreamSource::Recommended
arm. Move the logic to a new get_post_replies_ids(post_id: &str, author_id: &str) -> Result<Option<Vec<String>>, DynError>
function.
Please use the keyword |
47d5dcb
to
17d5240
Compare
Is this PR ready for review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is basically ready to merge I only left a few minor NIT comments that I think are worth adressing.
PostReplies, | ||
} | ||
|
||
pub struct UserStreamInput { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: IMO we are creating this Struct only to get around clippy
warning but we are not improving clarity nor saving on verbosity. On the contrary, we are constructing and deconstructing this object immediately only so the number of args in the function is no more than 7. Offering alternatives in the next comment...
depth: Option<u8>, | ||
) -> Result<Option<Self>, DynError> { | ||
let user_ids = Self::get_user_list_from_source(user_id, source, skip, limit).await?; | ||
pub async fn get_by_id(input: UserStreamInput) -> Result<Option<Self>, DynError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT part 2: I propose 2 solutions that I think are a bit more elegant than these adhoc structs . We can either:
#[allow(clippy::too_many_arguments)]
on top of this function.- Reuse our
Pagination
type soskip
andlimit
become a single argument.
.map(|reply| { | ||
reply | ||
.0 | ||
.split(":") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure on this one. I think reply.0.split(":").map(ToString::to_string).collect::<Vec<String>>()[0].clone()
could potentially panic if the reply string does not contain a colon (I know, it should contain it...). But instead, we could consider using iterator methods like .next()
with proper error handling.
.into_iter() | ||
.flatten() | ||
.collect(); | ||
Ok(Some(unique_user_ids.into_iter().collect())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: There is a lot going on in the 2 last statements of this function. Maybe we can divide them into more lines for clarity.
NIT: It might be nice to document (small comment right here //
) that we are using HashSet to enforce uniqueness, but the order of the repliers is not guaranteed. We could also sort the vector before returning (not worth it IMO, no requirement for ordering AFAIK).
let unique_user_ids: HashSet<String> = replies | ||
.map(|replies| { | ||
replies | ||
.into_iter() | ||
.map(|reply| { | ||
reply | ||
.0 | ||
.split(":") | ||
.map(ToString::to_string) | ||
.collect::<Vec<String>>()[0] | ||
.clone() | ||
}) | ||
.collect::<Vec<String>>() | ||
}) | ||
.into_iter() | ||
.flatten() | ||
.collect(); | ||
Ok(Some(unique_user_ids.into_iter().collect())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like this is clearer and panic safe.
let unique_user_ids: HashSet<String> = replies | |
.map(|replies| { | |
replies | |
.into_iter() | |
.map(|reply| { | |
reply | |
.0 | |
.split(":") | |
.map(ToString::to_string) | |
.collect::<Vec<String>>()[0] | |
.clone() | |
}) | |
.collect::<Vec<String>>() | |
}) | |
.into_iter() | |
.flatten() | |
.collect(); | |
Ok(Some(unique_user_ids.into_iter().collect())) | |
// If there are replies, extract unique user IDs using a HashSet. | |
let unique_user_ids: HashSet<String> = if let Some(replies) = replies { | |
replies | |
.into_iter() | |
.filter_map(|reply| reply.0.split(':').next().map(|s| s.to_string())) | |
.collect() | |
} else { | |
// If no replies are found, return None. | |
return Ok(None); | |
}; | |
// Convert the HashSet to a Vec. (Note: the ordering will be arbitrary.) | |
Ok(Some(unique_user_ids.into_iter().collect())) |
This PR fixes #228
Pre-submission Checklist
cargo test
.cargo bench