2021-07-08 21:23:44 +08:00
|
|
|
use flowy_dispatch::prelude::Module;
|
2021-07-13 23:08:20 +08:00
|
|
|
use flowy_user::prelude::*;
|
2021-07-14 21:12:52 +08:00
|
|
|
|
2021-07-17 10:26:05 +08:00
|
|
|
use crate::flowy_server::{ArcFlowyServer, FlowyServerMocker};
|
2021-07-14 21:12:52 +08:00
|
|
|
use flowy_database::DBConnection;
|
2021-07-16 23:18:12 +08:00
|
|
|
|
2021-07-13 23:08:20 +08:00
|
|
|
use flowy_workspace::prelude::*;
|
2021-07-09 23:31:44 +08:00
|
|
|
use std::sync::Arc;
|
2021-06-30 23:11:27 +08:00
|
|
|
|
2021-07-09 23:31:44 +08:00
|
|
|
pub struct ModuleConfig {
|
|
|
|
pub root: String,
|
|
|
|
}
|
|
|
|
|
2021-07-16 23:18:12 +08:00
|
|
|
pub fn build_modules(config: ModuleConfig, _server: ArcFlowyServer) -> Vec<Module> {
|
2021-07-14 21:12:52 +08:00
|
|
|
let user_session = Arc::new(
|
|
|
|
UserSessionBuilder::new()
|
|
|
|
.root_dir(&config.root)
|
2021-07-17 10:26:05 +08:00
|
|
|
.build(Arc::new(FlowyServerMocker {})),
|
2021-07-14 21:12:52 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
let workspace_user_impl = Arc::new(WorkspaceUserImpl {
|
|
|
|
user_session: user_session.clone(),
|
|
|
|
});
|
2021-07-13 23:08:20 +08:00
|
|
|
|
2021-07-14 08:07:25 +08:00
|
|
|
vec![
|
|
|
|
flowy_user::module::create(user_session),
|
2021-07-14 21:12:52 +08:00
|
|
|
flowy_workspace::module::create(workspace_user_impl),
|
2021-07-14 08:07:25 +08:00
|
|
|
]
|
2021-07-09 23:31:44 +08:00
|
|
|
}
|
2021-07-14 21:12:52 +08:00
|
|
|
|
|
|
|
pub struct WorkspaceUserImpl {
|
|
|
|
user_session: Arc<UserSession>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkspaceUser for WorkspaceUserImpl {
|
2021-07-16 23:18:12 +08:00
|
|
|
fn set_current_workspace(&self, id: &str) { UserSession::set_current_workspace(id); }
|
2021-07-14 21:12:52 +08:00
|
|
|
|
|
|
|
fn get_current_workspace(&self) -> Result<String, WorkspaceError> {
|
2021-07-16 23:18:12 +08:00
|
|
|
let user_detail = self.user_session.user_detail().map_err(|e| {
|
|
|
|
ErrorBuilder::new(WorkspaceErrorCode::UserNotLoginYet)
|
2021-07-14 21:12:52 +08:00
|
|
|
.error(e)
|
|
|
|
.build()
|
2021-07-16 23:18:12 +08:00
|
|
|
})?;
|
|
|
|
Ok(user_detail.id)
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn db_connection(&self) -> Result<DBConnection, WorkspaceError> {
|
|
|
|
self.user_session.get_db_connection().map_err(|e| {
|
|
|
|
ErrorBuilder::new(WorkspaceErrorCode::DatabaseConnectionFail)
|
|
|
|
.error(e)
|
|
|
|
.build()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|