From 1bb400c278f33402cf28bf3c09f849df69589a0a Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 18 Dec 2023 12:42:11 +0100 Subject: [PATCH] httpd: Add edits field to revision json --- radicle-cli/src/commands/patch/edit.rs | 2 +- radicle-httpd/src/api/json.rs | 40 +++---- radicle-httpd/src/api/v1/projects.rs | 138 ++++++++++++++++++++++++- radicle/src/cob/patch.rs | 4 + 4 files changed, 153 insertions(+), 31 deletions(-) diff --git a/radicle-cli/src/commands/patch/edit.rs b/radicle-cli/src/commands/patch/edit.rs index 3526ff69..e033f935 100644 --- a/radicle-cli/src/commands/patch/edit.rs +++ b/radicle-cli/src/commands/patch/edit.rs @@ -40,7 +40,7 @@ pub fn run( let target = patch.target(); let embeds = patch .embeds() - .into_iter() + .iter() .filter_map(|embed| resolve_embed(repository, embed.clone())) .collect::>(); diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 94bd491a..2d4d7d0f 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -9,7 +9,7 @@ use serde_json::{json, Value}; use radicle::cob::issue::{Issue, IssueId}; use radicle::cob::patch::{Merge, Patch, PatchId, Review}; -use radicle::cob::thread::{Comment, CommentId}; +use radicle::cob::thread::{Comment, CommentId, Edit}; use radicle::cob::{ActorId, Author}; use radicle::git::RefString; use radicle::node::{Alias, AliasStore}; @@ -129,6 +129,7 @@ pub(crate) fn patch( "id": id, "author": author(rev.author(), aliases.alias(rev.author().id())), "description": rev.description(), + "edits": rev.edits().map(|e| edit(e, aliases)).collect::>(), "base": rev.base(), "oid": rev.head(), "refs": get_refs(repo, patch.author().id(), &rev.head()).unwrap_or_default(), @@ -172,20 +173,23 @@ fn review(nid: &NodeId, review: &Review, aliases: &impl AliasStore) -> Value { }) } +/// Returns JSON for an `Edit`. +fn edit(edit: &Edit, aliases: &impl AliasStore) -> Value { + json!({ + "author": author(&Author::from(edit.author), aliases.alias(&edit.author)), + "body": edit.body, + "timestamp": edit.timestamp.as_secs(), + "embeds": edit.embeds, + }) +} + /// Returns JSON for a Issue `Comment`. fn issue_comment(id: &CommentId, comment: &Comment, aliases: &impl AliasStore) -> Value { json!({ "id": *id, "author": author(&Author::from(comment.author()), aliases.alias(&comment.author())), "body": comment.body(), - "edits": comment.edits().map(|edit| { - json!({ - "author": author(&Author::from(edit.author), aliases.alias(&edit.author)), - "body": edit.body, - "timestamp": edit.timestamp.as_secs(), - "embeds": edit.embeds, - }) - }).collect::>(), + "edits": comment.edits().map(|e| edit(e, aliases)).collect::>(), "embeds": comment.embeds().to_vec(), "reactions": comment.reactions().collect::>(), "timestamp": comment.timestamp().as_secs(), @@ -204,14 +208,7 @@ fn patch_comment( "id": *id, "author": author(&Author::from(comment.author()), aliases.alias(&comment.author())), "body": comment.body(), - "edits": comment.edits().map(|edit| { - json!({ - "author": author(&Author::from(edit.author), aliases.alias(&edit.author)), - "body": edit.body, - "timestamp": edit.timestamp.as_secs(), - "embeds": edit.embeds, - }) - }).collect::>(), + "edits": comment.edits().map(|e| edit(e, aliases)).collect::>(), "embeds": comment.embeds().to_vec(), "reactions": comment.reactions().collect::>(), "timestamp": comment.timestamp().as_secs(), @@ -231,14 +228,7 @@ fn review_comment( "id": *id, "author": author(&Author::from(comment.author()), aliases.alias(&comment.author())), "body": comment.body(), - "edits": comment.edits().map(|edit| { - json!({ - "author": author(&Author::from(edit.author), aliases.alias(&edit.author)), - "body": edit.body, - "timestamp": edit.timestamp.as_secs(), - "embeds": edit.embeds, - }) - }).collect::>(), + "edits": comment.edits().map(|e| edit(e, aliases)).collect::>(), "embeds": comment.embeds().to_vec(), "reactions": comment.reactions().collect::>(), "timestamp": comment.timestamp().as_secs(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index c4665683..b3a52ec6 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -2417,10 +2417,20 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "change `hello world` in README to something else", "author": { "id": CONTRIBUTOR_DID, }, + "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2459,10 +2469,20 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "change `hello world` in README to something else", "author": { "id": CONTRIBUTOR_DID, }, + "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2540,10 +2560,20 @@ mod routes { "revisions": [ { "id": CREATED_PATCH_ID, - "description": "Do some changes to README", "author": { "id": CONTRIBUTOR_DID, }, + "description": "Do some changes to README", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "Do some changes to README", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": INITIAL_COMMIT, "oid": HEAD, "refs": [ @@ -2606,6 +2636,16 @@ mod routes { "id": CONTRIBUTOR_DID, }, "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2666,10 +2706,20 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "change `hello world` in README to something else", "author": { "id": CONTRIBUTOR_DID, }, + "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2733,6 +2783,16 @@ mod routes { "id": CONTRIBUTOR_DID, }, "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2748,6 +2808,16 @@ mod routes { "id": CONTRIBUTOR_DID, }, "description": "This is a new revision", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "This is a new revision", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2807,10 +2877,20 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "change `hello world` in README to something else", "author": { "id": CONTRIBUTOR_DID, }, + "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2873,6 +2953,24 @@ mod routes { "id": CONTRIBUTOR_DID, }, "description": "Let's change the description a bit", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "Let's change the description a bit", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -2999,6 +3097,16 @@ mod routes { "id": CONTRIBUTOR_DID, }, "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -3216,6 +3324,16 @@ mod routes { "id": CONTRIBUTOR_DID, }, "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ @@ -3362,6 +3480,16 @@ mod routes { "id": CONTRIBUTOR_DID, }, "description": "change `hello world` in README to something else", + "edits": [ + { + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "change `hello world` in README to something else", + "timestamp": TIMESTAMP, + "embeds": [], + }, + ], "base": PARENT, "oid": HEAD, "refs": [ diff --git a/radicle/src/cob/patch.rs b/radicle/src/cob/patch.rs index 1eb7ef4a..6506b654 100644 --- a/radicle/src/cob/patch.rs +++ b/radicle/src/cob/patch.rs @@ -1308,6 +1308,10 @@ impl Revision { self.description.last().body.as_str() } + pub fn edits(&self) -> impl Iterator { + self.description.iter() + } + pub fn embeds(&self) -> &Vec> { &self.description.last().embeds }