chore: refactor backend around domain types #26

Merged
reya merged 9 commits from audit into master 2026-10-03 10:35:50 +00:00
4 changed files with 40 additions and 46 deletions
Showing only changes of commit 4656f1340b - Show all commits
+32 -24
View File
@@ -8,32 +8,40 @@ use nostr_sdk::prelude::*;
use crate::signer::UniversalSigner; use crate::signer::UniversalSigner;
pub async fn new_backend(db_path: impl AsRef<Path>) -> Result<(Client, UniversalSigner)> { pub struct NostrBackend {
let signer = UniversalSigner::new(Keys::generate()); pub client: Client,
let database = NostrLmdb::open(db_path) pub signer: UniversalSigner,
.await
.context("failed to open nostr database")?;
Ok(with_database(signer, database))
} }
fn with_database<D>(signer: UniversalSigner, database: D) -> (Client, UniversalSigner) impl NostrBackend {
where pub async fn open(db_path: impl AsRef<Path>) -> Result<Self> {
D: IntoNostrDatabase, let db_path = db_path.as_ref();
{ let signer = UniversalSigner::new(Keys::generate());
let authenticator = SignerAuthenticator::new(signer.clone()); let database = NostrLmdb::open(db_path)
.await
.context("failed to open nostr database")?;
Ok(Self::with_database(signer, database))
}
let client = ClientBuilder::default() fn with_database<D>(signer: UniversalSigner, database: D) -> Self
.database(database) where
.authenticator(authenticator) D: IntoNostrDatabase,
.gossip(NostrGossipMemory::unbounded()) {
.gossip_config(GossipConfig::default().no_background_refresh()) let authenticator = SignerAuthenticator::new(signer.clone());
.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();
(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 }
}
} }
+1 -1
View File
@@ -2,6 +2,6 @@ mod backend;
mod signer; mod signer;
mod update; mod update;
pub use backend::new_backend; pub use backend::NostrBackend;
pub use signer::{SignedAuthUrlHandler, UniversalSigner}; pub use signer::{SignedAuthUrlHandler, UniversalSigner};
pub use update::Update; pub use update::Update;
+1 -1
View File
@@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
use anyhow::Error; use anyhow::Error;
use gpui::{AppContext, Context, Task}; use gpui::{AppContext, Context, Task};
use nostr_sdk::prelude::*; 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; use crate::backend::Backend;
+6 -20
View File
@@ -23,9 +23,8 @@ pub use refresh::{RefreshGate, RefreshRequest};
pub use repo::RepoStore; pub use repo::RepoStore;
pub use repos::RepoListStore; pub use repos::RepoListStore;
pub use signed_git::{Nip34Binding, Nip34Kind}; pub use signed_git::{Nip34Binding, Nip34Kind};
use signed_nostr::new_backend; use signed_nostr::NostrBackend;
#[cfg(not(target_arch = "wasm32"))]
pub fn init( pub fn init(
db_path: impl AsRef<Path>, db_path: impl AsRef<Path>,
repos_root: impl Into<PathBuf>, repos_root: impl Into<PathBuf>,
@@ -36,12 +35,11 @@ pub fn init(
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
// Initialize the nostr client and signer // Initialize the nostr client and signer
let (client, signer) = cx.foreground_executor().block_on(async move { let backend = cx
let path = db_path.as_ref().to_path_buf(); .foreground_executor()
new_backend(path) .block_on(async move { NostrBackend::open(db_path.as_ref()).await });
.await let backend = backend.expect("failed to initialize nostr backend");
.expect("failed to initialize nostr backend") let (client, signer) = (backend.client, backend.signer);
});
// Set Git cache for the repos root // Set Git cache for the repos root
set_git_cache(repos_root); set_git_cache(repos_root);
@@ -61,15 +59,3 @@ pub fn init(
// Set global stores for the checkouts // Set global stores for the checkouts
CheckoutsStore::set_global(cx.new(CheckoutsStore::new), cx); 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);
}