|
@@ -5,15 +5,16 @@ use backend_service::{
|
|
|
response::FlowyResponse,
|
|
|
};
|
|
|
use flowy_core_data_model::entities::{
|
|
|
- app::{App, AppId, CreateAppParams, RepeatedApp, UpdateAppParams},
|
|
|
+ app::{App, AppId, CreateAppParams, UpdateAppParams},
|
|
|
trash::{RepeatedTrash, RepeatedTrashId},
|
|
|
- view::{CreateViewParams, RepeatedView, RepeatedViewId, UpdateViewParams, View, ViewId},
|
|
|
+ view::{CreateViewParams, RepeatedViewId, UpdateViewParams, View, ViewId},
|
|
|
workspace::{CreateWorkspaceParams, RepeatedWorkspace, UpdateWorkspaceParams, Workspace, WorkspaceId},
|
|
|
};
|
|
|
use flowy_error::FlowyError;
|
|
|
|
|
|
+use flowy_core::module::WorkspaceCloudService;
|
|
|
use lazy_static::lazy_static;
|
|
|
-use lib_infra::{future::FutureResult, timestamp, uuid_string};
|
|
|
+use lib_infra::future::FutureResult;
|
|
|
use std::sync::Arc;
|
|
|
use tokio::sync::broadcast;
|
|
|
|
|
@@ -25,10 +26,10 @@ impl CoreHttpCloudService {
|
|
|
pub fn new(config: ClientServerConfiguration) -> CoreHttpCloudService { Self { config } }
|
|
|
}
|
|
|
|
|
|
-impl CoreHttpCloudService {
|
|
|
- pub fn init(&self) {}
|
|
|
+impl WorkspaceCloudService for CoreHttpCloudService {
|
|
|
+ fn init(&self) {}
|
|
|
|
|
|
- pub fn create_workspace(&self, token: &str, params: CreateWorkspaceParams) -> FutureResult<Workspace, FlowyError> {
|
|
|
+ fn create_workspace(&self, token: &str, params: CreateWorkspaceParams) -> FutureResult<Workspace, FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.workspace_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -37,7 +38,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn read_workspace(&self, token: &str, params: WorkspaceId) -> FutureResult<RepeatedWorkspace, FlowyError> {
|
|
|
+ fn read_workspace(&self, token: &str, params: WorkspaceId) -> FutureResult<RepeatedWorkspace, FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.workspace_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -46,7 +47,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn update_workspace(&self, token: &str, params: UpdateWorkspaceParams) -> FutureResult<(), FlowyError> {
|
|
|
+ fn update_workspace(&self, token: &str, params: UpdateWorkspaceParams) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.workspace_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -55,7 +56,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn delete_workspace(&self, token: &str, params: WorkspaceId) -> FutureResult<(), FlowyError> {
|
|
|
+ fn delete_workspace(&self, token: &str, params: WorkspaceId) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.workspace_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -64,7 +65,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn create_view(&self, token: &str, params: CreateViewParams) -> FutureResult<View, FlowyError> {
|
|
|
+ fn create_view(&self, token: &str, params: CreateViewParams) -> FutureResult<View, FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.view_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -73,7 +74,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn read_view(&self, token: &str, params: ViewId) -> FutureResult<Option<View>, FlowyError> {
|
|
|
+ fn read_view(&self, token: &str, params: ViewId) -> FutureResult<Option<View>, FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.view_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -82,7 +83,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn delete_view(&self, token: &str, params: RepeatedViewId) -> FutureResult<(), FlowyError> {
|
|
|
+ fn delete_view(&self, token: &str, params: RepeatedViewId) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.view_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -91,7 +92,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn update_view(&self, token: &str, params: UpdateViewParams) -> FutureResult<(), FlowyError> {
|
|
|
+ fn update_view(&self, token: &str, params: UpdateViewParams) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.view_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -100,7 +101,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn create_app(&self, token: &str, params: CreateAppParams) -> FutureResult<App, FlowyError> {
|
|
|
+ fn create_app(&self, token: &str, params: CreateAppParams) -> FutureResult<App, FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.app_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -109,7 +110,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn read_app(&self, token: &str, params: AppId) -> FutureResult<Option<App>, FlowyError> {
|
|
|
+ fn read_app(&self, token: &str, params: AppId) -> FutureResult<Option<App>, FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.app_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -118,7 +119,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn update_app(&self, token: &str, params: UpdateAppParams) -> FutureResult<(), FlowyError> {
|
|
|
+ fn update_app(&self, token: &str, params: UpdateAppParams) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.app_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -127,7 +128,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn delete_app(&self, token: &str, params: AppId) -> FutureResult<(), FlowyError> {
|
|
|
+ fn delete_app(&self, token: &str, params: AppId) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.app_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -136,7 +137,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn create_trash(&self, token: &str, params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
|
|
+ fn create_trash(&self, token: &str, params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.trash_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -145,7 +146,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn delete_trash(&self, token: &str, params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
|
|
+ fn delete_trash(&self, token: &str, params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.trash_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -154,7 +155,7 @@ impl CoreHttpCloudService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn read_trash(&self, token: &str) -> FutureResult<RepeatedTrash, FlowyError> {
|
|
|
+ fn read_trash(&self, token: &str) -> FutureResult<RepeatedTrash, FlowyError> {
|
|
|
let token = token.to_owned();
|
|
|
let url = self.config.trash_url();
|
|
|
FutureResult::new(async move {
|
|
@@ -164,149 +165,6 @@ impl CoreHttpCloudService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pub struct CoreLocalCloudService {}
|
|
|
-impl CoreLocalCloudService {
|
|
|
- pub fn new(_config: &ClientServerConfiguration) -> Self { CoreLocalCloudService {} }
|
|
|
-}
|
|
|
-impl CoreLocalCloudService {
|
|
|
- pub fn init(&self) {}
|
|
|
-
|
|
|
- pub fn create_workspace(&self, _token: &str, params: CreateWorkspaceParams) -> FutureResult<Workspace, FlowyError> {
|
|
|
- let time = timestamp();
|
|
|
- let workspace = Workspace {
|
|
|
- id: uuid_string(),
|
|
|
- name: params.name,
|
|
|
- desc: params.desc,
|
|
|
- apps: RepeatedApp::default(),
|
|
|
- modified_time: time,
|
|
|
- create_time: time,
|
|
|
- };
|
|
|
-
|
|
|
- FutureResult::new(async { Ok(workspace) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn read_workspace(&self, _token: &str, _params: WorkspaceId) -> FutureResult<RepeatedWorkspace, FlowyError> {
|
|
|
- FutureResult::new(async {
|
|
|
- let repeated_workspace = RepeatedWorkspace { items: vec![] };
|
|
|
- Ok(repeated_workspace)
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn update_workspace(&self, _token: &str, _params: UpdateWorkspaceParams) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn delete_workspace(&self, _token: &str, _params: WorkspaceId) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn create_view(&self, _token: &str, params: CreateViewParams) -> FutureResult<View, FlowyError> {
|
|
|
- let time = timestamp();
|
|
|
- let view = View {
|
|
|
- id: params.view_id,
|
|
|
- belong_to_id: params.belong_to_id,
|
|
|
- name: params.name,
|
|
|
- desc: params.desc,
|
|
|
- view_type: params.view_type,
|
|
|
- version: 0,
|
|
|
- belongings: RepeatedView::default(),
|
|
|
- modified_time: time,
|
|
|
- create_time: time,
|
|
|
- };
|
|
|
- FutureResult::new(async { Ok(view) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn read_view(&self, _token: &str, _params: ViewId) -> FutureResult<Option<View>, FlowyError> {
|
|
|
- FutureResult::new(async { Ok(None) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn delete_view(&self, _token: &str, _params: RepeatedViewId) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn update_view(&self, _token: &str, _params: UpdateViewParams) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn create_app(&self, _token: &str, params: CreateAppParams) -> FutureResult<App, FlowyError> {
|
|
|
- let time = timestamp();
|
|
|
- let app = App {
|
|
|
- id: uuid_string(),
|
|
|
- workspace_id: params.workspace_id,
|
|
|
- name: params.name,
|
|
|
- desc: params.desc,
|
|
|
- belongings: RepeatedView::default(),
|
|
|
- version: 0,
|
|
|
- modified_time: time,
|
|
|
- create_time: time,
|
|
|
- };
|
|
|
- FutureResult::new(async { Ok(app) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn read_app(&self, _token: &str, _params: AppId) -> FutureResult<Option<App>, FlowyError> {
|
|
|
- FutureResult::new(async { Ok(None) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn update_app(&self, _token: &str, _params: UpdateAppParams) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn delete_app(&self, _token: &str, _params: AppId) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn create_trash(&self, _token: &str, _params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn delete_trash(&self, _token: &str, _params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
|
|
- FutureResult::new(async { Ok(()) })
|
|
|
- }
|
|
|
-
|
|
|
- pub fn read_trash(&self, _token: &str) -> FutureResult<RepeatedTrash, FlowyError> {
|
|
|
- FutureResult::new(async {
|
|
|
- let repeated_trash = RepeatedTrash { items: vec![] };
|
|
|
- Ok(repeated_trash)
|
|
|
- })
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-lazy_static! {
|
|
|
- static ref MIDDLEWARE: Arc<CoreResponseMiddleware> = Arc::new(CoreResponseMiddleware::new());
|
|
|
-}
|
|
|
-
|
|
|
-pub struct CoreResponseMiddleware {
|
|
|
- invalid_token_sender: broadcast::Sender<String>,
|
|
|
-}
|
|
|
-
|
|
|
-impl CoreResponseMiddleware {
|
|
|
- fn new() -> Self {
|
|
|
- let (sender, _) = broadcast::channel(10);
|
|
|
- CoreResponseMiddleware {
|
|
|
- invalid_token_sender: sender,
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- pub fn invalid_token_subscribe(&self) -> broadcast::Receiver<String> { self.invalid_token_sender.subscribe() }
|
|
|
-}
|
|
|
-
|
|
|
-impl ResponseMiddleware for CoreResponseMiddleware {
|
|
|
- fn receive_response(&self, token: &Option<String>, response: &FlowyResponse) {
|
|
|
- if let Some(error) = &response.error {
|
|
|
- if error.is_unauthorized() {
|
|
|
- tracing::error!("user is unauthorized");
|
|
|
- match token {
|
|
|
- None => {},
|
|
|
- Some(token) => match self.invalid_token_sender.send(token.clone()) {
|
|
|
- Ok(_) => {},
|
|
|
- Err(e) => tracing::error!("{:?}", e),
|
|
|
- },
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
fn request_builder() -> HttpRequestBuilder { HttpRequestBuilder::new().middleware(MIDDLEWARE.clone()) }
|
|
|
|
|
|
pub async fn create_workspace_request(
|
|
@@ -474,3 +332,40 @@ pub async fn read_trash_request(token: &str, url: &str) -> Result<RepeatedTrash,
|
|
|
.await?;
|
|
|
Ok(repeated_trash)
|
|
|
}
|
|
|
+
|
|
|
+lazy_static! {
|
|
|
+ static ref MIDDLEWARE: Arc<CoreResponseMiddleware> = Arc::new(CoreResponseMiddleware::new());
|
|
|
+}
|
|
|
+
|
|
|
+pub struct CoreResponseMiddleware {
|
|
|
+ invalid_token_sender: broadcast::Sender<String>,
|
|
|
+}
|
|
|
+
|
|
|
+impl CoreResponseMiddleware {
|
|
|
+ fn new() -> Self {
|
|
|
+ let (sender, _) = broadcast::channel(10);
|
|
|
+ CoreResponseMiddleware {
|
|
|
+ invalid_token_sender: sender,
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #[allow(dead_code)]
|
|
|
+ fn invalid_token_subscribe(&self) -> broadcast::Receiver<String> { self.invalid_token_sender.subscribe() }
|
|
|
+}
|
|
|
+
|
|
|
+impl ResponseMiddleware for CoreResponseMiddleware {
|
|
|
+ fn receive_response(&self, token: &Option<String>, response: &FlowyResponse) {
|
|
|
+ if let Some(error) = &response.error {
|
|
|
+ if error.is_unauthorized() {
|
|
|
+ tracing::error!("user is unauthorized");
|
|
|
+ match token {
|
|
|
+ None => {},
|
|
|
+ Some(token) => match self.invalid_token_sender.send(token.clone()) {
|
|
|
+ Ok(_) => {},
|
|
|
+ Err(e) => tracing::error!("{:?}", e),
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|