mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-11 23:20:34 +08:00
Some minor refactors on tauri commands
This commit is contained in:
@@ -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<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)?;
|
||||
@@ -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()?
|
||||
@@ -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())?;
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use radicle::cob::ObjectId;
|
||||
use radicle::git::Oid;
|
||||
use radicle::git;
|
||||
use radicle::identity::RepoId;
|
||||
use radicle::patch::cache::Patches;
|
||||
use radicle::storage::ReadStorage;
|
||||
|
||||
use crate::cob::query;
|
||||
use crate::error::Error;
|
||||
@@ -16,7 +14,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()?
|
||||
@@ -40,15 +38,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)
|
||||
}
|
||||
@@ -57,13 +53,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
|
||||
@@ -79,14 +73,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
|
||||
|
||||
@@ -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<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);
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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<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
|
||||
@@ -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()),
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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)]
|
||||
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user