diff --git a/frontend/app_flowy/lib/startup/deps_resolver.dart b/frontend/app_flowy/lib/startup/deps_resolver.dart index b378d6ee7b..56d28bbfe4 100644 --- a/frontend/app_flowy/lib/startup/deps_resolver.dart +++ b/frontend/app_flowy/lib/startup/deps_resolver.dart @@ -60,7 +60,7 @@ void _resolveHomeDeps(GetIt getIt) { getIt.registerFactoryParam( (user, _) => WelcomeBloc( - userService: UserService(), + userService: UserService(userId: user.id), userListener: getIt(param1: user), ), ); @@ -100,7 +100,6 @@ void _resolveFolderDeps(GetIt getIt) { getIt.registerFactoryParam( (user, _) => MenuUserBloc( user, - UserService(), getIt(param1: user), ), ); diff --git a/frontend/app_flowy/lib/user/application/user_service.dart b/frontend/app_flowy/lib/user/application/user_service.dart index 35486ac78b..627083d72f 100644 --- a/frontend/app_flowy/lib/user/application/user_service.dart +++ b/frontend/app_flowy/lib/user/application/user_service.dart @@ -1,15 +1,42 @@ import 'dart:async'; + import 'package:dartz/dartz.dart'; import 'package:flowy_sdk/dispatch/dispatch.dart'; -import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart'; +import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart'; class UserService { - Future> fetchUserProfile({required String userId}) { + final String userId; + UserService({ + required this.userId, + }); + Future> getUserProfile({required String userId}) { return UserEventGetUserProfile().send(); } + Future> updateUserProfile({ + String? name, + String? password, + String? email, + }) { + var payload = UpdateUserProfilePayload.create()..id = userId; + + if (name != null) { + payload.name = name; + } + + if (password != null) { + payload.password = password; + } + + if (email != null) { + payload.email = email; + } + + return UserEventUpdateUserProfile(payload).send(); + } + Future> deleteWorkspace({required String workspaceId}) { throw UnimplementedError(); } diff --git a/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart b/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart index 0da347c364..7016c2d74c 100644 --- a/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart @@ -11,11 +11,13 @@ import 'package:dartz/dartz.dart'; part 'menu_user_bloc.freezed.dart'; class MenuUserBloc extends Bloc { - final UserService userService; + final UserService _userService; final UserListener userListener; final UserProfile userProfile; - MenuUserBloc(this.userProfile, this.userService, this.userListener) : super(MenuUserState.initial(userProfile)) { + MenuUserBloc(this.userProfile, this.userListener) + : _userService = UserService(userId: userProfile.id), + super(MenuUserState.initial(userProfile)) { on((event, emit) async { await event.map( initial: (_) async { @@ -37,7 +39,7 @@ class MenuUserBloc extends Bloc { } Future _initUser() async { - final result = await userService.initUser(); + final result = await _userService.initUser(); result.fold((l) => null, (error) => Log.error(error)); } diff --git a/frontend/rust-lib/flowy-net/src/http_server/user.rs b/frontend/rust-lib/flowy-net/src/http_server/user.rs index 7a09ca5b95..875f696e21 100644 --- a/frontend/rust-lib/flowy-net/src/http_server/user.rs +++ b/frontend/rust-lib/flowy-net/src/http_server/user.rs @@ -2,7 +2,7 @@ use crate::{configuration::*, request::HttpRequestBuilder}; use flowy_error::FlowyError; use flowy_user::event_map::UserCloudService; use flowy_user_data_model::entities::{ - SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile, + SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile, }; use http_flowy::errors::ServerError; use lib_infra::future::FutureResult; @@ -42,7 +42,7 @@ impl UserCloudService for UserHttpCloudService { }) } - fn update_user(&self, token: &str, params: UpdateUserParams) -> FutureResult<(), FlowyError> { + fn update_user(&self, token: &str, params: UpdateUserProfileParams) -> FutureResult<(), FlowyError> { let token = token.to_owned(); let url = self.config.user_profile_url(); FutureResult::new(async move { @@ -101,7 +101,11 @@ pub async fn get_user_profile_request(token: &str, url: &str) -> Result Result<(), ServerError> { +pub async fn update_user_profile_request( + token: &str, + params: UpdateUserProfileParams, + url: &str, +) -> Result<(), ServerError> { let _ = request_builder() .patch(&url.to_owned()) .header(HEADER_TOKEN, token) diff --git a/frontend/rust-lib/flowy-net/src/local_server/server.rs b/frontend/rust-lib/flowy-net/src/local_server/server.rs index 905e2bccb4..79a9a1963b 100644 --- a/frontend/rust-lib/flowy-net/src/local_server/server.rs +++ b/frontend/rust-lib/flowy-net/src/local_server/server.rs @@ -264,7 +264,7 @@ use flowy_folder_data_model::revision::{AppRevision, TrashRevision, ViewRevision use flowy_text_block::BlockCloudService; use flowy_user::event_map::UserCloudService; use flowy_user_data_model::entities::{ - SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile, + SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile, }; use lib_infra::{future::FutureResult, util::timestamp}; @@ -401,7 +401,7 @@ impl UserCloudService for LocalServer { FutureResult::new(async { Ok(()) }) } - fn update_user(&self, _token: &str, _params: UpdateUserParams) -> FutureResult<(), FlowyError> { + fn update_user(&self, _token: &str, _params: UpdateUserProfileParams) -> FutureResult<(), FlowyError> { FutureResult::new(async { Ok(()) }) } diff --git a/frontend/rust-lib/flowy-user/src/event_map.rs b/frontend/rust-lib/flowy-user/src/event_map.rs index 76aa739616..8445d4a47c 100644 --- a/frontend/rust-lib/flowy-user/src/event_map.rs +++ b/frontend/rust-lib/flowy-user/src/event_map.rs @@ -1,6 +1,6 @@ use crate::{errors::FlowyError, handlers::*, services::UserSession}; use flowy_user_data_model::entities::{ - SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile, + SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile, }; use lib_dispatch::prelude::*; use lib_infra::future::FutureResult; @@ -15,7 +15,7 @@ pub fn create(user_session: Arc) -> Module { .event(UserEvent::InitUser, init_user_handler) .event(UserEvent::GetUserProfile, get_user_profile_handler) .event(UserEvent::SignOut, sign_out) - .event(UserEvent::UpdateUser, update_user_handler) + .event(UserEvent::UpdateUserProfile, update_user_profile_handler) .event(UserEvent::CheckUser, check_user_handler) .event(UserEvent::SetAppearanceSetting, set_appearance_setting) .event(UserEvent::GetAppearanceSetting, get_appearance_setting) @@ -25,7 +25,7 @@ pub trait UserCloudService: Send + Sync { fn sign_up(&self, params: SignUpParams) -> FutureResult; fn sign_in(&self, params: SignInParams) -> FutureResult; fn sign_out(&self, token: &str) -> FutureResult<(), FlowyError>; - fn update_user(&self, token: &str, params: UpdateUserParams) -> FutureResult<(), FlowyError>; + fn update_user(&self, token: &str, params: UpdateUserProfileParams) -> FutureResult<(), FlowyError>; fn get_user(&self, token: &str) -> FutureResult; fn ws_addr(&self) -> String; } @@ -48,8 +48,8 @@ pub enum UserEvent { #[event(passthrough)] SignOut = 3, - #[event(input = "UpdateUserPayload")] - UpdateUser = 4, + #[event(input = "UpdateUserProfilePayload")] + UpdateUserProfile = 4, #[event(output = "UserProfile")] GetUserProfile = 5, diff --git a/frontend/rust-lib/flowy-user/src/handlers/user_handler.rs b/frontend/rust-lib/flowy-user/src/handlers/user_handler.rs index a13f7187f7..0db37ea984 100644 --- a/frontend/rust-lib/flowy-user/src/handlers/user_handler.rs +++ b/frontend/rust-lib/flowy-user/src/handlers/user_handler.rs @@ -1,7 +1,7 @@ use crate::{errors::FlowyError, services::UserSession}; use flowy_database::kv::KV; use flowy_user_data_model::entities::{ - AppearanceSettings, UpdateUserParams, UpdateUserPayload, UserProfile, APPEARANCE_DEFAULT_THEME, + AppearanceSettings, UpdateUserProfileParams, UpdateUserProfilePayload, UserProfile, APPEARANCE_DEFAULT_THEME, }; use lib_dispatch::prelude::*; use std::{convert::TryInto, sync::Arc}; @@ -30,13 +30,13 @@ pub async fn sign_out(session: AppData>) -> Result<(), FlowyErr Ok(()) } -#[tracing::instrument(level = "debug", name = "update_user", skip(data, session))] -pub async fn update_user_handler( - data: Data, +#[tracing::instrument(level = "debug", skip(data, session))] +pub async fn update_user_profile_handler( + data: Data, session: AppData>, ) -> Result<(), FlowyError> { - let params: UpdateUserParams = data.into_inner().try_into()?; - session.update_user(params).await?; + let params: UpdateUserProfileParams = data.into_inner().try_into()?; + session.update_user_profile(params).await?; Ok(()) } diff --git a/frontend/rust-lib/flowy-user/src/services/database.rs b/frontend/rust-lib/flowy-user/src/services/database.rs index f46d86ad5e..6a76b0a021 100644 --- a/frontend/rust-lib/flowy-user/src/services/database.rs +++ b/frontend/rust-lib/flowy-user/src/services/database.rs @@ -1,7 +1,7 @@ use flowy_database::ConnectionPool; use flowy_database::{schema::user_table, DBConnection, Database}; use flowy_error::{ErrorCode, FlowyError}; -use flowy_user_data_model::entities::{SignInResponse, SignUpResponse, UpdateUserParams, UserProfile}; +use flowy_user_data_model::entities::{SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfile}; use lazy_static::lazy_static; use parking_lot::RwLock; use std::{collections::HashMap, sync::Arc, time::Duration}; @@ -134,7 +134,7 @@ pub struct UserTableChangeset { } impl UserTableChangeset { - pub fn new(params: UpdateUserParams) -> Self { + pub fn new(params: UpdateUserProfileParams) -> Self { UserTableChangeset { id: params.id, workspace: None, diff --git a/frontend/rust-lib/flowy-user/src/services/user_session.rs b/frontend/rust-lib/flowy-user/src/services/user_session.rs index 05eea59f87..97b3d3f5c5 100644 --- a/frontend/rust-lib/flowy-user/src/services/user_session.rs +++ b/frontend/rust-lib/flowy-user/src/services/user_session.rs @@ -15,7 +15,7 @@ use flowy_database::{ DBConnection, ExpressionMethods, UserDatabaseConnection, }; use flowy_user_data_model::entities::{ - SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile, + SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile, }; use parking_lot::RwLock; use serde::{Deserialize, Serialize}; @@ -126,7 +126,7 @@ impl UserSession { } #[tracing::instrument(level = "debug", skip(self))] - pub async fn update_user(&self, params: UpdateUserParams) -> Result<(), FlowyError> { + pub async fn update_user_profile(&self, params: UpdateUserProfileParams) -> Result<(), FlowyError> { let session = self.get_session()?; let changeset = UserTableChangeset::new(params.clone()); diesel_update_table!(user_table, changeset, &*self.db_connection()?); @@ -199,7 +199,7 @@ impl UserSession { Ok(()) } - async fn update_user_on_server(&self, token: &str, params: UpdateUserParams) -> Result<(), FlowyError> { + async fn update_user_on_server(&self, token: &str, params: UpdateUserProfileParams) -> Result<(), FlowyError> { let server = self.cloud_service.clone(); let token = token.to_owned(); let _ = tokio::spawn(async move { @@ -275,7 +275,7 @@ impl UserSession { pub async fn update_user( _cloud_service: Arc, pool: Arc, - params: UpdateUserParams, + params: UpdateUserProfileParams, ) -> Result<(), FlowyError> { let changeset = UserTableChangeset::new(params); let conn = pool.get()?; diff --git a/frontend/rust-lib/flowy-user/tests/event/user_profile_test.rs b/frontend/rust-lib/flowy-user/tests/event/user_profile_test.rs index 9485e1f730..49e959dfef 100644 --- a/frontend/rust-lib/flowy-user/tests/event/user_profile_test.rs +++ b/frontend/rust-lib/flowy-user/tests/event/user_profile_test.rs @@ -1,7 +1,7 @@ use crate::helper::*; use flowy_test::{event_builder::UserModuleEventBuilder, FlowySDKTest}; use flowy_user::{errors::ErrorCode, event_map::UserEvent::*}; -use flowy_user_data_model::entities::{UpdateUserPayload, UserProfile}; +use flowy_user_data_model::entities::{UpdateUserProfilePayload, UserProfile}; use nanoid::nanoid; // use serial_test::*; @@ -33,9 +33,9 @@ async fn user_update_with_name() { let sdk = FlowySDKTest::default(); let user = sdk.init_user().await; let new_name = "hello_world".to_owned(); - let request = UpdateUserPayload::new(&user.id).name(&new_name); + let request = UpdateUserProfilePayload::new(&user.id).name(&new_name); let _ = UserModuleEventBuilder::new(sdk.clone()) - .event(UpdateUser) + .event(UpdateUserProfile) .payload(request) .sync_send(); @@ -53,9 +53,9 @@ async fn user_update_with_email() { let sdk = FlowySDKTest::default(); let user = sdk.init_user().await; let new_email = format!("{}@gmail.com", nanoid!(6)); - let request = UpdateUserPayload::new(&user.id).email(&new_email); + let request = UpdateUserProfilePayload::new(&user.id).email(&new_email); let _ = UserModuleEventBuilder::new(sdk.clone()) - .event(UpdateUser) + .event(UpdateUserProfile) .payload(request) .sync_send(); let user_profile = UserModuleEventBuilder::new(sdk.clone()) @@ -72,10 +72,10 @@ async fn user_update_with_password() { let sdk = FlowySDKTest::default(); let user = sdk.init_user().await; let new_password = "H123world!".to_owned(); - let request = UpdateUserPayload::new(&user.id).password(&new_password); + let request = UpdateUserProfilePayload::new(&user.id).password(&new_password); let _ = UserModuleEventBuilder::new(sdk.clone()) - .event(UpdateUser) + .event(UpdateUserProfile) .payload(request) .sync_send() .assert_success(); @@ -86,10 +86,10 @@ async fn user_update_with_invalid_email() { let test = FlowySDKTest::default(); let user = test.init_user().await; for email in invalid_email_test_case() { - let request = UpdateUserPayload::new(&user.id).email(&email); + let request = UpdateUserProfilePayload::new(&user.id).email(&email); assert_eq!( UserModuleEventBuilder::new(test.clone()) - .event(UpdateUser) + .event(UpdateUserProfile) .payload(request) .sync_send() .error() @@ -104,10 +104,10 @@ async fn user_update_with_invalid_password() { let test = FlowySDKTest::default(); let user = test.init_user().await; for password in invalid_password_test_case() { - let request = UpdateUserPayload::new(&user.id).password(&password); + let request = UpdateUserProfilePayload::new(&user.id).password(&password); UserModuleEventBuilder::new(test.clone()) - .event(UpdateUser) + .event(UpdateUserProfile) .payload(request) .sync_send() .assert_error(); @@ -118,9 +118,9 @@ async fn user_update_with_invalid_password() { async fn user_update_with_invalid_name() { let test = FlowySDKTest::default(); let user = test.init_user().await; - let request = UpdateUserPayload::new(&user.id).name(""); + let request = UpdateUserProfilePayload::new(&user.id).name(""); UserModuleEventBuilder::new(test.clone()) - .event(UpdateUser) + .event(UpdateUserProfile) .payload(request) .sync_send() .assert_error(); diff --git a/shared-lib/flowy-user-data-model/src/entities/user_profile.rs b/shared-lib/flowy-user-data-model/src/entities/user_profile.rs index 664945f902..b5519e6812 100644 --- a/shared-lib/flowy-user-data-model/src/entities/user_profile.rs +++ b/shared-lib/flowy-user-data-model/src/entities/user_profile.rs @@ -28,7 +28,7 @@ pub struct UserProfile { } #[derive(ProtoBuf, Default)] -pub struct UpdateUserPayload { +pub struct UpdateUserProfilePayload { #[pb(index = 1)] pub id: String, @@ -42,7 +42,7 @@ pub struct UpdateUserPayload { pub password: Option, } -impl UpdateUserPayload { +impl UpdateUserProfilePayload { pub fn new(id: &str) -> Self { Self { id: id.to_owned(), @@ -67,7 +67,7 @@ impl UpdateUserPayload { } #[derive(ProtoBuf, Default, Clone, Debug)] -pub struct UpdateUserParams { +pub struct UpdateUserProfileParams { #[pb(index = 1)] pub id: String, @@ -81,7 +81,7 @@ pub struct UpdateUserParams { pub password: Option, } -impl UpdateUserParams { +impl UpdateUserProfileParams { pub fn new(user_id: &str) -> Self { Self { id: user_id.to_owned(), @@ -105,10 +105,10 @@ impl UpdateUserParams { } } -impl TryInto for UpdateUserPayload { +impl TryInto for UpdateUserProfilePayload { type Error = ErrorCode; - fn try_into(self) -> Result { + fn try_into(self) -> Result { let id = UserId::parse(self.id)?.0; let name = match self.name { @@ -126,7 +126,7 @@ impl TryInto for UpdateUserPayload { Some(password) => Some(UserPassword::parse(password)?.0), }; - Ok(UpdateUserParams { + Ok(UpdateUserProfileParams { id, name, email,