Skip to content

Commit

Permalink
WIP: rough subscription manager API proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
ksedgwic committed Dec 9, 2024
1 parent 35138cd commit 6edfa9c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod profile;
pub mod relay_pool_manager;
mod result;
mod route;
mod submgr;
mod subscriptions;
mod support;
mod test_data;
Expand Down
107 changes: 107 additions & 0 deletions src/submgr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#![allow(unused)]

use std::collections::HashMap;

use enostr::Filter;
use nostrdb;

/// The Subscription Manager
///
/// NOTE - This interface wishes it was called Subscriptions but there
/// already is one. Using a lame (but short) placeholder name instead
/// for now ...
///
/// ```
/// use crate::SubMgr;
///
/// let mut submgr = SubMgr::new();
///
/// let filter = Filter::new().kinds(vec![1, 2, 3]).build();
/// let ep = submgr.subscribe(SubSpecBuilder::new(vec![filter]).build())?;
/// loop {
/// match ep.next().await {
/// Ok(nks) => {
/// // process the note keys
/// },
/// Err(ReevaluateState) => {
/// // not really an error, break out of loop and reevaluate state
/// },
/// Err(err) => {
/// // something bad happened
/// },
/// }
/// }
/// submgr.unsubscribe(ep)?;
/// ```
pub enum SubError {
ReevaluateState,
InternalError(String),
NdbError(nostrdb::Error),
}

pub type SubResult<T> = Result<T, SubError>;

pub struct SubId(nostrdb::Subscription);

pub enum SubConstraint {
OneShot,
AllowedRelays(Vec<String>),
ProhibitedRelays(Vec<String>),
// other constraints as we think of them ...
}

pub struct SubSpecBuilder {
rmtid: Option<String>,
filters: Vec<Filter>,
allowed_relays: Vec<String>,
prohibited_relays: Vec<String>,
is_oneshot: bool,
}

impl SubSpecBuilder {
pub fn new(filters: Vec<Filter>) -> Self {
unimplemented!();
}
pub fn rmtid(mut self, id: String) -> Self {
unimplemented!();
}
// ... more here ...
pub fn build(self) -> SubSpec {
unimplemented!();
}
}

pub struct SubSpec {
rmtid: String,
filters: Vec<Filter>,
constraints: Vec<SubConstraint>,
allowed_relays: Vec<String>,
prohibited_relays: Vec<String>,
is_oneshot: bool,
}

pub struct SubMgr {
subs: HashMap<SubId, (SubSpec, SubEndpoint)>,
}

impl SubMgr {
pub fn subscribe(&mut self, sub: SubSpec) -> SubResult<SubEndpoint> {
unimplemented!();
}

pub fn unsubscribe(&mut self, ep: SubEndpoint) -> SubResult<()> {
unimplemented!();
}
}

pub struct SubEndpoint {
id: SubId,
// internals omitted ...
}

impl SubEndpoint {
pub async fn next(&self) -> SubResult<Vec<nostrdb::NoteKey>> {
unimplemented!();
}
}

0 comments on commit 6edfa9c

Please sign in to comment.