77 lines
2.3 KiB
Rust
Raw Normal View History

2021-09-23 13:15:35 +08:00
use crate::{
errors::DocError,
2021-09-27 23:23:23 +08:00
services::{
2021-11-13 11:11:24 +08:00
doc::{doc_controller::DocController, ClientEditDoc},
2021-09-27 23:23:23 +08:00
server::construct_doc_server,
ws::WsDocumentManager,
},
2021-09-23 13:15:35 +08:00
};
2021-11-20 08:35:04 +08:00
use backend_service::config::ServerConfig;
2021-10-06 15:23:38 +08:00
use flowy_database::ConnectionPool;
2021-11-13 11:11:24 +08:00
use flowy_document_infra::entities::doc::{DocDelta, DocIdentifier};
2021-10-06 15:23:38 +08:00
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>;
fn user_id(&self) -> Result<String, DocError>;
fn token(&self) -> Result<String, DocError>;
fn db_pool(&self) -> Result<Arc<ConnectionPool>, DocError>;
}
2021-09-13 23:09:57 +08:00
pub struct FlowyDocument {
2021-09-23 13:15:35 +08:00
doc_ctrl: Arc<DocController>,
user: Arc<dyn DocumentUser>,
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 {
pub fn new(
user: Arc<dyn DocumentUser>,
ws_manager: Arc<WsDocumentManager>,
server_config: &ServerConfig,
) -> FlowyDocument {
let server = construct_doc_server(server_config);
let doc_ctrl = Arc::new(DocController::new(server.clone(), user.clone(), ws_manager.clone()));
Self { doc_ctrl, user }
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(())
}
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(())
}
pub async fn open(&self, params: DocIdentifier) -> Result<Arc<ClientEditDoc>, DocError> {
let edit_context = self.doc_ctrl.open(params, self.user.db_pool()?).await?;
2021-09-27 23:23:23 +08:00
Ok(edit_context)
2021-09-13 23:09:57 +08:00
}
pub async fn close(&self, params: DocIdentifier) -> Result<(), DocError> {
let _ = self.doc_ctrl.close(&params.doc_id)?;
Ok(())
}
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
let doc = self
.doc_ctrl
.apply_local_delta(params.clone(), self.user.db_pool()?)
.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
}