thread.rs (2254B)
1 use crate::{ 2 multi_subscriber::MultiSubscriber, 3 note::NoteRef, 4 notecache::NoteCache, 5 notes_holder::NotesHolder, 6 timeline::{TimelineTab, ViewFilter}, 7 }; 8 use nostrdb::{Filter, FilterBuilder, Ndb, Transaction}; 9 10 #[derive(Default)] 11 pub struct Thread { 12 view: TimelineTab, 13 pub multi_subscriber: Option<MultiSubscriber>, 14 } 15 16 impl Thread { 17 pub fn new(notes: Vec<NoteRef>) -> Self { 18 let mut cap = ((notes.len() as f32) * 1.5) as usize; 19 if cap == 0 { 20 cap = 25; 21 } 22 let mut view = TimelineTab::new_with_capacity(ViewFilter::NotesAndReplies, cap); 23 view.notes = notes; 24 25 Thread { 26 view, 27 multi_subscriber: None, 28 } 29 } 30 31 pub fn view(&self) -> &TimelineTab { 32 &self.view 33 } 34 35 pub fn view_mut(&mut self) -> &mut TimelineTab { 36 &mut self.view 37 } 38 39 fn filters_raw(root: &[u8; 32]) -> Vec<FilterBuilder> { 40 vec![ 41 nostrdb::Filter::new().kinds([1]).event(root), 42 nostrdb::Filter::new().ids([root]).limit(1), 43 ] 44 } 45 46 pub fn filters_since(root: &[u8; 32], since: u64) -> Vec<Filter> { 47 Self::filters_raw(root) 48 .into_iter() 49 .map(|fb| fb.since(since).build()) 50 .collect() 51 } 52 53 pub fn filters(root: &[u8; 32]) -> Vec<Filter> { 54 Self::filters_raw(root) 55 .into_iter() 56 .map(|mut fb| fb.build()) 57 .collect() 58 } 59 } 60 61 impl NotesHolder for Thread { 62 fn get_multi_subscriber(&mut self) -> Option<&mut MultiSubscriber> { 63 self.multi_subscriber.as_mut() 64 } 65 66 fn filters(for_id: &[u8; 32]) -> Vec<Filter> { 67 Thread::filters(for_id) 68 } 69 70 fn new_notes_holder( 71 _: &Transaction, 72 _: &Ndb, 73 _: &mut NoteCache, 74 _: &[u8; 32], 75 _: Vec<Filter>, 76 notes: Vec<NoteRef>, 77 ) -> Self { 78 Thread::new(notes) 79 } 80 81 fn get_view(&mut self) -> &mut TimelineTab { 82 &mut self.view 83 } 84 85 fn filters_since(for_id: &[u8; 32], since: u64) -> Vec<Filter> { 86 Thread::filters_since(for_id, since) 87 } 88 89 fn set_multi_subscriber(&mut self, subscriber: MultiSubscriber) { 90 self.multi_subscriber = Some(subscriber); 91 } 92 }