chore: refactor backend around domain types #26
@@ -8,18 +8,25 @@ 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 {
|
||||||
|
pub client: Client,
|
||||||
|
pub signer: UniversalSigner,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 signer = UniversalSigner::new(Keys::generate());
|
||||||
let database = NostrLmdb::open(db_path)
|
let database = NostrLmdb::open(db_path)
|
||||||
.await
|
.await
|
||||||
.context("failed to open nostr database")?;
|
.context("failed to open nostr database")?;
|
||||||
Ok(with_database(signer, database))
|
Ok(Self::with_database(signer, database))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_database<D>(signer: UniversalSigner, database: D) -> (Client, UniversalSigner)
|
fn with_database<D>(signer: UniversalSigner, database: D) -> Self
|
||||||
where
|
where
|
||||||
D: IntoNostrDatabase,
|
D: IntoNostrDatabase,
|
||||||
{
|
{
|
||||||
let authenticator = SignerAuthenticator::new(signer.clone());
|
let authenticator = SignerAuthenticator::new(signer.clone());
|
||||||
|
|
||||||
let client = ClientBuilder::default()
|
let client = ClientBuilder::default()
|
||||||
@@ -35,5 +42,6 @@ where
|
|||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
(client, signer)
|
Self { client, signer }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user