feat: cloud storage test (#2663)

* chore: show default user name

* chore: update

* feat: change collab storage type after auth type changed

* chore: reload folder

* chore: initial the group controller if need

* chore: update patch

* chore: update patch ref
This commit is contained in:
Nathan.fooo
2023-05-31 17:42:14 +08:00
committed by GitHub
parent 09d61c79c9
commit 012b6c0066
27 changed files with 398 additions and 285 deletions

View File

@ -7,7 +7,7 @@ use flowy_error::FlowyResult;
use lib_dispatch::prelude::*;
use lib_infra::box_any::BoxAny;
use lib_infra::future::{Fut, FutureResult};
use lib_infra::future::{to_fut, Fut, FutureResult};
use crate::entities::{SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfile};
use crate::event_handler::*;
@ -31,7 +31,25 @@ pub fn init(user_session: Arc<UserSession>) -> AFPlugin {
.event(UserEvent::ThirdPartyAuth, third_party_auth_handler)
}
pub(crate) struct DefaultUserStatusCallback;
impl UserStatusCallback for DefaultUserStatusCallback {
fn auth_type_did_changed(&self, _auth_type: AuthType) {}
fn did_sign_in(&self, _user_id: i64, _workspace_id: &str) -> Fut<FlowyResult<()>> {
to_fut(async { Ok(()) })
}
fn did_sign_up(&self, _user_profile: &UserProfile) -> Fut<FlowyResult<()>> {
to_fut(async { Ok(()) })
}
fn did_expired(&self, _token: &str, _user_id: i64) -> Fut<FlowyResult<()>> {
to_fut(async { Ok(()) })
}
}
pub trait UserStatusCallback: Send + Sync + 'static {
fn auth_type_did_changed(&self, auth_type: AuthType);
fn did_sign_in(&self, user_id: i64, workspace_id: &str) -> Fut<FlowyResult<()>>;
fn did_sign_up(&self, user_profile: &UserProfile) -> Fut<FlowyResult<()>>;
fn did_expired(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>>;
@ -41,7 +59,7 @@ pub trait UserStatusCallback: Send + Sync + 'static {
/// The provider can be supabase, firebase, aws, or any other cloud service.
pub trait UserCloudServiceProvider: Send + Sync + 'static {
fn set_auth_type(&self, auth_type: AuthType);
fn get_auth_service(&self, auth_type: &AuthType) -> Result<Arc<dyn UserAuthService>, FlowyError>;
fn get_auth_service(&self) -> Result<Arc<dyn UserAuthService>, FlowyError>;
}
impl<T> UserCloudServiceProvider for Arc<T>
@ -52,8 +70,8 @@ where
(**self).set_auth_type(auth_type)
}
fn get_auth_service(&self, auth_type: &AuthType) -> Result<Arc<dyn UserAuthService>, FlowyError> {
(**self).get_auth_service(auth_type)
fn get_auth_service(&self) -> Result<Arc<dyn UserAuthService>, FlowyError> {
(**self).get_auth_service()
}
}

View File

@ -19,7 +19,7 @@ use crate::entities::{
AuthTypePB, SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfile,
};
use crate::entities::{UserProfilePB, UserSettingPB};
use crate::event_map::{UserCloudServiceProvider, UserStatusCallback};
use crate::event_map::{DefaultUserStatusCallback, UserCloudServiceProvider, UserStatusCallback};
use crate::{
errors::FlowyError,
event_map::UserAuthService,
@ -50,7 +50,7 @@ pub struct UserSession {
database: UserDB,
session_config: UserSessionConfig,
cloud_services: Arc<dyn UserCloudServiceProvider>,
user_status_callback: RwLock<Option<Arc<dyn UserStatusCallback>>>,
user_status_callback: RwLock<Arc<dyn UserStatusCallback>>,
}
impl UserSession {
@ -59,7 +59,8 @@ impl UserSession {
cloud_services: Arc<dyn UserCloudServiceProvider>,
) -> Self {
let db = UserDB::new(&session_config.root_dir);
let user_status_callback = RwLock::new(None);
let user_status_callback: RwLock<Arc<dyn UserStatusCallback>> =
RwLock::new(Arc::new(DefaultUserStatusCallback));
Self {
database: db,
session_config,
@ -74,7 +75,7 @@ impl UserSession {
.did_sign_in(session.user_id, &session.workspace_id)
.await;
}
*self.user_status_callback.write().await = Some(Arc::new(user_status_callback));
*self.user_status_callback.write().await = Arc::new(user_status_callback);
}
pub fn db_connection(&self) -> Result<DBConnection, FlowyError> {
@ -104,10 +105,16 @@ impl UserSession {
auth_type: &AuthType,
params: BoxAny,
) -> Result<UserProfile, FlowyError> {
self
.user_status_callback
.read()
.await
.auth_type_did_changed(auth_type.clone());
self.cloud_services.set_auth_type(auth_type.clone());
let resp = self
.cloud_services
.get_auth_service(auth_type)?
.get_auth_service()?
.sign_in(params)
.await?;
@ -118,8 +125,6 @@ impl UserSession {
.user_status_callback
.read()
.await
.as_ref()
.unwrap()
.did_sign_in(user_profile.id, &user_profile.workspace_id)
.await;
send_sign_in_notification()
@ -135,10 +140,16 @@ impl UserSession {
auth_type: &AuthType,
params: BoxAny,
) -> Result<UserProfile, FlowyError> {
self
.user_status_callback
.read()
.await
.auth_type_did_changed(auth_type.clone());
self.cloud_services.set_auth_type(auth_type.clone());
let resp = self
.cloud_services
.get_auth_service(auth_type)?
.get_auth_service()?
.sign_up(params)
.await?;
@ -150,8 +161,6 @@ impl UserSession {
.user_status_callback
.read()
.await
.as_ref()
.unwrap()
.did_sign_up(&user_profile)
.await;
Ok(user_profile)
@ -166,7 +175,7 @@ impl UserSession {
self.database.close_user_db(session.user_id)?;
self.set_session(None)?;
let server = self.cloud_services.get_auth_service(auth_type)?;
let server = self.cloud_services.get_auth_service()?;
let token = session.token;
let _ = tokio::spawn(async move {
match server.sign_out(token).await {
@ -256,12 +265,12 @@ impl UserSession {
impl UserSession {
async fn update_user(
&self,
auth_type: &AuthType,
_auth_type: &AuthType,
uid: i64,
token: &Option<String>,
params: UpdateUserProfileParams,
) -> Result<(), FlowyError> {
let server = self.cloud_services.get_auth_service(auth_type)?;
let server = self.cloud_services.get_auth_service()?;
let token = token.to_owned();
let _ = tokio::spawn(async move {
match server.update_user(uid, &token, params).await {