2023-06-03 15:35:55 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-06-03 05:55:43 +00:00
|
|
|
use collab_folder::core::{FolderData, RepeatedView, ViewIdentifier, Workspace};
|
|
|
|
use tokio::sync::RwLock;
|
2023-05-17 01:49:39 +00:00
|
|
|
|
2023-06-03 15:35:55 +00:00
|
|
|
use lib_infra::util::timestamp;
|
|
|
|
|
2023-06-03 05:55:43 +00:00
|
|
|
use crate::entities::{view_pb_with_child_views, ViewPB, WorkspacePB};
|
|
|
|
use crate::view_operation::{
|
|
|
|
FlattedViews, FolderOperationHandlers, ParentChildViews, WorkspaceViewBuilder,
|
|
|
|
};
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
pub struct DefaultFolderBuilder();
|
|
|
|
impl DefaultFolderBuilder {
|
|
|
|
pub async fn build(
|
2023-06-03 05:55:43 +00:00
|
|
|
_uid: i64,
|
2023-05-21 10:53:59 +00:00
|
|
|
workspace_id: String,
|
2023-05-31 06:08:54 +00:00
|
|
|
handlers: &FolderOperationHandlers,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> (FolderData, WorkspacePB) {
|
2023-06-03 05:55:43 +00:00
|
|
|
let workspace_view_builder =
|
|
|
|
Arc::new(RwLock::new(WorkspaceViewBuilder::new(workspace_id.clone())));
|
|
|
|
for handler in handlers.values() {
|
|
|
|
let _ = handler
|
|
|
|
.create_workspace_view(workspace_view_builder.clone())
|
|
|
|
.await;
|
|
|
|
}
|
2023-04-04 00:41:16 +00:00
|
|
|
|
2023-06-03 05:55:43 +00:00
|
|
|
let views = workspace_view_builder.write().await.build();
|
|
|
|
// Safe to unwrap because we have at least one view. check out the DocumentFolderOperation.
|
|
|
|
let first_view = views
|
|
|
|
.first()
|
|
|
|
.unwrap()
|
|
|
|
.child_views
|
|
|
|
.first()
|
|
|
|
.unwrap()
|
|
|
|
.parent_view
|
|
|
|
.clone();
|
2023-04-04 00:41:16 +00:00
|
|
|
|
2023-06-03 05:55:43 +00:00
|
|
|
let first_level_views = views
|
|
|
|
.iter()
|
|
|
|
.map(|value| ViewIdentifier {
|
|
|
|
id: value.parent_view.id.clone(),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
let workspace = Workspace {
|
|
|
|
id: workspace_id,
|
|
|
|
name: "Workspace".to_string(),
|
2023-06-03 05:55:43 +00:00
|
|
|
child_views: RepeatedView::new(first_level_views),
|
|
|
|
created_at: timestamp(),
|
2023-04-04 00:41:16 +00:00
|
|
|
};
|
|
|
|
|
2023-06-03 15:35:55 +00:00
|
|
|
let first_level_view_pbs = views.iter().map(ViewPB::from).collect::<Vec<_>>();
|
2023-06-03 05:55:43 +00:00
|
|
|
|
|
|
|
let workspace_pb = WorkspacePB {
|
|
|
|
id: workspace.id.clone(),
|
|
|
|
name: workspace.name.clone(),
|
|
|
|
views: first_level_view_pbs,
|
|
|
|
create_time: workspace.created_at,
|
|
|
|
};
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
(
|
|
|
|
FolderData {
|
|
|
|
current_workspace: workspace.id.clone(),
|
2023-06-03 05:55:43 +00:00
|
|
|
current_view: first_view.id,
|
2023-04-04 00:41:16 +00:00
|
|
|
workspaces: vec![workspace],
|
2023-06-03 05:55:43 +00:00
|
|
|
views: FlattedViews::flatten_views(views),
|
2023-04-04 00:41:16 +00:00
|
|
|
},
|
|
|
|
workspace_pb,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_workspace_id() -> String {
|
2023-06-07 14:27:13 +00:00
|
|
|
uuid::Uuid::new_v4().to_string()
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 05:55:43 +00:00
|
|
|
impl From<&ParentChildViews> for ViewPB {
|
|
|
|
fn from(value: &ParentChildViews) -> Self {
|
|
|
|
view_pb_with_child_views(
|
|
|
|
value.parent_view.clone(),
|
|
|
|
value
|
|
|
|
.child_views
|
|
|
|
.iter()
|
|
|
|
.map(|v| v.parent_view.clone())
|
|
|
|
.collect(),
|
|
|
|
)
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
}
|