manager.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. use std::sync::Weak;
  2. use std::{collections::HashMap, sync::Arc};
  3. use collab::core::collab::MutexCollab;
  4. use collab_document::blocks::DocumentData;
  5. use collab_document::document::Document;
  6. use collab_document::document_data::default_document_data;
  7. use collab_document::YrsDocAction;
  8. use collab_entity::CollabType;
  9. use parking_lot::RwLock;
  10. use tracing::instrument;
  11. use collab_integrate::collab_builder::AppFlowyCollabBuilder;
  12. use collab_integrate::RocksCollabDB;
  13. use flowy_document_deps::cloud::DocumentCloudService;
  14. use flowy_error::{internal_error, FlowyError, FlowyResult};
  15. use flowy_storage::FileStorageService;
  16. use crate::document::MutexDocument;
  17. use crate::entities::DocumentSnapshotPB;
  18. use crate::reminder::DocumentReminderAction;
  19. pub trait DocumentUser: Send + Sync {
  20. fn user_id(&self) -> Result<i64, FlowyError>;
  21. fn workspace_id(&self) -> Result<String, FlowyError>;
  22. fn token(&self) -> Result<Option<String>, FlowyError>; // unused now.
  23. fn collab_db(&self, uid: i64) -> Result<Weak<RocksCollabDB>, FlowyError>;
  24. }
  25. pub struct DocumentManager {
  26. pub user: Arc<dyn DocumentUser>,
  27. collab_builder: Arc<AppFlowyCollabBuilder>,
  28. documents: Arc<RwLock<HashMap<String, Arc<MutexDocument>>>>,
  29. #[allow(dead_code)]
  30. cloud_service: Arc<dyn DocumentCloudService>,
  31. storage_service: Weak<dyn FileStorageService>,
  32. }
  33. impl DocumentManager {
  34. pub fn new(
  35. user: Arc<dyn DocumentUser>,
  36. collab_builder: Arc<AppFlowyCollabBuilder>,
  37. cloud_service: Arc<dyn DocumentCloudService>,
  38. storage_service: Weak<dyn FileStorageService>,
  39. ) -> Self {
  40. Self {
  41. user,
  42. collab_builder,
  43. documents: Default::default(),
  44. cloud_service,
  45. storage_service,
  46. }
  47. }
  48. pub async fn initialize(&self, _uid: i64, _workspace_id: String) -> FlowyResult<()> {
  49. self.documents.write().clear();
  50. Ok(())
  51. }
  52. #[instrument(level = "debug", skip_all, err)]
  53. pub async fn initialize_with_new_user(&self, uid: i64, workspace_id: String) -> FlowyResult<()> {
  54. self.initialize(uid, workspace_id).await?;
  55. Ok(())
  56. }
  57. pub async fn handle_reminder_action(&self, action: DocumentReminderAction) {
  58. match action {
  59. DocumentReminderAction::Add { reminder: _ } => {},
  60. DocumentReminderAction::Remove { reminder_id: _ } => {},
  61. DocumentReminderAction::Update { reminder: _ } => {},
  62. }
  63. }
  64. /// Create a new document.
  65. ///
  66. /// if the document already exists, return the existing document.
  67. /// if the data is None, will create a document with default data.
  68. pub async fn create_document(
  69. &self,
  70. uid: i64,
  71. doc_id: &str,
  72. data: Option<DocumentData>,
  73. ) -> FlowyResult<Arc<MutexDocument>> {
  74. tracing::trace!("create a document: {:?}", doc_id);
  75. if self.is_doc_exist(doc_id).unwrap_or(false) {
  76. self.get_document(doc_id).await
  77. } else {
  78. let collab = self.collab_for_document(uid, doc_id, vec![]).await?;
  79. let data = data.unwrap_or_else(default_document_data);
  80. let document = Arc::new(MutexDocument::create_with_data(collab, data)?);
  81. Ok(document)
  82. }
  83. }
  84. /// Return the document
  85. #[tracing::instrument(level = "debug", skip(self), err)]
  86. pub async fn get_document(&self, doc_id: &str) -> FlowyResult<Arc<MutexDocument>> {
  87. if let Some(doc) = self.documents.read().get(doc_id) {
  88. return Ok(doc.clone());
  89. }
  90. let mut updates = vec![];
  91. if !self.is_doc_exist(doc_id)? {
  92. // Try to get the document from the cloud service
  93. updates = self
  94. .cloud_service
  95. .get_document_updates(&self.user.workspace_id()?, doc_id)
  96. .await?;
  97. }
  98. let uid = self.user.user_id()?;
  99. let collab = self.collab_for_document(uid, doc_id, updates).await?;
  100. let document = Arc::new(MutexDocument::open(doc_id, collab)?);
  101. // save the document to the memory and read it from the memory if we open the same document again.
  102. // and we don't want to subscribe to the document changes if we open the same document again.
  103. self
  104. .documents
  105. .write()
  106. .insert(doc_id.to_string(), document.clone());
  107. Ok(document)
  108. }
  109. pub async fn get_document_data(&self, doc_id: &str) -> FlowyResult<DocumentData> {
  110. let mut updates = vec![];
  111. if !self.is_doc_exist(doc_id)? {
  112. updates = self
  113. .cloud_service
  114. .get_document_updates(doc_id, &self.user.workspace_id()?)
  115. .await?;
  116. }
  117. let uid = self.user.user_id()?;
  118. let collab = self.collab_for_document(uid, doc_id, updates).await?;
  119. Document::open(collab)?
  120. .get_document_data()
  121. .map_err(internal_error)
  122. }
  123. pub fn close_document(&self, doc_id: &str) -> FlowyResult<()> {
  124. self.documents.write().remove(doc_id);
  125. Ok(())
  126. }
  127. pub fn delete_document(&self, doc_id: &str) -> FlowyResult<()> {
  128. let uid = self.user.user_id()?;
  129. if let Some(db) = self.user.collab_db(uid)?.upgrade() {
  130. let _ = db.with_write_txn(|txn| {
  131. txn.delete_doc(uid, &doc_id)?;
  132. Ok(())
  133. });
  134. self.documents.write().remove(doc_id);
  135. }
  136. Ok(())
  137. }
  138. /// Return the list of snapshots of the document.
  139. pub async fn get_document_snapshots(
  140. &self,
  141. document_id: &str,
  142. limit: usize,
  143. ) -> FlowyResult<Vec<DocumentSnapshotPB>> {
  144. let workspace_id = self.user.workspace_id()?;
  145. let snapshots = self
  146. .cloud_service
  147. .get_document_snapshots(document_id, limit, &workspace_id)
  148. .await?
  149. .into_iter()
  150. .map(|snapshot| DocumentSnapshotPB {
  151. snapshot_id: snapshot.snapshot_id,
  152. snapshot_desc: "".to_string(),
  153. created_at: snapshot.created_at,
  154. data: snapshot.data,
  155. })
  156. .collect::<Vec<_>>();
  157. Ok(snapshots)
  158. }
  159. async fn collab_for_document(
  160. &self,
  161. uid: i64,
  162. doc_id: &str,
  163. updates: Vec<Vec<u8>>,
  164. ) -> FlowyResult<Arc<MutexCollab>> {
  165. let db = self.user.collab_db(uid)?;
  166. let collab = self
  167. .collab_builder
  168. .build(uid, doc_id, CollabType::Document, updates, db)
  169. .await?;
  170. Ok(collab)
  171. // let doc_id = doc_id.to_string();
  172. // let (tx, rx) = oneshot::channel();
  173. // let collab_builder = self.collab_builder.clone();
  174. // tokio::spawn(async move {
  175. // let collab = collab_builder
  176. // .build(uid, &doc_id, CollabType::Document, updates, db)
  177. // .await
  178. // .unwrap();
  179. // let _ = tx.send(collab);
  180. // });
  181. //
  182. // Ok(rx.await.unwrap())
  183. }
  184. fn is_doc_exist(&self, doc_id: &str) -> FlowyResult<bool> {
  185. let uid = self.user.user_id()?;
  186. if let Some(collab_db) = self.user.collab_db(uid)?.upgrade() {
  187. let read_txn = collab_db.read_txn();
  188. Ok(read_txn.is_exist(uid, doc_id))
  189. } else {
  190. Ok(false)
  191. }
  192. }
  193. /// Only expose this method for testing
  194. #[cfg(debug_assertions)]
  195. pub fn get_cloud_service(&self) -> &Arc<dyn DocumentCloudService> {
  196. &self.cloud_service
  197. }
  198. /// Only expose this method for testing
  199. #[cfg(debug_assertions)]
  200. pub fn get_file_storage_service(&self) -> &Weak<dyn FileStorageService> {
  201. &self.storage_service
  202. }
  203. }