2022-01-01 14:23:58 +08:00
|
|
|
use bytes::Bytes;
|
|
|
|
use flowy_collaboration::entities::{
|
2022-02-25 22:27:44 +08:00
|
|
|
document_info::{BlockDelta, BlockId},
|
2022-01-03 12:20:06 +08:00
|
|
|
revision::{RepeatedRevision, Revision},
|
2022-01-01 14:23:58 +08:00
|
|
|
};
|
2022-01-14 09:09:25 +08:00
|
|
|
|
2022-01-15 23:58:36 +08:00
|
|
|
use flowy_collaboration::client_document::default::initial_delta_string;
|
2021-11-13 11:11:24 +08:00
|
|
|
use futures::{FutureExt, StreamExt};
|
|
|
|
use std::{collections::HashSet, sync::Arc};
|
2021-10-13 23:11:45 +08:00
|
|
|
|
2021-11-07 16:43:32 +08:00
|
|
|
use crate::{
|
2022-01-27 20:39:54 +08:00
|
|
|
dart_notification::{send_dart_notification, FolderNotification},
|
2021-11-07 16:43:32 +08:00
|
|
|
entities::{
|
2021-12-31 10:32:25 +08:00
|
|
|
trash::{RepeatedTrashId, TrashType},
|
|
|
|
view::{CreateViewParams, RepeatedView, UpdateViewParams, View, ViewId},
|
2021-11-07 16:43:32 +08:00
|
|
|
},
|
2021-12-31 10:32:25 +08:00
|
|
|
errors::{FlowyError, FlowyResult},
|
2022-01-30 10:33:21 +08:00
|
|
|
event_map::{FolderCouldServiceV1, WorkspaceUser},
|
2021-12-06 15:49:21 +08:00
|
|
|
services::{
|
2022-01-17 11:55:36 +08:00
|
|
|
persistence::{FolderPersistence, FolderPersistenceTransaction, ViewChangeset},
|
2022-01-24 17:35:58 +08:00
|
|
|
TrashController, TrashEvent,
|
2021-12-06 15:49:21 +08:00
|
|
|
},
|
2021-11-07 16:43:32 +08:00
|
|
|
};
|
2021-12-12 21:18:23 +08:00
|
|
|
use flowy_database::kv::KV;
|
2022-02-25 22:27:44 +08:00
|
|
|
use flowy_document::BlockManager;
|
2022-01-27 20:39:54 +08:00
|
|
|
use flowy_folder_data_model::entities::share::{ExportData, ExportParams};
|
2021-12-29 00:34:00 +08:00
|
|
|
use lib_infra::uuid_string;
|
2021-10-15 13:42:52 +08:00
|
|
|
|
2021-11-10 23:39:51 +08:00
|
|
|
const LATEST_VIEW_ID: &str = "latest_view_id";
|
|
|
|
|
2021-09-01 22:50:22 +08:00
|
|
|
pub(crate) struct ViewController {
|
2021-09-02 19:57:19 +08:00
|
|
|
user: Arc<dyn WorkspaceUser>,
|
2022-01-17 11:55:36 +08:00
|
|
|
cloud_service: Arc<dyn FolderCouldServiceV1>,
|
|
|
|
persistence: Arc<FolderPersistence>,
|
2021-12-29 21:49:31 +08:00
|
|
|
trash_controller: Arc<TrashController>,
|
2022-02-25 22:27:44 +08:00
|
|
|
block_manager: Arc<BlockManager>,
|
2021-07-19 22:44:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewController {
|
2021-09-13 23:09:57 +08:00
|
|
|
pub(crate) fn new(
|
|
|
|
user: Arc<dyn WorkspaceUser>,
|
2022-01-17 11:55:36 +08:00
|
|
|
persistence: Arc<FolderPersistence>,
|
|
|
|
cloud_service: Arc<dyn FolderCouldServiceV1>,
|
2021-12-06 15:49:21 +08:00
|
|
|
trash_can: Arc<TrashController>,
|
2022-02-25 22:27:44 +08:00
|
|
|
document_manager: Arc<BlockManager>,
|
2021-09-13 23:09:57 +08:00
|
|
|
) -> Self {
|
2021-09-07 21:31:04 +08:00
|
|
|
Self {
|
|
|
|
user,
|
2022-01-10 23:45:59 +08:00
|
|
|
cloud_service,
|
2022-01-14 09:09:25 +08:00
|
|
|
persistence,
|
2021-12-29 21:49:31 +08:00
|
|
|
trash_controller: trash_can,
|
2022-02-25 22:27:44 +08:00
|
|
|
block_manager: document_manager,
|
2021-09-07 21:31:04 +08:00
|
|
|
}
|
2021-07-20 14:03:21 +08:00
|
|
|
}
|
2021-07-19 22:44:37 +08:00
|
|
|
|
2022-01-18 22:56:57 +08:00
|
|
|
pub(crate) fn initialize(&self) -> Result<(), FlowyError> {
|
2022-02-25 22:27:44 +08:00
|
|
|
let _ = self.block_manager.init()?;
|
2021-10-13 23:11:45 +08:00
|
|
|
self.listen_trash_can_event();
|
2021-10-05 14:37:45 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:33:47 +08:00
|
|
|
#[tracing::instrument(level = "trace", skip(self, params), fields(name = %params.name), err)]
|
2022-01-01 23:09:13 +08:00
|
|
|
pub(crate) async fn create_view_from_params(&self, params: CreateViewParams) -> Result<View, FlowyError> {
|
2022-01-15 23:58:36 +08:00
|
|
|
let view_data = if params.view_data.is_empty() {
|
|
|
|
initial_delta_string()
|
|
|
|
} else {
|
|
|
|
params.view_data.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
let delta_data = Bytes::from(view_data);
|
2022-01-01 14:23:58 +08:00
|
|
|
let user_id = self.user.user_id()?;
|
|
|
|
let repeated_revision: RepeatedRevision =
|
|
|
|
Revision::initial_revision(&user_id, ¶ms.view_id, delta_data).into();
|
|
|
|
let _ = self
|
2022-02-25 22:27:44 +08:00
|
|
|
.block_manager
|
2022-02-19 11:34:31 +08:00
|
|
|
.reset_with_revisions(¶ms.view_id, repeated_revision)
|
2022-01-01 14:23:58 +08:00
|
|
|
.await?;
|
2021-12-29 00:34:00 +08:00
|
|
|
let view = self.create_view_on_server(params).await?;
|
2022-01-01 14:23:58 +08:00
|
|
|
let _ = self.create_view_on_local(view.clone()).await?;
|
2021-12-30 22:44:07 +08:00
|
|
|
|
|
|
|
Ok(view)
|
2021-11-08 19:19:02 +08:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:00:11 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, view_id, view_data), err)]
|
|
|
|
pub(crate) async fn create_view_document_content(
|
|
|
|
&self,
|
|
|
|
view_id: &str,
|
|
|
|
view_data: String,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
if view_data.is_empty() {
|
|
|
|
return Err(FlowyError::internal().context("The content of the view should not be empty"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let delta_data = Bytes::from(view_data);
|
|
|
|
let user_id = self.user.user_id()?;
|
|
|
|
let repeated_revision: RepeatedRevision = Revision::initial_revision(&user_id, view_id, delta_data).into();
|
2022-02-18 23:04:55 +08:00
|
|
|
let _ = self
|
2022-02-25 22:27:44 +08:00
|
|
|
.block_manager
|
2022-02-19 11:34:31 +08:00
|
|
|
.reset_with_revisions(view_id, repeated_revision)
|
2022-02-18 23:04:55 +08:00
|
|
|
.await?;
|
2022-01-19 16:00:11 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-01 14:23:58 +08:00
|
|
|
pub(crate) async fn create_view_on_local(&self, view: View) -> Result<(), FlowyError> {
|
2022-01-14 09:09:25 +08:00
|
|
|
let trash_controller = self.trash_controller.clone();
|
2022-01-20 23:51:11 +08:00
|
|
|
self.persistence
|
|
|
|
.begin_transaction(|transaction| {
|
|
|
|
let belong_to_id = view.belong_to_id.clone();
|
|
|
|
let _ = transaction.create_view(view)?;
|
|
|
|
let _ = notify_views_changed(&belong_to_id, trash_controller, &transaction)?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.await
|
2021-09-11 20:09:46 +08:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:49:18 +08:00
|
|
|
#[tracing::instrument(skip(self, view_id), fields(view_id = %view_id.value), err)]
|
|
|
|
pub(crate) async fn read_view(&self, view_id: ViewId) -> Result<View, FlowyError> {
|
2022-01-20 23:51:11 +08:00
|
|
|
let view = self
|
|
|
|
.persistence
|
|
|
|
.begin_transaction(|transaction| {
|
2022-02-24 21:49:18 +08:00
|
|
|
let view = transaction.read_view(&view_id.value)?;
|
2022-01-20 23:51:11 +08:00
|
|
|
let trash_ids = self.trash_controller.read_trash_ids(&transaction)?;
|
|
|
|
if trash_ids.contains(&view.id) {
|
|
|
|
return Err(FlowyError::record_not_found());
|
|
|
|
}
|
|
|
|
Ok(view)
|
|
|
|
})
|
|
|
|
.await?;
|
2022-02-24 21:49:18 +08:00
|
|
|
let _ = self.read_view_on_server(view_id);
|
2021-07-23 22:42:44 +08:00
|
|
|
Ok(view)
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:51:11 +08:00
|
|
|
pub(crate) async fn read_local_views(&self, ids: Vec<String>) -> Result<Vec<View>, FlowyError> {
|
|
|
|
self.persistence
|
|
|
|
.begin_transaction(|transaction| {
|
|
|
|
let mut views = vec![];
|
|
|
|
for view_id in ids {
|
|
|
|
views.push(transaction.read_view(&view_id)?);
|
|
|
|
}
|
|
|
|
Ok(views)
|
|
|
|
})
|
|
|
|
.await
|
2021-10-16 16:45:52 +08:00
|
|
|
}
|
|
|
|
|
2022-01-15 23:58:36 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2022-02-25 22:27:44 +08:00
|
|
|
pub(crate) async fn open_view(&self, view_id: &str) -> Result<BlockDelta, FlowyError> {
|
|
|
|
let editor = self.block_manager.open_block(view_id).await?;
|
|
|
|
KV::set_str(LATEST_VIEW_ID, view_id.to_owned());
|
|
|
|
let document_json = editor.block_json().await?;
|
|
|
|
Ok(BlockDelta {
|
|
|
|
block_id: view_id.to_string(),
|
2021-12-31 10:32:25 +08:00
|
|
|
delta_json: document_json,
|
|
|
|
})
|
2021-07-29 17:27:59 +08:00
|
|
|
}
|
|
|
|
|
2022-01-15 23:58:36 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
|
|
|
pub(crate) async fn close_view(&self, doc_id: &str) -> Result<(), FlowyError> {
|
2022-02-25 22:27:44 +08:00
|
|
|
let _ = self.block_manager.close_block(doc_id)?;
|
2021-10-19 13:04:09 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-24 21:49:18 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self,params), fields(doc_id = %params.value), err)]
|
2022-02-25 22:27:44 +08:00
|
|
|
pub(crate) async fn delete_view(&self, params: BlockId) -> Result<(), FlowyError> {
|
2021-11-11 14:11:45 +08:00
|
|
|
if let Some(view_id) = KV::get_str(LATEST_VIEW_ID) {
|
2022-02-24 21:49:18 +08:00
|
|
|
if view_id == params.value {
|
2021-11-11 19:56:30 +08:00
|
|
|
let _ = KV::remove(LATEST_VIEW_ID);
|
2021-11-11 14:11:45 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 22:27:44 +08:00
|
|
|
let _ = self.block_manager.close_block(¶ms.value)?;
|
2021-11-11 14:11:45 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-15 23:58:36 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2022-02-25 22:27:44 +08:00
|
|
|
pub(crate) async fn duplicate_view(&self, view_id: &str) -> Result<(), FlowyError> {
|
2022-01-14 09:09:25 +08:00
|
|
|
let view = self
|
|
|
|
.persistence
|
2022-02-25 22:27:44 +08:00
|
|
|
.begin_transaction(|transaction| transaction.read_view(view_id))
|
2022-01-20 23:51:11 +08:00
|
|
|
.await?;
|
2022-01-14 09:09:25 +08:00
|
|
|
|
2022-02-25 22:27:44 +08:00
|
|
|
let editor = self.block_manager.open_block(view_id).await?;
|
|
|
|
let document_json = editor.block_json().await?;
|
2021-10-20 22:19:01 +08:00
|
|
|
let duplicate_params = CreateViewParams {
|
|
|
|
belong_to_id: view.belong_to_id.clone(),
|
2021-11-11 22:37:40 +08:00
|
|
|
name: format!("{} (copy)", &view.name),
|
2021-10-20 22:19:01 +08:00
|
|
|
desc: view.desc.clone(),
|
|
|
|
thumbnail: "".to_owned(),
|
|
|
|
view_type: view.view_type.clone(),
|
2021-12-31 10:32:25 +08:00
|
|
|
view_data: document_json,
|
2021-12-29 00:34:00 +08:00
|
|
|
view_id: uuid_string(),
|
2021-10-20 22:19:01 +08:00
|
|
|
};
|
|
|
|
|
2021-11-08 19:19:02 +08:00
|
|
|
let _ = self.create_view_from_params(duplicate_params).await?;
|
2021-10-20 22:19:01 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-09 23:13:04 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, params), err)]
|
2022-02-25 22:27:44 +08:00
|
|
|
pub(crate) async fn export_view(&self, params: ExportParams) -> Result<ExportData, FlowyError> {
|
|
|
|
let editor = self.block_manager.open_block(¶ms.view_id).await?;
|
|
|
|
let delta_json = editor.block_json().await?;
|
2021-11-10 14:46:59 +08:00
|
|
|
Ok(ExportData {
|
2021-12-31 10:32:25 +08:00
|
|
|
data: delta_json,
|
2021-11-10 14:46:59 +08:00
|
|
|
export_type: params.export_type,
|
|
|
|
})
|
2021-11-09 16:00:09 +08:00
|
|
|
}
|
|
|
|
|
2021-09-02 19:57:19 +08:00
|
|
|
// belong_to_id will be the app_id or view_id.
|
2021-09-07 21:31:04 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-12-14 18:04:51 +08:00
|
|
|
pub(crate) async fn read_views_belong_to(&self, belong_to_id: &str) -> Result<RepeatedView, FlowyError> {
|
2022-01-20 23:51:11 +08:00
|
|
|
self.persistence
|
|
|
|
.begin_transaction(|transaction| {
|
|
|
|
read_belonging_views_on_local(belong_to_id, self.trash_controller.clone(), &transaction)
|
|
|
|
})
|
|
|
|
.await
|
2021-07-29 17:27:59 +08:00
|
|
|
}
|
|
|
|
|
2021-10-13 23:11:45 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, params), err)]
|
2021-12-14 18:04:51 +08:00
|
|
|
pub(crate) async fn update_view(&self, params: UpdateViewParams) -> Result<View, FlowyError> {
|
2022-01-13 11:16:26 +08:00
|
|
|
let changeset = ViewChangeset::new(params.clone());
|
2021-07-23 22:42:44 +08:00
|
|
|
let view_id = changeset.id.clone();
|
2022-01-20 23:51:11 +08:00
|
|
|
let view = self
|
|
|
|
.persistence
|
|
|
|
.begin_transaction(|transaction| {
|
|
|
|
let _ = transaction.update_view(changeset)?;
|
|
|
|
let view = transaction.read_view(&view_id)?;
|
2022-01-27 20:39:54 +08:00
|
|
|
send_dart_notification(&view_id, FolderNotification::ViewUpdated)
|
2022-01-20 23:51:11 +08:00
|
|
|
.payload(view.clone())
|
|
|
|
.send();
|
|
|
|
let _ = notify_views_changed(&view.belong_to_id, self.trash_controller.clone(), &transaction)?;
|
|
|
|
Ok(view)
|
|
|
|
})
|
|
|
|
.await?;
|
2021-10-15 13:10:52 +08:00
|
|
|
|
2021-09-07 21:31:04 +08:00
|
|
|
let _ = self.update_view_on_server(params);
|
2022-01-14 09:09:25 +08:00
|
|
|
Ok(view)
|
2021-09-02 19:57:19 +08:00
|
|
|
}
|
2021-09-11 14:26:30 +08:00
|
|
|
|
2022-02-25 22:27:44 +08:00
|
|
|
pub(crate) async fn receive_delta(&self, params: BlockDelta) -> Result<BlockDelta, FlowyError> {
|
|
|
|
let doc = self.block_manager.receive_local_delta(params).await?;
|
2021-09-15 16:35:40 +08:00
|
|
|
Ok(doc)
|
2021-09-11 14:26:30 +08:00
|
|
|
}
|
2021-11-10 23:39:51 +08:00
|
|
|
|
2022-01-20 23:51:11 +08:00
|
|
|
pub(crate) async fn latest_visit_view(&self) -> FlowyResult<Option<View>> {
|
2021-11-10 23:39:51 +08:00
|
|
|
match KV::get_str(LATEST_VIEW_ID) {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(view_id) => {
|
2022-01-14 09:09:25 +08:00
|
|
|
let view = self
|
|
|
|
.persistence
|
2022-01-20 23:51:11 +08:00
|
|
|
.begin_transaction(|transaction| transaction.read_view(&view_id))
|
|
|
|
.await?;
|
2022-01-14 09:09:25 +08:00
|
|
|
Ok(Some(view))
|
2022-01-24 17:35:58 +08:00
|
|
|
}
|
2021-11-10 23:39:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 17:35:58 +08:00
|
|
|
pub(crate) fn set_latest_view(&self, view: &View) {
|
|
|
|
KV::set_str(LATEST_VIEW_ID, view.id.clone());
|
|
|
|
}
|
2021-09-02 19:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewController {
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(skip(self), err)]
|
2021-12-14 18:04:51 +08:00
|
|
|
async fn create_view_on_server(&self, params: CreateViewParams) -> Result<View, FlowyError> {
|
2021-09-02 19:57:19 +08:00
|
|
|
let token = self.user.token()?;
|
2022-01-10 23:45:59 +08:00
|
|
|
let view = self.cloud_service.create_view(&token, params).await?;
|
2021-09-02 19:57:19 +08:00
|
|
|
Ok(view)
|
|
|
|
}
|
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(skip(self), err)]
|
2021-12-14 18:04:51 +08:00
|
|
|
fn update_view_on_server(&self, params: UpdateViewParams) -> Result<(), FlowyError> {
|
2021-09-02 19:57:19 +08:00
|
|
|
let token = self.user.token()?;
|
2022-01-10 23:45:59 +08:00
|
|
|
let server = self.cloud_service.clone();
|
2021-11-09 15:32:57 +08:00
|
|
|
tokio::spawn(async move {
|
2021-09-02 19:57:19 +08:00
|
|
|
match server.update_view(&token, params).await {
|
2022-01-24 17:35:58 +08:00
|
|
|
Ok(_) => {}
|
2021-09-02 19:57:19 +08:00
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Update view failed: {:?}", e);
|
2022-01-24 17:35:58 +08:00
|
|
|
}
|
2021-09-02 19:57:19 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(skip(self), err)]
|
2021-12-31 10:32:25 +08:00
|
|
|
fn read_view_on_server(&self, params: ViewId) -> Result<(), FlowyError> {
|
2021-09-02 19:57:19 +08:00
|
|
|
let token = self.user.token()?;
|
2022-01-10 23:45:59 +08:00
|
|
|
let server = self.cloud_service.clone();
|
2022-01-14 09:09:25 +08:00
|
|
|
let persistence = self.persistence.clone();
|
2022-01-01 14:23:58 +08:00
|
|
|
// TODO: Retry with RetryAction?
|
2021-11-09 15:32:57 +08:00
|
|
|
tokio::spawn(async move {
|
2021-09-02 19:57:19 +08:00
|
|
|
match server.read_view(&token, params).await {
|
2022-01-14 09:09:25 +08:00
|
|
|
Ok(Some(view)) => {
|
2022-01-20 23:51:11 +08:00
|
|
|
match persistence
|
|
|
|
.begin_transaction(|transaction| transaction.create_view(view.clone()))
|
|
|
|
.await
|
|
|
|
{
|
2022-01-14 09:09:25 +08:00
|
|
|
Ok(_) => {
|
2022-01-27 20:39:54 +08:00
|
|
|
send_dart_notification(&view.id, FolderNotification::ViewUpdated)
|
2022-01-14 09:09:25 +08:00
|
|
|
.payload(view.clone())
|
|
|
|
.send();
|
2022-01-24 17:35:58 +08:00
|
|
|
}
|
2022-01-14 09:09:25 +08:00
|
|
|
Err(e) => log::error!("Save view failed: {:?}", e),
|
|
|
|
}
|
2022-01-24 17:35:58 +08:00
|
|
|
}
|
|
|
|
Ok(None) => {}
|
2021-10-17 22:44:51 +08:00
|
|
|
Err(e) => log::error!("Read view failed: {:?}", e),
|
2021-09-02 19:57:19 +08:00
|
|
|
}
|
|
|
|
});
|
2021-07-23 22:42:44 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-13 23:11:45 +08:00
|
|
|
|
|
|
|
fn listen_trash_can_event(&self) {
|
2021-12-29 21:49:31 +08:00
|
|
|
let mut rx = self.trash_controller.subscribe();
|
2022-01-14 09:09:25 +08:00
|
|
|
let persistence = self.persistence.clone();
|
2022-02-25 22:27:44 +08:00
|
|
|
let document_manager = self.block_manager.clone();
|
2022-01-14 09:09:25 +08:00
|
|
|
let trash_controller = self.trash_controller.clone();
|
2021-10-13 23:11:45 +08:00
|
|
|
let _ = tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
let mut stream = Box::pin(rx.recv().into_stream().filter_map(|result| async move {
|
|
|
|
match result {
|
2022-02-08 14:36:59 +08:00
|
|
|
Ok(event) => event.select(TrashType::TrashView),
|
2021-10-19 13:04:09 +08:00
|
|
|
Err(_e) => None,
|
2021-10-13 23:11:45 +08:00
|
|
|
}
|
|
|
|
}));
|
2021-11-27 19:19:41 +08:00
|
|
|
|
|
|
|
if let Some(event) = stream.next().await {
|
2022-01-22 18:48:43 +08:00
|
|
|
handle_trash_event(
|
|
|
|
persistence.clone(),
|
|
|
|
document_manager.clone(),
|
|
|
|
trash_controller.clone(),
|
|
|
|
event,
|
|
|
|
)
|
|
|
|
.await
|
2021-10-13 23:11:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 18:48:43 +08:00
|
|
|
#[tracing::instrument(level = "trace", skip(persistence, document_manager, trash_can))]
|
2021-10-16 21:22:59 +08:00
|
|
|
async fn handle_trash_event(
|
2022-01-17 11:55:36 +08:00
|
|
|
persistence: Arc<FolderPersistence>,
|
2022-02-25 22:27:44 +08:00
|
|
|
document_manager: Arc<BlockManager>,
|
2021-12-06 15:49:21 +08:00
|
|
|
trash_can: Arc<TrashController>,
|
2021-10-16 21:22:59 +08:00
|
|
|
event: TrashEvent,
|
|
|
|
) {
|
2021-10-15 13:10:52 +08:00
|
|
|
match event {
|
2021-10-31 17:24:55 +08:00
|
|
|
TrashEvent::NewTrash(identifiers, ret) => {
|
2022-01-20 23:51:11 +08:00
|
|
|
let result = persistence
|
|
|
|
.begin_transaction(|transaction| {
|
|
|
|
let views = read_local_views_with_transaction(identifiers, &transaction)?;
|
|
|
|
for view in views {
|
|
|
|
let _ = notify_views_changed(&view.belong_to_id, trash_can.clone(), &transaction)?;
|
2022-01-27 20:39:54 +08:00
|
|
|
notify_dart(view, FolderNotification::ViewDeleted);
|
2022-01-20 23:51:11 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.await;
|
2022-01-14 09:09:25 +08:00
|
|
|
let _ = ret.send(result).await;
|
2022-01-24 17:35:58 +08:00
|
|
|
}
|
2021-10-31 17:24:55 +08:00
|
|
|
TrashEvent::Putback(identifiers, ret) => {
|
2022-01-20 23:51:11 +08:00
|
|
|
let result = persistence
|
|
|
|
.begin_transaction(|transaction| {
|
|
|
|
let views = read_local_views_with_transaction(identifiers, &transaction)?;
|
|
|
|
for view in views {
|
|
|
|
let _ = notify_views_changed(&view.belong_to_id, trash_can.clone(), &transaction)?;
|
2022-01-27 20:39:54 +08:00
|
|
|
notify_dart(view, FolderNotification::ViewRestored);
|
2022-01-20 23:51:11 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.await;
|
2022-01-14 09:09:25 +08:00
|
|
|
let _ = ret.send(result).await;
|
2022-01-24 17:35:58 +08:00
|
|
|
}
|
2021-10-17 22:44:51 +08:00
|
|
|
TrashEvent::Delete(identifiers, ret) => {
|
2022-01-20 23:51:11 +08:00
|
|
|
let result = persistence
|
|
|
|
.begin_transaction(|transaction| {
|
|
|
|
let mut notify_ids = HashSet::new();
|
|
|
|
for identifier in identifiers.items {
|
|
|
|
let view = transaction.read_view(&identifier.id)?;
|
|
|
|
let _ = transaction.delete_view(&identifier.id)?;
|
2022-01-22 18:48:43 +08:00
|
|
|
let _ = document_manager.delete(&identifier.id)?;
|
2022-01-20 23:51:11 +08:00
|
|
|
notify_ids.insert(view.belong_to_id);
|
|
|
|
}
|
2021-10-18 18:40:15 +08:00
|
|
|
|
2022-01-20 23:51:11 +08:00
|
|
|
for notify_id in notify_ids {
|
|
|
|
let _ = notify_views_changed(¬ify_id, trash_can.clone(), &transaction)?;
|
|
|
|
}
|
2021-10-18 18:40:15 +08:00
|
|
|
|
2022-01-20 23:51:11 +08:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.await;
|
2022-01-14 09:09:25 +08:00
|
|
|
let _ = ret.send(result).await;
|
2022-01-24 17:35:58 +08:00
|
|
|
}
|
2021-10-13 23:11:45 +08:00
|
|
|
}
|
2021-07-19 22:44:37 +08:00
|
|
|
}
|
2021-10-16 16:45:52 +08:00
|
|
|
|
2022-01-14 09:09:25 +08:00
|
|
|
fn read_local_views_with_transaction<'a>(
|
|
|
|
identifiers: RepeatedTrashId,
|
2022-01-17 11:55:36 +08:00
|
|
|
transaction: &'a (dyn FolderPersistenceTransaction + 'a),
|
2022-01-14 09:09:25 +08:00
|
|
|
) -> Result<Vec<View>, FlowyError> {
|
|
|
|
let mut views = vec![];
|
|
|
|
for identifier in identifiers.items {
|
|
|
|
let view = transaction.read_view(&identifier.id)?;
|
|
|
|
views.push(view);
|
|
|
|
}
|
|
|
|
Ok(views)
|
2021-10-31 17:24:55 +08:00
|
|
|
}
|
|
|
|
|
2022-01-27 20:39:54 +08:00
|
|
|
fn notify_dart(view: View, notification: FolderNotification) {
|
2021-10-31 17:24:55 +08:00
|
|
|
send_dart_notification(&view.id, notification).payload(view).send();
|
|
|
|
}
|
|
|
|
|
2022-01-14 09:09:25 +08:00
|
|
|
#[tracing::instrument(skip(belong_to_id, trash_controller, transaction), fields(view_count), err)]
|
|
|
|
fn notify_views_changed<'a>(
|
2021-12-06 15:49:21 +08:00
|
|
|
belong_to_id: &str,
|
2021-12-29 21:49:31 +08:00
|
|
|
trash_controller: Arc<TrashController>,
|
2022-01-17 11:55:36 +08:00
|
|
|
transaction: &'a (dyn FolderPersistenceTransaction + 'a),
|
2021-12-14 18:04:51 +08:00
|
|
|
) -> FlowyResult<()> {
|
2022-01-14 09:09:25 +08:00
|
|
|
let repeated_view = read_belonging_views_on_local(belong_to_id, trash_controller.clone(), transaction)?;
|
2021-10-28 13:42:39 +08:00
|
|
|
tracing::Span::current().record("view_count", &format!("{}", repeated_view.len()).as_str());
|
2022-01-27 20:39:54 +08:00
|
|
|
send_dart_notification(belong_to_id, FolderNotification::AppViewsChanged)
|
2021-10-16 16:45:52 +08:00
|
|
|
.payload(repeated_view)
|
|
|
|
.send();
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-16 21:22:59 +08:00
|
|
|
|
2022-01-14 09:09:25 +08:00
|
|
|
fn read_belonging_views_on_local<'a>(
|
2021-10-16 21:22:59 +08:00
|
|
|
belong_to_id: &str,
|
2021-12-29 00:34:00 +08:00
|
|
|
trash_controller: Arc<TrashController>,
|
2022-01-17 11:55:36 +08:00
|
|
|
transaction: &'a (dyn FolderPersistenceTransaction + 'a),
|
2021-12-14 18:04:51 +08:00
|
|
|
) -> FlowyResult<RepeatedView> {
|
2022-01-14 09:09:25 +08:00
|
|
|
let mut views = transaction.read_views(belong_to_id)?;
|
|
|
|
let trash_ids = trash_controller.read_trash_ids(transaction)?;
|
|
|
|
views.retain(|view_table| !trash_ids.contains(&view_table.id));
|
2021-10-30 17:19:50 +08:00
|
|
|
|
|
|
|
Ok(RepeatedView { items: views })
|
2021-10-16 21:22:59 +08:00
|
|
|
}
|