Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
httpd: Add reactions to issue and patch json
Sebastian Martinez committed 3 years ago
commit 8826ec4f9cafdaf190dae19cc642f5e5c4b9ed51
parent 5d7a2f19d668a221737fbd2055fd648a4d756d97
2 files changed +48 -27
modified radicle-httpd/src/api/json.rs
@@ -8,8 +8,9 @@ use serde_json::{json, Value};

use radicle::cob::issue::{Issue, IssueId};
use radicle::cob::patch::{Patch, PatchId};
-
use radicle::cob::thread::{self, CommentId};
-
use radicle::cob::{ActorId, Author, Timestamp};
+
use radicle::cob::thread;
+
use radicle::cob::thread::{CommentId, Thread};
+
use radicle::cob::{ActorId, Author, Reaction, Timestamp};
use radicle::git::RefString;
use radicle::storage::{git, refs, ReadRepository};
use radicle_surf::blob::Blob;
@@ -98,7 +99,10 @@ pub(crate) fn issue(id: IssueId, issue: Issue) -> Value {
        "title": issue.title(),
        "state": issue.state(),
        "assignees": issue.assigned().collect::<Vec<_>>(),
-
        "discussion": issue.comments().collect::<Comments>(),
+
        "discussion": issue
+
          .comments()
+
          .map(|(id, comment)| Comment::new(id, comment, issue.thread()))
+
          .collect::<Vec<_>>(),
        "tags": issue.tags().collect::<Vec<_>>(),
    })
}
@@ -121,7 +125,10 @@ pub(crate) fn patch(id: PatchId, patch: Patch, repo: &git::Repository) -> Value
                "oid": rev.oid,
                "refs": get_refs(repo, patch.author().id(), &rev.oid).unwrap_or(vec![]),
                "merges": rev.merges().collect::<Vec<_>>(),
-
                "discussions": rev.discussion.comments().collect::<Comments>(),
+
                "discussions": rev.discussion
+
                  .comments()
+
                  .map(|(id, comment)| Comment::new(id, comment, &rev.discussion))
+
                  .collect::<Vec<_>>(),
                "timestamp": rev.timestamp,
                "reviews": rev.reviews().collect::<Vec<_>>(),
            })
@@ -160,33 +167,24 @@ fn get_refs(

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
-
struct Comment {
+
struct Comment<'a> {
    id: CommentId,
    author: Author,
-
    body: String,
-
    reactions: [String; 0],
+
    body: &'a str,
+
    reactions: Vec<(&'a ActorId, &'a Reaction)>,
    timestamp: Timestamp,
    reply_to: Option<CommentId>,
}

-
#[derive(Serialize)]
-
struct Comments(Vec<Comment>);
-

-
impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments {
-
    fn from_iter<I: IntoIterator<Item = (&'a CommentId, &'a thread::Comment)>>(iter: I) -> Self {
-
        let mut comments = Vec::new();
-

-
        for (id, comment) in iter {
-
            comments.push(Comment {
-
                id: id.to_owned(),
-
                author: comment.author().into(),
-
                body: comment.body().to_owned(),
-
                reactions: [],
-
                timestamp: comment.timestamp(),
-
                reply_to: comment.reply_to(),
-
            });
+
impl<'a> Comment<'a> {
+
    fn new(id: &'a CommentId, comment: &'a thread::Comment, thread: &'a Thread) -> Self {
+
        Self {
+
            id: *id,
+
            author: Author::new(comment.author()),
+
            body: comment.body(),
+
            reactions: thread.reactions(id).collect::<Vec<_>>(),
+
            timestamp: comment.timestamp(),
+
            reply_to: comment.reply_to(),
        }
-

-
        Comments(comments)
    }
}
modified radicle-httpd/src/api/v1/projects.rs
@@ -1523,6 +1523,24 @@ mod routes {
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(response.json().await, json!({ "success": true }));

+
        let body = serde_json::to_vec(&json!({
+
          "type": "thread",
+
          "action": {
+
            "type": "react",
+
            "to": "9685b141c2e939c3d60f8ca34f8c7bf01a609af1",
+
            "reaction": "🚀",
+
            "active": true,
+
          }
+
        }))
+
        .unwrap();
+
        patch(
+
            &app,
+
            format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"),
+
            Some(Body::from(body)),
+
            Some(SESSION_ID.to_string()),
+
        )
+
        .await;
+

        let response = get(
            &app,
            format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"),
@@ -1536,11 +1554,11 @@ mod routes {
              "author": {
                "id": CONTRIBUTOR_DID,
              },
-
              "assignees": [],
              "title": "Issue #1",
              "state": {
                "status": "open",
              },
+
              "assignees": [],
              "discussion": [
                {
                  "id": ISSUE_DISCUSSION_ID,
@@ -1558,7 +1576,12 @@ mod routes {
                    "id": CONTRIBUTOR_DID,
                  },
                  "body": "This is first-level comment",
-
                  "reactions": [],
+
                  "reactions": [
+
                    [
+
                      "z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8",
+
                      "🚀",
+
                    ],
+
                  ],
                  "timestamp": TIMESTAMP,
                  "replyTo": null,
                },