55 lines
1.9 KiB
Rust
Raw Normal View History

2021-09-14 16:22:44 +08:00
use crate::{
entities::doc::{CreateDocParams, Doc, DocChangeset, QueryDocParams},
2021-09-22 14:42:14 +08:00
errors::DocError,
services::{doc_controller::DocController, server::construct_doc_server, ws::WsManager},
2021-09-14 16:22:44 +08:00
};
2021-09-21 16:21:35 +08:00
use bytes::Bytes;
2021-09-13 23:09:57 +08:00
use diesel::SqliteConnection;
use flowy_database::ConnectionPool;
2021-09-20 15:38:55 +08:00
use parking_lot::RwLock;
2021-09-21 15:07:07 +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>;
}
2021-09-13 23:09:57 +08:00
pub struct FlowyDocument {
controller: 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();
let controller = Arc::new(DocController::new(server.clone(), user.clone(), ws_manager.clone()));
Self { controller }
2021-09-13 23:09:57 +08:00
}
pub fn create(&self, params: CreateDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
let _ = self.controller.create(params, conn)?;
Ok(())
}
pub fn delete(&self, params: QueryDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
let _ = self.controller.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> {
let open_doc = self.controller.open(params, pool).await?;
Ok(open_doc.doc())
2021-09-13 23:09:57 +08:00
}
pub async fn apply_changeset(&self, params: DocChangeset, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> {
// let _ = self.doc_manager.apply_changeset(&params.id,
// Bytes::from(params.data), pool).await?;
//
// // workaround: compare the rust's delta with flutter's delta. Will be removed
// // very soon
// let doc = self.doc_manager.read_doc(&params.id)?;
// Ok(doc)
unimplemented!()
2021-09-11 14:26:30 +08:00
}
2021-07-22 22:26:38 +08:00
}