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;
|
|
|
|
use appflowy_integrate::RocksCollabDB;
|
2023-04-13 10:53:51 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
|
2023-04-28 06:08:53 +00:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
use crate::document_data::DocumentDataWrapper;
|
2023-04-13 10:53:51 +00:00
|
|
|
use crate::{
|
2023-05-16 06:58:24 +00:00
|
|
|
document::Document,
|
2023-04-24 06:25:00 +00:00
|
|
|
entities::DocEventPB,
|
2023-04-13 10:53:51 +00:00
|
|
|
notification::{send_notification, DocumentNotification},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub trait DocumentUser: Send + Sync {
|
|
|
|
fn user_id(&self) -> Result<i64, FlowyError>;
|
2023-05-21 10:53:59 +00:00
|
|
|
fn token(&self) -> Result<Option<String>, FlowyError>; // unused now.
|
2023-05-15 14:16:05 +00:00
|
|
|
fn collab_db(&self) -> Result<Arc<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>,
|
|
|
|
documents: Arc<RwLock<HashMap<String, Arc<Document>>>>,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentManager {
|
2023-05-15 14:16:05 +00:00
|
|
|
pub fn new(user: Arc<dyn DocumentUser>, collab_builder: Arc<AppFlowyCollabBuilder>) -> 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-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
pub fn create_document(
|
2023-04-18 11:06:21 +00:00
|
|
|
&self,
|
|
|
|
doc_id: String,
|
|
|
|
data: DocumentDataWrapper,
|
|
|
|
) -> FlowyResult<Arc<Document>> {
|
2023-05-16 06:58:24 +00:00
|
|
|
tracing::debug!("create a document: {:?}", &doc_id);
|
2023-05-15 14:16:05 +00:00
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let db = self.user.collab_db()?;
|
|
|
|
let collab = self.collab_builder.build(uid, &doc_id, db);
|
2023-04-24 06:25:00 +00:00
|
|
|
let document = Arc::new(Document::create_with_data(collab, data.0)?);
|
2023-04-17 02:12:04 +00:00
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
pub fn open_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
|
|
|
|
tracing::debug!("open a document: {:?}", &doc_id);
|
2023-04-13 10:53:51 +00:00
|
|
|
if let Some(doc) = self.documents.read().get(&doc_id) {
|
|
|
|
return Ok(doc.clone());
|
|
|
|
}
|
2023-04-24 06:25:00 +00:00
|
|
|
tracing::debug!("open_document: {:?}", &doc_id);
|
2023-05-15 14:16:05 +00:00
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let db = self.user.collab_db()?;
|
|
|
|
let collab = self.collab_builder.build(uid, &doc_id, db);
|
2023-05-16 06:58:24 +00:00
|
|
|
// read the existing document from the disk.
|
2023-04-24 06:25:00 +00:00
|
|
|
let document = Arc::new(Document::new(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()
|
|
|
|
.insert(doc_id.clone(), document.clone());
|
|
|
|
|
|
|
|
// subscribe to the document changes.
|
|
|
|
document.lock().open(move |events, is_remote| {
|
|
|
|
tracing::debug!(
|
|
|
|
"document changed: {:?}, from remote: {}",
|
|
|
|
&events,
|
|
|
|
is_remote
|
|
|
|
);
|
|
|
|
// send notification to the client.
|
|
|
|
send_notification(&doc_id, DocumentNotification::DidReceiveUpdate)
|
|
|
|
.payload::<DocEventPB>((events, is_remote).into())
|
|
|
|
.send();
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(document)
|
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
pub fn get_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
|
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let db = self.user.collab_db()?;
|
|
|
|
let collab = self.collab_builder.build(uid, &doc_id, db);
|
|
|
|
// read the existing document from the disk.
|
|
|
|
let document = Arc::new(Document::new(collab)?);
|
2023-04-13 10:53:51 +00:00
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close_document(&self, doc_id: String) -> FlowyResult<()> {
|
|
|
|
self.documents.write().remove(&doc_id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|