httpd: Conform response from issue handlers to previous model

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2022-12-08 19:13:07 +00:00 committed by Alexis Sellier
parent 5d2be57cd9
commit fb8020e582
No known key found for this signature in database
1 changed files with 44 additions and 6 deletions

View File

@ -11,8 +11,10 @@ use serde_json::json;
use tower_http::set_header::SetResponseHeaderLayer;
use radicle::cob::issue::Issues;
use radicle::cob::thread::{self, CommentId};
use radicle::cob::Timestamp;
use radicle::git::raw::BranchType;
use radicle::identity::{Doc, Id};
use radicle::identity::{Doc, Id, PublicKey};
use radicle::node::NodeId;
use radicle::storage::{Oid, ReadRepository, WriteRepository, WriteStorage};
use radicle_surf::git::History;
@ -330,11 +332,11 @@ async fn issues_handler(
.filter_map(|r| r.ok())
.map(|(id, issue, _)| {
json!({
"id": id,
"id": id.to_string(),
"author": issue.author(),
"title": issue.title(),
"status": issue.state(),
"discussion": issue.comments().collect::<Vec<_>>(),
"state": issue.state(),
"discussion": issue.comments().collect::<Comments>(),
"tags": issue.tags().collect::<Vec<_>>(),
})
})
@ -360,14 +362,50 @@ async fn issue_handler(
"id": issue_id,
"author": issue.author(),
"title": issue.title(),
"status": issue.state(),
"discussion": issue.comments().collect::<Vec<_>>(),
"state": issue.state(),
"discussion": issue.comments().collect::<Comments>(),
"tags": issue.tags().collect::<Vec<_>>(),
});
Ok::<_, Error>(Json(issue))
}
#[derive(Serialize)]
struct Author {
id: PublicKey,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Comment {
author: Author,
body: String,
reactions: [String; 0],
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 (comment_id, comment) in iter {
comments.push(Comment {
author: Author { id: comment_id.1 },
body: comment.body.to_owned(),
reactions: [],
timestamp: comment.timestamp,
reply_to: comment.reply_to,
});
}
Comments(comments)
}
}
#[derive(Serialize)]
struct Stats {
branches: usize,