2021-07-23 14:37:18 +08:00
|
|
|
use crate::{
|
2021-12-25 21:44:45 +08:00
|
|
|
context::DocumentUser,
|
2021-12-14 18:04:51 +08:00
|
|
|
errors::FlowyError,
|
2021-10-02 17:19:54 +08:00
|
|
|
services::{
|
|
|
|
doc::{
|
2021-12-31 10:32:25 +08:00
|
|
|
edit::ClientDocumentEditor,
|
2021-12-08 21:51:06 +08:00
|
|
|
revision::{RevisionCache, RevisionManager, RevisionServer},
|
2021-12-25 21:44:45 +08:00
|
|
|
DocumentWSReceivers,
|
|
|
|
DocumentWebSocket,
|
|
|
|
WSStateReceiver,
|
2021-10-02 17:19:54 +08:00
|
|
|
},
|
|
|
|
server::Server,
|
|
|
|
},
|
2021-09-22 23:21:44 +08:00
|
|
|
};
|
2021-11-09 16:00:09 +08:00
|
|
|
use bytes::Bytes;
|
2021-12-10 11:05:23 +08:00
|
|
|
use dashmap::DashMap;
|
2021-12-31 10:32:25 +08:00
|
|
|
use flowy_collaboration::entities::doc::{DocumentDelta, DocumentId, DocumentInfo};
|
2021-10-06 15:23:38 +08:00
|
|
|
use flowy_database::ConnectionPool;
|
2021-12-14 18:04:51 +08:00
|
|
|
use flowy_error::FlowyResult;
|
2021-12-13 13:55:44 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
2021-11-09 16:00:09 +08:00
|
|
|
use std::sync::Arc;
|
2021-07-23 14:37:18 +08:00
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
pub struct DocumentController {
|
2021-09-09 15:43:05 +08:00
|
|
|
server: Server,
|
2021-12-25 21:44:45 +08:00
|
|
|
ws_receivers: Arc<DocumentWSReceivers>,
|
|
|
|
ws_sender: Arc<dyn DocumentWebSocket>,
|
2021-12-10 11:05:23 +08:00
|
|
|
open_cache: Arc<OpenDocCache>,
|
2021-09-09 15:43:05 +08:00
|
|
|
user: Arc<dyn DocumentUser>,
|
2021-07-23 14:37:18 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
impl DocumentController {
|
2021-12-25 21:44:45 +08:00
|
|
|
pub(crate) fn new(
|
|
|
|
server: Server,
|
|
|
|
user: Arc<dyn DocumentUser>,
|
|
|
|
ws_receivers: Arc<DocumentWSReceivers>,
|
|
|
|
ws_sender: Arc<dyn DocumentWebSocket>,
|
|
|
|
) -> Self {
|
2021-12-10 11:05:23 +08:00
|
|
|
let open_cache = Arc::new(OpenDocCache::new());
|
2021-12-18 00:23:26 +08:00
|
|
|
Self {
|
|
|
|
server,
|
2021-12-25 21:44:45 +08:00
|
|
|
ws_receivers,
|
|
|
|
ws_sender,
|
2021-12-18 00:23:26 +08:00
|
|
|
open_cache,
|
|
|
|
user,
|
|
|
|
}
|
2021-07-23 14:37:18 +08:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
pub(crate) fn init(&self) -> FlowyResult<()> {
|
2021-12-25 21:44:45 +08:00
|
|
|
let notify = self.ws_sender.subscribe_state_changed();
|
|
|
|
listen_ws_state_changed(notify, self.ws_receivers.clone());
|
|
|
|
|
2021-10-05 14:37:45 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, doc_id, pool), fields(doc_id), err)]
|
|
|
|
pub async fn open<T: AsRef<str>>(
|
2021-09-26 16:39:57 +08:00
|
|
|
&self,
|
2021-12-31 10:32:25 +08:00
|
|
|
doc_id: T,
|
2021-09-26 16:39:57 +08:00
|
|
|
pool: Arc<ConnectionPool>,
|
2021-12-31 10:32:25 +08:00
|
|
|
) -> Result<Arc<ClientDocumentEditor>, FlowyError> {
|
|
|
|
let doc_id = doc_id.as_ref();
|
|
|
|
tracing::Span::current().record("doc_id", &doc_id);
|
|
|
|
if !self.open_cache.contains(doc_id) {
|
|
|
|
let editor = self.make_editor(doc_id, pool.clone()).await?;
|
2021-12-30 22:44:07 +08:00
|
|
|
return Ok(editor);
|
2021-09-11 20:09:46 +08:00
|
|
|
}
|
2021-12-31 10:32:25 +08:00
|
|
|
self.open_cache.get(doc_id)
|
2021-09-09 15:43:05 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, doc_id), fields(doc_id), err)]
|
|
|
|
pub fn close<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 11:05:23 +08:00
|
|
|
self.open_cache.remove(doc_id);
|
2021-12-30 22:44:07 +08:00
|
|
|
self.ws_receivers.remove(doc_id);
|
2021-09-22 23:21:44 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-31 10:32:25 +08: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 11:05:23 +08:00
|
|
|
self.open_cache.remove(doc_id);
|
2021-12-30 22:44:07 +08:00
|
|
|
self.ws_receivers.remove(doc_id);
|
2021-07-23 14:37:18 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-23 13:15:35 +08:00
|
|
|
|
2021-11-13 11:53:50 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, delta, db_pool), fields(doc_id = %delta.doc_id), err)]
|
2021-12-31 10:32:25 +08:00
|
|
|
pub async fn apply_document_delta(
|
2021-11-13 11:53:50 +08:00
|
|
|
&self,
|
2021-12-23 23:17:57 +08:00
|
|
|
delta: DocumentDelta,
|
2021-11-13 11:53:50 +08:00
|
|
|
db_pool: Arc<ConnectionPool>,
|
2021-12-23 23:17:57 +08:00
|
|
|
) -> Result<DocumentDelta, FlowyError> {
|
2021-12-10 11:05:23 +08:00
|
|
|
if !self.open_cache.contains(&delta.doc_id) {
|
2021-12-31 10:32:25 +08:00
|
|
|
let _ = self.open(&delta.doc_id, db_pool).await?;
|
2021-11-13 11:53:50 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
let editor = self.open_cache.get(&delta.doc_id)?;
|
|
|
|
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 13:15:35 +08:00
|
|
|
}
|
2021-12-31 10:32:25 +08:00
|
|
|
|
|
|
|
pub async fn save_document_delta(&self, delta: DocumentDelta) {}
|
2021-09-09 15:43:05 +08:00
|
|
|
}
|
2021-07-23 14:37:18 +08:00
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
impl DocumentController {
|
|
|
|
async fn make_editor(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
pool: Arc<ConnectionPool>,
|
|
|
|
) -> Result<Arc<ClientDocumentEditor>, FlowyError> {
|
2021-12-08 21:51:06 +08:00
|
|
|
let user = self.user.clone();
|
2021-12-18 00:23:26 +08:00
|
|
|
let token = self.user.token()?;
|
2021-12-08 21:51:06 +08:00
|
|
|
let rev_manager = self.make_rev_manager(doc_id, pool.clone())?;
|
2021-12-18 00:23:26 +08:00
|
|
|
let server = Arc::new(RevisionServerImpl {
|
|
|
|
token,
|
|
|
|
server: self.server.clone(),
|
|
|
|
});
|
2021-12-31 10:32:25 +08:00
|
|
|
let doc_editor =
|
|
|
|
ClientDocumentEditor::new(doc_id, user, pool, rev_manager, self.ws_sender.clone(), server).await?;
|
2021-12-30 22:44:07 +08:00
|
|
|
self.ws_receivers.add(doc_id, doc_editor.ws_handler());
|
2021-12-16 21:31:36 +08:00
|
|
|
self.open_cache.insert(&doc_id, &doc_editor);
|
|
|
|
Ok(doc_editor)
|
2021-12-08 21:51:06 +08:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
fn make_rev_manager(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<RevisionManager, FlowyError> {
|
2021-12-09 22:28:11 +08:00
|
|
|
let user_id = self.user.user_id()?;
|
2021-12-18 00:23:26 +08:00
|
|
|
let cache = Arc::new(RevisionCache::new(&user_id, doc_id, pool));
|
2021-12-16 21:31:36 +08:00
|
|
|
Ok(RevisionManager::new(&user_id, doc_id, cache))
|
2021-07-23 14:37:18 +08:00
|
|
|
}
|
2021-09-21 15:07:07 +08:00
|
|
|
}
|
2021-09-26 16:39:57 +08:00
|
|
|
|
2021-10-02 17:19:54 +08:00
|
|
|
struct RevisionServerImpl {
|
|
|
|
token: String,
|
|
|
|
server: Server,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionServer for RevisionServerImpl {
|
2021-10-18 22:08:35 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-12-23 23:17:57 +08:00
|
|
|
fn fetch_document(&self, doc_id: &str) -> FutureResult<DocumentInfo, FlowyError> {
|
2021-12-31 10:32:25 +08:00
|
|
|
let params = DocumentId {
|
2021-10-02 17:19:54 +08:00
|
|
|
doc_id: doc_id.to_string(),
|
|
|
|
};
|
|
|
|
let server = self.server.clone();
|
|
|
|
let token = self.token.clone();
|
|
|
|
|
2021-12-13 13:55:44 +08:00
|
|
|
FutureResult::new(async move {
|
2021-10-02 17:19:54 +08:00
|
|
|
match server.read_doc(&token, params).await? {
|
2021-12-14 18:04:51 +08:00
|
|
|
None => Err(FlowyError::record_not_found().context("Remote doesn't have this document")),
|
2021-10-06 15:23:38 +08:00
|
|
|
Some(doc) => Ok(doc),
|
2021-10-02 17:19:54 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 11:05:23 +08:00
|
|
|
pub struct OpenDocCache {
|
2021-12-31 10:32:25 +08:00
|
|
|
inner: DashMap<String, Arc<ClientDocumentEditor>>,
|
2021-12-10 11:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpenDocCache {
|
|
|
|
fn new() -> Self { Self { inner: DashMap::new() } }
|
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
pub(crate) fn insert(&self, doc_id: &str, doc: &Arc<ClientDocumentEditor>) {
|
2021-12-16 21:31:36 +08:00
|
|
|
if self.inner.contains_key(doc_id) {
|
|
|
|
log::warn!("Doc:{} already exists in cache", doc_id);
|
2021-12-10 11:05:23 +08:00
|
|
|
}
|
2021-12-16 21:31:36 +08:00
|
|
|
self.inner.insert(doc_id.to_string(), doc.clone());
|
2021-12-10 11:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn contains(&self, doc_id: &str) -> bool { self.inner.get(doc_id).is_some() }
|
|
|
|
|
2021-12-31 10:32:25 +08:00
|
|
|
pub(crate) fn get(&self, doc_id: &str) -> Result<Arc<ClientDocumentEditor>, FlowyError> {
|
2021-12-10 11:05:23 +08:00
|
|
|
if !self.contains(&doc_id) {
|
|
|
|
return Err(doc_not_found());
|
|
|
|
}
|
|
|
|
let opened_doc = self.inner.get(doc_id).unwrap();
|
|
|
|
Ok(opened_doc.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn remove(&self, id: &str) {
|
|
|
|
let doc_id = id.to_string();
|
|
|
|
match self.get(id) {
|
2021-12-20 15:37:37 +08:00
|
|
|
Ok(editor) => editor.stop(),
|
2021-12-10 11:05:23 +08:00
|
|
|
Err(e) => log::error!("{}", e),
|
2021-09-26 16:39:57 +08:00
|
|
|
}
|
2021-12-10 11:05:23 +08:00
|
|
|
self.inner.remove(&doc_id);
|
|
|
|
}
|
2021-09-26 16:39:57 +08:00
|
|
|
}
|
2021-12-10 11:05:23 +08:00
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
fn doc_not_found() -> FlowyError {
|
|
|
|
FlowyError::record_not_found().context("Doc is close or you should call open first")
|
|
|
|
}
|
2021-12-25 21:44:45 +08: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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|