2022-01-10 23:45:59 +08:00
|
|
|
use backend_service::configuration::ClientServerConfiguration;
|
2021-09-20 15:38:55 +08:00
|
|
|
use bytes::Bytes;
|
2022-01-10 23:45:59 +08:00
|
|
|
use flowy_collaboration::entities::{
|
|
|
|
doc::{CreateDocParams, DocumentId, DocumentInfo, ResetDocumentParams},
|
|
|
|
ws::DocumentClientWSData,
|
|
|
|
};
|
2021-11-19 12:18:46 +08:00
|
|
|
use flowy_database::ConnectionPool;
|
2021-09-20 15:38:55 +08:00
|
|
|
use flowy_document::{
|
2021-12-25 21:44:45 +08:00
|
|
|
context::DocumentUser,
|
2022-01-04 15:05:52 +08:00
|
|
|
core::{DocumentWSReceivers, DocumentWebSocket, WSStateReceiver},
|
2021-12-14 18:04:51 +08:00
|
|
|
errors::{internal_error, FlowyError},
|
2022-01-10 23:45:59 +08:00
|
|
|
DocumentCloudService,
|
|
|
|
};
|
|
|
|
use flowy_net::{
|
|
|
|
cloud::document::{DocumentHttpCloudService, DocumentLocalCloudService},
|
|
|
|
services::ws_conn::FlowyWebSocketConnect,
|
2021-11-19 12:18:46 +08:00
|
|
|
};
|
2021-12-14 18:04:51 +08:00
|
|
|
use flowy_user::services::user::UserSession;
|
2022-01-10 23:45:59 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
2021-12-26 19:10:37 +08:00
|
|
|
use lib_ws::{WSMessageReceiver, WSModule, WebSocketRawMessage};
|
2021-11-19 12:18:46 +08:00
|
|
|
use std::{convert::TryInto, path::Path, sync::Arc};
|
2021-09-20 15:38:55 +08:00
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
pub struct DocumentDependencies {
|
|
|
|
pub user: Arc<dyn DocumentUser>,
|
|
|
|
pub ws_receivers: Arc<DocumentWSReceivers>,
|
|
|
|
pub ws_sender: Arc<dyn DocumentWebSocket>,
|
|
|
|
pub cloud_service: Arc<dyn DocumentCloudService>,
|
|
|
|
}
|
|
|
|
|
2021-12-14 15:31:44 +08:00
|
|
|
pub struct DocumentDepsResolver();
|
2021-09-20 15:38:55 +08:00
|
|
|
impl DocumentDepsResolver {
|
2021-12-14 15:31:44 +08:00
|
|
|
pub fn resolve(
|
2022-01-07 17:37:11 +08:00
|
|
|
ws_conn: Arc<FlowyWebSocketConnect>,
|
2021-12-14 15:31:44 +08:00
|
|
|
user_session: Arc<UserSession>,
|
2022-01-10 23:45:59 +08:00
|
|
|
server_config: &ClientServerConfiguration,
|
|
|
|
) -> DocumentDependencies {
|
|
|
|
let user = Arc::new(DocumentUserImpl(user_session));
|
|
|
|
let ws_sender = Arc::new(DocumentWebSocketImpl(ws_conn.clone()));
|
2021-12-25 21:44:45 +08:00
|
|
|
let ws_receivers = Arc::new(DocumentWSReceivers::new());
|
2022-01-10 23:45:59 +08:00
|
|
|
let receiver = Arc::new(WSMessageReceiverImpl(ws_receivers.clone()));
|
2022-01-07 17:37:11 +08:00
|
|
|
ws_conn.add_ws_message_receiver(receiver).unwrap();
|
2022-01-10 23:45:59 +08:00
|
|
|
let cloud_service = make_document_cloud_service(server_config);
|
|
|
|
DocumentDependencies {
|
|
|
|
user,
|
|
|
|
ws_receivers,
|
|
|
|
ws_sender,
|
|
|
|
cloud_service,
|
|
|
|
}
|
2021-09-20 15:38:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
struct DocumentUserImpl(Arc<UserSession>);
|
2021-09-20 15:38:55 +08:00
|
|
|
impl DocumentUser for DocumentUserImpl {
|
2021-12-14 18:04:51 +08:00
|
|
|
fn user_dir(&self) -> Result<String, FlowyError> {
|
2022-01-10 23:45:59 +08:00
|
|
|
let dir = self.0.user_dir().map_err(|e| FlowyError::unauthorized().context(e))?;
|
2021-09-20 15:38:55 +08:00
|
|
|
|
2021-12-21 11:13:38 +08:00
|
|
|
let doc_dir = format!("{}/document", dir);
|
2021-09-20 15:38:55 +08:00
|
|
|
if !Path::new(&doc_dir).exists() {
|
|
|
|
let _ = std::fs::create_dir_all(&doc_dir)?;
|
|
|
|
}
|
|
|
|
Ok(doc_dir)
|
|
|
|
}
|
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
fn user_id(&self) -> Result<String, FlowyError> { self.0.user_id() }
|
2021-09-20 15:38:55 +08:00
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
fn token(&self) -> Result<String, FlowyError> { self.0.token() }
|
2021-11-13 11:53:50 +08:00
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
fn db_pool(&self) -> Result<Arc<ConnectionPool>, FlowyError> { self.0.db_pool() }
|
2021-09-20 15:38:55 +08:00
|
|
|
}
|
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
struct DocumentWebSocketImpl(Arc<FlowyWebSocketConnect>);
|
|
|
|
impl DocumentWebSocket for DocumentWebSocketImpl {
|
2021-12-26 10:48:09 +08:00
|
|
|
fn send(&self, data: DocumentClientWSData) -> Result<(), FlowyError> {
|
2021-12-10 22:18:44 +08:00
|
|
|
let bytes: Bytes = data.try_into().unwrap();
|
2021-12-26 19:10:37 +08:00
|
|
|
let msg = WebSocketRawMessage {
|
2021-12-16 22:24:05 +08:00
|
|
|
module: WSModule::Doc,
|
2021-12-10 22:18:44 +08:00
|
|
|
data: bytes.to_vec(),
|
|
|
|
};
|
2022-01-10 23:45:59 +08:00
|
|
|
let sender = self.0.ws_sender()?;
|
2021-12-10 22:18:44 +08:00
|
|
|
sender.send(msg).map_err(internal_error)?;
|
2021-09-20 15:38:55 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-04 14:24:35 +08:00
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
fn subscribe_state_changed(&self) -> WSStateReceiver { self.0.subscribe_websocket_state() }
|
2021-09-20 15:38:55 +08:00
|
|
|
}
|
|
|
|
|
2022-01-10 23:45:59 +08:00
|
|
|
struct WSMessageReceiverImpl(Arc<DocumentWSReceivers>);
|
|
|
|
impl WSMessageReceiver for WSMessageReceiverImpl {
|
2021-12-16 22:24:05 +08:00
|
|
|
fn source(&self) -> WSModule { WSModule::Doc }
|
2021-12-26 19:10:37 +08:00
|
|
|
fn receive_message(&self, msg: WebSocketRawMessage) { self.0.did_receive_data(Bytes::from(msg.data)); }
|
2021-09-20 15:38:55 +08:00
|
|
|
}
|
2022-01-10 23:45:59 +08:00
|
|
|
|
|
|
|
fn make_document_cloud_service(server_config: &ClientServerConfiguration) -> Arc<dyn DocumentCloudService> {
|
|
|
|
if cfg!(feature = "http_server") {
|
|
|
|
Arc::new(DocumentHttpCloudServiceAdaptor::new(server_config.clone()))
|
|
|
|
} else {
|
|
|
|
Arc::new(DocumentLocalCloudServiceAdaptor::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DocumentHttpCloudServiceAdaptor(DocumentHttpCloudService);
|
|
|
|
impl DocumentHttpCloudServiceAdaptor {
|
|
|
|
fn new(config: ClientServerConfiguration) -> Self {
|
|
|
|
DocumentHttpCloudServiceAdaptor(DocumentHttpCloudService::new(config))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DocumentCloudService for DocumentHttpCloudServiceAdaptor {
|
|
|
|
fn create_document(&self, token: &str, params: CreateDocParams) -> FutureResult<(), FlowyError> {
|
|
|
|
self.0.create_document_request(token, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_document(&self, token: &str, params: DocumentId) -> FutureResult<Option<DocumentInfo>, FlowyError> {
|
|
|
|
self.0.read_document_request(token, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_document(&self, token: &str, params: ResetDocumentParams) -> FutureResult<(), FlowyError> {
|
|
|
|
self.0.update_document_request(token, params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DocumentLocalCloudServiceAdaptor(DocumentLocalCloudService);
|
|
|
|
impl DocumentLocalCloudServiceAdaptor {
|
|
|
|
fn new() -> Self { Self(DocumentLocalCloudService {}) }
|
|
|
|
}
|
|
|
|
impl DocumentCloudService for DocumentLocalCloudServiceAdaptor {
|
|
|
|
fn create_document(&self, token: &str, params: CreateDocParams) -> FutureResult<(), FlowyError> {
|
|
|
|
self.0.create_document_request(token, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_document(&self, token: &str, params: DocumentId) -> FutureResult<Option<DocumentInfo>, FlowyError> {
|
|
|
|
self.0.read_document_request(token, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_document(&self, token: &str, params: ResetDocumentParams) -> FutureResult<(), FlowyError> {
|
|
|
|
self.0.update_document_request(token, params)
|
|
|
|
}
|
|
|
|
}
|