split notedeck into crates
This splits notedeck into crates, separating the browser chrome and individual apps: * notedeck: binary file, browser chrome * notedeck_columns: our columns app * enostr: same as before We still need to do more work to cleanly separate the chrome apis from the app apis. Soon I will create notedeck-notebook to see what makes sense to be shared between the apps. Some obvious ones that come to mind: 1. ImageCache We will likely want to move this to the notedeck crate, as most apps will want some kind of image cache. In web browsers, web pages do not need to worry about this, so we will likely have to do something similar 2. Ndb Since NdbRef is threadsafe and Ndb is an Arc<NdbRef>, it can be safely copied to each app. This will simplify things. In the future we might want to create an abstraction over this? Maybe each app shouldn't have access to the same database... we assume the data in DBs are all public anyways, but if we have unwrapped giftwraps that could be a problem. 3. RelayPool / Subscription Manager The browser should probably maintain these. Then apps can use ken's high level subscription manager api and not have to worry about connection pool details 4. Accounts Accounts and key management should be handled by the chrome. Apps should only have a simple signer interface. That's all for now, just something to think about! Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
132
crates/notedeck_columns/src/profile.rs
Normal file
132
crates/notedeck_columns/src/profile.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
use enostr::{Filter, Pubkey};
|
||||
use nostrdb::{FilterBuilder, Ndb, ProfileRecord, Transaction};
|
||||
|
||||
use crate::{
|
||||
filter::{self, FilterState},
|
||||
multi_subscriber::MultiSubscriber,
|
||||
muted::MuteFun,
|
||||
note::NoteRef,
|
||||
notecache::NoteCache,
|
||||
notes_holder::NotesHolder,
|
||||
timeline::{copy_notes_into_timeline, PubkeySource, Timeline, TimelineKind},
|
||||
};
|
||||
|
||||
pub enum DisplayName<'a> {
|
||||
One(&'a str),
|
||||
|
||||
Both {
|
||||
username: &'a str,
|
||||
display_name: &'a str,
|
||||
},
|
||||
}
|
||||
|
||||
impl<'a> DisplayName<'a> {
|
||||
pub fn username(&self) -> &'a str {
|
||||
match self {
|
||||
Self::One(n) => n,
|
||||
Self::Both { username, .. } => username,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(s: &str) -> bool {
|
||||
s.chars().all(|c| c.is_whitespace())
|
||||
}
|
||||
|
||||
pub fn get_profile_name<'a>(record: &ProfileRecord<'a>) -> Option<DisplayName<'a>> {
|
||||
let profile = record.record().profile()?;
|
||||
let display_name = profile.display_name().filter(|n| !is_empty(n));
|
||||
let name = profile.name().filter(|n| !is_empty(n));
|
||||
|
||||
match (display_name, name) {
|
||||
(None, None) => None,
|
||||
(Some(disp), None) => Some(DisplayName::One(disp)),
|
||||
(None, Some(username)) => Some(DisplayName::One(username)),
|
||||
(Some(display_name), Some(username)) => Some(DisplayName::Both {
|
||||
display_name,
|
||||
username,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Profile {
|
||||
pub timeline: Timeline,
|
||||
pub multi_subscriber: Option<MultiSubscriber>,
|
||||
}
|
||||
|
||||
impl Profile {
|
||||
pub fn new(
|
||||
txn: &Transaction,
|
||||
ndb: &Ndb,
|
||||
note_cache: &mut NoteCache,
|
||||
source: PubkeySource,
|
||||
filters: Vec<Filter>,
|
||||
notes: Vec<NoteRef>,
|
||||
is_muted: &MuteFun,
|
||||
) -> Self {
|
||||
let mut timeline =
|
||||
Timeline::new(TimelineKind::profile(source), FilterState::ready(filters));
|
||||
|
||||
copy_notes_into_timeline(&mut timeline, txn, ndb, note_cache, notes, is_muted);
|
||||
|
||||
Profile {
|
||||
timeline,
|
||||
multi_subscriber: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn filters_raw(pk: &[u8; 32]) -> Vec<FilterBuilder> {
|
||||
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 {
|
||||
self.timeline.current_view_mut()
|
||||
}
|
||||
|
||||
fn filters(for_id: &[u8; 32]) -> Vec<enostr::Filter> {
|
||||
Profile::filters_raw(for_id)
|
||||
.into_iter()
|
||||
.map(|mut f| f.build())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn filters_since(for_id: &[u8; 32], since: u64) -> Vec<enostr::Filter> {
|
||||
Profile::filters_raw(for_id)
|
||||
.into_iter()
|
||||
.map(|f| f.since(since).build())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn new_notes_holder(
|
||||
txn: &Transaction,
|
||||
ndb: &Ndb,
|
||||
note_cache: &mut NoteCache,
|
||||
id: &[u8; 32],
|
||||
filters: Vec<Filter>,
|
||||
notes: Vec<NoteRef>,
|
||||
is_muted: &MuteFun,
|
||||
) -> Self {
|
||||
Profile::new(
|
||||
txn,
|
||||
ndb,
|
||||
note_cache,
|
||||
PubkeySource::Explicit(Pubkey::new(*id)),
|
||||
filters,
|
||||
notes,
|
||||
is_muted,
|
||||
)
|
||||
}
|
||||
|
||||
fn set_multi_subscriber(&mut self, subscriber: MultiSubscriber) {
|
||||
self.multi_subscriber = Some(subscriber);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user