/ src / api / json / thread.rs
thread.rs
 1  use serde_json::{json, Value};
 2  
 3  use radicle::cob;
 4  use radicle::cob::CodeLocation;
 5  use radicle::git::Oid;
 6  use radicle::node::AliasStore;
 7  
 8  use super::{diff, edit, embeds, reactions, Author};
 9  
10  pub(crate) enum Comment<'a> {
11      Patch(&'a cob::thread::Comment<CodeLocation>),
12      Issue(&'a cob::thread::Comment),
13  }
14  
15  impl Comment<'_> {
16      pub fn as_json(&self, id: &Oid, aliases: &impl AliasStore) -> Value {
17          match self {
18              Comment::Issue(c) => json!({
19                  "id": *id,
20                  "author": Author::new(&c.author().into()).as_json(aliases),
21                  "body": c.body(),
22                  "edits": c.edits().map(|e| edit(e, aliases)).collect::<Vec<_>>(),
23                  "embeds": embeds(c.embeds()),
24                  "reactions": reactions(c.reactions(), None, aliases),
25                  "timestamp": c.timestamp().as_secs(),
26                  "replyTo": c.reply_to(),
27                  "resolved": c.is_resolved(),
28              }),
29              Comment::Patch(c) => json!({
30                  "id": *id,
31                  "author": Author::new(&c.author().into()).as_json(aliases),
32                  "body": c.body(),
33                  "edits": c.edits().map(|e| edit(e, aliases)).collect::<Vec<_>>(),
34                  "embeds": embeds(c.embeds()),
35                  "reactions": reactions(c.reactions(), None, aliases),
36                  "timestamp": c.timestamp().as_secs(),
37                  "replyTo": c.reply_to(),
38                  "location": c.location().map(|l| diff::CodeLocation::new(l).as_json()),
39                  "resolved": c.is_resolved(),
40              }),
41          }
42      }
43  }