2021-07-23 06:37:18 +00:00
|
|
|
use crate::{
|
2021-12-25 13:44:45 +00:00
|
|
|
context::DocumentUser,
|
2022-01-04 07:05:52 +00:00
|
|
|
core::{
|
|
|
|
edit::ClientDocumentEditor,
|
2022-01-07 09:37:11 +00:00
|
|
|
revision::{DocumentRevisionCache, DocumentRevisionManager, RevisionServer},
|
2022-01-04 07:05:52 +00:00
|
|
|
DocumentWSReceivers,
|
|
|
|
DocumentWebSocket,
|
|
|
|
WSStateReceiver,
|
2021-10-02 09:19:54 +00:00
|
|
|
},
|
2022-01-04 07:05:52 +00:00
|
|
|
errors::FlowyError,
|
2022-01-10 15:45:59 +00:00
|
|
|
DocumentCloudService,
|
2021-09-22 15:21:44 +00:00
|
|
|
};
|
2021-11-09 08:00:09 +00:00
|
|
|
use bytes::Bytes;
|
2021-12-10 03:05:23 +00:00
|
|
|
use dashmap::DashMap;
|
2022-01-01 06:23:58 +00:00
|
|
|
use flowy_collaboration::entities::{
|
|
|
|
doc::{DocumentDelta, DocumentId, DocumentInfo},
|
|
|
|
revision::RepeatedRevision,
|
|
|
|
};
|
2021-10-06 07:23:38 +00:00
|
|
|
use flowy_database::ConnectionPool;
|
2021-12-14 10:04:51 +00:00
|
|
|
use flowy_error::FlowyResult;
|
2021-12-13 05:55:44 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2021-11-09 08:00:09 +00:00
|
|
|
use std::sync::Arc;
|
2021-07-23 06:37:18 +00:00
|
|
|
|
2021-12-31 02:32:25 +00:00
|
|
|
pub struct DocumentController {
|
2022-01-10 15:45:59 +00:00
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
2021-12-25 13:44:45 +00:00
|
|
|
ws_receivers: Arc<DocumentWSReceivers>,
|
|
|
|
ws_sender: Arc<dyn DocumentWebSocket>,
|
2021-12-10 03:05:23 +00:00
|
|
|
open_cache: Arc<OpenDocCache>,
|
2021-09-09 07:43:05 +00:00
|
|
|
user: Arc<dyn DocumentUser>,
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 02:32:25 +00:00
|
|
|
impl DocumentController {
|
2021-12-25 13:44:45 +00:00
|
|
|
pub(crate) fn new(
|
2022-01-10 15:45:59 +00:00
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
2021-12-25 13:44:45 +00:00
|
|
|
user: Arc<dyn DocumentUser>,
|
|
|
|
ws_receivers: Arc<DocumentWSReceivers>,
|
|
|
|
ws_sender: Arc<dyn DocumentWebSocket>,
|
|
|
|
) -> Self {
|
2021-12-10 03:05:23 +00:00
|
|
|
let open_cache = Arc::new(OpenDocCache::new());
|
2021-12-17 16:23:26 +00:00
|
|
|
Self {
|
2022-01-10 15:45:59 +00:00
|
|
|
cloud_service,
|
2021-12-25 13:44:45 +00:00
|
|
|
ws_receivers,
|
|
|
|
ws_sender,
|
2021-12-17 16:23:26 +00:00
|
|
|
open_cache,
|
|
|
|
user,
|
|
|
|
}
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
pub(crate) fn init(&self) -> FlowyResult<()> {
|
2021-12-25 13:44:45 +00:00
|
|
|
let notify = self.ws_sender.subscribe_state_changed();
|
|
|
|
listen_ws_state_changed(notify, self.ws_receivers.clone());
|
|
|
|
|
2021-10-05 06:37:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-01 06:23:58 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, doc_id), fields(doc_id), err)]
|
2022-01-07 09:37:11 +00:00
|
|
|
pub async fn open_document<T: AsRef<str>>(&self, doc_id: T) -> Result<Arc<ClientDocumentEditor>, FlowyError> {
|
2021-12-31 02:32:25 +00:00
|
|
|
let doc_id = doc_id.as_ref();
|
|
|
|
tracing::Span::current().record("doc_id", &doc_id);
|
2022-01-01 06:23:58 +00:00
|
|
|
self.get_editor(doc_id).await
|
2021-09-09 07:43:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 02:32:25 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, doc_id), fields(doc_id), err)]
|
2022-01-07 09:37:11 +00:00
|
|
|
pub fn close_document<T: AsRef<str>>(&self, doc_id: T) -> Result<(), FlowyError> {
|
2021-12-31 02:32:25 +00:00
|
|
|
let doc_id = doc_id.as_ref();
|
|
|
|
tracing::Span::current().record("doc_id", &doc_id);
|
2021-12-10 03:05:23 +00:00
|
|
|
self.open_cache.remove(doc_id);
|
2021-12-30 14:44:07 +00:00
|
|
|
self.ws_receivers.remove(doc_id);
|
2021-09-22 15:21:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-31 02:32:25 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, doc_id), fields(doc_id), err)]
|
|
|
|
pub fn delete<T: AsRef<str>>(&self, doc_id: T) -> Result<(), FlowyError> {
|
|
|
|
let doc_id = doc_id.as_ref();
|
|
|
|
tracing::Span::current().record("doc_id", &doc_id);
|
2021-12-10 03:05:23 +00:00
|
|
|
self.open_cache.remove(doc_id);
|
2021-12-30 14:44:07 +00:00
|
|
|
self.ws_receivers.remove(doc_id);
|
2021-07-23 06:37:18 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-23 05:15:35 +00:00
|
|
|
|
2022-01-01 06:23:58 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, delta), fields(doc_id = %delta.doc_id), err)]
|
2022-01-05 15:15:55 +00:00
|
|
|
pub async fn receive_local_delta(&self, delta: DocumentDelta) -> Result<DocumentDelta, FlowyError> {
|
2022-01-01 06:23:58 +00:00
|
|
|
let editor = self.get_editor(&delta.doc_id).await?;
|
2021-12-31 02:32:25 +00:00
|
|
|
let _ = editor.compose_local_delta(Bytes::from(delta.delta_json)).await?;
|
|
|
|
let document_json = editor.document_json().await?;
|
|
|
|
Ok(DocumentDelta {
|
|
|
|
doc_id: delta.doc_id.clone(),
|
|
|
|
delta_json: document_json,
|
|
|
|
})
|
2021-09-23 05:15:35 +00:00
|
|
|
}
|
2021-12-31 02:32:25 +00:00
|
|
|
|
2022-01-01 06:23:58 +00:00
|
|
|
pub async fn save_document<T: AsRef<str>>(&self, doc_id: T, revisions: RepeatedRevision) -> FlowyResult<()> {
|
|
|
|
let doc_id = doc_id.as_ref().to_owned();
|
|
|
|
let db_pool = self.user.db_pool()?;
|
|
|
|
let rev_manager = self.make_rev_manager(&doc_id, db_pool)?;
|
|
|
|
let _ = rev_manager.reset_document(revisions).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_editor(&self, doc_id: &str) -> FlowyResult<Arc<ClientDocumentEditor>> {
|
|
|
|
match self.open_cache.get(doc_id) {
|
|
|
|
None => {
|
|
|
|
let db_pool = self.user.db_pool()?;
|
|
|
|
self.make_editor(&doc_id, db_pool).await
|
|
|
|
},
|
|
|
|
Some(editor) => Ok(editor),
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 07:43:05 +00:00
|
|
|
}
|
2021-07-23 06:37:18 +00:00
|
|
|
|
2021-12-31 02:32:25 +00:00
|
|
|
impl DocumentController {
|
|
|
|
async fn make_editor(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
pool: Arc<ConnectionPool>,
|
|
|
|
) -> Result<Arc<ClientDocumentEditor>, FlowyError> {
|
2021-12-08 13:51:06 +00:00
|
|
|
let user = self.user.clone();
|
2021-12-17 16:23:26 +00:00
|
|
|
let token = self.user.token()?;
|
2021-12-08 13:51:06 +00:00
|
|
|
let rev_manager = self.make_rev_manager(doc_id, pool.clone())?;
|
2021-12-17 16:23:26 +00:00
|
|
|
let server = Arc::new(RevisionServerImpl {
|
|
|
|
token,
|
2022-01-10 15:45:59 +00:00
|
|
|
server: self.cloud_service.clone(),
|
2021-12-17 16:23:26 +00:00
|
|
|
});
|
2022-01-05 15:15:55 +00:00
|
|
|
let doc_editor = ClientDocumentEditor::new(doc_id, user, rev_manager, self.ws_sender.clone(), server).await?;
|
2021-12-30 14:44:07 +00:00
|
|
|
self.ws_receivers.add(doc_id, doc_editor.ws_handler());
|
2021-12-16 13:31:36 +00:00
|
|
|
self.open_cache.insert(&doc_id, &doc_editor);
|
|
|
|
Ok(doc_editor)
|
2021-12-08 13:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 09:37:11 +00:00
|
|
|
fn make_rev_manager(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<DocumentRevisionManager, FlowyError> {
|
2021-12-09 14:28:11 +00:00
|
|
|
let user_id = self.user.user_id()?;
|
2022-01-07 09:37:11 +00:00
|
|
|
let cache = Arc::new(DocumentRevisionCache::new(&user_id, doc_id, pool));
|
|
|
|
Ok(DocumentRevisionManager::new(&user_id, doc_id, cache))
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
2021-09-21 07:07:07 +00:00
|
|
|
}
|
2021-09-26 08:39:57 +00:00
|
|
|
|
2021-10-02 09:19:54 +00:00
|
|
|
struct RevisionServerImpl {
|
|
|
|
token: String,
|
2022-01-10 15:45:59 +00:00
|
|
|
server: Arc<dyn DocumentCloudService>,
|
2021-10-02 09:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionServer for RevisionServerImpl {
|
2021-10-18 14:08:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-12-23 15:17:57 +00:00
|
|
|
fn fetch_document(&self, doc_id: &str) -> FutureResult<DocumentInfo, FlowyError> {
|
2021-12-31 02:32:25 +00:00
|
|
|
let params = DocumentId {
|
2021-10-02 09:19:54 +00:00
|
|
|
doc_id: doc_id.to_string(),
|
|
|
|
};
|
|
|
|
let server = self.server.clone();
|
|
|
|
let token = self.token.clone();
|
|
|
|
|
2021-12-13 05:55:44 +00:00
|
|
|
FutureResult::new(async move {
|
2022-01-10 15:45:59 +00:00
|
|
|
match server.read_document(&token, params).await? {
|
2021-12-14 10:04:51 +00:00
|
|
|
None => Err(FlowyError::record_not_found().context("Remote doesn't have this document")),
|
2021-10-06 07:23:38 +00:00
|
|
|
Some(doc) => Ok(doc),
|
2021-10-02 09:19:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 03:05:23 +00:00
|
|
|
pub struct OpenDocCache {
|
2021-12-31 02:32:25 +00:00
|
|
|
inner: DashMap<String, Arc<ClientDocumentEditor>>,
|
2021-12-10 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpenDocCache {
|
|
|
|
fn new() -> Self { Self { inner: DashMap::new() } }
|
|
|
|
|
2021-12-31 02:32:25 +00:00
|
|
|
pub(crate) fn insert(&self, doc_id: &str, doc: &Arc<ClientDocumentEditor>) {
|
2021-12-16 13:31:36 +00:00
|
|
|
if self.inner.contains_key(doc_id) {
|
|
|
|
log::warn!("Doc:{} already exists in cache", doc_id);
|
2021-12-10 03:05:23 +00:00
|
|
|
}
|
2021-12-16 13:31:36 +00:00
|
|
|
self.inner.insert(doc_id.to_string(), doc.clone());
|
2021-12-10 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn contains(&self, doc_id: &str) -> bool { self.inner.get(doc_id).is_some() }
|
|
|
|
|
2022-01-01 06:23:58 +00:00
|
|
|
pub(crate) fn get(&self, doc_id: &str) -> Option<Arc<ClientDocumentEditor>> {
|
2021-12-10 03:05:23 +00:00
|
|
|
if !self.contains(&doc_id) {
|
2022-01-01 06:23:58 +00:00
|
|
|
return None;
|
2021-12-10 03:05:23 +00:00
|
|
|
}
|
|
|
|
let opened_doc = self.inner.get(doc_id).unwrap();
|
2022-01-01 06:23:58 +00:00
|
|
|
Some(opened_doc.clone())
|
2021-12-10 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn remove(&self, id: &str) {
|
|
|
|
let doc_id = id.to_string();
|
2022-01-01 06:23:58 +00:00
|
|
|
if let Some(editor) = self.get(id) {
|
|
|
|
editor.stop()
|
2021-09-26 08:39:57 +00:00
|
|
|
}
|
2021-12-10 03:05:23 +00:00
|
|
|
self.inner.remove(&doc_id);
|
|
|
|
}
|
2021-09-26 08:39:57 +00:00
|
|
|
}
|
2021-12-10 03:05:23 +00:00
|
|
|
|
2021-12-25 13:44:45 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(state_receiver, receivers))]
|
|
|
|
fn listen_ws_state_changed(mut state_receiver: WSStateReceiver, receivers: Arc<DocumentWSReceivers>) {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Ok(state) = state_receiver.recv().await {
|
|
|
|
receivers.ws_connect_state_changed(&state);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|