2024-08-14 10:33:23 +08:00
|
|
|
use async_trait::async_trait;
|
2023-04-04 08:41:16 +08:00
|
|
|
use bytes::Bytes;
|
2024-07-08 13:45:57 +08:00
|
|
|
use collab::entity::EncodedCollab;
|
2024-08-25 15:14:54 +08:00
|
|
|
use collab_entity::CollabType;
|
2024-10-03 23:45:29 +08:00
|
|
|
use collab_folder::hierarchy_builder::NestedViewBuilder;
|
2023-11-01 11:45:35 +08:00
|
|
|
pub use collab_folder::View;
|
2023-12-29 13:02:27 +08:00
|
|
|
use collab_folder::ViewLayout;
|
2024-08-14 10:33:23 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
2023-06-05 09:42:11 +08:00
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
2023-04-04 08:41:16 +08:00
|
|
|
use flowy_error::FlowyError;
|
2023-12-29 13:02:27 +08:00
|
|
|
|
2023-04-04 08:41:16 +08:00
|
|
|
use lib_infra::util::timestamp;
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-06-05 09:42:11 +08:00
|
|
|
use crate::entities::{CreateViewParams, ViewLayoutPB};
|
2024-07-22 13:35:42 +08:00
|
|
|
use crate::manager::FolderUser;
|
2023-06-05 18:29:52 +08:00
|
|
|
use crate::share::ImportType;
|
2023-04-04 08:41:16 +08:00
|
|
|
|
2024-07-22 13:35:42 +08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum EncodedCollabWrapper {
|
|
|
|
Document(DocumentEncodedCollab),
|
|
|
|
Database(DatabaseEncodedCollab),
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DocumentEncodedCollab {
|
|
|
|
pub document_encoded_collab: EncodedCollab,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DatabaseEncodedCollab {
|
|
|
|
pub database_encoded_collab: EncodedCollab,
|
|
|
|
pub database_row_encoded_collabs: HashMap<String, EncodedCollab>,
|
2024-09-09 19:40:52 +08:00
|
|
|
pub database_row_document_encoded_collabs: HashMap<String, EncodedCollab>,
|
2024-07-22 13:35:42 +08:00
|
|
|
pub database_relations: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
2024-08-25 15:14:54 +08:00
|
|
|
pub type ImportedData = (String, CollabType, EncodedCollab);
|
|
|
|
|
2023-05-31 14:08:54 +08:00
|
|
|
/// The handler will be used to handler the folder operation for a specific
|
|
|
|
/// view layout. Each [ViewLayout] will have a handler. So when creating a new
|
|
|
|
/// view, the [ViewLayout] will be used to get the handler.
|
2024-08-14 10:33:23 +08:00
|
|
|
#[async_trait]
|
2023-05-31 14:08:54 +08:00
|
|
|
pub trait FolderOperationHandler {
|
2024-10-29 11:21:49 +08:00
|
|
|
fn name(&self) -> &str;
|
2023-06-03 13:55:43 +08:00
|
|
|
/// Create the view for the workspace of new user.
|
|
|
|
/// Only called once when the user is created.
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn create_workspace_view(
|
2023-06-03 13:55:43 +08:00
|
|
|
&self,
|
2023-08-17 23:46:39 +08:00
|
|
|
_uid: i64,
|
2024-04-10 11:40:02 +08:00
|
|
|
_workspace_view_builder: Arc<RwLock<NestedViewBuilder>>,
|
2024-08-14 10:33:23 +08:00
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
Ok(())
|
2023-06-03 13:55:43 +08:00
|
|
|
}
|
|
|
|
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn open_view(&self, view_id: &str) -> Result<(), FlowyError>;
|
2023-04-04 08:41:16 +08:00
|
|
|
/// Closes the view and releases the resources that this view has in
|
|
|
|
/// the backend
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn close_view(&self, view_id: &str) -> Result<(), FlowyError>;
|
2023-04-04 08:41:16 +08:00
|
|
|
|
2023-06-05 09:42:11 +08:00
|
|
|
/// Called when the view is deleted.
|
|
|
|
/// This will called after the view is deleted from the trash.
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn delete_view(&self, view_id: &str) -> Result<(), FlowyError>;
|
2023-06-05 09:42:11 +08:00
|
|
|
|
2023-05-31 14:08:54 +08:00
|
|
|
/// Returns the [ViewData] that can be used to create the same view.
|
2024-10-29 11:21:49 +08:00
|
|
|
async fn duplicate_view(&self, view_id: &str) -> Result<Bytes, FlowyError>;
|
2023-04-04 08:41:16 +08:00
|
|
|
|
2024-07-22 13:35:42 +08:00
|
|
|
/// get the encoded collab data from the disk.
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn get_encoded_collab_v1_from_disk(
|
2024-07-08 13:45:57 +08:00
|
|
|
&self,
|
2024-07-22 13:35:42 +08:00
|
|
|
_user: Arc<dyn FolderUser>,
|
|
|
|
_view_id: &str,
|
2024-08-14 10:33:23 +08:00
|
|
|
) -> Result<EncodedCollabWrapper, FlowyError> {
|
|
|
|
Err(FlowyError::not_support())
|
2024-07-22 13:35:42 +08:00
|
|
|
}
|
2024-07-08 13:45:57 +08:00
|
|
|
|
2023-06-01 20:23:27 +08:00
|
|
|
/// Create a view with the data.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id`: the user id
|
|
|
|
/// * `view_id`: the view id
|
|
|
|
/// * `name`: the name of the view
|
|
|
|
/// * `data`: initial data of the view. The data should be parsed by the [FolderOperationHandler]
|
2024-05-05 22:04:34 +08:00
|
|
|
/// implementation.
|
|
|
|
/// For example,
|
|
|
|
/// 1. the data of the database will be [DatabaseData] that is serialized to JSON
|
|
|
|
/// 2. the data of the document will be [DocumentData] that is serialized to JSON
|
2023-06-01 20:23:27 +08:00
|
|
|
/// * `layout`: the layout of the view
|
|
|
|
/// * `meta`: use to carry extra information. For example, the database view will use this
|
|
|
|
/// to carry the reference database id.
|
2024-07-02 13:02:15 +08:00
|
|
|
///
|
|
|
|
/// The return value is the [Option<EncodedCollab>] that can be used to create the view.
|
|
|
|
/// It can be used in syncing the view data to cloud.
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn create_view_with_view_data(
|
2023-04-04 08:41:16 +08:00
|
|
|
&self,
|
|
|
|
user_id: i64,
|
2024-06-27 04:37:08 +03:30
|
|
|
params: CreateViewParams,
|
2024-08-14 10:33:23 +08:00
|
|
|
) -> Result<Option<EncodedCollab>, FlowyError>;
|
2023-04-04 08:41:16 +08:00
|
|
|
|
2023-05-31 14:08:54 +08:00
|
|
|
/// Create a view with the pre-defined data.
|
|
|
|
/// For example, the initial data of the grid/calendar/kanban board when
|
|
|
|
/// you create a new view.
|
2024-10-29 11:21:49 +08:00
|
|
|
async fn create_view_with_default_data(
|
2023-04-04 08:41:16 +08:00
|
|
|
&self,
|
|
|
|
user_id: i64,
|
|
|
|
view_id: &str,
|
|
|
|
name: &str,
|
|
|
|
layout: ViewLayout,
|
2024-08-14 10:33:23 +08:00
|
|
|
) -> Result<(), FlowyError>;
|
2023-05-31 14:08:54 +08:00
|
|
|
|
|
|
|
/// Create a view by importing data
|
2024-07-01 14:44:08 +08:00
|
|
|
///
|
|
|
|
/// The return value
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn import_from_bytes(
|
2023-05-31 14:08:54 +08:00
|
|
|
&self,
|
2023-08-17 23:46:39 +08:00
|
|
|
uid: i64,
|
2023-05-31 14:08:54 +08:00
|
|
|
view_id: &str,
|
|
|
|
name: &str,
|
2023-06-05 18:29:52 +08:00
|
|
|
import_type: ImportType,
|
2023-05-31 14:08:54 +08:00
|
|
|
bytes: Vec<u8>,
|
2024-08-25 15:14:54 +08:00
|
|
|
) -> Result<Vec<ImportedData>, FlowyError>;
|
2023-05-31 14:08:54 +08:00
|
|
|
|
|
|
|
/// Create a view by importing data from a file
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn import_from_file_path(
|
2023-05-31 14:08:54 +08:00
|
|
|
&self,
|
|
|
|
view_id: &str,
|
|
|
|
name: &str,
|
|
|
|
path: String,
|
2024-08-14 10:33:23 +08:00
|
|
|
) -> Result<(), FlowyError>;
|
2023-06-01 20:23:27 +08:00
|
|
|
|
|
|
|
/// Called when the view is updated. The handler is the `old` registered handler.
|
2024-08-14 10:33:23 +08:00
|
|
|
async fn did_update_view(&self, _old: &View, _new: &View) -> Result<(), FlowyError> {
|
|
|
|
Ok(())
|
2023-06-01 20:23:27 +08:00
|
|
|
}
|
2023-04-04 08:41:16 +08:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:08:54 +08:00
|
|
|
pub type FolderOperationHandlers =
|
|
|
|
Arc<HashMap<ViewLayout, Arc<dyn FolderOperationHandler + Send + Sync>>>;
|
2023-04-04 08:41:16 +08:00
|
|
|
|
2023-04-04 14:08:50 +08:00
|
|
|
impl From<ViewLayoutPB> for ViewLayout {
|
|
|
|
fn from(pb: ViewLayoutPB) -> Self {
|
2023-04-04 08:41:16 +08:00
|
|
|
match pb {
|
2023-04-04 14:08:50 +08:00
|
|
|
ViewLayoutPB::Document => ViewLayout::Document,
|
|
|
|
ViewLayoutPB::Grid => ViewLayout::Grid,
|
|
|
|
ViewLayoutPB::Board => ViewLayout::Board,
|
|
|
|
ViewLayoutPB::Calendar => ViewLayout::Calendar,
|
2024-06-03 14:27:28 +08:00
|
|
|
ViewLayoutPB::Chat => ViewLayout::Chat,
|
2023-04-04 08:41:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 21:48:17 +08:00
|
|
|
pub(crate) fn create_view(uid: i64, params: CreateViewParams, layout: ViewLayout) -> View {
|
2023-04-04 08:41:16 +08:00
|
|
|
let time = timestamp();
|
|
|
|
View {
|
|
|
|
id: params.view_id,
|
2023-06-01 20:23:27 +08:00
|
|
|
parent_view_id: params.parent_view_id,
|
2023-04-04 08:41:16 +08:00
|
|
|
name: params.name,
|
|
|
|
created_at: time,
|
2023-08-02 18:50:51 +05:30
|
|
|
is_favorite: false,
|
2023-04-04 08:41:16 +08:00
|
|
|
layout,
|
2024-06-25 10:03:02 +08:00
|
|
|
icon: params.icon,
|
2023-11-08 21:48:17 +08:00
|
|
|
created_by: Some(uid),
|
|
|
|
last_edited_time: 0,
|
|
|
|
last_edited_by: Some(uid),
|
2024-06-25 10:03:02 +08:00
|
|
|
extra: params.extra,
|
|
|
|
children: Default::default(),
|
2023-04-04 08:41:16 +08:00
|
|
|
}
|
|
|
|
}
|
2024-10-29 11:21:49 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum ViewData {
|
|
|
|
/// Indicate the data is duplicated from another view.
|
|
|
|
DuplicateData(Bytes),
|
|
|
|
/// Indicate the data is created by the user.
|
|
|
|
Data(Bytes),
|
|
|
|
Empty,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewData {
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
ViewData::DuplicateData(data) => data.is_empty(),
|
|
|
|
ViewData::Data(data) => data.is_empty(),
|
|
|
|
ViewData::Empty => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|