2021-07-17 02:26:05 +00:00
|
|
|
use crate::flowy_server::{ArcFlowyServer, FlowyServerMocker};
|
2021-07-14 13:12:52 +00:00
|
|
|
use flowy_database::DBConnection;
|
2021-07-18 15:56:36 +00:00
|
|
|
use flowy_dispatch::prelude::Module;
|
|
|
|
use flowy_user::{errors::UserError, prelude::*};
|
2021-07-13 15:08:20 +00:00
|
|
|
use flowy_workspace::prelude::*;
|
2021-07-18 15:56:36 +00:00
|
|
|
use futures_core::future::BoxFuture;
|
|
|
|
use std::{pin::Pin, sync::Arc};
|
2021-06-30 15:11:27 +00:00
|
|
|
|
2021-07-09 15:31:44 +00:00
|
|
|
pub struct ModuleConfig {
|
|
|
|
pub root: String,
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:18:12 +00:00
|
|
|
pub fn build_modules(config: ModuleConfig, _server: ArcFlowyServer) -> Vec<Module> {
|
2021-07-14 13:12:52 +00:00
|
|
|
let user_session = Arc::new(
|
|
|
|
UserSessionBuilder::new()
|
|
|
|
.root_dir(&config.root)
|
2021-07-17 02:26:05 +00:00
|
|
|
.build(Arc::new(FlowyServerMocker {})),
|
2021-07-14 13:12:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let workspace_user_impl = Arc::new(WorkspaceUserImpl {
|
|
|
|
user_session: user_session.clone(),
|
|
|
|
});
|
2021-07-13 15:08:20 +00:00
|
|
|
|
2021-07-14 00:07:25 +00:00
|
|
|
vec![
|
|
|
|
flowy_user::module::create(user_session),
|
2021-07-14 13:12:52 +00:00
|
|
|
flowy_workspace::module::create(workspace_user_impl),
|
2021-07-14 00:07:25 +00:00
|
|
|
]
|
2021-07-09 15:31:44 +00:00
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
|
|
|
|
pub struct WorkspaceUserImpl {
|
|
|
|
user_session: Arc<UserSession>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkspaceUser for WorkspaceUserImpl {
|
2021-07-18 15:56:36 +00:00
|
|
|
fn set_current_workspace(&self, workspace_id: &str) -> BoxFuture<()> {
|
|
|
|
let user_session = self.user_session.clone();
|
|
|
|
let workspace_id = workspace_id.to_owned();
|
|
|
|
Box::pin(async move {
|
|
|
|
match user_session.set_current_workspace(&workspace_id).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Set current workspace error: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
|
|
|
|
fn get_current_workspace(&self) -> Result<String, WorkspaceError> {
|
2021-07-16 15:18:12 +00:00
|
|
|
let user_detail = self.user_session.user_detail().map_err(|e| {
|
|
|
|
ErrorBuilder::new(WorkspaceErrorCode::UserNotLoginYet)
|
2021-07-14 13:12:52 +00:00
|
|
|
.error(e)
|
|
|
|
.build()
|
2021-07-16 15:18:12 +00:00
|
|
|
})?;
|
|
|
|
Ok(user_detail.id)
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn db_connection(&self) -> Result<DBConnection, WorkspaceError> {
|
|
|
|
self.user_session.get_db_connection().map_err(|e| {
|
|
|
|
ErrorBuilder::new(WorkspaceErrorCode::DatabaseConnectionFail)
|
|
|
|
.error(e)
|
|
|
|
.build()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|