diff --git a/src/profile.rs b/src/profile.rs index 9da7026d..77ac8c01 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -1,4 +1,13 @@ -use nostrdb::ProfileRecord; +use enostr::Filter; +use nostrdb::{FilterBuilder, ProfileRecord}; + +use crate::{ + filter, + multi_subscriber::MultiSubscriber, + note::NoteRef, + notes_holder::NotesHolder, + timeline::{Timeline, TimelineTab, ViewFilter}, +}; pub enum DisplayName<'a> { One(&'a str), @@ -37,3 +46,59 @@ pub fn get_profile_name<'a>(record: &'a ProfileRecord) -> Option }), } } + +pub struct Profile { + view: TimelineTab, + pub multi_subscriber: Option, +} + +impl Profile { + pub fn new(notes: Vec) -> Self { + let mut cap = ((notes.len() as f32) * 1.5) as usize; + if cap == 0 { + cap = 25; + } + let mut view = TimelineTab::new_with_capacity(ViewFilter::NotesAndReplies, cap); + view.notes = notes; + + Profile { + view, + multi_subscriber: None, + } + } + + fn filters_raw(pk: &[u8; 32]) -> Vec { + vec![Filter::new() + .authors([pk]) + .kinds([1]) + .limit(filter::default_limit())] + } +} + +impl NotesHolder for Profile { + fn get_multi_subscriber(&mut self) -> Option<&mut MultiSubscriber> { + self.multi_subscriber.as_mut() + } + + fn get_view(&mut self) -> &mut crate::timeline::TimelineTab { + &mut self.view + } + + fn filters(for_id: &[u8; 32]) -> Vec { + Profile::filters_raw(for_id) + .into_iter() + .map(|mut f| f.build()) + .collect() + } + + fn filters_since(for_id: &[u8; 32], since: u64) -> Vec { + Profile::filters_raw(for_id) + .into_iter() + .map(|f| f.since(since).build()) + .collect() + } + + fn new_notes_holder(notes: Vec) -> Self { + Profile::new(notes) + } +}