urbit_http_api/apps/
collections.rs1use crate::apps::notebook::Comment;
2use crate::graph::NodeContents;
3use crate::helper::{get_current_da_time, get_current_time};
4use crate::AuthoredMessage;
5use crate::{Channel, Node, Result, UrbitAPIError};
6
7pub struct Collection<'a> {
9 pub channel: &'a mut Channel,
10}
11
12#[derive(Clone, Debug)]
14pub struct Link {
15 pub title: String,
16 pub author: String,
17 pub time_sent: String,
18 pub url: String,
19 pub comments: Vec<Comment>,
20 pub index: String,
21}
22
23impl Link {
24 pub fn new(
26 title: &str,
27 author: &str,
28 time_sent: &str,
29 url: &str,
30 comments: &Vec<Comment>,
31 index: &str,
32 ) -> Link {
33 Link {
34 title: title.to_string(),
35 author: author.to_string(),
36 time_sent: time_sent.to_string(),
37 url: url.to_string(),
38 comments: comments.clone(),
39 index: index.to_string(),
40 }
41 }
42
43 pub fn from_node(node: &Node) -> Result<Link> {
45 let mut comments: Vec<Comment> = vec![];
46 if node.children.len() > 0 && node.children[0].children.len() > 0 {
48 for comment_node in &node.children {
50 let mut latest_comment_revision_node = comment_node.children[0].clone();
51 for revision_node in &comment_node.children {
52 if revision_node.index_tail() > latest_comment_revision_node.index_tail() {
53 latest_comment_revision_node = revision_node.clone();
54 }
55 }
56 comments.push(Comment::from_node(&latest_comment_revision_node));
57 }
58 }
59
60 let title = format!("{}", node.contents.content_list[0]["text"]);
62 let url = format!("{}", node.contents.content_list[1]["url"]);
64 let author = node.author.clone();
65 let time_sent = node.time_sent_formatted();
66
67 Ok(Link::new(
69 &title,
70 &author,
71 &time_sent,
72 &url,
73 &comments,
74 &node.index,
75 ))
76 }
77}
78
79impl<'a> Collection<'a> {
80 pub fn export_collection(
82 &mut self,
83 collection_ship: &str,
84 collection_name: &str,
85 ) -> Result<Vec<Link>> {
86 let graph = &self
87 .channel
88 .graph_store()
89 .get_graph(collection_ship, collection_name)?;
90
91 let mut links = vec![];
93 for node in &graph.nodes {
94 let link = Link::from_node(node)?;
95 links.push(link);
96 }
97
98 Ok(links)
99 }
100
101 pub fn add_link(
104 &mut self,
105 collection_ship: &str,
106 collection_name: &str,
107 title: &str,
108 url: &str,
109 ) -> Result<String> {
110 let mut gs = self.channel.graph_store();
111
112 let link_contents = NodeContents::new().add_text(title).add_url(url);
113 let link_node = gs.new_node(&link_contents);
114
115 if let Ok(_) = gs.add_node(collection_ship, collection_name, &link_node) {
116 Ok(link_node.index)
117 } else {
118 Err(UrbitAPIError::FailedToCreateNote(
119 link_node.to_json().dump(),
120 ))
121 }
122 }
123}