chore: update user name with UserService

This commit is contained in:
appflowy 2022-07-03 16:52:06 +08:00
parent 6fc7f63a8b
commit ce73da8212
11 changed files with 81 additions and 49 deletions

View File

@ -60,7 +60,7 @@ void _resolveHomeDeps(GetIt getIt) {
getIt.registerFactoryParam<WelcomeBloc, UserProfile, void>(
(user, _) => WelcomeBloc(
userService: UserService(),
userService: UserService(userId: user.id),
userListener: getIt<UserListener>(param1: user),
),
);
@ -100,7 +100,6 @@ void _resolveFolderDeps(GetIt getIt) {
getIt.registerFactoryParam<MenuUserBloc, UserProfile, void>(
(user, _) => MenuUserBloc(
user,
UserService(),
getIt<UserListener>(param1: user),
),
);

View File

@ -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<Either<UserProfile, FlowyError>> fetchUserProfile({required String userId}) {
final String userId;
UserService({
required this.userId,
});
Future<Either<UserProfile, FlowyError>> getUserProfile({required String userId}) {
return UserEventGetUserProfile().send();
}
Future<Either<Unit, FlowyError>> 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<Either<Unit, FlowyError>> deleteWorkspace({required String workspaceId}) {
throw UnimplementedError();
}

View File

@ -11,11 +11,13 @@ import 'package:dartz/dartz.dart';
part 'menu_user_bloc.freezed.dart';
class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
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<MenuUserEvent>((event, emit) async {
await event.map(
initial: (_) async {
@ -37,7 +39,7 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
}
Future<void> _initUser() async {
final result = await userService.initUser();
final result = await _userService.initUser();
result.fold((l) => null, (error) => Log.error(error));
}

View File

@ -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<UserProf
Ok(user_profile)
}
pub async fn update_user_profile_request(token: &str, params: UpdateUserParams, url: &str) -> 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)

View File

@ -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(()) })
}

View File

@ -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<UserSession>) -> 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<SignUpResponse, FlowyError>;
fn sign_in(&self, params: SignInParams) -> FutureResult<SignInResponse, FlowyError>;
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<UserProfile, FlowyError>;
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,

View File

@ -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<Arc<UserSession>>) -> Result<(), FlowyErr
Ok(())
}
#[tracing::instrument(level = "debug", name = "update_user", skip(data, session))]
pub async fn update_user_handler(
data: Data<UpdateUserPayload>,
#[tracing::instrument(level = "debug", skip(data, session))]
pub async fn update_user_profile_handler(
data: Data<UpdateUserProfilePayload>,
session: AppData<Arc<UserSession>>,
) -> 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(())
}

View File

@ -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,

View File

@ -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<dyn UserCloudService>,
pool: Arc<ConnectionPool>,
params: UpdateUserParams,
params: UpdateUserProfileParams,
) -> Result<(), FlowyError> {
let changeset = UserTableChangeset::new(params);
let conn = pool.get()?;

View File

@ -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();

View File

@ -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<String>,
}
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<String>,
}
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<UpdateUserParams> for UpdateUserPayload {
impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayload {
type Error = ErrorCode;
fn try_into(self) -> Result<UpdateUserParams, Self::Error> {
fn try_into(self) -> Result<UpdateUserProfileParams, Self::Error> {
let id = UserId::parse(self.id)?.0;
let name = match self.name {
@ -126,7 +126,7 @@ impl TryInto<UpdateUserParams> for UpdateUserPayload {
Some(password) => Some(UserPassword::parse(password)?.0),
};
Ok(UpdateUserParams {
Ok(UpdateUserProfileParams {
id,
name,
email,