2023-07-29 01:46:24 +00:00
|
|
|
use std::sync::{Arc, Weak};
|
2023-06-05 01:42:11 +00:00
|
|
|
|
2023-08-17 15:46:39 +00:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
|
|
|
|
|
2023-06-14 14:16:33 +00:00
|
|
|
use crate::entities::*;
|
2023-07-05 12:57:09 +00:00
|
|
|
use crate::manager::FolderManager;
|
2023-05-31 06:08:54 +00:00
|
|
|
use crate::share::ImportParams;
|
2023-04-04 00:41:16 +00:00
|
|
|
|
2023-07-29 01:46:24 +00:00
|
|
|
fn upgrade_folder(
|
|
|
|
folder_manager: AFPluginState<Weak<FolderManager>>,
|
|
|
|
) -> FlowyResult<Arc<FolderManager>> {
|
|
|
|
let folder = folder_manager
|
|
|
|
.upgrade()
|
2023-08-21 16:19:15 +00:00
|
|
|
.ok_or(FlowyError::internal().with_context("The folder manager is already dropped"))?;
|
2023-07-29 01:46:24 +00:00
|
|
|
Ok(folder)
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, folder), err)]
|
|
|
|
pub(crate) async fn create_workspace_handler(
|
|
|
|
data: AFPluginData<CreateWorkspacePayloadPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> DataResult<WorkspacePB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let params: CreateWorkspaceParams = data.into_inner().try_into()?;
|
|
|
|
let workspace = folder.create_workspace(params).await?;
|
2023-11-01 03:45:35 +00:00
|
|
|
let views = folder
|
|
|
|
.get_views_belong_to(&workspace.id)
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(view_pb_without_child_views)
|
|
|
|
.collect::<Vec<ViewPB>>();
|
|
|
|
data_result_ok(WorkspacePB {
|
|
|
|
id: workspace.id,
|
|
|
|
name: workspace.name,
|
|
|
|
views,
|
|
|
|
create_time: workspace.created_at,
|
|
|
|
})
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
2023-07-14 05:37:13 +00:00
|
|
|
pub(crate) async fn get_workspace_views_handler(
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> DataResult<RepeatedViewPB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let child_views = folder.get_current_workspace_views().await?;
|
|
|
|
let repeated_view: RepeatedViewPB = child_views.into();
|
|
|
|
data_result_ok(repeated_view)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(data, folder), err)]
|
|
|
|
pub(crate) async fn open_workspace_handler(
|
|
|
|
data: AFPluginData<WorkspaceIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> DataResult<WorkspacePB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-11-01 03:45:35 +00:00
|
|
|
let workspace_id = data.into_inner().value;
|
|
|
|
if workspace_id.is_empty() {
|
|
|
|
Err(FlowyError::workspace_id().with_context("workspace id should not be empty"))
|
|
|
|
} else {
|
|
|
|
let workspace = folder.open_workspace(&workspace_id).await?;
|
|
|
|
let views = folder.get_workspace_views(&workspace_id).await?;
|
|
|
|
let workspace_pb: WorkspacePB = (workspace, views).into();
|
|
|
|
data_result_ok(workspace_pb)
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 03:45:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
|
|
|
pub(crate) async fn read_current_workspace_setting_handler(
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-11-01 03:45:35 +00:00
|
|
|
) -> DataResult<WorkspaceSettingPB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-11-01 03:45:35 +00:00
|
|
|
let setting = folder
|
|
|
|
.get_workspace_setting_pb()
|
|
|
|
.await
|
|
|
|
.ok_or(FlowyError::record_not_found())?;
|
|
|
|
data_result_ok(setting)
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
2023-11-01 03:45:35 +00:00
|
|
|
pub(crate) async fn read_current_workspace_handler(
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-11-01 03:45:35 +00:00
|
|
|
) -> DataResult<WorkspacePB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-11-01 03:45:35 +00:00
|
|
|
let workspace = folder
|
|
|
|
.get_workspace_pb()
|
|
|
|
.await
|
|
|
|
.ok_or(FlowyError::record_not_found())?;
|
|
|
|
data_result_ok(workspace)
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn create_view_handler(
|
|
|
|
data: AFPluginData<CreateViewPayloadPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> DataResult<ViewPB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let params: CreateViewParams = data.into_inner().try_into()?;
|
2023-06-03 05:40:12 +00:00
|
|
|
let set_as_current = params.set_as_current;
|
2023-04-04 00:41:16 +00:00
|
|
|
let view = folder.create_view_with_params(params).await?;
|
2023-06-03 05:40:12 +00:00
|
|
|
if set_as_current {
|
|
|
|
let _ = folder.set_current_view(&view.id).await;
|
|
|
|
}
|
2023-06-23 15:19:34 +00:00
|
|
|
data_result_ok(view_pb_without_child_views(Arc::new(view)))
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 14:16:33 +00:00
|
|
|
pub(crate) async fn create_orphan_view_handler(
|
|
|
|
data: AFPluginData<CreateOrphanViewPayloadPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-06-14 14:16:33 +00:00
|
|
|
) -> DataResult<ViewPB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-06-14 14:16:33 +00:00
|
|
|
let params: CreateViewParams = data.into_inner().try_into()?;
|
|
|
|
let set_as_current = params.set_as_current;
|
|
|
|
let view = folder.create_orphan_view_with_params(params).await?;
|
|
|
|
if set_as_current {
|
|
|
|
let _ = folder.set_current_view(&view.id).await;
|
|
|
|
}
|
2023-06-23 15:19:34 +00:00
|
|
|
data_result_ok(view_pb_without_child_views(Arc::new(view)))
|
2023-06-14 14:16:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
pub(crate) async fn read_view_handler(
|
|
|
|
data: AFPluginData<ViewIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> DataResult<ViewPB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let view_id: ViewIdPB = data.into_inner();
|
2023-11-01 03:45:35 +00:00
|
|
|
let view_pb = folder.get_view_pb(&view_id.value).await?;
|
2023-04-04 00:41:16 +00:00
|
|
|
data_result_ok(view_pb)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(data, folder), err)]
|
|
|
|
pub(crate) async fn update_view_handler(
|
|
|
|
data: AFPluginData<UpdateViewPayloadPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let params: UpdateViewParams = data.into_inner().try_into()?;
|
2023-05-10 11:43:32 +00:00
|
|
|
folder.update_view_with_params(params).await?;
|
2023-04-04 00:41:16 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:27:14 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, folder), err)]
|
|
|
|
pub(crate) async fn update_view_icon_handler(
|
|
|
|
data: AFPluginData<UpdateViewIconPayloadPB>,
|
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let folder = upgrade_folder(folder)?;
|
|
|
|
let params: UpdateViewIconParams = data.into_inner().try_into()?;
|
|
|
|
folder.update_view_icon_with_params(params).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
pub(crate) async fn delete_view_handler(
|
|
|
|
data: AFPluginData<RepeatedViewIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let params: RepeatedViewIdPB = data.into_inner();
|
|
|
|
for view_id in ¶ms.items {
|
|
|
|
let _ = folder.move_view_to_trash(view_id).await;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-08-02 13:20:51 +00:00
|
|
|
pub(crate) async fn toggle_favorites_handler(
|
|
|
|
data: AFPluginData<RepeatedViewIdPB>,
|
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let params: RepeatedViewIdPB = data.into_inner();
|
|
|
|
let folder = upgrade_folder(folder)?;
|
|
|
|
for view_id in ¶ms.items {
|
|
|
|
let _ = folder.toggle_favorites(view_id).await;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
pub(crate) async fn set_latest_view_handler(
|
|
|
|
data: AFPluginData<ViewIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let view_id: ViewIdPB = data.into_inner();
|
|
|
|
let _ = folder.set_current_view(&view_id.value).await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn close_view_handler(
|
|
|
|
data: AFPluginData<ViewIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let view_id: ViewIdPB = data.into_inner();
|
|
|
|
let _ = folder.close_view(&view_id.value).await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip_all, err)]
|
|
|
|
pub(crate) async fn move_view_handler(
|
2023-06-05 08:09:18 +00:00
|
|
|
data: AFPluginData<MoveViewPayloadPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let params: MoveViewParams = data.into_inner().try_into()?;
|
|
|
|
folder
|
2023-06-06 16:05:27 +00:00
|
|
|
.move_view(¶ms.view_id, params.from, params.to)
|
2023-04-04 00:41:16 +00:00
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-26 08:49:50 +00:00
|
|
|
pub(crate) async fn move_nested_view_handler(
|
|
|
|
data: AFPluginData<MoveNestedViewPayloadPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-07-26 08:49:50 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-07-26 08:49:50 +00:00
|
|
|
let params: MoveNestedViewParams = data.into_inner().try_into()?;
|
|
|
|
folder
|
|
|
|
.move_nested_view(params.view_id, params.new_parent_id, params.prev_view_id)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, folder), err)]
|
|
|
|
pub(crate) async fn duplicate_view_handler(
|
|
|
|
data: AFPluginData<ViewPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let view: ViewPB = data.into_inner();
|
|
|
|
folder.duplicate_view(&view.id).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-08-02 13:20:51 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
|
|
|
pub(crate) async fn read_favorites_handler(
|
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
|
|
|
) -> DataResult<RepeatedViewPB, FlowyError> {
|
|
|
|
let folder = upgrade_folder(folder)?;
|
|
|
|
let favorites = folder.get_all_favorites().await;
|
|
|
|
let mut views = vec![];
|
|
|
|
for info in favorites {
|
2023-11-01 03:45:35 +00:00
|
|
|
if let Ok(view) = folder.get_view_pb(&info.id).await {
|
|
|
|
views.push(view);
|
2023-08-02 13:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data_result_ok(RepeatedViewPB { items: views })
|
|
|
|
}
|
2023-04-04 00:41:16 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
|
|
|
pub(crate) async fn read_trash_handler(
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> DataResult<RepeatedTrashPB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let trash = folder.get_all_trash().await;
|
|
|
|
data_result_ok(trash.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(identifier, folder), err)]
|
|
|
|
pub(crate) async fn putback_trash_handler(
|
|
|
|
identifier: AFPluginData<TrashIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
folder.restore_trash(&identifier.id).await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(identifiers, folder), err)]
|
|
|
|
pub(crate) async fn delete_trash_handler(
|
|
|
|
identifiers: AFPluginData<RepeatedTrashIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
let trash_ids = identifiers.into_inner().items;
|
|
|
|
for trash_id in trash_ids {
|
2023-06-05 01:42:11 +00:00
|
|
|
let _ = folder.delete_trash(&trash_id.id).await;
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
|
|
|
pub(crate) async fn restore_all_trash_handler(
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
folder.restore_all_trash().await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
|
|
|
pub(crate) async fn delete_all_trash_handler(
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-04-04 00:41:16 +00:00
|
|
|
folder.delete_all_trash().await;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-31 06:08:54 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(data, folder), err)]
|
|
|
|
pub(crate) async fn import_data_handler(
|
|
|
|
data: AFPluginData<ImportPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-05-31 06:08:54 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-05-31 06:08:54 +00:00
|
|
|
let params: ImportParams = data.into_inner().try_into()?;
|
|
|
|
folder.import(params).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(folder), err)]
|
|
|
|
pub(crate) async fn get_folder_snapshots_handler(
|
|
|
|
data: AFPluginData<WorkspaceIdPB>,
|
2023-07-29 01:46:24 +00:00
|
|
|
folder: AFPluginState<Weak<FolderManager>>,
|
2023-07-05 12:57:09 +00:00
|
|
|
) -> DataResult<RepeatedFolderSnapshotPB, FlowyError> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let folder = upgrade_folder(folder)?;
|
2023-11-01 03:45:35 +00:00
|
|
|
let data = data.into_inner();
|
|
|
|
let snapshots = folder.get_folder_snapshots(&data.value, 10).await?;
|
|
|
|
data_result_ok(RepeatedFolderSnapshotPB { items: snapshots })
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|