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:
142
crates/notedeck_columns/src/multi_subscriber.rs
Normal file
142
crates/notedeck_columns/src/multi_subscriber.rs
Normal file
@@ -0,0 +1,142 @@
|
||||
use enostr::{Filter, RelayPool};
|
||||
use nostrdb::{Ndb, Note, Transaction};
|
||||
use tracing::{debug, error, info};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{filter::UnifiedSubscription, muted::MuteFun, note::NoteRef, Error};
|
||||
|
||||
pub struct MultiSubscriber {
|
||||
filters: Vec<Filter>,
|
||||
sub: Option<UnifiedSubscription>,
|
||||
subscribers: u32,
|
||||
}
|
||||
|
||||
impl MultiSubscriber {
|
||||
pub fn new(filters: Vec<Filter>) -> Self {
|
||||
Self {
|
||||
filters,
|
||||
sub: None,
|
||||
subscribers: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn real_subscribe(
|
||||
ndb: &Ndb,
|
||||
pool: &mut RelayPool,
|
||||
filters: Vec<Filter>,
|
||||
) -> Option<UnifiedSubscription> {
|
||||
let subid = Uuid::new_v4().to_string();
|
||||
let sub = ndb.subscribe(&filters).ok()?;
|
||||
|
||||
pool.subscribe(subid.clone(), filters);
|
||||
|
||||
Some(UnifiedSubscription {
|
||||
local: sub,
|
||||
remote: subid,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn unsubscribe(&mut self, ndb: &Ndb, pool: &mut RelayPool) {
|
||||
if self.subscribers == 0 {
|
||||
error!("No subscribers to unsubscribe from");
|
||||
return;
|
||||
}
|
||||
|
||||
self.subscribers -= 1;
|
||||
if self.subscribers == 0 {
|
||||
let sub = match self.sub {
|
||||
Some(ref sub) => sub,
|
||||
None => {
|
||||
error!("No remote subscription to unsubscribe from");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let local_sub = &sub.local;
|
||||
if let Err(e) = ndb.unsubscribe(*local_sub) {
|
||||
error!(
|
||||
"failed to unsubscribe from object: {e}, subid:{}, {} active subscriptions",
|
||||
local_sub.id(),
|
||||
ndb.subscription_count()
|
||||
);
|
||||
} else {
|
||||
info!(
|
||||
"Unsubscribed from object subid:{}. {} active subscriptions",
|
||||
local_sub.id(),
|
||||
ndb.subscription_count()
|
||||
);
|
||||
}
|
||||
|
||||
// unsub from remote
|
||||
pool.unsubscribe(sub.remote.clone());
|
||||
self.sub = None;
|
||||
} else {
|
||||
info!(
|
||||
"Locally unsubscribing. {} active ndb subscriptions. {} active subscriptions for this object",
|
||||
ndb.subscription_count(),
|
||||
self.subscribers,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribe(&mut self, ndb: &Ndb, pool: &mut RelayPool) {
|
||||
self.subscribers += 1;
|
||||
if self.subscribers == 1 {
|
||||
if self.sub.is_some() {
|
||||
error!("Object is first subscriber, but it already had remote subscription");
|
||||
return;
|
||||
}
|
||||
|
||||
self.sub = Self::real_subscribe(ndb, pool, self.filters.clone());
|
||||
info!(
|
||||
"Remotely subscribing to object. {} total active subscriptions, {} on this object",
|
||||
ndb.subscription_count(),
|
||||
self.subscribers,
|
||||
);
|
||||
|
||||
if self.sub.is_none() {
|
||||
error!("Error subscribing remotely to object");
|
||||
}
|
||||
} else {
|
||||
info!(
|
||||
"Locally subscribing. {} total active subscriptions, {} for this object",
|
||||
ndb.subscription_count(),
|
||||
self.subscribers,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_for_notes(
|
||||
&mut self,
|
||||
ndb: &Ndb,
|
||||
txn: &Transaction,
|
||||
is_muted: &MuteFun,
|
||||
) -> Result<Vec<NoteRef>, Error> {
|
||||
let sub = self.sub.as_ref().ok_or(Error::no_active_sub())?;
|
||||
let new_note_keys = ndb.poll_for_notes(sub.local, 500);
|
||||
|
||||
if new_note_keys.is_empty() {
|
||||
return Ok(vec![]);
|
||||
} else {
|
||||
debug!("{} new notes! {:?}", new_note_keys.len(), new_note_keys);
|
||||
}
|
||||
|
||||
let mut notes: Vec<Note<'_>> = Vec::with_capacity(new_note_keys.len());
|
||||
for key in new_note_keys {
|
||||
let note = if let Ok(note) = ndb.get_note_by_key(txn, key) {
|
||||
note
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if is_muted(¬e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
notes.push(note);
|
||||
}
|
||||
|
||||
let note_refs: Vec<NoteRef> = notes.iter().map(|n| NoteRef::from_note(n)).collect();
|
||||
|
||||
Ok(note_refs)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user