2023-07-29 01:46:24 +00:00
|
|
|
use std::sync::Weak;
|
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-29 01:46:24 +00:00
|
|
|
use appflowy_integrate::{CollabType, RocksCollabDB};
|
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-08-03 01:14:52 +00:00
|
|
|
use collab_document::document_data::default_document_data;
|
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-29 01:46:24 +00:00
|
|
|
use flowy_document_deps::cloud::DocumentCloudService;
|
2023-07-05 12:57:09 +00:00
|
|
|
use flowy_error::{internal_error, FlowyError, FlowyResult};
|
2023-04-28 06:08:53 +00:00
|
|
|
|
2023-08-05 07:02:05 +00:00
|
|
|
use crate::document::MutexDocument;
|
2023-07-05 12:57:09 +00:00
|
|
|
use crate::entities::DocumentSnapshotPB;
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-07-29 01:46:24 +00:00
|
|
|
pub trait DocumentUser: Send + Sync {
|
|
|
|
fn user_id(&self) -> Result<i64, FlowyError>;
|
|
|
|
fn token(&self) -> Result<Option<String>, FlowyError>; // unused now.
|
|
|
|
fn collab_db(&self, uid: i64) -> Result<Weak<RocksCollabDB>, FlowyError>;
|
|
|
|
}
|
|
|
|
|
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-07-29 01:46:24 +00:00
|
|
|
pub async fn initialize(&self, _uid: i64, _workspace_id: String) -> FlowyResult<()> {
|
|
|
|
self.documents.write().clear();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn initialize_with_new_user(&self, uid: i64, workspace_id: String) -> FlowyResult<()> {
|
|
|
|
self.initialize(uid, workspace_id).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
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);
|
2023-07-14 05:37:13 +00:00
|
|
|
let collab = self.collab_for_document(doc_id, vec![])?;
|
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
|
2023-07-14 05:37:13 +00:00
|
|
|
pub async 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-14 05:37:13 +00:00
|
|
|
let mut updates = vec![];
|
2023-07-05 12:57:09 +00:00
|
|
|
if !self.is_doc_exist(doc_id)? {
|
2023-07-14 05:37:13 +00:00
|
|
|
// Try to get the document from the cloud service
|
2023-08-05 07:02:05 +00:00
|
|
|
match self.cloud_service.get_document_updates(doc_id).await {
|
|
|
|
Ok(document_updates) => updates = document_updates,
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Get document data failed: {:?}", e);
|
|
|
|
return Err(FlowyError::internal().context("Can't not read the document data"));
|
|
|
|
},
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
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()?;
|
2023-07-14 05:37:13 +00:00
|
|
|
let db = self.user.collab_db(uid)?;
|
|
|
|
let collab = self
|
|
|
|
.collab_builder
|
2023-07-29 01:46:24 +00:00
|
|
|
.build(uid, doc_id, CollabType::Document, updates, db)?;
|
2023-07-05 12:57:09 +00:00
|
|
|
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-14 05:37:13 +00:00
|
|
|
pub async fn get_document_data(&self, doc_id: &str) -> FlowyResult<DocumentData> {
|
|
|
|
let mut updates = vec![];
|
2023-07-05 12:57:09 +00:00
|
|
|
if !self.is_doc_exist(doc_id)? {
|
2023-07-14 05:37:13 +00:00
|
|
|
if let Ok(document_updates) = self.cloud_service.get_document_updates(doc_id).await {
|
|
|
|
updates = document_updates;
|
|
|
|
} else {
|
|
|
|
return Err(
|
|
|
|
FlowyError::record_not_found().context(format!("document: {} is not exist", doc_id)),
|
|
|
|
);
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-07-14 05:37:13 +00:00
|
|
|
let collab = self.collab_for_document(doc_id, updates)?;
|
2023-07-05 12:57:09 +00:00
|
|
|
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()?;
|
2023-07-29 01:46:24 +00:00
|
|
|
if let Some(db) = self.user.collab_db(uid)?.upgrade() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-14 05:37:13 +00:00
|
|
|
fn collab_for_document(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
updates: Vec<Vec<u8>>,
|
|
|
|
) -> FlowyResult<Arc<MutexCollab>> {
|
2023-07-05 12:57:09 +00:00
|
|
|
let uid = self.user.user_id()?;
|
2023-07-14 05:37:13 +00:00
|
|
|
let db = self.user.collab_db(uid)?;
|
|
|
|
let collab = self
|
|
|
|
.collab_builder
|
2023-07-29 01:46:24 +00:00
|
|
|
.build(uid, doc_id, CollabType::Document, updates, db)?;
|
2023-07-14 05:37:13 +00:00
|
|
|
Ok(collab)
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_doc_exist(&self, doc_id: &str) -> FlowyResult<bool> {
|
|
|
|
let uid = self.user.user_id()?;
|
2023-07-29 01:46:24 +00:00
|
|
|
if let Some(collab_db) = self.user.collab_db(uid)?.upgrade() {
|
|
|
|
let read_txn = collab_db.read_txn();
|
|
|
|
Ok(read_txn.is_exist(uid, doc_id))
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
}
|