From 4656f1340bb050051fa4a8d69cf500ec3da0e0c8 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 3 Oct 2026 12:07:49 +0700 Subject: [PATCH] consolidate backend init into NostrBackend struct --- crates/signed_nostr/src/backend.rs | 56 +++++++++++++++++------------- crates/signed_nostr/src/lib.rs | 2 +- crates/signed_state/src/inbox.rs | 2 +- crates/signed_state/src/lib.rs | 26 ++++---------- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/crates/signed_nostr/src/backend.rs b/crates/signed_nostr/src/backend.rs index 5a1f805..1ab564c 100644 --- a/crates/signed_nostr/src/backend.rs +++ b/crates/signed_nostr/src/backend.rs @@ -8,32 +8,40 @@ use nostr_sdk::prelude::*; use crate::signer::UniversalSigner; -pub async fn new_backend(db_path: impl AsRef) -> 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(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) -> Result { + 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(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 } + } } diff --git a/crates/signed_nostr/src/lib.rs b/crates/signed_nostr/src/lib.rs index 3c99075..1328bf4 100644 --- a/crates/signed_nostr/src/lib.rs +++ b/crates/signed_nostr/src/lib.rs @@ -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; diff --git a/crates/signed_state/src/inbox.rs b/crates/signed_state/src/inbox.rs index fa557be..d7be438 100644 --- a/crates/signed_state/src/inbox.rs +++ b/crates/signed_state/src/inbox.rs @@ -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; diff --git a/crates/signed_state/src/lib.rs b/crates/signed_state/src/lib.rs index c8fadb5..217cc56 100644 --- a/crates/signed_state/src/lib.rs +++ b/crates/signed_state/src/lib.rs @@ -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, repos_root: impl Into, @@ -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); -}