1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::graph::NodeContents;
use crate::{Channel, Node, Result, UrbitAPIError};
use crossbeam::channel::{unbounded, Receiver};
use json::JsonValue;
use std::thread;
use std::time::Duration;
pub struct Chat<'a> {
pub channel: &'a mut Channel,
}
pub type Message = NodeContents;
#[derive(Clone, Debug)]
pub struct AuthoredMessage {
pub author: String,
pub contents: Message,
pub time_sent: String,
pub index: String,
}
impl AuthoredMessage {
pub fn new(author: &str, contents: &Message, time_sent: &str, index: &str) -> Self {
AuthoredMessage {
author: author.to_string(),
contents: contents.clone(),
time_sent: time_sent.to_string(),
index: index.to_string(),
}
}
pub fn from_node(node: &Node) -> Self {
Self::new(
&node.author,
&node.contents,
&node.time_sent_formatted(),
&node.index,
)
}
pub fn to_formatted_string(&self) -> String {
let content = self.contents.to_formatted_string();
format!("{} - ~{}:{}", self.time_sent, self.author, content)
}
}
impl<'a> Chat<'a> {
pub fn send_message(
&mut self,
chat_ship: &str,
chat_name: &str,
message: &Message,
) -> Result<String> {
let node = self.channel.graph_store().new_node(message);
if let Ok(_) = self
.channel
.graph_store()
.add_node(chat_ship, chat_name, &node)
{
Ok(node.index)
} else {
Err(UrbitAPIError::FailedToSendChatMessage(
message.to_json().dump(),
))
}
}
pub fn export_chat_log(&mut self, chat_ship: &str, chat_name: &str) -> Result<Vec<String>> {
let mut export_log = vec![];
let authored_messages = self.export_authored_messages(chat_ship, chat_name)?;
for am in authored_messages {
if !am.contents.is_empty() {
export_log.push(am.to_formatted_string());
}
}
Ok(export_log)
}
pub fn export_authored_messages(
&mut self,
chat_ship: &str,
chat_name: &str,
) -> Result<Vec<AuthoredMessage>> {
let mut authored_messages = vec![];
let nodes = self.export_chat_nodes(chat_ship, chat_name)?;
for node in nodes {
if !node.contents.is_empty() {
let authored_message = AuthoredMessage::from_node(&node);
authored_messages.push(authored_message);
}
}
Ok(authored_messages)
}
fn export_chat_nodes(&mut self, chat_ship: &str, chat_name: &str) -> Result<Vec<Node>> {
let chat_graph = &self.channel.graph_store().get_graph(chat_ship, chat_name)?;
let mut nodes = chat_graph.clone().nodes;
nodes.sort_by(|a, b| a.time_sent.cmp(&b.time_sent));
Ok(nodes)
}
pub fn subscribe_to_chat(
&mut self,
chat_ship: &str,
chat_name: &str,
) -> Result<Receiver<AuthoredMessage>> {
let chat_ship = chat_ship.to_string();
let chat_name = chat_name.to_string();
let (s, r) = unbounded();
let mut new_channel = self.channel.ship_interface.create_channel()?;
thread::spawn(move || {
let channel = &mut new_channel;
channel
.create_new_subscription("graph-store", "/updates")
.ok();
loop {
channel.parse_event_messages();
let res_graph_updates = &mut channel.find_subscription("graph-store", "/updates");
if let Some(graph_updates) = res_graph_updates {
loop {
let pop_res = graph_updates.pop_message();
if let Some(mess) = &pop_res {
if let Ok(json) = json::parse(mess) {
if !Self::check_resource_json(&chat_ship, &chat_name, &json) {
continue;
}
if let Ok(node) = Node::from_graph_update_json(&json) {
let authored_message = AuthoredMessage::from_node(&node);
let _ = s.send(authored_message);
}
}
}
if let None = &pop_res {
break;
}
}
}
thread::sleep(Duration::new(0, 500000000));
}
});
Ok(r)
}
fn check_resource_json(chat_ship: &str, chat_name: &str, resource_json: &JsonValue) -> bool {
let resource = resource_json["graph-update"]["add-nodes"]["resource"].clone();
let json_chat_name = format!("{}", resource["name"]);
let json_chat_ship = format!("~{}", resource["ship"]);
if json_chat_name == chat_name && json_chat_ship == chat_ship {
return true;
}
false
}
}