diff --git a/crates/radicle-tauri/src/commands.rs b/crates/radicle-tauri/src/commands.rs index fae1019bc..600925679 100644 --- a/crates/radicle-tauri/src/commands.rs +++ b/crates/radicle-tauri/src/commands.rs @@ -2,6 +2,7 @@ pub mod auth; pub mod cob; pub mod diff; pub mod inbox; +pub mod init; pub mod profile; pub mod repo; pub mod thread; diff --git a/crates/radicle-tauri/src/commands/init.rs b/crates/radicle-tauri/src/commands/init.rs new file mode 100644 index 000000000..3fd14d353 --- /dev/null +++ b/crates/radicle-tauri/src/commands/init.rs @@ -0,0 +1,114 @@ +use std::collections::BTreeMap; + +use radicle::cob::cache::COBS_DB_FILE; +use radicle::identity::RepoId; +use radicle::node::{Handle, Node, NOTIFICATIONS_DB_FILE}; +use radicle::storage::ReadStorage; +use tauri::{AppHandle, Emitter, Manager}; + +use radicle_types::config::Config; +use radicle_types::error::Error; +use radicle_types::traits::Profile; +use radicle_types::{domain, AppState}; + +#[tauri::command] +pub(crate) fn startup(app: AppHandle) -> Result { + let profile = radicle::Profile::load()?; + + let inbox_db = radicle_types::outbound::sqlite::Sqlite::reader( + profile.node().join(NOTIFICATIONS_DB_FILE), + )?; + let cob_db = + radicle_types::outbound::sqlite::Sqlite::reader(profile.cobs().join(COBS_DB_FILE))?; + + let inbox_service = domain::inbox::service::Service::new(inbox_db); + let patch_service = domain::patch::service::Service::new(cob_db); + + app.manage(inbox_service); + app.manage(patch_service); + + let state = AppState { profile }; + app.manage(state.clone()); + + Ok(state.config()) +} + +#[tauri::command] +pub(crate) fn node_status_events(app: AppHandle, ctx: tauri::State) -> Result<(), Error> { + let app_handle = app.clone(); + + let node = Node::new(ctx.profile.socket()); + let node_status = node.clone(); + + tauri::async_runtime::spawn(async move { + loop { + let _ = app_handle.emit("node_running", node_status.is_running()); + tokio::time::sleep(std::time::Duration::from_secs(2)).await; + } + }); + + Ok(()) +} + +#[tauri::command] +pub(crate) fn repo_sync_events(app: AppHandle, ctx: tauri::State) -> Result<(), Error> { + let profile = &ctx.profile; + let repositories = profile.storage.repositories()?; + + let app_handle = app.clone(); + let public_key = profile.public_key; + + let node = Node::new(profile.socket()); + let mut node_seeds = node.clone(); + + tauri::async_runtime::spawn(async move { + loop { + let mut sync_status = + BTreeMap::>::new(); + for repo in &repositories { + if let Ok(seeds) = node_seeds.seeds(repo.rid).map(Into::>::into) { + if let Some(status) = + seeds + .iter() + .find_map(|radicle::node::Seed { nid, sync, .. }| { + (*nid == public_key).then_some(sync.clone()) + }) + { + sync_status.insert(repo.rid, status.map(Into::into)); + } else { + // The local node wasn't found in the seed nodes table. + sync_status.insert(repo.rid, None); + } + } + } + let _ = app_handle.emit("sync_status", sync_status); + tokio::time::sleep(std::time::Duration::from_secs(10)).await; + } + }); + + Ok(()) +} + +#[tauri::command] +pub(crate) fn node_events(app: AppHandle, ctx: tauri::State) -> Result<(), Error> { + let app_handle = app.clone(); + let node = Node::new(ctx.profile.socket()); + + tauri::async_runtime::spawn(async move { + loop { + if node.is_running() { + log::debug!("node: spawned node event subscription."); + while let Ok(events) = node.subscribe(std::time::Duration::MAX) { + for event in events.into_iter().flatten() { + let _ = app_handle.emit("event", event); + } + } + log::debug!("node: event subscription loop has exited."); + } + + tokio::time::sleep(std::time::Duration::from_secs(2)).await; + } + }); + + Ok(()) +} diff --git a/crates/radicle-tauri/src/lib.rs b/crates/radicle-tauri/src/lib.rs index e67296889..260dae1a6 100644 --- a/crates/radicle-tauri/src/lib.rs +++ b/crates/radicle-tauri/src/lib.rs @@ -1,19 +1,8 @@ mod commands; -use std::collections::BTreeMap; - -use tauri::{Emitter, Manager}; - -use radicle::cob::cache::COBS_DB_FILE; -use radicle::identity::RepoId; -use radicle::node::{Handle, Node, NOTIFICATIONS_DB_FILE}; -use radicle::storage::ReadStorage; - -use radicle_types::domain; -use radicle_types::error::Error; use radicle_types::AppState; -use commands::{auth, cob, diff, inbox, profile, repo, thread}; +use commands::{auth, cob, diff, inbox, init, profile, repo, thread}; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { @@ -25,94 +14,14 @@ pub fn run() { let builder = tauri::Builder::default(); builder - .setup(|app| { - let profile: radicle::Profile = match radicle::Profile::load() { - Ok(profile) => Ok::(profile), - Err(radicle::profile::Error::NotFound(path)) => Err(Error::WithHint { - err: anyhow::anyhow!("Radicle profile not found in '{}'.", path.display()), - hint: "To setup your radicle profile, run `rad auth`.", - }), - Err(e) => Err(Error::WithHint { - err: e.into(), - hint: "Could not load radicle profile", - }), - }?; - let public_key = profile.public_key; - let repositories = profile.storage.repositories()?; - - let inbox_db = radicle_types::outbound::sqlite::Sqlite::reader( - profile.node().join(NOTIFICATIONS_DB_FILE), - )?; - let inbox_service = domain::inbox::service::Service::new(inbox_db); - - let patch_db = - radicle_types::outbound::sqlite::Sqlite::reader(profile.cobs().join(COBS_DB_FILE))?; - let patch_service = domain::patch::service::Service::new(patch_db); - - let events_handler = app.handle().clone(); - let node_handler = app.handle().clone(); - let seed_handler = app.handle().clone(); - - let node = Node::new(profile.socket()); - let node_status = node.clone(); - let mut node_seeds = node.clone(); - - app.manage(inbox_service); - app.manage(patch_service); - app.manage(AppState { profile }); - - tauri::async_runtime::spawn(async move { - loop { - let _ = node_handler.emit("node_running", node_status.is_running()); - tokio::time::sleep(std::time::Duration::from_secs(2)).await; - } - }); - - tauri::async_runtime::spawn(async move { - loop { - let mut sync_status = - BTreeMap::>::new(); - for repo in &repositories { - if let Ok(seeds) = node_seeds.seeds(repo.rid).map(Into::>::into) { - if let Some(status) = seeds.iter().find_map( - |radicle::node::Seed { nid, sync, .. }| { - (*nid == public_key).then_some(sync.clone()) - }, - ) { - sync_status.insert(repo.rid, status.map(Into::into)); - } else { - // The local node wasn't found in the seed nodes table. - sync_status.insert(repo.rid, None); - } - } - } - let _ = seed_handler.emit("sync_status", sync_status); - tokio::time::sleep(std::time::Duration::from_secs(10)).await; - } - }); - - tauri::async_runtime::spawn(async move { - loop { - if node.is_running() { - log::debug!("node: spawned node event subscription."); - while let Ok(events) = node.subscribe(std::time::Duration::MAX) { - for event in events.into_iter().flatten() { - let _ = events_handler.emit("event", event); - } - } - log::debug!("node: event subscription loop has exited."); - } - - tokio::time::sleep(std::time::Duration::from_secs(2)).await; - } - }); - - Ok(()) - }) .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_clipboard_manager::init()) .plugin(tauri_plugin_window_state::Builder::default().build()) .invoke_handler(tauri::generate_handler![ + init::startup, + init::node_status_events, + init::repo_sync_events, + init::node_events, auth::authenticate, repo::repo_count, repo::list_repos, diff --git a/crates/radicle-types/src/lib.rs b/crates/radicle-types/src/lib.rs index ede288ef2..9e7c6aba4 100644 --- a/crates/radicle-types/src/lib.rs +++ b/crates/radicle-types/src/lib.rs @@ -17,6 +17,7 @@ pub mod syntax; pub mod test; pub mod traits; +#[derive(Clone)] pub struct AppState { pub profile: radicle::Profile, } diff --git a/src/App.svelte b/src/App.svelte index 626903e20..992a1cb68 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,4 +1,5 @@ {#if $activeRouteStore.resource === "booting"} + {#if error && typeof error === "object" && "err" in error && typeof error.err === "string"} + + {/if} {:else if $activeRouteStore.resource === "home"}