Skip to content

Commit

Permalink
Some minor refactors on tauri commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinez committed Sep 27, 2024
1 parent 1a0de05 commit fe270f7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 117 deletions.
11 changes: 6 additions & 5 deletions src-tauri/src/commands/cob/issue.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +14,7 @@ pub fn create_issue(
rid: RepoId,
new: cobs::NewIssue,
) -> Result<cobs::Issue, Error> {
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)?;
Expand All @@ -35,7 +36,7 @@ pub fn list_issues(
rid: RepoId,
status: query::IssueStatus,
) -> Result<Vec<cobs::Issue>, 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()?
Expand All @@ -59,9 +60,9 @@ pub fn list_issues(
pub fn issue_by_id(
ctx: tauri::State<AppState>,
rid: RepoId,
id: Oid,
id: git::Oid,
) -> Result<Option<cobs::Issue>, 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())?;

Expand Down
38 changes: 16 additions & 22 deletions src-tauri/src/commands/cob/patch.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +16,7 @@ pub fn list_patches(
rid: RepoId,
status: query::PatchStatus,
) -> Result<Vec<cobs::Patch>, 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()?
Expand All @@ -40,15 +40,13 @@ pub fn list_patches(
pub fn patch_by_id(
ctx: tauri::State<AppState>,
rid: RepoId,
id: String,
id: git::Oid,
) -> Result<Option<cobs::Patch>, 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)
}
Expand All @@ -57,13 +55,11 @@ pub fn patch_by_id(
pub fn revisions_by_patch(
ctx: tauri::State<AppState>,
rid: RepoId,
id: String,
id: git::Oid,
) -> Result<Option<Vec<cobs::Revision>>, 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
Expand All @@ -79,14 +75,12 @@ pub fn revisions_by_patch(
pub fn revision_by_patch_and_id(
ctx: tauri::State<AppState>,
rid: RepoId,
id: String,
revision_id: String,
id: git::Oid,
revision_id: git::Oid,
) -> Result<Option<cobs::Revision>, 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
Expand Down
80 changes: 67 additions & 13 deletions src-tauri/src/commands/repo.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
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.
#[tauri::command]
pub fn list_repos(ctx: tauri::State<AppState>) -> Result<Vec<types::repo::RepoInfo>, Error> {
let storage = &ctx.profile.storage;
let policies = ctx.profile.policies()?;

let mut repos = storage.repositories()?.into_iter().collect::<Vec<_>>();
repos.sort_by_key(|p| p.rid);

Expand All @@ -22,8 +29,9 @@ pub fn list_repos(ctx: tauri::State<AppState>) -> Result<Vec<types::repo::RepoIn
if !policies.is_seeding(&info.rid).unwrap_or_default() {
return None;
}
let (repo, doc) = ctx.repo(info.rid).ok()?;
let repo_info = ctx.repo_info(&repo, doc).ok()?;
let repo = ctx.profile.storage.repository(info.rid).ok()?;
let DocAt { doc, .. } = repo.identity_doc().ok()?;
let repo_info = repo_info(&ctx.profile, &repo, &doc).ok()?;

Some(repo_info)
})
Expand All @@ -37,8 +45,10 @@ pub fn repo_by_id(
ctx: tauri::State<AppState>,
rid: RepoId,
) -> Result<types::repo::RepoInfo, Error> {
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)
}
Expand All @@ -49,14 +59,58 @@ pub async fn diff_stats(
rid: RepoId,
base: String,
head: String,
) -> Result<cobs::Stats, Error> {
let (repo, _) = ctx.repo(rid)?;
let repo = radicle_surf::Repository::open(repo.path())?;
) -> Result<types::cobs::Stats, Error> {
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<Verified>,
) -> Result<types::repo::RepoInfo, Error> {
let aliases = profile.aliases();
let delegates = doc
.delegates
.clone()
.into_iter()
.map(|did| types::cobs::Author::new(did, &aliases))
.collect::<Vec<_>>();
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,
})
}
12 changes: 3 additions & 9 deletions src-tauri/src/commands/thread.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,10 +17,9 @@ pub fn create_issue_comment(
) -> Result<Oid, Error> {
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
Expand All @@ -46,13 +42,11 @@ pub fn create_patch_comment(
) -> Result<Oid, Error> {
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()),
Expand Down
65 changes: 0 additions & 65 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: ReadRepository + radicle::cob::Store>(
&self,
repo: &R,
doc: DocAt,
) -> Result<types::repo::RepoInfo, error::Error> {
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::<Vec<_>>();
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)]
Expand Down
9 changes: 6 additions & 3 deletions src-tauri/src/types/cobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>")]
#[ts(optional)]
Expand Down Expand Up @@ -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<String>")]
#[ts(optional)]
Expand Down

0 comments on commit fe270f7

Please sign in to comment.