2023-11-14 09:21:09 +00:00
|
|
|
use std::sync::Arc;
|
2023-07-29 01:46:24 +00:00
|
|
|
use std::sync::Weak;
|
2023-05-10 05:27:50 +00:00
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
use collab::core::collab::DataSource;
|
2023-12-27 03:42:39 +00:00
|
|
|
use collab::core::origin::CollabOrigin;
|
2024-04-24 06:38:47 +00:00
|
|
|
use collab::entity::EncodedCollab;
|
2023-12-27 03:42:39 +00:00
|
|
|
use collab::preclude::Collab;
|
2023-06-03 05:55:43 +00:00
|
|
|
use collab_document::blocks::DocumentData;
|
2024-08-05 23:56:13 +00:00
|
|
|
use collab_document::conversions::convert_document_to_plain_text;
|
2023-07-05 12:57:09 +00:00
|
|
|
use collab_document::document::Document;
|
2024-03-28 09:46:31 +00:00
|
|
|
use collab_document::document_awareness::DocumentAwarenessState;
|
|
|
|
use collab_document::document_awareness::DocumentAwarenessUser;
|
2024-01-04 00:02:12 +00:00
|
|
|
use collab_document::document_data::default_document_data;
|
2023-10-10 11:05:55 +00:00
|
|
|
use collab_entity::CollabType;
|
2024-08-18 03:16:42 +00:00
|
|
|
use collab_plugins::local_storage::kv::doc::CollabKVAction;
|
|
|
|
use collab_plugins::local_storage::kv::KVTransactionDB;
|
2024-01-22 05:34:15 +00:00
|
|
|
use collab_plugins::CollabKVDB;
|
2024-03-30 08:28:24 +00:00
|
|
|
use dashmap::DashMap;
|
2024-03-28 09:46:31 +00:00
|
|
|
use lib_infra::util::timestamp;
|
2024-08-18 03:16:42 +00:00
|
|
|
use tokio::sync::RwLock;
|
2024-06-19 23:44:57 +00:00
|
|
|
use tracing::trace;
|
2023-11-05 06:00:24 +00:00
|
|
|
use tracing::{event, instrument};
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
use crate::document::{
|
|
|
|
subscribe_document_changed, subscribe_document_snapshot_state, subscribe_document_sync_state,
|
|
|
|
};
|
|
|
|
use collab_integrate::collab_builder::{
|
|
|
|
AppFlowyCollabBuilder, CollabBuilderConfig, KVDBCollabPersistenceImpl,
|
|
|
|
};
|
2024-01-11 06:42:03 +00:00
|
|
|
use flowy_document_pub::cloud::DocumentCloudService;
|
2023-12-27 03:42:39 +00:00
|
|
|
use flowy_error::{internal_error, ErrorCode, FlowyError, FlowyResult};
|
2024-07-22 07:43:48 +00:00
|
|
|
use flowy_storage_pub::storage::{CreatedUpload, StorageService};
|
2024-01-29 21:36:27 +00:00
|
|
|
use lib_dispatch::prelude::af_spawn;
|
2023-04-28 06:08:53 +00:00
|
|
|
|
2024-03-28 09:46:31 +00:00
|
|
|
use crate::entities::UpdateDocumentAwarenessStatePB;
|
2024-01-07 03:12:05 +00:00
|
|
|
use crate::entities::{
|
|
|
|
DocumentSnapshotData, DocumentSnapshotMeta, DocumentSnapshotMetaPB, DocumentSnapshotPB,
|
|
|
|
};
|
2023-10-02 07:12:24 +00:00
|
|
|
use crate::reminder::DocumentReminderAction;
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2024-01-07 03:12:05 +00:00
|
|
|
pub trait DocumentUserService: Send + Sync {
|
2023-07-29 01:46:24 +00:00
|
|
|
fn user_id(&self) -> Result<i64, FlowyError>;
|
2024-03-28 09:46:31 +00:00
|
|
|
fn device_id(&self) -> Result<String, FlowyError>;
|
2023-10-23 03:43:31 +00:00
|
|
|
fn workspace_id(&self) -> Result<String, FlowyError>;
|
2024-01-04 16:05:38 +00:00
|
|
|
fn collab_db(&self, uid: i64) -> Result<Weak<CollabKVDB>, FlowyError>;
|
2023-07-29 01:46:24 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 03:12:05 +00:00
|
|
|
pub trait DocumentSnapshotService: Send + Sync {
|
|
|
|
fn get_document_snapshot_metas(
|
|
|
|
&self,
|
|
|
|
document_id: &str,
|
|
|
|
) -> FlowyResult<Vec<DocumentSnapshotMeta>>;
|
|
|
|
fn get_document_snapshot(&self, snapshot_id: &str) -> FlowyResult<DocumentSnapshotData>;
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
pub struct DocumentManager {
|
2024-01-07 03:12:05 +00:00
|
|
|
pub user_service: Arc<dyn DocumentUserService>,
|
2023-05-15 14:16:05 +00:00
|
|
|
collab_builder: Arc<AppFlowyCollabBuilder>,
|
2024-08-18 03:16:42 +00:00
|
|
|
documents: Arc<DashMap<String, Arc<RwLock<Document>>>>,
|
|
|
|
removing_documents: Arc<DashMap<String, Arc<RwLock<Document>>>>,
|
2023-07-05 12:57:09 +00:00
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
2024-06-19 23:44:57 +00:00
|
|
|
storage_service: Weak<dyn StorageService>,
|
2024-01-07 03:12:05 +00:00
|
|
|
snapshot_service: Arc<dyn DocumentSnapshotService>,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentManager {
|
2023-07-05 12:57:09 +00:00
|
|
|
pub fn new(
|
2024-01-07 03:12:05 +00:00
|
|
|
user_service: Arc<dyn DocumentUserService>,
|
2023-07-05 12:57:09 +00:00
|
|
|
collab_builder: Arc<AppFlowyCollabBuilder>,
|
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
2024-06-19 23:44:57 +00:00
|
|
|
storage_service: Weak<dyn StorageService>,
|
2024-01-07 03:12:05 +00:00
|
|
|
snapshot_service: Arc<dyn DocumentSnapshotService>,
|
2023-07-05 12:57:09 +00:00
|
|
|
) -> Self {
|
2023-04-13 10:53:51 +00:00
|
|
|
Self {
|
2024-01-07 03:12:05 +00:00
|
|
|
user_service,
|
2023-05-15 14:16:05 +00:00
|
|
|
collab_builder,
|
2024-03-30 08:28:24 +00:00
|
|
|
documents: Arc::new(Default::default()),
|
|
|
|
removing_documents: Arc::new(Default::default()),
|
2023-07-05 12:57:09 +00:00
|
|
|
cloud_service,
|
2023-09-01 14:27:29 +00:00
|
|
|
storage_service,
|
2024-01-07 03:12:05 +00:00
|
|
|
snapshot_service,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 05:45:57 +00:00
|
|
|
/// Get the encoded collab of the document.
|
2024-08-18 03:16:42 +00:00
|
|
|
pub fn get_encoded_collab_with_view_id(&self, doc_id: &str) -> FlowyResult<EncodedCollab> {
|
2024-07-08 05:45:57 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
2024-08-18 03:16:42 +00:00
|
|
|
let doc_state =
|
|
|
|
KVDBCollabPersistenceImpl::new(self.user_service.collab_db(uid)?, uid).into_data_source();
|
|
|
|
let collab = self.collab_for_document(uid, doc_id, doc_state, false)?;
|
|
|
|
let encoded_collab = collab
|
|
|
|
.try_read()
|
|
|
|
.unwrap()
|
2024-07-22 05:35:42 +00:00
|
|
|
.encode_collab_v1(|collab| CollabType::Document.validate_require_data(collab))
|
2024-08-18 03:16:42 +00:00
|
|
|
.map_err(internal_error)?;
|
|
|
|
Ok(encoded_collab)
|
2024-07-08 05:45:57 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 01:44:07 +00:00
|
|
|
pub async fn initialize(&self, _uid: i64) -> FlowyResult<()> {
|
2024-06-03 08:34:54 +00:00
|
|
|
trace!("initialize document manager");
|
2024-03-30 08:28:24 +00:00
|
|
|
self.documents.clear();
|
2024-06-03 08:34:54 +00:00
|
|
|
self.removing_documents.clear();
|
2023-07-29 01:46:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:11:06 +00:00
|
|
|
#[instrument(
|
|
|
|
name = "document_initialize_with_new_user",
|
|
|
|
level = "debug",
|
|
|
|
skip_all,
|
|
|
|
err
|
|
|
|
)]
|
2024-04-26 01:44:07 +00:00
|
|
|
pub async fn initialize_with_new_user(&self, uid: i64) -> FlowyResult<()> {
|
|
|
|
self.initialize(uid).await?;
|
2023-07-29 01:46:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-10-02 07:12:24 +00:00
|
|
|
|
|
|
|
pub async fn handle_reminder_action(&self, action: DocumentReminderAction) {
|
|
|
|
match action {
|
|
|
|
DocumentReminderAction::Add { reminder: _ } => {},
|
|
|
|
DocumentReminderAction::Remove { reminder_id: _ } => {},
|
|
|
|
DocumentReminderAction::Update { reminder: _ } => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:43:46 +00:00
|
|
|
/// Create a new document.
|
|
|
|
///
|
|
|
|
/// if the document already exists, return the existing document.
|
|
|
|
/// if the data is None, will create a document with default data.
|
2024-01-04 00:02:12 +00:00
|
|
|
#[instrument(level = "info", skip(self, data))]
|
2023-08-28 05:28:24 +00:00
|
|
|
pub async fn create_document(
|
2023-06-03 12:43:46 +00:00
|
|
|
&self,
|
2023-08-17 15:46:39 +00:00
|
|
|
uid: i64,
|
2023-06-17 06:25:30 +00:00
|
|
|
doc_id: &str,
|
2023-06-03 12:43:46 +00:00
|
|
|
data: Option<DocumentData>,
|
2024-07-01 06:44:08 +00:00
|
|
|
) -> FlowyResult<EncodedCollab> {
|
2024-01-22 05:34:15 +00:00
|
|
|
if self.is_doc_exist(doc_id).await.unwrap_or(false) {
|
2023-12-27 03:42:39 +00:00
|
|
|
Err(FlowyError::new(
|
|
|
|
ErrorCode::RecordAlreadyExists,
|
|
|
|
format!("document {} already exists", doc_id),
|
|
|
|
))
|
2023-08-28 15:20:56 +00:00
|
|
|
} else {
|
2024-08-18 03:16:42 +00:00
|
|
|
let db = self
|
|
|
|
.user_service
|
|
|
|
.collab_db(uid)?
|
|
|
|
.upgrade()
|
|
|
|
.ok_or_else(|| FlowyError::internal().with_context("Failed to get collab db"))?;
|
2024-07-01 06:44:08 +00:00
|
|
|
let encoded_collab = doc_state_from_document_data(
|
2024-06-09 05:42:09 +00:00
|
|
|
doc_id,
|
|
|
|
data.unwrap_or_else(|| default_document_data(doc_id)),
|
|
|
|
)
|
2024-07-01 06:44:08 +00:00
|
|
|
.await?;
|
2024-08-18 03:16:42 +00:00
|
|
|
|
|
|
|
db.with_write_txn(|write_txn| {
|
|
|
|
write_txn.flush_doc(
|
2024-07-01 06:44:08 +00:00
|
|
|
uid,
|
|
|
|
doc_id,
|
2024-08-18 03:16:42 +00:00
|
|
|
encoded_collab.state_vector.to_vec(),
|
|
|
|
encoded_collab.doc_state.to_vec(),
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2024-07-01 06:44:08 +00:00
|
|
|
|
|
|
|
Ok(encoded_collab)
|
2023-08-28 05:28:24 +00:00
|
|
|
}
|
2023-04-17 02:12:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
fn collab_for_document(
|
|
|
|
&self,
|
|
|
|
uid: i64,
|
|
|
|
doc_id: &str,
|
|
|
|
data_source: DataSource,
|
|
|
|
sync_enable: bool,
|
|
|
|
) -> FlowyResult<Arc<RwLock<Document>>> {
|
|
|
|
let db = self.user_service.collab_db(uid)?;
|
|
|
|
let workspace_id = self.user_service.workspace_id()?;
|
|
|
|
let collab_object =
|
|
|
|
self
|
|
|
|
.collab_builder
|
|
|
|
.collab_object(&workspace_id, uid, doc_id, CollabType::Document)?;
|
|
|
|
let document = self.collab_builder.create_document(
|
|
|
|
collab_object,
|
|
|
|
data_source,
|
|
|
|
db,
|
|
|
|
CollabBuilderConfig::default().sync_enable(sync_enable),
|
|
|
|
None,
|
|
|
|
)?;
|
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a document instance if the document is already opened.
|
|
|
|
pub async fn editable_document(&self, doc_id: &str) -> FlowyResult<Arc<RwLock<Document>>> {
|
2024-03-30 08:28:24 +00:00
|
|
|
if let Some(doc) = self.documents.get(doc_id).map(|item| item.value().clone()) {
|
|
|
|
return Ok(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(doc) = self.restore_document_from_removing(doc_id) {
|
2023-11-14 09:21:09 +00:00
|
|
|
return Ok(doc);
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
2024-08-18 03:16:42 +00:00
|
|
|
|
2024-07-25 23:58:54 +00:00
|
|
|
Err(FlowyError::internal().with_context("Call open document first"))
|
2024-06-03 08:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns Document for given object id
|
|
|
|
/// If the document does not exist in local disk, try get the doc state from the cloud.
|
|
|
|
/// If the document exists, open the document and cache it
|
|
|
|
#[tracing::instrument(level = "info", skip(self), err)]
|
2024-08-18 03:16:42 +00:00
|
|
|
async fn create_document_instance(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
enable_sync: bool,
|
|
|
|
) -> FlowyResult<Arc<RwLock<Document>>> {
|
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
let mut doc_state =
|
|
|
|
KVDBCollabPersistenceImpl::new(self.user_service.collab_db(uid)?, uid).into_data_source();
|
2024-02-24 23:49:44 +00:00
|
|
|
// If the document does not exist in local disk, try get the doc state from the cloud. This happens
|
|
|
|
// When user_device_a create a document and user_device_b open the document.
|
2024-01-22 05:34:15 +00:00
|
|
|
if !self.is_doc_exist(doc_id).await? {
|
2024-04-15 06:50:28 +00:00
|
|
|
doc_state = DataSource::DocStateV1(
|
2024-03-23 01:18:47 +00:00
|
|
|
self
|
|
|
|
.cloud_service
|
|
|
|
.get_document_doc_state(doc_id, &self.user_service.workspace_id()?)
|
|
|
|
.await?,
|
|
|
|
);
|
2024-02-24 23:49:44 +00:00
|
|
|
|
|
|
|
// the doc_state should not be empty if remote return the doc state without error.
|
|
|
|
if doc_state.is_empty() {
|
|
|
|
return Err(FlowyError::new(
|
|
|
|
ErrorCode::RecordNotFound,
|
|
|
|
format!("document {} not found", doc_id),
|
|
|
|
));
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 08:34:54 +00:00
|
|
|
event!(
|
|
|
|
tracing::Level::DEBUG,
|
|
|
|
"Initialize document: {}, workspace_id: {:?}",
|
|
|
|
doc_id,
|
|
|
|
self.user_service.workspace_id()
|
|
|
|
);
|
2024-08-18 03:16:42 +00:00
|
|
|
let result = self.collab_for_document(uid, doc_id, doc_state, enable_sync);
|
|
|
|
match result {
|
2024-02-24 23:49:44 +00:00
|
|
|
Ok(document) => {
|
2024-08-18 03:16:42 +00:00
|
|
|
// Only push the document to the cache if the sync is enabled.
|
|
|
|
if enable_sync {
|
|
|
|
{
|
|
|
|
let mut lock = document.write().await;
|
|
|
|
subscribe_document_changed(doc_id, &mut lock);
|
|
|
|
subscribe_document_snapshot_state(&lock);
|
|
|
|
subscribe_document_sync_state(&lock);
|
|
|
|
}
|
|
|
|
self.documents.insert(doc_id.to_string(), document.clone());
|
|
|
|
}
|
2024-02-24 23:49:44 +00:00
|
|
|
Ok(document)
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_invalid_data() {
|
|
|
|
if let Some(db) = self.user_service.collab_db(uid)?.upgrade() {
|
|
|
|
db.delete_doc(uid, doc_id).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Err(err);
|
|
|
|
},
|
|
|
|
}
|
2023-06-17 06:25:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 05:37:13 +00:00
|
|
|
pub async fn get_document_data(&self, doc_id: &str) -> FlowyResult<DocumentData> {
|
2024-08-05 23:56:13 +00:00
|
|
|
let document = self.get_document(doc_id).await?;
|
2024-08-18 03:16:42 +00:00
|
|
|
let document = document.read().await;
|
2024-08-05 23:56:13 +00:00
|
|
|
document.get_document_data().map_err(internal_error)
|
|
|
|
}
|
|
|
|
pub async fn get_document_text(&self, doc_id: &str) -> FlowyResult<String> {
|
|
|
|
let document = self.get_document(doc_id).await?;
|
2024-08-18 03:16:42 +00:00
|
|
|
let document = document.read().await;
|
|
|
|
let text = convert_document_to_plain_text(&document)?;
|
2024-08-05 23:56:13 +00:00
|
|
|
Ok(text)
|
|
|
|
}
|
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
/// Return a document instance.
|
|
|
|
/// The returned document might or might not be able to sync with the cloud.
|
|
|
|
async fn get_document(&self, doc_id: &str) -> FlowyResult<Arc<RwLock<Document>>> {
|
|
|
|
if let Some(doc) = self.documents.get(doc_id).map(|item| item.value().clone()) {
|
|
|
|
return Ok(doc);
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
2024-08-18 03:16:42 +00:00
|
|
|
|
|
|
|
if let Some(doc) = self.restore_document_from_removing(doc_id) {
|
|
|
|
return Ok(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
let document = self.create_document_instance(doc_id, false).await?;
|
2024-08-05 23:56:13 +00:00
|
|
|
Ok(document)
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 08:28:24 +00:00
|
|
|
pub async fn open_document(&self, doc_id: &str) -> FlowyResult<()> {
|
2024-04-02 14:15:42 +00:00
|
|
|
if let Some(mutex_document) = self.restore_document_from_removing(doc_id) {
|
2024-08-18 03:16:42 +00:00
|
|
|
let lock = mutex_document.read().await;
|
|
|
|
lock.start_init_sync();
|
2024-04-02 14:15:42 +00:00
|
|
|
}
|
2024-06-03 08:34:54 +00:00
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
if self.documents.contains_key(doc_id) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = self.create_document_instance(doc_id, true).await?;
|
2024-03-30 08:28:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:54:47 +00:00
|
|
|
pub async fn close_document(&self, doc_id: &str) -> FlowyResult<()> {
|
2024-03-30 08:28:24 +00:00
|
|
|
if let Some((doc_id, document)) = self.documents.remove(doc_id) {
|
2024-08-18 03:16:42 +00:00
|
|
|
{
|
2024-03-28 09:46:31 +00:00
|
|
|
// clear the awareness state when close the document
|
2024-08-18 03:16:42 +00:00
|
|
|
let mut lock = document.write().await;
|
|
|
|
lock.clean_awareness_local_state();
|
|
|
|
lock.flush();
|
2023-11-20 12:54:47 +00:00
|
|
|
}
|
2024-03-30 08:28:24 +00:00
|
|
|
let clone_doc_id = doc_id.clone();
|
|
|
|
trace!("move document to removing_documents: {}", doc_id);
|
|
|
|
self.removing_documents.insert(doc_id, document);
|
|
|
|
|
|
|
|
let weak_removing_documents = Arc::downgrade(&self.removing_documents);
|
|
|
|
af_spawn(async move {
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(120)).await;
|
|
|
|
if let Some(removing_documents) = weak_removing_documents.upgrade() {
|
|
|
|
if removing_documents.remove(&clone_doc_id).is_some() {
|
|
|
|
trace!("drop document from removing_documents: {}", clone_doc_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-11-20 12:54:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 01:42:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:34:15 +00:00
|
|
|
pub async fn delete_document(&self, doc_id: &str) -> FlowyResult<()> {
|
2024-01-07 03:12:05 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
if let Some(db) = self.user_service.collab_db(uid)?.upgrade() {
|
2024-01-22 05:34:15 +00:00
|
|
|
db.delete_doc(uid, doc_id).await?;
|
2023-11-14 09:21:09 +00:00
|
|
|
// When deleting a document, we need to remove it from the cache.
|
2024-03-30 08:28:24 +00:00
|
|
|
self.documents.remove(doc_id);
|
2023-07-29 01:46:24 +00:00
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
|
2024-06-03 08:34:54 +00:00
|
|
|
#[instrument(level = "debug", skip_all, err)]
|
2024-03-28 09:46:31 +00:00
|
|
|
pub async fn set_document_awareness_local_state(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
state: UpdateDocumentAwarenessStatePB,
|
|
|
|
) -> FlowyResult<bool> {
|
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
let device_id = self.user_service.device_id()?;
|
2024-08-18 03:16:42 +00:00
|
|
|
if let Ok(doc) = self.editable_document(doc_id).await {
|
|
|
|
let mut doc = doc.write().await;
|
|
|
|
let user = DocumentAwarenessUser { uid, device_id };
|
|
|
|
let selection = state.selection.map(|s| s.into());
|
|
|
|
let state = DocumentAwarenessState {
|
|
|
|
version: 1,
|
|
|
|
user,
|
|
|
|
selection,
|
|
|
|
metadata: state.metadata,
|
|
|
|
timestamp: timestamp(),
|
|
|
|
};
|
|
|
|
doc.set_awareness_local_state(state);
|
|
|
|
return Ok(true);
|
2024-03-28 09:46:31 +00:00
|
|
|
}
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
/// Return the list of snapshots of the document.
|
2024-01-07 03:12:05 +00:00
|
|
|
pub async fn get_document_snapshot_meta(
|
2023-07-05 12:57:09 +00:00
|
|
|
&self,
|
|
|
|
document_id: &str,
|
2024-01-07 03:12:05 +00:00
|
|
|
_limit: usize,
|
|
|
|
) -> FlowyResult<Vec<DocumentSnapshotMetaPB>> {
|
|
|
|
let metas = self
|
|
|
|
.snapshot_service
|
|
|
|
.get_document_snapshot_metas(document_id)?
|
2023-08-17 15:46:39 +00:00
|
|
|
.into_iter()
|
2024-01-07 03:12:05 +00:00
|
|
|
.map(|meta| DocumentSnapshotMetaPB {
|
|
|
|
snapshot_id: meta.snapshot_id,
|
|
|
|
object_id: meta.object_id,
|
|
|
|
created_at: meta.created_at,
|
2023-07-05 12:57:09 +00:00
|
|
|
})
|
2023-08-17 15:46:39 +00:00
|
|
|
.collect::<Vec<_>>();
|
2023-07-05 12:57:09 +00:00
|
|
|
|
2024-01-07 03:12:05 +00:00
|
|
|
Ok(metas)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_document_snapshot(&self, snapshot_id: &str) -> FlowyResult<DocumentSnapshotPB> {
|
|
|
|
let snapshot = self
|
|
|
|
.snapshot_service
|
|
|
|
.get_document_snapshot(snapshot_id)
|
|
|
|
.map(|snapshot| DocumentSnapshotPB {
|
|
|
|
object_id: snapshot.object_id,
|
|
|
|
encoded_v1: snapshot.encoded_v1,
|
|
|
|
})?;
|
|
|
|
Ok(snapshot)
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 23:44:57 +00:00
|
|
|
#[instrument(level = "debug", skip_all, err)]
|
2024-01-16 18:59:15 +00:00
|
|
|
pub async fn upload_file(
|
|
|
|
&self,
|
|
|
|
workspace_id: String,
|
2024-06-19 23:44:57 +00:00
|
|
|
document_id: &str,
|
2024-01-16 18:59:15 +00:00
|
|
|
local_file_path: &str,
|
2024-07-22 07:43:48 +00:00
|
|
|
) -> FlowyResult<CreatedUpload> {
|
2024-01-16 18:59:15 +00:00
|
|
|
let storage_service = self.storage_service_upgrade()?;
|
2024-07-22 07:43:48 +00:00
|
|
|
let upload = storage_service
|
2024-08-11 12:39:25 +00:00
|
|
|
.create_upload(&workspace_id, document_id, local_file_path, false)
|
|
|
|
.await?
|
|
|
|
.0;
|
2024-07-22 07:43:48 +00:00
|
|
|
Ok(upload)
|
2024-01-16 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn download_file(&self, local_file_path: String, url: String) -> FlowyResult<()> {
|
2024-06-19 23:44:57 +00:00
|
|
|
let storage_service = self.storage_service_upgrade()?;
|
|
|
|
storage_service.download_object(url, local_file_path)?;
|
2024-01-16 18:59:15 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_file(&self, local_file_path: String, url: String) -> FlowyResult<()> {
|
|
|
|
let storage_service = self.storage_service_upgrade()?;
|
2024-06-19 23:44:57 +00:00
|
|
|
storage_service.delete_object(url, local_file_path)?;
|
2024-01-16 18:59:15 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:34:15 +00:00
|
|
|
async fn is_doc_exist(&self, doc_id: &str) -> FlowyResult<bool> {
|
2024-01-07 03:12:05 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
if let Some(collab_db) = self.user_service.collab_db(uid)?.upgrade() {
|
2024-01-22 05:34:15 +00:00
|
|
|
let is_exist = collab_db.is_exist(uid, doc_id).await?;
|
|
|
|
Ok(is_exist)
|
2023-07-29 01:46:24 +00:00
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 23:44:57 +00:00
|
|
|
fn storage_service_upgrade(&self) -> FlowyResult<Arc<dyn StorageService>> {
|
2024-01-16 18:59:15 +00:00
|
|
|
let storage_service = self.storage_service.upgrade().ok_or_else(|| {
|
|
|
|
FlowyError::internal().with_context("The file storage service is already dropped")
|
|
|
|
})?;
|
|
|
|
Ok(storage_service)
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
/// Only expose this method for testing
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
pub fn get_cloud_service(&self) -> &Arc<dyn DocumentCloudService> {
|
|
|
|
&self.cloud_service
|
|
|
|
}
|
2023-09-01 14:27:29 +00:00
|
|
|
/// Only expose this method for testing
|
|
|
|
#[cfg(debug_assertions)]
|
2024-06-19 23:44:57 +00:00
|
|
|
pub fn get_file_storage_service(&self) -> &Weak<dyn StorageService> {
|
2023-09-01 14:27:29 +00:00
|
|
|
&self.storage_service
|
|
|
|
}
|
2024-03-30 08:28:24 +00:00
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
fn restore_document_from_removing(&self, doc_id: &str) -> Option<Arc<RwLock<Document>>> {
|
2024-03-30 08:28:24 +00:00
|
|
|
let (doc_id, doc) = self.removing_documents.remove(doc_id)?;
|
|
|
|
trace!(
|
|
|
|
"move document {} from removing_documents to documents",
|
|
|
|
doc_id
|
|
|
|
);
|
|
|
|
self.documents.insert(doc_id, doc.clone());
|
|
|
|
Some(doc)
|
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
2023-12-27 03:42:39 +00:00
|
|
|
|
2024-01-15 04:53:53 +00:00
|
|
|
async fn doc_state_from_document_data(
|
2023-12-27 03:42:39 +00:00
|
|
|
doc_id: &str,
|
|
|
|
data: DocumentData,
|
2024-01-03 03:41:29 +00:00
|
|
|
) -> Result<EncodedCollab, FlowyError> {
|
2024-01-15 04:53:53 +00:00
|
|
|
let doc_id = doc_id.to_string();
|
|
|
|
// spawn_blocking is used to avoid blocking the tokio thread pool if the document is large.
|
|
|
|
let encoded_collab = tokio::task::spawn_blocking(move || {
|
2024-08-18 03:16:42 +00:00
|
|
|
let collab = Collab::new_with_origin(CollabOrigin::Empty, doc_id, vec![], false);
|
|
|
|
let document = Document::open_with(collab, Some(data)).map_err(internal_error)?;
|
2024-04-03 03:26:48 +00:00
|
|
|
let encode_collab = document.encode_collab()?;
|
|
|
|
Ok::<_, FlowyError>(encode_collab)
|
2024-01-15 04:53:53 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
Ok(encoded_collab)
|
2023-12-27 03:42:39 +00:00
|
|
|
}
|