From 1a63024a58463c7206b954a49b01c4c7c3d5f83e Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 13 Dec 2023 14:09:10 +0100 Subject: [PATCH] cob: improve Comment generics `Infallible` is a more appropriate type in comparsion to `()` for comments that do not require a `CodeLocation`. That is because if the type for `location` is `Option<()>`, this means that `Some(())` can be constructed, however, we never intend to construct that variant. On the other hand, `Infallible` can never be constructed, and so, only `None` can be used. This requires updates to some functions to use the generic `T` and also handle the different kinds of `Comment`s -- `Infallible` and `CodeLocation`. X-Co-Authored-By: Fintan Halpenny --- radicle-httpd/src/api/json.rs | 35 ++++++++++++++++++++++++---- radicle-httpd/src/api/v1/projects.rs | 2 ++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 11b28fe5a..0e829dae9 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -103,7 +103,7 @@ pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &impl AliasStore) -> Val "title": issue.title(), "state": issue.state(), "assignees": issue.assignees().collect::>(), - "discussion": issue.comments().map(|(id, c)| comment(id, c, aliases)).collect::>(), + "discussion": issue.comments().map(|(id, c)| issue_comment(id, c, aliases)).collect::>(), "labels": issue.labels().collect::>(), }) } @@ -132,7 +132,7 @@ pub(crate) fn patch( "base": rev.base(), "oid": rev.head(), "refs": get_refs(repo, patch.author().id(), &rev.head()).unwrap_or_default(), - "discussions": rev.discussion().comments().map(|(id, c)| comment(id, c, aliases)).collect::>(), + "discussions": rev.discussion().comments().map(|(id, c)| patch_comment(id, c, aliases)).collect::>(), "timestamp": rev.timestamp().as_secs(), "reviews": rev.reviews().map(|(nid, r)| review(nid, r, aliases)).collect::>(), }) @@ -172,8 +172,8 @@ fn review(nid: &NodeId, review: &Review, aliases: &impl AliasStore) -> Value { }) } -/// Returns JSON for a `Comment`. -fn comment(id: &CommentId, comment: &Comment, aliases: &impl AliasStore) -> Value { +/// 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())), @@ -194,6 +194,33 @@ fn comment(id: &CommentId, comment: &Comment, aliases: &impl AliasStore) -> Valu }) } +/// Returns JSON for a Patch `Comment`. +fn patch_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::>(), + "embeds": comment.embeds().to_vec(), + "reactions": comment.reactions().collect::>(), + "timestamp": comment.timestamp().as_secs(), + "replyTo": comment.reply_to(), + "location": comment.location(), + "resolved": comment.resolved(), + }) +} + /// Returns JSON for a `Review`. fn review_comment( id: &CommentId, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index d8f411cbd..79ac4025c 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -3041,6 +3041,7 @@ mod routes { "reactions": [["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8","🚀"]], "timestamp": TIMESTAMP, "replyTo": null, + "location": null, "resolved": false, }, { @@ -3063,6 +3064,7 @@ mod routes { "reactions": [], "timestamp": TIMESTAMP, "replyTo": comment_id, + "location": null, "resolved": false, }, ],