66 lines
1.9 KiB
Rust
Raw Normal View History

2021-09-23 13:15:35 +08:00
use std::sync::Arc;
2021-09-13 23:09:57 +08:00
use diesel::SqliteConnection;
2021-09-20 15:38:55 +08:00
use parking_lot::RwLock;
2021-09-23 13:15:35 +08:00
use flowy_database::ConnectionPool;
use flowy_net::config::ServerConfig;
2021-09-23 13:15:35 +08:00
use crate::{
entities::doc::{CreateDocParams, Doc, DocDelta, QueryDocParams},
errors::DocError,
2021-09-27 23:23:23 +08:00
services::{
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-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>;
}
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 {
pub fn new(
user: Arc<dyn DocumentUser>,
ws_manager: Arc<RwLock<WsDocumentManager>>,
server_config: &ServerConfig,
) -> FlowyDocument {
let server = construct_doc_server(server_config);
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
}
pub fn create(&self, params: CreateDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
2021-09-23 13:15:35 +08:00
let _ = self.doc_ctrl.create(params, conn)?;
2021-09-13 23:09:57 +08:00
Ok(())
}
pub fn delete(&self, params: QueryDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
2021-09-23 13:15:35 +08:00
let _ = self.doc_ctrl.delete(params, conn)?;
2021-09-13 23:09:57 +08:00
Ok(())
}
2021-09-27 23:23:23 +08:00
pub async fn open(
&self,
params: QueryDocParams,
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
}
pub async fn apply_doc_delta(&self, params: DocDelta) -> Result<Doc, 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.edit_doc(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
}