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-23 13:15:35 +08:00
|
|
|
|
|
|
|
use flowy_database::ConnectionPool;
|
2021-10-01 19:39:08 +08:00
|
|
|
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::{
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
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>,
|
2021-10-02 17:19:54 +08:00
|
|
|
) -> 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-09-25 21:47:02 +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
|
2021-10-01 19:39:08 +08:00
|
|
|
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
|
|
|
}
|