2021-09-23 13:15:35 +08:00
|
|
|
use crate::{
|
2021-10-19 13:04:09 +08:00
|
|
|
entities::doc::{DocDelta, DocIdentifier},
|
2021-09-23 13:15:35 +08:00
|
|
|
errors::DocError,
|
2021-09-27 23:23:23 +08:00
|
|
|
services::{
|
2021-10-02 17:19:54 +08:00
|
|
|
doc::{doc_controller::DocController, edit::ClientEditDoc},
|
2021-09-27 23:23:23 +08:00
|
|
|
server::construct_doc_server,
|
|
|
|
ws::WsDocumentManager,
|
|
|
|
},
|
2021-09-23 13:15:35 +08:00
|
|
|
};
|
2021-10-06 15:23:38 +08:00
|
|
|
use flowy_database::ConnectionPool;
|
|
|
|
use flowy_net::config::ServerConfig;
|
|
|
|
use std::sync::Arc;
|
2021-07-22 22:26:38 +08:00
|
|
|
|
2021-07-31 10:50:56 +08:00
|
|
|
pub trait DocumentUser: Send + Sync {
|
2021-09-20 15:38:55 +08:00
|
|
|
fn user_dir(&self) -> Result<String, DocError>;
|
2021-09-09 15:43:05 +08:00
|
|
|
fn user_id(&self) -> Result<String, DocError>;
|
|
|
|
fn token(&self) -> Result<String, DocError>;
|
2021-07-23 14:37:18 +08:00
|
|
|
}
|
|
|
|
|
2021-09-13 23:09:57 +08:00
|
|
|
pub struct FlowyDocument {
|
2021-09-23 13:15:35 +08:00
|
|
|
doc_ctrl: Arc<DocController>,
|
2021-09-11 14:26:30 +08:00
|
|
|
}
|
2021-07-22 22:26:38 +08:00
|
|
|
|
2021-09-13 23:09:57 +08:00
|
|
|
impl FlowyDocument {
|
2021-09-28 15:29:29 +08:00
|
|
|
pub fn new(
|
|
|
|
user: Arc<dyn DocumentUser>,
|
2021-10-04 14:24:35 +08:00
|
|
|
ws_manager: Arc<WsDocumentManager>,
|
2021-09-28 15:29:29 +08:00
|
|
|
server_config: &ServerConfig,
|
|
|
|
) -> FlowyDocument {
|
|
|
|
let server = construct_doc_server(server_config);
|
2021-09-22 23:21:44 +08:00
|
|
|
let controller = Arc::new(DocController::new(server.clone(), user.clone(), ws_manager.clone()));
|
2021-09-23 13:15:35 +08:00
|
|
|
Self { doc_ctrl: controller }
|
2021-09-13 23:09:57 +08:00
|
|
|
}
|
|
|
|
|
2021-10-05 14:37:45 +08:00
|
|
|
pub fn init(&self) -> Result<(), DocError> {
|
|
|
|
let _ = self.doc_ctrl.init()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:52:08 +08:00
|
|
|
pub fn delete(&self, params: DocIdentifier) -> Result<(), DocError> {
|
2021-10-06 15:23:38 +08:00
|
|
|
let _ = self.doc_ctrl.delete(params)?;
|
2021-09-13 23:09:57 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:52:08 +08:00
|
|
|
pub async fn open(&self, params: DocIdentifier, pool: Arc<ConnectionPool>) -> Result<Arc<ClientEditDoc>, DocError> {
|
2021-09-27 23:23:23 +08:00
|
|
|
let edit_context = self.doc_ctrl.open(params, pool).await?;
|
|
|
|
Ok(edit_context)
|
2021-09-13 23:09:57 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:04:09 +08:00
|
|
|
pub async fn close(&self, params: DocIdentifier) -> Result<(), DocError> {
|
|
|
|
let _ = self.doc_ctrl.close(¶ms.doc_id)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:19:01 +08:00
|
|
|
pub async fn read_document_data(
|
|
|
|
&self,
|
|
|
|
params: DocIdentifier,
|
|
|
|
pool: Arc<ConnectionPool>,
|
|
|
|
) -> Result<DocDelta, DocError> {
|
|
|
|
let edit_context = self.doc_ctrl.open(params, pool).await?;
|
|
|
|
let delta = edit_context.delta().await?;
|
|
|
|
Ok(delta)
|
|
|
|
}
|
|
|
|
|
2021-10-06 15:23:38 +08:00
|
|
|
pub async fn apply_doc_delta(&self, params: DocDelta) -> Result<DocDelta, DocError> {
|
2021-09-23 13:15:35 +08:00
|
|
|
// workaround: compare the rust's delta with flutter's delta. Will be removed
|
|
|
|
// very soon
|
2021-11-04 12:47:41 +08:00
|
|
|
let doc = self.doc_ctrl.apply_local_delta(params.clone()).await?;
|
2021-09-23 13:15:35 +08:00
|
|
|
Ok(doc)
|
2021-09-11 14:26:30 +08:00
|
|
|
}
|
2021-07-22 22:26:38 +08:00
|
|
|
}
|