From fe270f7f9b02cd1c2b0639ce2d9443cf522b0263 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 27 Sep 2024 14:23:57 +0200 Subject: [PATCH] Some minor refactors on tauri commands --- src-tauri/src/commands/cob/issue.rs | 11 ++-- src-tauri/src/commands/cob/patch.rs | 38 ++++++-------- src-tauri/src/commands/repo.rs | 80 ++++++++++++++++++++++++----- src-tauri/src/commands/thread.rs | 12 ++--- src-tauri/src/lib.rs | 65 ----------------------- src-tauri/src/types/cobs.rs | 9 ++-- 6 files changed, 98 insertions(+), 117 deletions(-) diff --git a/src-tauri/src/commands/cob/issue.rs b/src-tauri/src/commands/cob/issue.rs index b01fbd6..aceca34 100644 --- a/src-tauri/src/commands/cob/issue.rs +++ b/src-tauri/src/commands/cob/issue.rs @@ -1,6 +1,7 @@ -use radicle::git::Oid; +use radicle::git; use radicle::identity::RepoId; use radicle::issue::cache::Issues; +use radicle::storage::ReadStorage; use crate::cob::query; use crate::error::Error; @@ -13,7 +14,7 @@ pub fn create_issue( rid: RepoId, new: cobs::NewIssue, ) -> Result { - let (repo, _) = ctx.repo(rid)?; + let repo = ctx.profile.storage.repository(rid)?; let signer = ctx.profile.signer()?; let aliases = ctx.profile.aliases(); let mut issues = ctx.profile.issues_mut(&repo)?; @@ -35,7 +36,7 @@ pub fn list_issues( rid: RepoId, status: query::IssueStatus, ) -> Result, Error> { - let (repo, _) = ctx.repo(rid)?; + let repo = ctx.profile.storage.repository(rid)?; let issues = ctx.profile.issues(&repo)?; let mut issues: Vec<_> = issues .list()? @@ -59,9 +60,9 @@ pub fn list_issues( pub fn issue_by_id( ctx: tauri::State, rid: RepoId, - id: Oid, + id: git::Oid, ) -> Result, Error> { - let (repo, _) = ctx.repo(rid)?; + let repo = ctx.profile.storage.repository(rid)?; let issues = ctx.profile.issues(&repo)?; let issue = issues.get(&id.into())?; diff --git a/src-tauri/src/commands/cob/patch.rs b/src-tauri/src/commands/cob/patch.rs index 31f2ac3..3fb24d6 100644 --- a/src-tauri/src/commands/cob/patch.rs +++ b/src-tauri/src/commands/cob/patch.rs @@ -1,9 +1,9 @@ -use std::str::FromStr; - -use radicle::cob::ObjectId; -use radicle::git::Oid; +use radicle::cob::Label; +use radicle::git; use radicle::identity::RepoId; use radicle::patch::cache::Patches; +use radicle::patch::Verdict; +use radicle::storage::ReadStorage; use crate::cob::query; use crate::error::Error; @@ -16,7 +16,7 @@ pub fn list_patches( rid: RepoId, status: query::PatchStatus, ) -> Result, Error> { - let (repo, _) = ctx.repo(rid)?; + let repo = ctx.profile.storage.repository(rid)?; let patches = ctx.profile.patches(&repo)?; let mut patches: Vec<_> = patches .list()? @@ -40,15 +40,13 @@ pub fn list_patches( pub fn patch_by_id( ctx: tauri::State, rid: RepoId, - id: String, + id: git::Oid, ) -> Result, Error> { - let id = ObjectId::from_str(&id)?; - let (repo, _) = ctx.repo(rid)?; + let repo = ctx.profile.storage.repository(rid)?; let patches = ctx.profile.patches(&repo)?; - let patch = patches.get(&id)?; - + let patch = patches.get(&id.into())?; let aliases = &ctx.profile.aliases(); - let patches = patch.map(|patch| cobs::Patch::new(id, patch, aliases)); + let patches = patch.map(|patch| cobs::Patch::new(id.into(), patch, aliases)); Ok::<_, Error>(patches) } @@ -57,13 +55,11 @@ pub fn patch_by_id( pub fn revisions_by_patch( ctx: tauri::State, rid: RepoId, - id: String, + id: git::Oid, ) -> Result>, Error> { - let id = ObjectId::from_str(&id)?; - let (repo, _) = ctx.repo(rid)?; + let repo = ctx.profile.storage.repository(rid)?; let patches = ctx.profile.patches(&repo)?; - - let revisions = patches.get(&id)?.map(|patch| { + let revisions = patches.get(&id.into())?.map(|patch| { let aliases = &ctx.profile.aliases(); patch @@ -79,14 +75,12 @@ pub fn revisions_by_patch( pub fn revision_by_patch_and_id( ctx: tauri::State, rid: RepoId, - id: String, - revision_id: String, + id: git::Oid, + revision_id: git::Oid, ) -> Result, Error> { - let id = ObjectId::from_str(&id)?; - let (repo, _) = ctx.repo(rid)?; + let repo = ctx.profile.storage.repository(rid)?; let patches = ctx.profile.patches(&repo)?; - let revision = patches.get(&id)?.and_then(|patch| { - let revision_id = Oid::from_str(&revision_id).ok()?; + let revision = patches.get(&id.into())?.and_then(|patch| { let aliases = &ctx.profile.aliases(); patch diff --git a/src-tauri/src/commands/repo.rs b/src-tauri/src/commands/repo.rs index e31cabb..82f67b4 100644 --- a/src-tauri/src/commands/repo.rs +++ b/src-tauri/src/commands/repo.rs @@ -1,10 +1,18 @@ -use radicle::identity::RepoId; -use radicle::storage::ReadRepository; +use radicle::crypto::Verified; +use radicle::prelude::Doc; +use radicle::storage::git::Repository; +use serde_json::json; + +use radicle::identity::doc::PayloadId; +use radicle::identity::{DocAt, RepoId}; +use radicle::issue::cache::Issues; +use radicle::node::routing::Store; +use radicle::patch::cache::Patches; use radicle::storage::ReadStorage; +use radicle::storage::{self, ReadRepository}; use crate::error::Error; use crate::types; -use crate::types::cobs; use crate::AppState; /// List all repos. @@ -12,7 +20,6 @@ use crate::AppState; pub fn list_repos(ctx: tauri::State) -> Result, Error> { let storage = &ctx.profile.storage; let policies = ctx.profile.policies()?; - let mut repos = storage.repositories()?.into_iter().collect::>(); repos.sort_by_key(|p| p.rid); @@ -22,8 +29,9 @@ pub fn list_repos(ctx: tauri::State) -> Result, rid: RepoId, ) -> Result { - let (repo, doc) = ctx.repo(rid)?; - let repo_info = ctx.repo_info(&repo, doc)?; + let repo = ctx.profile.storage.repository(rid)?; + let DocAt { doc, .. } = repo.identity_doc()?; + + let repo_info = repo_info(&ctx.profile, &repo, &doc)?; Ok::<_, Error>(repo_info) } @@ -49,14 +59,58 @@ pub async fn diff_stats( rid: RepoId, base: String, head: String, -) -> Result { - let (repo, _) = ctx.repo(rid)?; - let repo = radicle_surf::Repository::open(repo.path())?; +) -> Result { + let repo = radicle_surf::Repository::open(storage::git::paths::repository( + &ctx.profile.storage, + &rid, + ))?; let base = repo.commit(base)?; let commit = repo.commit(head)?; let diff = repo.diff(base.id, commit.id)?; - let stats = diff.stats(); - Ok::<_, Error>(cobs::Stats::new(stats)) + Ok::<_, Error>(types::cobs::Stats::new(stats)) +} + +pub fn repo_info( + profile: &radicle::Profile, + repo: &Repository, + doc: &Doc, +) -> Result { + let aliases = profile.aliases(); + let delegates = doc + .delegates + .clone() + .into_iter() + .map(|did| types::cobs::Author::new(did, &aliases)) + .collect::>(); + let db = profile.database()?; + let seeding = db.count(&repo.id).unwrap_or_default(); + let project = doc.payload.get(&PayloadId::project()).and_then(|payload| { + let (_, head) = repo.head().ok()?; + let commit = repo.commit(head).ok()?; + let patches = profile.patches(repo).ok()?; + let patches = patches.counts().ok()?; + let issues = profile.issues(repo).ok()?; + let issues = issues.counts().ok()?; + + Some(json!({ + "data": payload, + "meta": { + "issues": issues, + "patches": patches, + "head": head, + "lastCommit": commit.time().seconds() * 1000, + }, + })) + }); + + Ok::<_, Error>(types::repo::RepoInfo { + payloads: types::repo::SupportedPayloads { project }, + delegates, + threshold: doc.threshold, + visibility: doc.visibility.clone(), + rid: repo.id, + seeding, + }) } diff --git a/src-tauri/src/commands/thread.rs b/src-tauri/src/commands/thread.rs index 2910e63..27bf194 100644 --- a/src-tauri/src/commands/thread.rs +++ b/src-tauri/src/commands/thread.rs @@ -1,6 +1,3 @@ -use std::str::FromStr; - -use radicle::cob::{EntryId, ObjectId}; use radicle::git::Oid; use radicle::identity::RepoId; use radicle::node::Handle; @@ -20,10 +17,9 @@ pub fn create_issue_comment( ) -> Result { let mut node = Node::new(ctx.profile.socket()); let signer = ctx.profile.signer()?; - let issue_id = ObjectId::from_str(&new.id)?; let repo = ctx.profile.storage.repository(rid)?; let mut issues = ctx.profile.issues_mut(&repo)?; - let mut issue = issues.get_mut(&issue_id)?; + let mut issue = issues.get_mut(&new.id.into())?; let id = new.reply_to.unwrap_or_else(|| { let (root_id, _) = issue.root(); *root_id @@ -46,13 +42,11 @@ pub fn create_patch_comment( ) -> Result { let mut node = Node::new(ctx.profile.socket()); let signer = ctx.profile.signer()?; - let patch_id = ObjectId::from_str(&new.id)?; - let revision_id = EntryId::from_str(&new.revision)?; let repo = ctx.profile.storage.repository(rid)?; let mut patches = ctx.profile.patches_mut(&repo)?; - let mut patch = patches.get_mut(&patch_id)?; + let mut patch = patches.get_mut(&new.id.into())?; let oid = patch.comment( - revision_id.into(), + new.revision.into(), new.body, new.reply_to, new.location.map(|l| l.into()), diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 37ac1fe..d0ec8fa 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2,83 +2,18 @@ mod commands; mod error; mod types; -use serde_json::json; use tauri::Emitter; use tauri::Manager; -use radicle::identity::doc::PayloadId; -use radicle::identity::DocAt; -use radicle::identity::RepoId; -use radicle::issue::cache::Issues; -use radicle::node::routing::Store; use radicle::node::Handle; -use radicle::patch::cache::Patches; -use radicle::storage::git::Repository; -use radicle::storage::{ReadRepository, ReadStorage}; use radicle::Node; use commands::{auth, cob, profile, repo, thread}; -use types::repo::SupportedPayloads; struct AppState { profile: radicle::Profile, } -impl AppState { - pub fn repo_info( - &self, - repo: &R, - doc: DocAt, - ) -> Result { - let DocAt { doc, .. } = doc; - let rid = repo.id(); - - let aliases = self.profile.aliases(); - let delegates = doc - .delegates - .into_iter() - .map(|did| types::cobs::Author::new(did, &aliases)) - .collect::>(); - let db = &self.profile.database()?; - let seeding = db.count(&rid).unwrap_or_default(); - - let project = doc.payload.get(&PayloadId::project()).and_then(|payload| { - let (_, head) = repo.head().ok()?; - let commit = repo.commit(head).ok()?; - let patches = self.profile.patches(repo).ok()?; - let patches = patches.counts().ok()?; - let issues = self.profile.issues(repo).ok()?; - let issues = issues.counts().ok()?; - - Some(json!({ - "data": payload, - "meta": { - "issues": issues, - "patches": patches, - "head": head, - "lastCommit": commit.time().seconds() * 1000, - }, - })) - }); - - Ok(types::repo::RepoInfo { - payloads: SupportedPayloads { project }, - delegates, - threshold: doc.threshold, - visibility: doc.visibility, - rid, - seeding, - }) - } - - /// Get a repository by RID, checking to make sure we're allowed to view it. - pub fn repo(&self, rid: RepoId) -> Result<(Repository, DocAt), error::Error> { - let repo = self.profile.storage.repository(rid)?; - let doc = repo.identity_doc()?; - Ok((repo, doc)) - } -} - #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { #[cfg(debug_assertions)] diff --git a/src-tauri/src/types/cobs.rs b/src-tauri/src/types/cobs.rs index cef0782..84b9fe5 100644 --- a/src-tauri/src/types/cobs.rs +++ b/src-tauri/src/types/cobs.rs @@ -349,8 +349,10 @@ impl Comment { #[ts(export)] #[serde(rename_all = "camelCase")] pub struct NewPatchComment { - pub id: String, - pub revision: String, + #[ts(as = "String")] + pub id: git::Oid, + #[ts(as = "String")] + pub revision: git::Oid, pub body: String, #[ts(as = "Option")] #[ts(optional)] @@ -378,7 +380,8 @@ pub struct NewIssue { #[ts(export)] #[serde(rename_all = "camelCase")] pub struct NewIssueComment { - pub id: String, + #[ts(as = "String")] + pub id: git::Oid, pub body: String, #[ts(as = "Option")] #[ts(optional)]