refine Chat::Dialogue.post_find()

This commit is contained in:
nworbnhoj 2023-08-11 06:40:37 +10:00
parent dc8102c627
commit 0ef1d79725

View file

@ -95,33 +95,20 @@ impl Dialogue {
pub fn post_find(&self, author: &str, timestamp: u64) -> Option<usize> {
if let Some(author_id) = self.author_lookup.get(author) {
match self
.posts
.binary_search_by(|x| x.timestamp().cmp(&timestamp))
{
Ok(index) => {
// matched timestamp but maybe duplicates by different authors
for (id, post) in self.posts[..index].iter().rev().enumerate() {
if post.timestamp() != timestamp {
break;
}
let i = self.posts.partition_point(|p| p.timestamp() < timestamp);
let last = self.posts.len()-1;
for n in i..last {
if let Some(post) = self.posts.get(n) {
if post.timestamp() == timestamp {
if post.author_id() == *author_id {
return Some(id);
}
return Some(n);
}
} else {
break;
}
for (id, post) in self.posts[index..].iter().enumerate() {
if post.timestamp() != timestamp {
break;
}
if post.author_id() == *author_id {
return Some(id);
}
}
return None;
}
Err(_) => None::<usize>,
};
};
}
}
None
}