chore: refactor backend around domain types #26
@@ -8,32 +8,40 @@ use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::signer::UniversalSigner;
|
||||
|
||||
pub async fn new_backend(db_path: impl AsRef<Path>) -> Result<(Client, UniversalSigner)> {
|
||||
let signer = UniversalSigner::new(Keys::generate());
|
||||
let database = NostrLmdb::open(db_path)
|
||||
.await
|
||||
.context("failed to open nostr database")?;
|
||||
Ok(with_database(signer, database))
|
||||
pub struct NostrBackend {
|
||||
pub client: Client,
|
||||
pub signer: UniversalSigner,
|
||||
}
|
||||
|
||||
fn with_database<D>(signer: UniversalSigner, database: D) -> (Client, UniversalSigner)
|
||||
where
|
||||
D: IntoNostrDatabase,
|
||||
{
|
||||
let authenticator = SignerAuthenticator::new(signer.clone());
|
||||
impl NostrBackend {
|
||||
pub async fn open(db_path: impl AsRef<Path>) -> Result<Self> {
|
||||
let db_path = db_path.as_ref();
|
||||
let signer = UniversalSigner::new(Keys::generate());
|
||||
let database = NostrLmdb::open(db_path)
|
||||
.await
|
||||
.context("failed to open nostr database")?;
|
||||
Ok(Self::with_database(signer, database))
|
||||
}
|
||||
|
||||
let client = ClientBuilder::default()
|
||||
.database(database)
|
||||
.authenticator(authenticator)
|
||||
.gossip(NostrGossipMemory::unbounded())
|
||||
.gossip_config(GossipConfig::default().no_background_refresh())
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
.verify_subscriptions(true)
|
||||
.ban_relay_on_mismatch(true)
|
||||
.sleep_when_idle(SleepWhenIdle::Enabled {
|
||||
timeout: Duration::from_secs(600),
|
||||
})
|
||||
.build();
|
||||
fn with_database<D>(signer: UniversalSigner, database: D) -> Self
|
||||
where
|
||||
D: IntoNostrDatabase,
|
||||
{
|
||||
let authenticator = SignerAuthenticator::new(signer.clone());
|
||||
|
||||
(client, signer)
|
||||
let client = ClientBuilder::default()
|
||||
.database(database)
|
||||
.authenticator(authenticator)
|
||||
.gossip(NostrGossipMemory::unbounded())
|
||||
.gossip_config(GossipConfig::default().no_background_refresh())
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
.verify_subscriptions(true)
|
||||
.ban_relay_on_mismatch(true)
|
||||
.sleep_when_idle(SleepWhenIdle::Enabled {
|
||||
timeout: Duration::from_secs(600),
|
||||
})
|
||||
.build();
|
||||
|
||||
Self { client, signer }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ mod backend;
|
||||
mod signer;
|
||||
mod update;
|
||||
|
||||
pub use backend::new_backend;
|
||||
pub use backend::NostrBackend;
|
||||
pub use signer::{SignedAuthUrlHandler, UniversalSigner};
|
||||
pub use update::Update;
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
|
||||
use anyhow::Error;
|
||||
use gpui::{AppContext, Context, Task};
|
||||
use nostr_sdk::prelude::*;
|
||||
use signed_core::{Deletions, Filters, InboxItem, InboxReadState, GitEvent, inbox};
|
||||
use signed_core::{Deletions, Filters, GitEvent, InboxItem, InboxReadState, inbox};
|
||||
|
||||
use crate::backend::Backend;
|
||||
|
||||
|
||||
@@ -23,9 +23,8 @@ pub use refresh::{RefreshGate, RefreshRequest};
|
||||
pub use repo::RepoStore;
|
||||
pub use repos::RepoListStore;
|
||||
pub use signed_git::{Nip34Binding, Nip34Kind};
|
||||
use signed_nostr::new_backend;
|
||||
use signed_nostr::NostrBackend;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn init(
|
||||
db_path: impl AsRef<Path>,
|
||||
repos_root: impl Into<PathBuf>,
|
||||
@@ -36,12 +35,11 @@ pub fn init(
|
||||
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||
|
||||
// Initialize the nostr client and signer
|
||||
let (client, signer) = cx.foreground_executor().block_on(async move {
|
||||
let path = db_path.as_ref().to_path_buf();
|
||||
new_backend(path)
|
||||
.await
|
||||
.expect("failed to initialize nostr backend")
|
||||
});
|
||||
let backend = cx
|
||||
.foreground_executor()
|
||||
.block_on(async move { NostrBackend::open(db_path.as_ref()).await });
|
||||
let backend = backend.expect("failed to initialize nostr backend");
|
||||
let (client, signer) = (backend.client, backend.signer);
|
||||
|
||||
// Set Git cache for the repos root
|
||||
set_git_cache(repos_root);
|
||||
@@ -61,15 +59,3 @@ pub fn init(
|
||||
// Set global stores for the checkouts
|
||||
CheckoutsStore::set_global(cx.new(CheckoutsStore::new), cx);
|
||||
}
|
||||
|
||||
/// Initialize the backend with an in-memory database on wasm.
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub fn init(cx: &mut App) {
|
||||
let (client, signer) = new_backend().expect("failed to initialize nostr backend");
|
||||
set_git_cache(PathBuf::new());
|
||||
Backend::set_global(cx.new(|cx| Backend::new(client, signer, cx)), cx);
|
||||
ProfileStore::set_global(cx.new(ProfileStore::new), cx);
|
||||
RepoListStore::set_global(cx.new(RepoListStore::new), cx);
|
||||
LocalReposStore::set_global(cx.new(|cx| LocalReposStore::new(Vec::new(), cx)), cx);
|
||||
CheckoutsStore::set_global(cx.new(|cx| CheckoutsStore::new(cx)), cx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user