From 218bd9f16eaff54957af1d757d83fae56ca6d256 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 13 Aug 2024 10:17:38 +0200 Subject: [PATCH] Add first tauri commands --- src-tauri/src/commands.rs | 2 + src-tauri/src/commands/node.rs | 33 +++++++++++++++ src-tauri/src/commands/repos.rs | 74 +++++++++++++++++++++++++++++++++ src-tauri/src/lib.rs | 15 ++++--- 4 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 src-tauri/src/commands.rs create mode 100644 src-tauri/src/commands/node.rs create mode 100644 src-tauri/src/commands/repos.rs diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs new file mode 100644 index 0000000..4252379 --- /dev/null +++ b/src-tauri/src/commands.rs @@ -0,0 +1,2 @@ +pub mod node; +pub mod repos; diff --git a/src-tauri/src/commands/node.rs b/src-tauri/src/commands/node.rs new file mode 100644 index 0000000..32630f6 --- /dev/null +++ b/src-tauri/src/commands/node.rs @@ -0,0 +1,33 @@ +use tauri::State; + +use radicle::node::address::Store; +use radicle::node::Handle; +use radicle::{Node, Profile}; + +use crate::error::Error; +use crate::types::NodeInfo; + +#[tauri::command] +pub fn node_info(profile: State) -> Result { + let node = Node::new(profile.socket()); + let home = profile.home.database()?; + let agent = home + .get(&profile.public_key) + .unwrap_or_default() + .map(|node| node.agent); + let node_state = if node.is_running() { + "running" + } else { + "stopped" + }; + + Ok(NodeInfo { + id: profile.public_key, + alias: node.config().ok().map(|c| c.alias), + agent, + state: node_state.to_string(), + avatar_url: profile.config.web.avatar_url.clone(), + banner_url: profile.config.web.banner_url.clone(), + description: profile.config.web.description.clone(), + }) +} diff --git a/src-tauri/src/commands/repos.rs b/src-tauri/src/commands/repos.rs new file mode 100644 index 0000000..d28f635 --- /dev/null +++ b/src-tauri/src/commands/repos.rs @@ -0,0 +1,74 @@ +use radicle::issue::cache::Issues; +use radicle::node::routing::Store; +use radicle::patch::cache::Patches; +use radicle::storage::{ReadRepository, ReadStorage}; +use radicle::Profile; +use tauri::State; + +use crate::types::Author; +use crate::{error::Error, types::RepositoryInfo}; + +#[tauri::command] +pub fn repo_list(profile: State) -> Result, Error> { + let storage = &profile.storage; + let db = &profile.database()?; + + let mut repos = storage.repositories()?.into_iter().collect::>(); + repos.sort_by_key(|p| p.rid); + + let infos = repos + .into_iter() + .filter_map(|info| { + let Ok(repo) = storage.repository(info.rid) else { + return None; + }; + let Ok((_, head)) = repo.head() else { + return None; + }; + let Ok(commit) = repo.commit(head) else { + return None; + }; + let Ok(payload) = info.doc.project() else { + return None; + }; + let Ok(issues) = profile.issues(&repo) else { + return None; + }; + let Ok(issues) = issues.counts() else { + return None; + }; + let Ok(patches) = profile.patches(&repo) else { + return None; + }; + let Ok(patches) = patches.counts() else { + return None; + }; + let aliases = profile.aliases(); + let delegates = info + .doc + .delegates + .into_iter() + .map(|did| Author::new(*did, &aliases)) + .collect::>(); + let seeding = db.count(&info.rid).unwrap_or_default(); + let last_update = commit.committer().when().seconds(); + + Some(RepositoryInfo { + name: payload.name().to_owned(), + description: payload.description().to_owned(), + default_branch: payload.default_branch().to_owned(), + delegates, + head, + threshold: info.doc.threshold, + visibility: info.doc.visibility, + issues, + patches, + id: info.rid, + seeding, + last_update, + }) + }) + .collect::>(); + + Ok(infos) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a808544..247d47b 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,13 +1,18 @@ -#[tauri::command] -fn greet(name: &str) -> String { - format!("Hello, {}!", name) -} +mod commands; +mod error; +mod types; + +use commands::{node, repos}; +use radicle::Profile; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + let profile = Profile::load().expect("Not able to load profile"); + tauri::Builder::default() + .manage(profile) .plugin(tauri_plugin_shell::init()) - .invoke_handler(tauri::generate_handler![greet]) + .invoke_handler(tauri::generate_handler![repos::repo_list, node::node_info]) .run(tauri::generate_context!()) .expect("error while running tauri application"); }