From fab442997c959c506527c25188e381f8dcd17bc8 Mon Sep 17 00:00:00 2001 From: appflowy Date: Wed, 13 Oct 2021 11:10:29 +0800 Subject: [PATCH] temp --- .../src/entities/view/view_create.rs | 13 ++++- .../src/handlers/trash_handler.rs | 6 +-- rust-lib/flowy-workspace/src/module.rs | 9 ++-- .../flowy-workspace/src/notify/observable.rs | 12 +++-- .../src/services/app_controller.rs | 1 + rust-lib/flowy-workspace/src/services/mod.rs | 4 +- .../flowy-workspace/src/services/trash_can.rs | 19 +++++++ .../src/services/trash_controller.rs | 8 --- .../src/services/view_controller.rs | 50 +++++++++++++------ .../src/services/workspace_controller.rs | 8 +-- .../src/sql_tables/view/view_sql.rs | 2 +- 11 files changed, 87 insertions(+), 45 deletions(-) create mode 100644 rust-lib/flowy-workspace/src/services/trash_can.rs delete mode 100644 rust-lib/flowy-workspace/src/services/trash_controller.rs diff --git a/rust-lib/flowy-workspace/src/entities/view/view_create.rs b/rust-lib/flowy-workspace/src/entities/view/view_create.rs index 7da5de27c4..f31e004096 100644 --- a/rust-lib/flowy-workspace/src/entities/view/view_create.rs +++ b/rust-lib/flowy-workspace/src/entities/view/view_create.rs @@ -1,5 +1,5 @@ use crate::{ - entities::{app::parser::AppId, view::parser::*}, + entities::{app::parser::AppId, trash::Trash, view::parser::*}, errors::WorkspaceError, impl_def_and_def_mut, }; @@ -153,3 +153,14 @@ pub struct RepeatedView { } impl_def_and_def_mut!(RepeatedView, View); + +impl std::convert::Into(Trash) for View { + fn into(self) -> (Trash) { + Trash { + id: self.id, + name: self.name, + modified_time: self.modified_time, + create_time: self.create_time, + } + } +} diff --git a/rust-lib/flowy-workspace/src/handlers/trash_handler.rs b/rust-lib/flowy-workspace/src/handlers/trash_handler.rs index d6dea97e10..f72f8c67d8 100644 --- a/rust-lib/flowy-workspace/src/handlers/trash_handler.rs +++ b/rust-lib/flowy-workspace/src/handlers/trash_handler.rs @@ -1,15 +1,13 @@ use crate::{ entities::{trash::RepeatedTrash, view::RepeatedView}, errors::WorkspaceError, - services::TrashController, + services::TrashCan, }; use flowy_dispatch::prelude::{data_result, Data, DataResult, Unit}; use std::{convert::TryInto, sync::Arc}; #[tracing::instrument(skip(data, controller), err)] -pub(crate) async fn read_trash_handler( - controller: Unit>, -) -> DataResult { +pub(crate) async fn read_trash_handler(controller: Unit>) -> DataResult { let repeated_trash = controller.read_trash()?; data_result(repeated_trash) } diff --git a/rust-lib/flowy-workspace/src/module.rs b/rust-lib/flowy-workspace/src/module.rs index 5497dc3521..8090ef0176 100644 --- a/rust-lib/flowy-workspace/src/module.rs +++ b/rust-lib/flowy-workspace/src/module.rs @@ -5,7 +5,7 @@ use crate::{ services::{server::construct_workspace_server, AppController, ViewController, WorkspaceController}, }; -use crate::services::TrashController; +use crate::services::TrashCan; use flowy_database::DBConnection; use flowy_dispatch::prelude::*; use flowy_document::module::FlowyDocument; @@ -38,12 +38,13 @@ pub fn mk_workspace( ) -> Arc { let server = construct_workspace_server(server_config); - let trash_controller = Arc::new(TrashController::new()); + let trash_can = Arc::new(TrashCan::new(database.clone())); let view_controller = Arc::new(ViewController::new( user.clone(), database.clone(), server.clone(), + trash_can.clone(), flowy_document, )); @@ -54,7 +55,7 @@ pub fn mk_workspace( database.clone(), app_controller.clone(), view_controller.clone(), - trash_controller.clone(), + trash_can.clone(), server.clone(), )); workspace_controller @@ -66,7 +67,7 @@ pub fn create(workspace: Arc) -> Module { .data(workspace.clone()) .data(workspace.app_controller.clone()) .data(workspace.view_controller.clone()) - .data(workspace.trash_controller.clone()) + .data(workspace.trash_can.clone()) .event(WorkspaceEvent::InitWorkspace, init_workspace_handler); module = module diff --git a/rust-lib/flowy-workspace/src/notify/observable.rs b/rust-lib/flowy-workspace/src/notify/observable.rs index bd21b471c9..ac5cce3aff 100644 --- a/rust-lib/flowy-workspace/src/notify/observable.rs +++ b/rust-lib/flowy-workspace/src/notify/observable.rs @@ -3,7 +3,7 @@ use flowy_derive::ProtoBuf_Enum; const OBSERVABLE_CATEGORY: &'static str = "Workspace"; #[derive(ProtoBuf_Enum, Debug)] -pub(crate) enum WorkspaceObservable { +pub(crate) enum Notification { Unknown = 0, UserCreateWorkspace = 10, UserDeleteWorkspace = 11, @@ -18,14 +18,16 @@ pub(crate) enum WorkspaceObservable { UserUnauthorized = 100, } -impl std::default::Default for WorkspaceObservable { - fn default() -> Self { WorkspaceObservable::Unknown } +impl std::default::Default for Notification { + fn default() -> Self { Notification::Unknown } } -impl std::convert::Into for WorkspaceObservable { +impl std::convert::Into for Notification { fn into(self) -> i32 { self as i32 } } -pub(crate) fn dart_notify(id: &str, ty: WorkspaceObservable) -> DartNotifyBuilder { +pub(crate) fn dart_notify(id: &str, ty: Notification) -> DartNotifyBuilder { + tracing::Span::current().record("dart_notify", &format!("{:?} => notify_id: {}", ty, id).as_str()); + DartNotifyBuilder::new(id, ty, OBSERVABLE_CATEGORY) } diff --git a/rust-lib/flowy-workspace/src/services/app_controller.rs b/rust-lib/flowy-workspace/src/services/app_controller.rs index 454f815dbc..46da40e9d6 100644 --- a/rust-lib/flowy-workspace/src/services/app_controller.rs +++ b/rust-lib/flowy-workspace/src/services/app_controller.rs @@ -59,6 +59,7 @@ impl AppController { Ok(app_table.into()) } + #[tracing::instrument(level = "debug", skip(self), fields(dart_notify) err)] pub(crate) async fn delete_app(&self, app_id: &str) -> Result<(), WorkspaceError> { let conn = &*self.database.db_connection()?; conn.immediate_transaction::<_, WorkspaceError, _>(|| { diff --git a/rust-lib/flowy-workspace/src/services/mod.rs b/rust-lib/flowy-workspace/src/services/mod.rs index af5c6b625e..0ea771e46a 100644 --- a/rust-lib/flowy-workspace/src/services/mod.rs +++ b/rust-lib/flowy-workspace/src/services/mod.rs @@ -1,5 +1,5 @@ pub(crate) use app_controller::*; -pub(crate) use trash_controller::*; +pub(crate) use trash_can::*; pub(crate) use view_controller::*; pub use workspace_controller::*; @@ -7,6 +7,6 @@ mod app_controller; mod database; mod helper; pub mod server; -mod trash_controller; +mod trash_can; mod view_controller; mod workspace_controller; diff --git a/rust-lib/flowy-workspace/src/services/trash_can.rs b/rust-lib/flowy-workspace/src/services/trash_can.rs new file mode 100644 index 0000000000..301791b496 --- /dev/null +++ b/rust-lib/flowy-workspace/src/services/trash_can.rs @@ -0,0 +1,19 @@ +use crate::{ + entities::trash::{RepeatedTrash, Trash}, + errors::WorkspaceError, + module::WorkspaceDatabase, +}; +use std::sync::Arc; + +pub struct TrashCan { + database: Arc, +} + +impl TrashCan { + pub fn new(database: Arc) -> Self { Self { database } } + pub fn read_trash(&self) -> Result { Ok(RepeatedTrash { items: vec![] }) } + + pub fn add>(&self, trash: T) { let trash = trash.into(); } + + pub fn remove(&self, trash_id: &str) {} +} diff --git a/rust-lib/flowy-workspace/src/services/trash_controller.rs b/rust-lib/flowy-workspace/src/services/trash_controller.rs deleted file mode 100644 index b1a4b2ca1a..0000000000 --- a/rust-lib/flowy-workspace/src/services/trash_controller.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::{entities::trash::RepeatedTrash, errors::WorkspaceError}; - -pub struct TrashController {} - -impl TrashController { - pub fn new() -> Self { Self {} } - pub fn read_trash(&self) -> Result { Ok(RepeatedTrash { items: vec![] }) } -} diff --git a/rust-lib/flowy-workspace/src/services/view_controller.rs b/rust-lib/flowy-workspace/src/services/view_controller.rs index 8540486144..6ccb32a103 100644 --- a/rust-lib/flowy-workspace/src/services/view_controller.rs +++ b/rust-lib/flowy-workspace/src/services/view_controller.rs @@ -8,10 +8,14 @@ use crate::{ }; use crate::{ - entities::view::{DeleteViewParams, QueryViewParams, RepeatedView}, + entities::{ + trash::Trash, + view::{DeleteViewParams, QueryViewParams, RepeatedView}, + }, errors::internal_error, module::WorkspaceUser, notify::WorkspaceObservable, + services::TrashCan, }; use flowy_database::SqliteConnection; use flowy_document::{ @@ -25,6 +29,7 @@ pub(crate) struct ViewController { sql: Arc, server: Server, database: Arc, + trash_can: Arc, document: Arc, } @@ -33,6 +38,7 @@ impl ViewController { user: Arc, database: Arc, server: Server, + trash_can: Arc, document: Arc, ) -> Self { let sql = Arc::new(ViewTableSql {}); @@ -41,6 +47,7 @@ impl ViewController { sql, server, database, + trash_can, document, } } @@ -50,6 +57,7 @@ impl ViewController { Ok(()) } + #[tracing::instrument(level = "debug", skip(self, params), fields(dart_notify) err)] pub(crate) async fn create_view(&self, params: CreateViewParams) -> Result { let view = self.create_view_on_server(params.clone()).await?; let conn = &*self.database.db_connection()?; @@ -58,7 +66,7 @@ impl ViewController { let _ = self.save_view(view.clone(), conn)?; self.document.create(CreateDocParams::new(&view.id, params.data))?; - let repeated_view = self.sql.read_views_belong_to(&view.belong_to_id, Some(false), conn)?; + let repeated_view = self.sql.read_views(&view.belong_to_id, Some(false), conn)?; dart_notify(&view.belong_to_id, WorkspaceObservable::AppCreateView) .payload(repeated_view) .send(); @@ -88,6 +96,7 @@ impl ViewController { Ok(edit_context.delta().await.map_err(internal_error)?) } + #[tracing::instrument(level = "debug", skip(self, params), fields(dart_notify) err)] pub(crate) async fn delete_view(&self, params: DeleteViewParams) -> Result<(), WorkspaceError> { let conn = &*self.database.db_connection()?; let _ = self.delete_view_on_server(¶ms.view_id); @@ -96,9 +105,7 @@ impl ViewController { let view_table = self.sql.delete_view(¶ms.view_id, conn)?; let _ = self.document.delete(params.into())?; - let repeated_view = self - .sql - .read_views_belong_to(&view_table.belong_to_id, Some(false), conn)?; + let repeated_view = self.sql.read_views(&view_table.belong_to_id, Some(false), conn)?; dart_notify(&view_table.belong_to_id, WorkspaceObservable::AppDeleteView) .payload(repeated_view) @@ -114,7 +121,7 @@ impl ViewController { pub(crate) async fn read_views_belong_to(&self, belong_to_id: &str) -> Result { // TODO: read from server let conn = self.database.db_connection()?; - let repeated_view = self.sql.read_views_belong_to(belong_to_id, Some(false), &*conn)?; + let repeated_view = self.sql.read_views(belong_to_id, Some(false), &*conn)?; Ok(repeated_view) } @@ -127,22 +134,33 @@ impl ViewController { let updated_view = conn.immediate_transaction::<_, WorkspaceError, _>(|| { let _ = self.sql.update_view(changeset, conn)?; let view: View = self.sql.read_view(&view_id, None, conn)?.into(); + let _ = self.add_to_trash_if_need(view.clone(), params.is_trash.clone())?; + + match params.is_trash { + None => { + dart_notify(&view_id, WorkspaceObservable::ViewUpdated) + .payload(view.clone()) + .send(); + }, + Some(is_trash) => { + let repeated_view = self.sql.read_views(&view.belong_to_id, Some(false), conn)?; + dart_notify(&view.belong_to_id, WorkspaceObservable::AppDeleteView) + .payload(repeated_view) + .send(); + + match is_trash { + true => self.trash_can.add(view.clone()), + false => self.trash_can.remove(&view.id), + } + }, + } if params.is_trash.is_some() { - let repeated_view = self.sql.read_views_belong_to(&view.belong_to_id, Some(false), conn)?; - tracing::Span::current().record( - "dart_notify", - &format!("{:?}: {}", WorkspaceObservable::AppDeleteView, &view.belong_to_id).as_str(), - ); + let repeated_view = self.sql.read_views(&view.belong_to_id, Some(false), conn)?; dart_notify(&view.belong_to_id, WorkspaceObservable::AppDeleteView) .payload(repeated_view) .send(); } else { - tracing::Span::current().record( - "dart_notify", - &format!("{:?}: {}", WorkspaceObservable::ViewUpdated, &view_id).as_str(), - ); - dart_notify(&view_id, WorkspaceObservable::ViewUpdated) .payload(view.clone()) .send(); diff --git a/rust-lib/flowy-workspace/src/services/workspace_controller.rs b/rust-lib/flowy-workspace/src/services/workspace_controller.rs index 497b706948..4db1a6993f 100644 --- a/rust-lib/flowy-workspace/src/services/workspace_controller.rs +++ b/rust-lib/flowy-workspace/src/services/workspace_controller.rs @@ -6,7 +6,7 @@ use crate::{ errors::*, module::{WorkspaceDatabase, WorkspaceUser}, notify::*, - services::{helper::spawn, server::Server, AppController, TrashController, ViewController}, + services::{helper::spawn, server::Server, AppController, TrashCan, ViewController}, sql_tables::workspace::{WorkspaceTable, WorkspaceTableChangeset, WorkspaceTableSql}, }; use flowy_database::SqliteConnection; @@ -19,7 +19,7 @@ pub struct WorkspaceController { pub(crate) view_controller: Arc, pub(crate) database: Arc, pub(crate) app_controller: Arc, - pub(crate) trash_controller: Arc, + pub(crate) trash_can: Arc, server: Server, } @@ -29,7 +29,7 @@ impl WorkspaceController { database: Arc, app_controller: Arc, view_controller: Arc, - trash_controller: Arc, + trash_can: Arc, server: Server, ) -> Self { let workspace_sql = Arc::new(WorkspaceTableSql {}); @@ -39,7 +39,7 @@ impl WorkspaceController { database, app_controller, view_controller, - trash_controller, + trash_can, server, } } diff --git a/rust-lib/flowy-workspace/src/sql_tables/view/view_sql.rs b/rust-lib/flowy-workspace/src/sql_tables/view/view_sql.rs index 41b7b14d57..91cdfa28c2 100644 --- a/rust-lib/flowy-workspace/src/sql_tables/view/view_sql.rs +++ b/rust-lib/flowy-workspace/src/sql_tables/view/view_sql.rs @@ -39,7 +39,7 @@ impl ViewTableSql { } // belong_to_id will be the app_id or view_id. - pub(crate) fn read_views_belong_to( + pub(crate) fn read_views( &self, belong_to_id: &str, is_trash: Option,