2021-09-23 13:15:35 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-09-23 15:49:10 +08:00
|
|
|
|
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 crate::{
|
|
|
|
entities::doc::{CreateDocParams, Doc, DocDelta, QueryDocParams},
|
|
|
|
errors::DocError,
|
|
|
|
services::{doc::doc_controller::DocController, server::construct_doc_server, ws::WsManager},
|
|
|
|
};
|
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-21 15:07:07 +08:00
|
|
|
pub fn new(user: Arc<dyn DocumentUser>, ws_manager: Arc<RwLock<WsManager>>) -> FlowyDocument {
|
2021-09-11 14:26:30 +08:00
|
|
|
let server = construct_doc_server();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn open(&self, params: QueryDocParams, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> {
|
2021-09-23 13:15:35 +08:00
|
|
|
let open_doc = self.doc_ctrl.open(params, pool).await?;
|
2021-09-22 23:21:44 +08:00
|
|
|
Ok(open_doc.doc())
|
2021-09-13 23:09:57 +08:00
|
|
|
}
|
|
|
|
|
2021-09-23 13:15:35 +08:00
|
|
|
pub async fn apply_doc_delta(&self, params: DocDelta, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> {
|
|
|
|
// workaround: compare the rust's delta with flutter's delta. Will be removed
|
|
|
|
// very soon
|
|
|
|
let doc = self.doc_ctrl.edit_doc(params.clone(), pool)?;
|
|
|
|
Ok(doc)
|
2021-09-11 14:26:30 +08:00
|
|
|
}
|
2021-07-22 22:26:38 +08:00
|
|
|
}
|