2023-05-17 01:49:39 +00:00
|
|
|
use std::collections::HashMap;
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
use chrono::Utc;
|
|
|
|
use collab_folder::core::{Belonging, Belongings, FolderData, View, ViewLayout, Workspace};
|
|
|
|
use nanoid::nanoid;
|
2023-05-17 01:49:39 +00:00
|
|
|
|
|
|
|
use crate::entities::{view_pb_with_child_views, WorkspacePB};
|
|
|
|
use crate::view_ext::{gen_view_id, ViewDataProcessorMap};
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
pub struct DefaultFolderBuilder();
|
|
|
|
impl DefaultFolderBuilder {
|
|
|
|
pub async fn build(
|
|
|
|
uid: i64,
|
|
|
|
view_processors: &ViewDataProcessorMap,
|
|
|
|
) -> (FolderData, WorkspacePB) {
|
|
|
|
let time = Utc::now().timestamp();
|
|
|
|
let workspace_id = gen_workspace_id();
|
|
|
|
let view_id = gen_view_id();
|
|
|
|
let child_view_id = gen_view_id();
|
|
|
|
|
|
|
|
let child_view_layout = ViewLayout::Document;
|
|
|
|
let child_view = View {
|
|
|
|
id: child_view_id.clone(),
|
|
|
|
bid: view_id.clone(),
|
|
|
|
name: "Read me".to_string(),
|
|
|
|
desc: "".to_string(),
|
|
|
|
belongings: Default::default(),
|
|
|
|
created_at: time,
|
|
|
|
layout: child_view_layout.clone(),
|
|
|
|
database_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// create the document
|
|
|
|
let data = initial_read_me().into_bytes();
|
|
|
|
let processor = view_processors.get(&child_view_layout).unwrap();
|
|
|
|
processor
|
|
|
|
.create_view_with_custom_data(
|
|
|
|
uid,
|
|
|
|
&child_view.id,
|
|
|
|
&child_view.name,
|
|
|
|
data,
|
|
|
|
child_view_layout.clone(),
|
|
|
|
HashMap::default(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let view = View {
|
|
|
|
id: view_id,
|
|
|
|
bid: workspace_id.clone(),
|
|
|
|
name: "⭐️ Getting started".to_string(),
|
|
|
|
desc: "".to_string(),
|
|
|
|
belongings: Belongings::new(vec![Belonging {
|
|
|
|
id: child_view.id.clone(),
|
|
|
|
name: child_view.name.clone(),
|
|
|
|
}]),
|
|
|
|
created_at: time,
|
|
|
|
layout: ViewLayout::Document,
|
|
|
|
database_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let workspace = Workspace {
|
|
|
|
id: workspace_id,
|
|
|
|
name: "Workspace".to_string(),
|
|
|
|
belongings: Belongings::new(vec![Belonging {
|
|
|
|
id: view.id.clone(),
|
|
|
|
name: view.name.clone(),
|
|
|
|
}]),
|
|
|
|
created_at: time,
|
|
|
|
};
|
|
|
|
|
|
|
|
let workspace_pb = workspace_pb_from_workspace(&workspace, &view, &child_view);
|
|
|
|
|
|
|
|
(
|
|
|
|
FolderData {
|
|
|
|
current_workspace: workspace.id.clone(),
|
|
|
|
current_view: child_view_id,
|
|
|
|
workspaces: vec![workspace],
|
|
|
|
views: vec![view, child_view],
|
|
|
|
},
|
|
|
|
workspace_pb,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_workspace_id() -> String {
|
|
|
|
nanoid!(10)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn workspace_pb_from_workspace(
|
|
|
|
workspace: &Workspace,
|
|
|
|
view: &View,
|
|
|
|
child_view: &View,
|
|
|
|
) -> WorkspacePB {
|
2023-05-10 11:43:32 +00:00
|
|
|
let view_pb = view_pb_with_child_views(view.clone(), vec![child_view.clone()]);
|
2023-04-04 00:41:16 +00:00
|
|
|
WorkspacePB {
|
|
|
|
id: workspace.id.clone(),
|
|
|
|
name: workspace.name.clone(),
|
|
|
|
views: vec![view_pb],
|
|
|
|
create_time: workspace.created_at,
|
|
|
|
}
|
|
|
|
}
|
2023-05-17 01:49:39 +00:00
|
|
|
|
|
|
|
pub fn initial_read_me() -> String {
|
|
|
|
let document_content = include_str!("READ_ME.json");
|
|
|
|
document_content.to_string()
|
|
|
|
}
|