2023-05-10 05:27:50 +00:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
|
|
|
|
2023-05-15 14:16:05 +00:00
|
|
|
use appflowy_integrate::collab_builder::AppFlowyCollabBuilder;
|
2023-07-05 12:57:09 +00:00
|
|
|
use collab::core::collab::MutexCollab;
|
2023-06-03 05:55:43 +00:00
|
|
|
use collab_document::blocks::DocumentData;
|
2023-07-05 12:57:09 +00:00
|
|
|
use collab_document::document::Document;
|
2023-06-05 01:42:11 +00:00
|
|
|
use collab_document::YrsDocAction;
|
2023-04-13 10:53:51 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
use flowy_error::{internal_error, FlowyError, FlowyResult};
|
2023-04-28 06:08:53 +00:00
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
use crate::deps::{DocumentCloudService, DocumentUser};
|
|
|
|
use crate::entities::DocumentSnapshotPB;
|
|
|
|
use crate::{document::MutexDocument, document_data::default_document_data};
|
2023-04-13 10:53:51 +00:00
|
|
|
|
|
|
|
pub struct DocumentManager {
|
|
|
|
user: Arc<dyn DocumentUser>,
|
2023-05-15 14:16:05 +00:00
|
|
|
collab_builder: Arc<AppFlowyCollabBuilder>,
|
2023-07-05 12:57:09 +00:00
|
|
|
documents: Arc<RwLock<HashMap<String, Arc<MutexDocument>>>>,
|
|
|
|
#[allow(dead_code)]
|
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentManager {
|
2023-07-05 12:57:09 +00:00
|
|
|
pub fn new(
|
|
|
|
user: Arc<dyn DocumentUser>,
|
|
|
|
collab_builder: Arc<AppFlowyCollabBuilder>,
|
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
|
|
|
) -> Self {
|
2023-04-13 10:53:51 +00:00
|
|
|
Self {
|
|
|
|
user,
|
2023-05-15 14:16:05 +00:00
|
|
|
collab_builder,
|
|
|
|
documents: Default::default(),
|
2023-07-05 12:57:09 +00:00
|
|
|
cloud_service,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:43:46 +00:00
|
|
|
/// Create a new document.
|
|
|
|
///
|
|
|
|
/// if the document already exists, return the existing document.
|
|
|
|
/// if the data is None, will create a document with default data.
|
|
|
|
pub fn create_document(
|
|
|
|
&self,
|
2023-06-17 06:25:30 +00:00
|
|
|
doc_id: &str,
|
2023-06-03 12:43:46 +00:00
|
|
|
data: Option<DocumentData>,
|
2023-07-05 12:57:09 +00:00
|
|
|
) -> FlowyResult<Arc<MutexDocument>> {
|
|
|
|
tracing::trace!("create a document: {:?}", doc_id);
|
|
|
|
let collab = self.collab_for_document(doc_id)?;
|
2023-06-06 08:03:29 +00:00
|
|
|
let data = data.unwrap_or_else(default_document_data);
|
2023-07-05 12:57:09 +00:00
|
|
|
let document = Arc::new(MutexDocument::create_with_data(collab, data)?);
|
2023-04-17 02:12:04 +00:00
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
/// Return the document
|
|
|
|
pub fn get_document(&self, doc_id: &str) -> FlowyResult<Arc<MutexDocument>> {
|
2023-06-17 06:25:30 +00:00
|
|
|
if let Some(doc) = self.documents.read().get(doc_id) {
|
2023-04-13 10:53:51 +00:00
|
|
|
return Ok(doc.clone());
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
// Check if the document exists. If not, return error.
|
|
|
|
if !self.is_doc_exist(doc_id)? {
|
|
|
|
return Err(
|
|
|
|
FlowyError::record_not_found().context(format!("document: {} is not exist", doc_id)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-17 06:25:30 +00:00
|
|
|
tracing::debug!("open_document: {:?}", doc_id);
|
2023-07-05 12:57:09 +00:00
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let db = self.user.collab_db()?;
|
|
|
|
let collab = self.collab_builder.build(uid, doc_id, "document", db);
|
|
|
|
let document = Arc::new(MutexDocument::open(doc_id, collab)?);
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
// save the document to the memory and read it from the memory if we open the same document again.
|
|
|
|
// and we don't want to subscribe to the document changes if we open the same document again.
|
|
|
|
self
|
|
|
|
.documents
|
|
|
|
.write()
|
2023-06-17 06:25:30 +00:00
|
|
|
.insert(doc_id.to_string(), document.clone());
|
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
pub fn get_document_data(&self, doc_id: &str) -> FlowyResult<DocumentData> {
|
|
|
|
if !self.is_doc_exist(doc_id)? {
|
|
|
|
return Err(
|
|
|
|
FlowyError::record_not_found().context(format!("document: {} is not exist", doc_id)),
|
2023-05-16 06:58:24 +00:00
|
|
|
);
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
let collab = self.collab_for_document(doc_id)?;
|
|
|
|
Document::open(collab)?
|
|
|
|
.get_document_data()
|
|
|
|
.map_err(internal_error)
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 01:42:11 +00:00
|
|
|
pub fn close_document(&self, doc_id: &str) -> FlowyResult<()> {
|
|
|
|
self.documents.write().remove(doc_id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_document(&self, doc_id: &str) -> FlowyResult<()> {
|
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let db = self.user.collab_db()?;
|
|
|
|
let _ = db.with_write_txn(|txn| {
|
|
|
|
txn.delete_doc(uid, &doc_id)?;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
self.documents.write().remove(doc_id);
|
2023-04-13 10:53:51 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
|
|
|
|
/// Return the list of snapshots of the document.
|
|
|
|
pub async fn get_document_snapshots(
|
|
|
|
&self,
|
|
|
|
document_id: &str,
|
|
|
|
) -> FlowyResult<Vec<DocumentSnapshotPB>> {
|
|
|
|
let mut snapshots = vec![];
|
|
|
|
if let Some(snapshot) = self
|
|
|
|
.cloud_service
|
|
|
|
.get_document_latest_snapshot(document_id)
|
|
|
|
.await?
|
|
|
|
.map(|snapshot| DocumentSnapshotPB {
|
|
|
|
snapshot_id: snapshot.snapshot_id,
|
|
|
|
snapshot_desc: "".to_string(),
|
|
|
|
created_at: snapshot.created_at,
|
|
|
|
data: snapshot.data,
|
|
|
|
})
|
|
|
|
{
|
|
|
|
snapshots.push(snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(snapshots)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collab_for_document(&self, doc_id: &str) -> FlowyResult<Arc<MutexCollab>> {
|
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let db = self.user.collab_db()?;
|
|
|
|
Ok(self.collab_builder.build(uid, doc_id, "document", db))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_doc_exist(&self, doc_id: &str) -> FlowyResult<bool> {
|
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let db = self.user.collab_db()?;
|
|
|
|
let read_txn = db.read_txn();
|
|
|
|
Ok(read_txn.is_exist(uid, doc_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only expose this method for testing
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
pub fn get_cloud_service(&self) -> &Arc<dyn DocumentCloudService> {
|
|
|
|
&self.cloud_service
|
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|