refactor: template

This commit is contained in:
nathan 2024-01-24 11:46:23 +08:00
parent bb103531e2
commit bb1029077c
2 changed files with 79 additions and 40 deletions

View File

@ -34,6 +34,8 @@ pub struct TemplateData {
pub type WorkspaceTemplateHandlers = HashMap<ViewLayout, Arc<dyn WorkspaceTemplate + Send + Sync>>; pub type WorkspaceTemplateHandlers = HashMap<ViewLayout, Arc<dyn WorkspaceTemplate + Send + Sync>>;
/// A builder for creating a workspace template.
/// workspace template is a set of views that are created when a workspace is created.
pub struct WorkspaceTemplateBuilder { pub struct WorkspaceTemplateBuilder {
pub uid: i64, pub uid: i64,
pub workspace_id: String, pub workspace_id: String,
@ -50,12 +52,25 @@ impl WorkspaceTemplateBuilder {
} }
} }
pub fn with_template<T: WorkspaceTemplate>(mut self, template: T) -> Self { pub fn with_template<T>(mut self, template: T) -> Self
where
T: WorkspaceTemplate + Send + Sync + 'static,
{
self.handlers.insert(template.layout(), Arc::new(template)); self.handlers.insert(template.layout(), Arc::new(template));
self self
} }
pub async fn default_workspace(&self) -> Result<Vec<TemplateData>> { pub fn with_templates<T>(mut self, templates: Vec<T>) -> Self
where
T: WorkspaceTemplate + Send + Sync + 'static,
{
for template in templates {
self.handlers.insert(template.layout(), Arc::new(template));
}
self
}
pub async fn build(&self) -> Result<Vec<TemplateData>> {
let workspace_view_builder = Arc::new(RwLock::new(WorkspaceViewBuilder::new( let workspace_view_builder = Arc::new(RwLock::new(WorkspaceViewBuilder::new(
self.workspace_id.clone(), self.workspace_id.clone(),
self.uid, self.uid,

View File

@ -22,11 +22,11 @@ use database::user::{create_user, is_user_exist};
use realtime::entities::RealtimeUser; use realtime::entities::RealtimeUser;
use shared_entity::dto::auth_dto::UpdateUserParams; use shared_entity::dto::auth_dto::UpdateUserParams;
use snowflake::Snowflake; use snowflake::Snowflake;
use sqlx::{types::uuid, PgPool}; use sqlx::{types::uuid, PgPool, Transaction};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tracing::{debug, event, instrument}; use tracing::{debug, event, instrument};
use workspace_template::document::get_started::GetStartedDocumentTemplate; use workspace_template::document::get_started::GetStartedDocumentTemplate;
use workspace_template::WorkspaceTemplateBuilder; use workspace_template::{WorkspaceTemplate, WorkspaceTemplateBuilder};
/// Verify the token from the gotrue server and create the user if it is a new user /// Verify the token from the gotrue server and create the user if it is a new user
/// Return true if the user is a new user /// Return true if the user is a new user
@ -87,11 +87,40 @@ where
) )
.await?; .await?;
// Create the default workspace for the user. A default workspace might contain multiple // Create a workspace with the GetStarted template
// templates, e.g. a document template, a database template, etc. create_workspace_for_user(
let templates = WorkspaceTemplateBuilder::new(new_uid, &workspace_id) new_uid,
.with_template(GetStartedDocumentTemplate) &workspace_id,
.default_workspace() collab_access_control,
&mut txn,
vec![GetStartedDocumentTemplate],
)
.await?;
}
txn
.commit()
.await
.context("fail to commit transaction to verify token")?;
Ok(is_new)
}
/// Create a workspace for a user.
/// This function generates a workspace along with its templates and stores them in the database.
/// Each template is stored as an individual collaborative object.
async fn create_workspace_for_user<C, T>(
new_uid: i64,
workspace_id: &str,
collab_access_control: &C,
txn: &mut Transaction<'_, sqlx::Postgres>,
templates: Vec<T>,
) -> Result<(), AppError>
where
C: CollabAccessControl,
T: WorkspaceTemplate + Send + Sync + 'static,
{
let templates = WorkspaceTemplateBuilder::new(new_uid, workspace_id)
.with_templates(templates)
.build()
.await?; .await?;
debug!("create {} templates for user:{}", templates.len(), new_uid); debug!("create {} templates for user:{}", templates.len(), new_uid);
@ -111,9 +140,9 @@ where
.await?; .await?;
insert_into_af_collab( insert_into_af_collab(
&mut txn, txn,
&new_uid, &new_uid,
&workspace_id, workspace_id,
&CollabParams { &CollabParams {
object_id, object_id,
encoded_collab_v1, encoded_collab_v1,
@ -123,12 +152,7 @@ where
) )
.await?; .await?;
} }
} Ok(())
txn
.commit()
.await
.context("fail to commit transaction to verify token")?;
Ok(is_new)
} }
pub async fn get_profile(pg_pool: &PgPool, uuid: &Uuid) -> Result<AFUserProfile, AppError> { pub async fn get_profile(pg_pool: &PgPool, uuid: &Uuid) -> Result<AFUserProfile, AppError> {