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