1use std::collections::HashMap;
2use std::ops::Deref;
3use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
5use std::time::Duration;
6
7use base64::Engine;
8use base64::engine::general_purpose::STANDARD;
9use serde::{Deserialize, Serialize};
10use serde_json::{Value, json};
11use tokio::sync::Notify;
12use yrs::types::ToJson;
13use yrs::updates::decoder::Decode;
14use yrs::{
15 Any, Array, ArrayRef, Doc, GetString, Map, MapPrelim, MapRef, OffsetKind, Options, Origin, Out,
16 ReadTxn, Subscription, Text, Transact, Update,
17};
18
19use crate::drive::{self, DriveAuthorization};
20use crate::error::{Error, Result};
21use crate::parrot::share::Attachment;
22use crate::shared::server::{
23 Authorization, Hooks as ServerHooks, Presence, Room, connect as connect_server,
24};
25
26#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(rename_all = "lowercase")]
31pub enum Kind {
32 Text,
33 Table,
34 Image,
35 #[default]
36 View,
37}
38
39#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
42#[serde(rename_all = "lowercase")]
43pub enum Role {
44 #[default]
45 Read,
46 Write,
47}
48
49pub const EDIT_MAX_BYTES: u64 = 1024 * 1024;
51
52const TEXT_TYPES: [&str; 7] = [
53 "application/json",
54 "application/xml",
55 "application/javascript",
56 "application/x-sh",
57 "application/x-yaml",
58 "application/toml",
59 "application/x-ndjson",
60];
61const TEXT_EXTENSIONS: [&str; 48] = [
62 "txt", "md", "markdown", "json", "jsonl", "xml", "html", "htm", "css", "js", "mjs", "cjs",
63 "jsx", "ts", "tsx", "py", "rb", "go", "rs", "java", "kt", "c", "h", "cc", "cpp", "hpp", "cs",
64 "php", "sh", "bash", "zsh", "sql", "yaml", "yml", "toml", "ini", "cfg", "conf", "log", "env",
65 "r", "swift", "lua", "pl", "scala", "vue", "svelte", "tsv",
66];
67
68fn extension(name: &str) -> Option<String> {
69 let (_, extension) = name.rsplit_once('.')?;
70 Some(extension.to_ascii_lowercase())
71}
72
73pub fn is_text(content_type: &str, name: &str) -> bool {
75 content_type.starts_with("text/")
76 || TEXT_TYPES.contains(&content_type)
77 || extension(name).is_some_and(|extension| TEXT_EXTENSIONS.contains(&extension.as_str()))
78}
79
80pub fn kind_of(content_type: &str, name: &str, size: u64) -> Kind {
82 if content_type.starts_with("image/") {
83 return Kind::Image;
84 }
85 if size > EDIT_MAX_BYTES {
86 return Kind::View;
87 }
88 if content_type == "text/csv" || extension(name).as_deref() == Some("csv") {
89 return Kind::Table;
90 }
91 if is_text(content_type, name) {
92 return Kind::Text;
93 }
94 Kind::View
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
99#[serde(rename_all = "lowercase")]
100pub enum MarkKind {
101 Pen,
102 Box,
103 Arrow,
104 Note,
105 Pin,
106}
107
108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
110pub struct Mark {
111 pub id: String,
112 pub kind: MarkKind,
113 pub points: Vec<[f64; 2]>,
114 #[serde(default, skip_serializing_if = "Option::is_none")]
116 pub text: Option<String>,
117 pub author: String,
119 pub color: String,
120 pub at: f64,
121}
122
123pub fn parse_csv(text: &str) -> Vec<Vec<String>> {
125 let mut rows = Vec::new();
126 let mut row = Vec::new();
127 let mut cell = String::new();
128 let mut quoted = false;
129 let mut chars = text.chars().peekable();
130 while let Some(c) = chars.next() {
131 if quoted {
132 if c == '"' && chars.peek() == Some(&'"') {
133 cell.push('"');
134 chars.next();
135 } else if c == '"' {
136 quoted = false;
137 } else {
138 cell.push(c);
139 }
140 } else if c == '"' && cell.is_empty() {
141 quoted = true;
142 } else if c == ',' {
143 row.push(std::mem::take(&mut cell));
144 } else if c == '\n' || c == '\r' {
145 if c == '\r' && chars.peek() == Some(&'\n') {
146 chars.next();
147 }
148 row.push(std::mem::take(&mut cell));
149 rows.push(std::mem::take(&mut row));
150 } else {
151 cell.push(c);
152 }
153 }
154 if !cell.is_empty() || !row.is_empty() {
155 row.push(cell);
156 rows.push(row);
157 }
158 rows
159}
160
161pub fn format_csv(rows: &[Vec<String>]) -> String {
163 let quote = |cell: &String| {
164 let needs = cell.contains(['"', ',', '\r', '\n'])
165 || cell.starts_with(char::is_whitespace)
166 || cell.ends_with(char::is_whitespace);
167 if needs {
168 format!("\"{}\"", cell.replace('"', "\"\""))
169 } else {
170 cell.clone()
171 }
172 };
173 let mut text = rows
174 .iter()
175 .map(|row| row.iter().map(quote).collect::<Vec<_>>().join(","))
176 .collect::<Vec<_>>()
177 .join("\n");
178 if !rows.is_empty() {
179 text.push('\n');
180 }
181 text
182}
183
184fn radix36(mut value: u128) -> String {
190 const DIGITS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";
191 let mut out = Vec::new();
192 loop {
193 out.push(DIGITS[(value % 36) as usize]);
194 value /= 36;
195 if value == 0 {
196 break;
197 }
198 }
199 out.reverse();
200 String::from_utf8(out).unwrap_or_default()
201}
202
203fn id() -> String {
204 let random = radix36(uuid::Uuid::new_v4().as_u128());
205 let now = radix36(chrono::Utc::now().timestamp_millis().max(0) as u128);
206 format!("{}{}", &random[..8], &now[now.len().saturating_sub(4)..])
207}
208
209fn new_doc() -> Doc {
211 Doc::with_options(Options {
212 offset_kind: OffsetKind::Utf16,
213 ..Options::default()
214 })
215}
216
217fn out_text<T: ReadTxn>(out: Out, txn: &T) -> String {
218 match out {
219 Out::Any(Any::String(text)) => text.to_string(),
220 out => out.to_string(txn),
221 }
222}
223
224fn strings<T: ReadTxn>(array: &ArrayRef, txn: &T) -> Vec<String> {
225 array.iter(txn).map(|out| out_text(out, txn)).collect()
226}
227
228fn position(array: &[String], item: &str) -> Option<u32> {
229 array
230 .iter()
231 .position(|other| other == item)
232 .map(|index| index as u32)
233}
234
235pub fn table_rows(doc: &Doc) -> Vec<Vec<String>> {
237 let columns = doc.get_or_insert_array("columns");
238 let rows = doc.get_or_insert_array("rows");
239 let cells = doc.get_or_insert_map("cells");
240 let txn = doc.transact();
241 let columns = strings(&columns, &txn);
242 strings(&rows, &txn)
243 .iter()
244 .map(|row| {
245 let map: Option<MapRef> = match cells.get(&txn, row) {
246 Some(Out::YMap(map)) => Some(map),
247 _ => None,
248 };
249 columns
250 .iter()
251 .map(|column| {
252 map.as_ref()
253 .and_then(|map| map.get(&txn, column))
254 .map(|out| out_text(out, &txn))
255 .unwrap_or_default()
256 })
257 .collect()
258 })
259 .collect()
260}
261
262pub fn fill_table(doc: &Doc, rows: &[Vec<String>]) {
264 let width = rows.iter().map(Vec::len).max().unwrap_or(0).max(1);
265 let columns: Vec<String> = (0..width).map(|_| id()).collect();
266 let row_ids: Vec<String> = rows.iter().map(|_| id()).collect();
267 let columns_ref = doc.get_or_insert_array("columns");
268 let rows_ref = doc.get_or_insert_array("rows");
269 let cells = doc.get_or_insert_map("cells");
270 let mut txn = doc.transact_mut();
271 let length = columns_ref.len(&txn);
272 columns_ref.insert_range(&mut txn, length, columns.clone());
273 let length = rows_ref.len(&txn);
274 rows_ref.insert_range(&mut txn, length, row_ids.clone());
275 for (r, row) in rows.iter().enumerate() {
276 let map: MapPrelim = columns
277 .iter()
278 .zip(row)
279 .filter(|(_, cell)| !cell.is_empty())
280 .map(|(column, cell)| (column.clone(), cell.clone()))
281 .collect();
282 cells.insert(&mut txn, row_ids[r].clone(), map);
283 }
284}
285
286pub fn add_row(doc: &Doc, after: Option<&str>) -> String {
288 let rows = doc.get_or_insert_array("rows");
289 let cells = doc.get_or_insert_map("cells");
290 let row = id();
291 let mut txn = doc.transact_mut();
292 let length = rows.len(&txn);
293 let index = match after {
294 Some(after) => position(&strings(&rows, &txn), after).map_or(0, |index| index + 1),
295 None => length,
296 };
297 rows.insert(&mut txn, index.min(length), row.clone());
298 cells.insert(&mut txn, row.clone(), MapPrelim::default());
299 row
300}
301
302pub fn add_column(doc: &Doc, after: Option<&str>) -> String {
304 let columns = doc.get_or_insert_array("columns");
305 let column = id();
306 let mut txn = doc.transact_mut();
307 let length = columns.len(&txn);
308 let index = match after {
309 Some(after) => position(&strings(&columns, &txn), after).map_or(0, |index| index + 1),
310 None => length,
311 };
312 columns.insert(&mut txn, index.min(length), column.clone());
313 column
314}
315
316pub fn remove_row(doc: &Doc, row: &str) {
317 let rows = doc.get_or_insert_array("rows");
318 let cells = doc.get_or_insert_map("cells");
319 let mut txn = doc.transact_mut();
320 if let Some(index) = position(&strings(&rows, &txn), row) {
321 rows.remove(&mut txn, index);
322 }
323 cells.remove(&mut txn, row);
324}
325
326pub fn remove_column(doc: &Doc, column: &str) {
327 let columns = doc.get_or_insert_array("columns");
328 let mut txn = doc.transact_mut();
329 if let Some(index) = position(&strings(&columns, &txn), column) {
330 columns.remove(&mut txn, index);
331 }
332}
333
334fn decode(content: Option<&[u8]>) -> String {
335 let text = String::from_utf8_lossy(content.unwrap_or_default());
336 text.strip_prefix('\u{feff}').unwrap_or(&text).to_string()
337}
338
339fn any_from_json(value: &Value) -> Any {
340 match value {
341 Value::Null => Any::Null,
342 Value::Bool(value) => Any::Bool(*value),
343 Value::Number(number) => Any::Number(number.as_f64().unwrap_or_default()),
344 Value::String(value) => Any::String(value.as_str().into()),
345 Value::Array(values) => Any::Array(values.iter().map(any_from_json).collect()),
346 Value::Object(fields) => Any::Map(Arc::new(
347 fields
348 .iter()
349 .map(|(key, value)| (key.clone(), any_from_json(value)))
350 .collect::<HashMap<_, _>>(),
351 )),
352 }
353}
354
355const MAX_SAFE_INTEGER: f64 = 9_007_199_254_740_991.0;
357
358const MARK_FIELDS: [&str; 7] = ["id", "kind", "points", "text", "author", "color", "at"];
360
361fn write_json(any: &Any, out: &mut String) {
362 match any {
363 Any::Null | Any::Undefined => out.push_str("null"),
364 Any::Bool(value) => out.push_str(if *value { "true" } else { "false" }),
365 Any::Number(number) if number.trunc() == *number && number.abs() <= MAX_SAFE_INTEGER => {
366 out.push_str(&(*number as i64).to_string())
367 }
368 Any::Number(number) => out.push_str(&Value::from(*number).to_string()),
369 Any::BigInt(int) => out.push_str(&int.to_string()),
370 Any::String(value) => out.push_str(&Value::from(value.as_ref()).to_string()),
371 Any::Buffer(bytes) => out.push_str(&Value::from(bytes.to_vec()).to_string()),
372 Any::Array(values) => {
373 out.push('[');
374 for (index, value) in values.iter().enumerate() {
375 if index > 0 {
376 out.push(',');
377 }
378 write_json(value, out);
379 }
380 out.push(']');
381 }
382 Any::Map(fields) => {
383 let mut keys: Vec<&String> = fields
384 .iter()
385 .filter(|(_, value)| !matches!(value, Any::Undefined))
386 .map(|(key, _)| key)
387 .collect();
388 keys.sort_by_key(|key| {
389 (
390 MARK_FIELDS
391 .iter()
392 .position(|field| field == key)
393 .unwrap_or(MARK_FIELDS.len()),
394 key.as_str(),
395 )
396 });
397 out.push('{');
398 for (index, key) in keys.into_iter().enumerate() {
399 if index > 0 {
400 out.push(',');
401 }
402 out.push_str(&Value::from(key.as_str()).to_string());
403 out.push(':');
404 write_json(&fields[key.as_str()], out);
405 }
406 out.push('}');
407 }
408 }
409}
410
411pub fn doc_from(kind: Kind, content: Option<&[u8]>) -> Doc {
413 let doc = new_doc();
414 let text = decode(content);
415 match kind {
416 Kind::Text => {
417 let shared = doc.get_or_insert_text("text");
418 shared.insert(&mut doc.transact_mut(), 0, &text);
419 }
420 Kind::Table => fill_table(&doc, &parse_csv(&text)),
421 Kind::Image => {
422 let marks = if text.is_empty() {
423 Vec::new()
424 } else {
425 match serde_json::from_str::<Value>(&text) {
426 Ok(Value::Array(marks)) => marks,
427 _ => Vec::new(),
428 }
429 };
430 let shared = doc.get_or_insert_array("marks");
431 shared.insert_range(&mut doc.transact_mut(), 0, marks.iter().map(any_from_json));
432 }
433 Kind::View => {}
434 }
435 doc
436}
437
438pub fn content_of(kind: Kind, doc: &Doc) -> Vec<u8> {
440 match kind {
441 Kind::Text => {
442 let text = doc.get_or_insert_text("text");
443 text.get_string(&doc.transact()).into_bytes()
444 }
445 Kind::Table => format_csv(&table_rows(doc)).into_bytes(),
446 Kind::Image => {
447 let marks = doc.get_or_insert_array("marks");
448 let mut json = String::new();
449 write_json(&marks.to_json(&doc.transact()), &mut json);
450 json.into_bytes()
451 }
452 Kind::View => Vec::new(),
453 }
454}
455
456pub fn to_base64(bytes: &[u8]) -> String {
458 STANDARD.encode(bytes)
459}
460
461pub fn from_base64(text: &str) -> Result<Vec<u8>> {
462 STANDARD.decode(text).map_err(|_| {
463 Error::Message("The string to be decoded is not correctly encoded.".to_string())
464 })
465}
466
467#[derive(Debug, Clone)]
470#[allow(clippy::large_enum_variant)]
471pub enum Target {
472 File(drive::File),
473 Attachment(Attachment),
474}
475
476pub trait Hooks: Send + Sync {
479 fn on_people(&self, _room: &ViewRoom) {}
481 fn on_reload(&self, _room: &ViewRoom) {}
483 fn on_saved(&self, _room: &ViewRoom) {}
485 fn on_closed(&self, _room: &ViewRoom) {}
487}
488
489impl Hooks for () {}
490
491#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
494#[serde(rename_all = "camelCase")]
495pub struct ViewFile {
496 pub name: String,
497 pub content_type: String,
498 pub content_length: u64,
499}
500
501const BATCH: Duration = Duration::from_millis(50);
503const REMOTE: &str = "remote";
504
505#[derive(Default)]
506struct View {
507 kind: Kind,
508 role: Role,
509 owner: bool,
510 file: ViewFile,
511 ended: bool,
512 saved: u64,
514 hydrated: bool,
516}
517
518struct ViewState {
519 target: Target,
520 hooks: Mutex<Arc<dyn Hooks>>,
521 view: Mutex<View>,
522 doc: Mutex<Doc>,
523 subscription: Mutex<Option<Subscription>>,
524 outgoing: Mutex<Vec<Vec<u8>>>,
525 armed: AtomicBool,
526 notify: Notify,
527}
528
529fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
530 mutex.lock().unwrap_or_else(PoisonError::into_inner)
531}
532
533#[derive(Clone)]
538pub struct ViewRoom {
539 room: Room,
540 inner: Arc<ViewState>,
541}
542
543impl Deref for ViewRoom {
544 type Target = Room;
545 fn deref(&self) -> &Room {
546 &self.room
547 }
548}
549
550impl ViewRoom {
551 fn view(&self) -> MutexGuard<'_, View> {
552 lock(&self.inner.view)
553 }
554
555 fn hooks(&self) -> Arc<dyn Hooks> {
556 lock(&self.inner.hooks).clone()
557 }
558
559 pub fn target(&self) -> &Target {
560 &self.inner.target
561 }
562
563 pub fn kind(&self) -> Kind {
564 self.view().kind
565 }
566
567 pub fn role(&self) -> Role {
568 self.view().role
569 }
570
571 pub fn owner(&self) -> bool {
573 self.view().owner
574 }
575
576 pub fn file(&self) -> ViewFile {
578 self.view().file.clone()
579 }
580
581 pub fn doc(&self) -> Doc {
583 lock(&self.inner.doc).clone()
584 }
585
586 pub fn set_hooks(&self, hooks: Arc<dyn Hooks>) {
588 *lock(&self.inner.hooks) = hooks;
589 }
590
591 pub fn people(&self, me: Option<&Presence>) -> Vec<Presence> {
593 self.get_presences()
594 .into_iter()
595 .filter(|presence| me.is_none_or(|me| presence.id() != me.id()))
596 .collect()
597 }
598
599 pub fn people_changed(&self) {
601 self.hooks().on_people(self);
602 }
603
604 pub fn hydrate(&self, state: &Value) {
605 let reconnected = {
606 let mut view = self.view();
607 let reconnected = view.hydrated;
608 view.hydrated = true;
609 view.kind = serde_json::from_value(state["kind"].clone()).unwrap_or_default();
610 view.role = if state["role"] == "write" {
611 Role::Write
612 } else {
613 Role::Read
614 };
615 view.owner = state["owner"] == true;
616 let file = &state["file"];
617 view.file = ViewFile {
618 name: match &file["name"] {
619 Value::Null => String::new(),
620 Value::String(name) => name.clone(),
621 name => name.to_string(),
622 },
623 content_type: match &file["contentType"] {
624 Value::Null => String::new(),
625 Value::String(content_type) => content_type.clone(),
626 content_type => content_type.to_string(),
627 },
628 content_length: file["contentLength"].as_f64().unwrap_or_default() as u64,
629 };
630 view.saved = state["saved"].as_f64().unwrap_or_default() as u64;
631 reconnected
632 };
633 self.replace(state["doc"].as_str());
634 if reconnected {
636 self.hooks().on_reload(self);
637 }
638 }
639
640 pub fn handle_event(&self, packet: &Value) -> bool {
641 let data = &packet["data"];
642 match packet["event"].as_str() {
643 Some("doc-update") if data["update"].is_string() => {
644 if let Ok(update) = from_base64(data["update"].as_str().unwrap_or_default()) {
645 apply_remote(&self.doc(), &update);
646 }
647 true
648 }
649 Some("doc-reload") => {
650 if let Some(snapshot) = data["doc"].as_str() {
651 self.replace(Some(snapshot));
652 }
653 self.hooks().on_reload(self);
654 true
655 }
656 Some("doc-saved") => {
657 let saved = data["saved"].as_f64().unwrap_or_default() as u64;
658 let newer = {
659 let mut view = self.view();
660 let newer = saved > view.saved;
661 if newer {
662 view.saved = saved;
663 }
664 newer
665 };
666 if newer {
667 self.hooks().on_saved(self);
668 }
669 true
670 }
671 _ => false,
672 }
673 }
674
675 fn replace(&self, snapshot: Option<&str>) {
676 let doc = new_doc();
677 if let Some(update) = snapshot.and_then(|snapshot| from_base64(snapshot).ok()) {
678 apply_remote(&doc, &update);
679 }
680 let state = Arc::downgrade(&self.inner);
681 let subscription = doc.observe_update_v1(move |txn, event| {
682 if txn.origin() == Some(&Origin::from(REMOTE)) {
683 return;
684 }
685 let Some(state) = state.upgrade() else {
686 return;
687 };
688 {
689 let view = lock(&state.view);
690 if view.role != Role::Write || view.ended {
691 return;
692 }
693 }
694 lock(&state.outgoing).push(event.update.clone());
695 if !state.armed.swap(true, Ordering::SeqCst) {
696 state.notify.notify_one();
697 }
698 });
699 *lock(&self.inner.subscription) = subscription.ok();
700 *lock(&self.inner.doc) = doc;
701 }
702
703 pub fn flush(&self) {
705 self.inner.armed.store(false, Ordering::SeqCst);
706 let outgoing = std::mem::take(&mut *lock(&self.inner.outgoing));
707 if outgoing.is_empty() {
708 return;
709 }
710 let updates = if outgoing.len() == 1 {
711 outgoing
712 } else {
713 match yrs::merge_updates_v1(&outgoing) {
714 Ok(update) => vec![update],
715 Err(_) => outgoing,
716 }
717 };
718 for update in updates {
719 self.post(&json!({ "event": "doc-update", "data": { "update": to_base64(&update) } }));
720 }
721 }
722
723 pub fn closed(&self) {
725 {
726 let mut view = self.view();
727 if view.ended {
728 return;
729 }
730 view.ended = true;
731 }
732 self.inner.notify.notify_one();
733 self.hooks().on_closed(self);
734 }
735
736 pub fn disconnect(&self) {
737 self.flush();
738 self.view().ended = true;
739 self.inner.notify.notify_one();
740 self.room.disconnect();
741 }
742}
743
744fn apply_remote(doc: &Doc, update: &[u8]) {
745 if let Ok(update) = Update::decode_v1(update) {
746 let _ = doc.transact_mut_with(REMOTE).apply_update(update);
747 }
748}
749
750struct ViewHooks {
751 inner: Arc<ViewState>,
752}
753
754impl ViewHooks {
755 fn room(&self, room: &Room) -> ViewRoom {
756 ViewRoom {
757 room: room.clone(),
758 inner: self.inner.clone(),
759 }
760 }
761}
762
763impl ServerHooks for ViewHooks {
764 fn on_connection(&self, room: &Room, state: &Value) {
765 self.room(room).hydrate(state);
766 }
767
768 fn on_event(&self, room: &Room, packet: &Value) -> bool {
769 self.room(room).handle_event(packet)
770 }
771
772 fn on_presence(&self, room: &Room) {
773 self.room(room).people_changed();
774 }
775
776 fn on_closed(&self, room: &Room, _reason: Option<&str>) {
777 self.room(room).closed();
778 }
779}
780
781fn authorization_for(target: &Target) -> Result<Authorization> {
782 let file = match target {
783 Target::Attachment(attachment) => {
784 if !attachment.chat.is_empty() && !attachment.r#ref.is_empty() {
785 let authorization = match &attachment.file.authorization {
786 DriveAuthorization::Path { authorization, .. }
787 | DriveAuthorization::AccessCode { authorization, .. }
788 | DriveAuthorization::Repository { authorization, .. } => authorization.clone(),
789 };
790 return Ok(Authorization::Server {
791 authorization,
792 server: format!("fauna:file:chat:{}/{}", attachment.chat, attachment.r#ref),
793 group: None,
794 });
795 }
796 &attachment.file
797 }
798 Target::File(file) => file,
799 };
800 Ok(match &file.authorization {
801 DriveAuthorization::AccessCode {
802 authorization,
803 access_code,
804 } => Authorization::AccessCode {
805 authorization: authorization.clone(),
806 access_code: access_code.clone(),
807 },
808 DriveAuthorization::Repository {
809 authorization,
810 repository,
811 path,
812 group,
813 } => Authorization::Server {
814 authorization: authorization.clone(),
815 server: format!(
816 "fauna:file:repository:{repository}/{}",
817 path.trim_start_matches('/')
818 ),
819 group: group.clone(),
820 },
821 DriveAuthorization::Path {
822 authorization,
823 path,
824 group,
825 } => Authorization::Server {
826 authorization: authorization.clone(),
827 server: format!("fauna:file:{path}"),
828 group: group.clone(),
829 },
830 })
831}
832
833pub async fn connect(
837 target: Target,
838 hooks: Option<Arc<dyn Hooks>>,
839) -> Result<(ViewRoom, Presence)> {
840 let authorization = authorization_for(&target)?;
841 let inner = Arc::new(ViewState {
842 target,
843 hooks: Mutex::new(hooks.unwrap_or_else(|| Arc::new(()))),
844 view: Mutex::new(View::default()),
845 doc: Mutex::new(new_doc()),
846 subscription: Mutex::new(None),
847 outgoing: Mutex::new(Vec::new()),
848 armed: AtomicBool::new(false),
849 notify: Notify::new(),
850 });
851 let me = connect_server(
852 authorization,
853 &format!("{}/drive/live", crate::get_url()),
854 Arc::new(ViewHooks {
855 inner: inner.clone(),
856 }),
857 )
858 .await?;
859 let room = ViewRoom {
860 room: me.room().clone(),
861 inner,
862 };
863
864 let batching = room.clone();
865 tokio::spawn(async move {
866 loop {
867 batching.inner.notify.notified().await;
868 if batching.view().ended {
869 break;
870 }
871 tokio::time::sleep(BATCH).await;
872 if batching.view().ended {
873 break;
874 }
875 batching.flush();
876 }
877 });
878
879 let mut socket_closed = room.socket_closed();
880 let closing = room.clone();
881 tokio::spawn(async move {
882 let _ = socket_closed.wait_for(|closed| *closed).await;
883 closing.closed();
884 });
885
886 Ok((room, me))
887}
888
889#[cfg(test)]
890mod tests {
891 use std::sync::{Arc, Mutex};
892
893 use serde_json::{Value, json};
894 use tokio::sync::mpsc;
895 use tokio_tungstenite::tungstenite::Message;
896 use yrs::{GetString, ReadTxn, StateVector, Text, Transact};
897
898 use super::*;
899 use crate::authentification::{ApiKey, CredentialsOrApiKey};
900 use crate::drive::{FileMeta, from_access_code};
901
902 #[derive(Default)]
903 struct Recorder {
904 events: Mutex<Vec<&'static str>>,
905 }
906
907 impl Hooks for Recorder {
908 fn on_reload(&self, _room: &ViewRoom) {
909 self.events.lock().unwrap().push("reload");
910 }
911 fn on_saved(&self, _room: &ViewRoom) {
912 self.events.lock().unwrap().push("saved");
913 }
914 fn on_closed(&self, _room: &ViewRoom) {
915 self.events.lock().unwrap().push("closed");
916 }
917 }
918
919 fn view_room(recorder: Arc<Recorder>) -> (ViewRoom, mpsc::UnboundedReceiver<Message>) {
920 let (ws, outbox) = mpsc::unbounded_channel();
921 let authorization = CredentialsOrApiKey::ApiKey(ApiKey {
922 id: "key".to_string(),
923 ..ApiKey::default()
924 });
925 let file = from_access_code(authorization.clone(), "code", FileMeta::default());
926 let room = Room::new(
927 "http://localhost/drive/live".to_string(),
928 ws,
929 Authorization::AccessCode {
930 authorization,
931 access_code: "code".to_string(),
932 },
933 "room".to_string(),
934 );
935 let inner = Arc::new(ViewState {
936 target: Target::File(file),
937 hooks: Mutex::new(recorder),
938 view: Mutex::new(View::default()),
939 doc: Mutex::new(new_doc()),
940 subscription: Mutex::new(None),
941 outgoing: Mutex::new(Vec::new()),
942 armed: AtomicBool::new(false),
943 notify: Notify::new(),
944 });
945 (ViewRoom { room, inner }, outbox)
946 }
947
948 fn snapshot(text: &str) -> String {
949 let doc = doc_from(Kind::Text, Some(text.as_bytes()));
950 to_base64(
951 &doc.transact()
952 .encode_state_as_update_v1(&StateVector::default()),
953 )
954 }
955
956 fn text_of(doc: &Doc) -> String {
957 doc.get_or_insert_text("text").get_string(&doc.transact())
958 }
959
960 fn sent(outbox: &mut mpsc::UnboundedReceiver<Message>) -> Vec<Value> {
961 let mut frames = Vec::new();
962 while let Ok(Message::Text(frame)) = outbox.try_recv() {
963 frames.push(serde_json::from_str(frame.as_str()).unwrap());
964 }
965 frames
966 }
967
968 #[test]
969 fn writers_send_their_edits_and_never_echo_remote_ones() {
970 let recorder = Arc::new(Recorder::default());
971 let (room, mut outbox) = view_room(recorder.clone());
972 room.hydrate(&json!({
973 "kind": "text",
974 "role": "write",
975 "owner": true,
976 "file": { "name": "plan.md", "contentType": "text/markdown", "contentLength": 5 },
977 "saved": 2,
978 "doc": snapshot("hello"),
979 }));
980 assert_eq!(room.kind(), Kind::Text);
981 assert_eq!(room.role(), Role::Write);
982 assert!(room.owner());
983 assert_eq!(room.file().name, "plan.md");
984 assert_eq!(text_of(&room.doc()), "hello");
985
986 let peer = Doc::new();
987 apply_remote(
988 &peer,
989 &room
990 .doc()
991 .transact()
992 .encode_state_as_update_v1(&StateVector::default()),
993 );
994 let text = room.doc().get_or_insert_text("text");
995 text.insert(&mut room.doc().transact_mut(), 5, " there");
996 text.insert(&mut room.doc().transact_mut(), 0, ">");
997 room.flush();
998 let frames = sent(&mut outbox);
999 assert_eq!(frames.len(), 1);
1000 assert_eq!(frames[0]["event"], "doc-update");
1001 apply_remote(
1002 &peer,
1003 &from_base64(frames[0]["data"]["update"].as_str().unwrap()).unwrap(),
1004 );
1005 assert_eq!(text_of(&peer), ">hello there");
1006
1007 let peer_text = peer.get_or_insert_text("text");
1008 let update = {
1009 let before = room.doc().transact().state_vector();
1010 peer_text.insert(&mut peer.transact_mut(), 0, "!");
1011 peer.transact().encode_state_as_update_v1(&before)
1012 };
1013 assert!(room.handle_event(
1014 &json!({ "event": "doc-update", "data": { "update": to_base64(&update) } })
1015 ));
1016 assert_eq!(text_of(&room.doc()), "!>hello there");
1017 room.flush();
1018 assert!(sent(&mut outbox).is_empty());
1019
1020 room.handle_event(&json!({ "event": "doc-saved", "data": { "saved": 3 } }));
1021 room.handle_event(&json!({ "event": "doc-saved", "data": { "saved": 1 } }));
1022 room.handle_event(&json!({ "event": "doc-reload", "data": { "doc": snapshot("fresh") } }));
1023 assert_eq!(text_of(&room.doc()), "fresh");
1024 room.closed();
1025 room.closed();
1026 assert_eq!(
1027 *recorder.events.lock().unwrap(),
1028 vec!["saved", "reload", "closed"]
1029 );
1030
1031 let text = room.doc().get_or_insert_text("text");
1032 text.insert(&mut room.doc().transact_mut(), 0, "late ");
1033 room.flush();
1034 assert!(sent(&mut outbox).is_empty());
1035 }
1036
1037 #[test]
1038 fn readers_keep_their_edits_local() {
1039 let (room, mut outbox) = view_room(Arc::new(Recorder::default()));
1040 room.hydrate(&json!({ "kind": "text", "role": "read", "doc": snapshot("hi") }));
1041 let text = room.doc().get_or_insert_text("text");
1042 text.insert(&mut room.doc().transact_mut(), 2, "!");
1043 room.flush();
1044 assert!(sent(&mut outbox).is_empty());
1045 assert!(!room.handle_event(&json!({ "event": "something-else" })));
1046 }
1047
1048 #[test]
1049 fn repository_files_open_in_their_room() {
1050 let authorization = CredentialsOrApiKey::ApiKey(ApiKey {
1051 id: "key".to_string(),
1052 ..ApiKey::default()
1053 });
1054 let file = drive::File::from_json(
1055 &json!({ "name": "a.ts", "contentType": "text/typescript", "contentLength": 3 }),
1056 DriveAuthorization::Repository {
1057 authorization,
1058 repository: 41,
1059 path: "/repository/src/a.ts".to_string(),
1060 group: Some("7".to_string()),
1061 },
1062 );
1063 match authorization_for(&Target::File(file)).unwrap() {
1064 Authorization::Server { server, group, .. } => {
1065 assert_eq!(server, "fauna:file:repository:41/repository/src/a.ts");
1066 assert_eq!(group.as_deref(), Some("7"));
1067 }
1068 _ => panic!("a repository file opens by its server reference"),
1069 }
1070 }
1071}