From c3fc4e09e80e87b14f7401d6b8570d51f7399bab Mon Sep 17 00:00:00 2001 From: William Casarin Date: Wed, 31 Jul 2024 13:25:04 -0700 Subject: [PATCH] thread: fix ordering and duplication bugs Signed-off-by: William Casarin --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/filter.rs | 18 ++++++++--------- src/thread.rs | 50 ++++++++++++++++-------------------------------- src/ui/thread.rs | 3 ++- 5 files changed, 30 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a31a146..f7d3f91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2296,7 +2296,7 @@ dependencies = [ [[package]] name = "nostrdb" version = "0.3.4" -source = "git+https://github.com/damus-io/nostrdb-rs?branch=threads#27e7c19c8941fe996490a82512fd2660e5da1900" +source = "git+https://github.com/damus-io/nostrdb-rs?rev=86ff69438221932a1b6d26a349b9c65c80d51989#86ff69438221932a1b6d26a349b9c65c80d51989" dependencies = [ "bindgen", "cc", diff --git a/Cargo.toml b/Cargo.toml index f51873b..1eda2b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ serde_json = "1.0.89" env_logger = "0.10.0" puffin_egui = { version = "0.27.0", optional = true } puffin = { version = "0.19.0", optional = true } -nostrdb = { git = "https://github.com/damus-io/nostrdb-rs", branch = "threads" } +nostrdb = { git = "https://github.com/damus-io/nostrdb-rs", rev = "86ff69438221932a1b6d26a349b9c65c80d51989" } #nostrdb = { path = "/Users/jb55/dev/github/damus-io/nostrdb-rs" } #nostrdb = "0.3.4" hex = "0.4.3" diff --git a/src/filter.rs b/src/filter.rs index 8679f50..b9be13f 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -29,43 +29,43 @@ pub fn convert_enostr_filter(filter: &enostr::Filter) -> nostrdb::Filter { let mut nfilter = nostrdb::Filter::new(); if let Some(ref ids) = filter.ids { - nfilter.ids(ids.iter().map(|a| *a.bytes()).collect()); + nfilter = nfilter.ids(ids.iter().map(|a| *a.bytes()).collect()); } if let Some(ref authors) = filter.authors { let authors: Vec<[u8; 32]> = authors.iter().map(|a| *a.bytes()).collect(); - nfilter.authors(authors); + nfilter = nfilter.authors(authors); } if let Some(ref kinds) = filter.kinds { - nfilter.kinds(kinds.clone()); + nfilter = nfilter.kinds(kinds.clone()); } // #e if let Some(ref events) = filter.events { - nfilter.events(events.iter().map(|a| *a.bytes()).collect()); + nfilter = nfilter.events(events.iter().map(|a| *a.bytes()).collect()); } // #p if let Some(ref pubkeys) = filter.pubkeys { - nfilter.pubkeys(pubkeys.iter().map(|a| *a.bytes()).collect()); + nfilter = nfilter.pubkeys(pubkeys.iter().map(|a| *a.bytes()).collect()); } // #t if let Some(ref hashtags) = filter.hashtags { - nfilter.tags(hashtags.clone(), 't'); + nfilter = nfilter.tags(hashtags.clone(), 't'); } if let Some(since) = filter.since { - nfilter.since(since); + nfilter = nfilter.since(since); } if let Some(until) = filter.until { - nfilter.until(until); + nfilter = nfilter.until(until); } if let Some(limit) = filter.limit { - nfilter.limit(limit.into()); + nfilter = nfilter.limit(limit.into()); } nfilter.build() diff --git a/src/thread.rs b/src/thread.rs index 17c9371..a43180f 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -1,7 +1,7 @@ use crate::note::NoteRef; use crate::timeline::{TimelineTab, ViewFilter}; use crate::Error; -use nostrdb::{Filter, Ndb, Subscription, Transaction}; +use nostrdb::{Filter, FilterBuilder, Ndb, Subscription, Transaction}; use std::cmp::Ordering; use std::collections::HashMap; use tracing::debug; @@ -49,7 +49,7 @@ impl Thread { } let last_note = notes[0]; - let filters = Thread::filters_since(root_id, last_note.created_at - 60); + let filters = Thread::filters_since(root_id, last_note.created_at + 1); if let Ok(results) = ndb.query(txn, filters, 1000) { debug!("got {} results from thread update", results.len()); @@ -64,7 +64,6 @@ impl Thread { } pub fn decrement_sub(&mut self) -> Result { - debug!("decrementing sub {:?}", self.subscription().map(|s| s.id)); self.subscribers -= 1; match self.subscribers.cmp(&0) { @@ -88,29 +87,25 @@ impl Thread { &mut self.sub } - pub fn filters_since(root: &[u8; 32], since: u64) -> Vec { + fn filters_raw(root: &[u8; 32]) -> Vec { vec![ - nostrdb::Filter::new() - .since(since) - .kinds(vec![1]) - .event(root) - .build(), - nostrdb::Filter::new() - .kinds(vec![1]) - .ids(vec![*root]) - .since(since) - .build(), + nostrdb::Filter::new().kinds(vec![1]).event(root), + nostrdb::Filter::new().ids(vec![*root]).limit(1), ] } + pub fn filters_since(root: &[u8; 32], since: u64) -> Vec { + Self::filters_raw(root) + .into_iter() + .map(|fb| fb.since(since).build()) + .collect() + } + pub fn filters(root: &[u8; 32]) -> Vec { - vec![ - nostrdb::Filter::new().kinds(vec![1]).event(root).build(), - nostrdb::Filter::new() - .kinds(vec![1]) - .ids(vec![*root]) - .build(), - ] + Self::filters_raw(root) + .into_iter() + .map(|mut fb| fb.build()) + .collect() } } @@ -162,17 +157,6 @@ impl Threads { return ThreadResult::Stale(self.root_id_to_thread.get_mut(root_id).unwrap()); } - // looks like we don't have this thread yet, populate it - // TODO: should we do this in the caller? - let root = if let Ok(root) = ndb.get_note_by_id(txn, root_id) { - root - } else { - debug!("couldnt find root note root_id:{}", hex::encode(root_id)); - self.root_id_to_thread - .insert(root_id.to_owned(), Thread::new(vec![])); - return ThreadResult::Fresh(self.root_id_to_thread.get_mut(root_id).unwrap()); - }; - // we don't have the thread, query for it! let filters = Thread::filters(root_id); @@ -184,7 +168,7 @@ impl Threads { } else { debug!( "got no results from thread lookup for {}", - hex::encode(root.id()) + hex::encode(root_id) ); vec![] }; diff --git a/src/ui/thread.rs b/src/ui/thread.rs index 7ed4b89..7ea0d56 100644 --- a/src/ui/thread.rs +++ b/src/ui/thread.rs @@ -96,13 +96,14 @@ impl<'a> ThreadView<'a> { ui.spacing_mut().item_spacing.y = 0.0; ui.spacing_mut().item_spacing.x = 4.0; + let ind = len - 1 - start_index; let note_key = { let thread = self .app .threads .thread_mut(&self.app.ndb, &txn, root_id) .get_ptr(); - thread.view.notes[start_index].key + thread.view.notes[ind].key }; let note = if let Ok(note) = self.app.ndb.get_note_by_key(&txn, note_key) {