AppFlowy/frontend/rust-lib/flowy-user/src/services/user_session.rs

433 lines
12 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use appflowy_integrate::RocksCollabDB;
use serde::{Deserialize, Serialize};
use serde_repr::*;
use tokio::sync::RwLock;
use flowy_error::internal_error;
use flowy_sqlite::ConnectionPool;
use flowy_sqlite::{
kv::KV,
query_dsl::*,
schema::{user_table, user_table::dsl},
DBConnection, ExpressionMethods, UserDatabaseConnection,
};
use lib_infra::box_any::BoxAny;
use crate::entities::{
AuthTypePB, SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfile,
};
use crate::entities::{UserProfilePB, UserSettingPB};
use crate::event_map::{
DefaultUserStatusCallback, UserCloudServiceProvider, UserCredentials, UserStatusCallback,
};
use crate::{
errors::FlowyError,
event_map::UserAuthService,
notification::*,
services::database::{UserDB, UserTable, UserTableChangeset},
};
pub struct UserSessionConfig {
root_dir: String,
feat: Customize the storage folder path (#1538) * feat: support customize folder path * feat: add l10n and optimize the logic * chore: code refactor * feat: add file read/write permission for macOS * fix: add toast for restoring path * feat: fetch apps and show them * feat: fetch apps and show them * feat: implement select document logic * feat: l10n and add select item callback * feat: add space between tile * chore: move file exporter to settings * chore: update UI * feat: support customizing folder when launching the app * feat: auto register after customizing folder * feat: l10n * feat: l10n * chore: reinitialize flowy sdk when calling init_sdk * chore: remove flowysdk const keyword to make sure it can be rebuild * chore: clear kv values when user logout * chore: replace current workspace id key in kv.db * feat: add config.name as a part of seesion_cache_key * feat: support open folder when launching * chore: fix some bugs * chore: dart fix & flutter analyze * chore: wrap 'sign up with ramdom user' as interface * feat: dismiss settings view after changing the folder * fix: read kv value after initializaing with new path * chore: remove user_id prefix from current workspace key * fix: move open latest view action to bloc * test: add test utils for integration tests * chore: move integration_test to its parent directory * test: add integration_test ci * test: switch to B from A, then switch to A again * chore: fix warings and format code and fix tests * chore: remove comment out codes * chore: rename some properties name and optimize the logic * chore: abstract logic of settings file exporter widget to cubit * chore: abstract location customizer view from file system view * chore: abstract settings page index to enum type * chore: remove the redundant underscore * test: fix integration test error * chore: enable integration test for windows and ubuntu * feat: abstract file picker as service and mock it under integration test * chore: fix bloc test Co-authored-by: nathan <nathan@appflowy.io>
2022-12-20 03:14:42 +00:00
/// Used as the key of `Session` when saving session information to KV.
session_cache_key: String,
}
impl UserSessionConfig {
/// The `root_dir` represents as the root of the user folders. It must be unique for each
/// users.
pub fn new(name: &str, root_dir: &str) -> Self {
let session_cache_key = format!("{}_session_cache", name);
Self {
root_dir: root_dir.to_owned(),
session_cache_key,
}
}
}
pub struct UserSession {
database: UserDB,
session_config: UserSessionConfig,
cloud_services: Arc<dyn UserCloudServiceProvider>,
user_status_callback: RwLock<Arc<dyn UserStatusCallback>>,
}
impl UserSession {
pub fn new(
session_config: UserSessionConfig,
cloud_services: Arc<dyn UserCloudServiceProvider>,
) -> Self {
let db = UserDB::new(&session_config.root_dir);
let user_status_callback: RwLock<Arc<dyn UserStatusCallback>> =
RwLock::new(Arc::new(DefaultUserStatusCallback));
Self {
database: db,
session_config,
cloud_services,
user_status_callback,
2021-08-31 15:01:46 +00:00
}
}
2021-08-31 15:01:46 +00:00
pub async fn init<C: UserStatusCallback + 'static>(&self, user_status_callback: C) {
if let Ok(session) = self.get_session() {
let _ = user_status_callback
.did_sign_in(session.user_id, &session.workspace_id)
.await;
}
*self.user_status_callback.write().await = Arc::new(user_status_callback);
}
pub fn db_connection(&self) -> Result<DBConnection, FlowyError> {
let user_id = self.get_session()?.user_id;
self.database.get_connection(user_id)
}
// The caller will be not 'Sync' before of the return value,
// PooledConnection<ConnectionManager> is not sync. You can use
// db_connection_pool function to require the ConnectionPool that is 'Sync'.
//
// let pool = self.db_connection_pool()?;
// let conn: PooledConnection<ConnectionManager> = pool.get()?;
pub fn db_pool(&self) -> Result<Arc<ConnectionPool>, FlowyError> {
let user_id = self.get_session()?.user_id;
self.database.get_pool(user_id)
}
pub fn get_collab_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError> {
let user_id = self.get_session()?.user_id;
self.database.get_collab_db(user_id)
}
#[tracing::instrument(level = "debug", skip(self, params))]
pub async fn sign_in(
&self,
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()?
.sign_in(params)
.await?;
let session: Session = resp.clone().into();
self.set_session(Some(session))?;
let user_profile: UserProfile = self.save_user(resp.into()).await?.into();
let _ = self
.user_status_callback
.read()
.await
.did_sign_in(user_profile.id, &user_profile.workspace_id)
.await;
send_sign_in_notification()
.payload::<UserProfilePB>(user_profile.clone().into())
.send();
Ok(user_profile)
}
#[tracing::instrument(level = "debug", skip(self, params))]
pub async fn sign_up(
&self,
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 auth_service = self.cloud_services.get_auth_service()?;
let resp = auth_service.sign_up(params).await?;
let is_new = resp.is_new;
let session: Session = resp.clone().into();
self.set_session(Some(session))?;
let user_table = self.save_user(resp.into()).await?;
let user_profile: UserProfile = user_table.into();
let _ = self
.user_status_callback
.read()
.await
.did_sign_up(is_new, &user_profile)
.await;
Ok(user_profile)
}
#[tracing::instrument(level = "debug", skip(self))]
pub async fn sign_out(&self, auth_type: &AuthType) -> Result<(), FlowyError> {
let session = self.get_session()?;
let uid = session.user_id.to_string();
let _ = diesel::delete(dsl::user_table.filter(dsl::id.eq(&uid)))
.execute(&*(self.db_connection()?))?;
self.database.close_user_db(session.user_id)?;
self.set_session(None)?;
let server = self.cloud_services.get_auth_service()?;
let token = session.token;
tokio::spawn(async move {
match server.sign_out(token).await {
Ok(_) => {},
Err(e) => tracing::error!("Sign out failed: {:?}", e),
}
});
2022-11-11 09:24:10 +00:00
Ok(())
}
2021-09-01 08:08:32 +00:00
#[tracing::instrument(level = "debug", skip(self))]
pub async fn update_user_profile(
&self,
params: UpdateUserProfileParams,
) -> Result<(), FlowyError> {
let auth_type = params.auth_type.clone();
let session = self.get_session()?;
let changeset = UserTableChangeset::new(params.clone());
diesel_update_table!(user_table, changeset, &*self.db_connection()?);
let user_profile = self.get_user_profile().await?;
let profile_pb: UserProfilePB = user_profile.into();
send_notification(
&session.user_id.to_string(),
UserNotification::DidUpdateUserProfile,
)
.payload(profile_pb)
.send();
self
.update_user(&auth_type, session.user_id, &session.token, params)
.await?;
Ok(())
}
2021-12-09 14:28:11 +00:00
pub async fn init_user(&self) -> Result<(), FlowyError> {
Ok(())
}
pub async fn check_user(&self, credential: UserCredentials) -> Result<(), FlowyError> {
let auth_service = self.cloud_services.get_auth_service()?;
auth_service.check_user(credential).await
}
pub async fn get_user_profile(&self) -> Result<UserProfile, FlowyError> {
let (user_id, _) = self.get_session()?.into_part();
let user_id = user_id.to_string();
let user = dsl::user_table
.filter(user_table::id.eq(&user_id))
.first::<UserTable>(&*(self.db_connection()?))?;
Ok(user.into())
}
pub fn user_dir(&self) -> Result<String, FlowyError> {
let session = self.get_session()?;
Ok(format!(
"{}/{}",
self.session_config.root_dir, session.user_id
))
}
pub fn user_setting(&self) -> Result<UserSettingPB, FlowyError> {
let user_setting = UserSettingPB {
user_folder: self.user_dir()?,
};
Ok(user_setting)
}
pub fn user_id(&self) -> Result<i64, FlowyError> {
Ok(self.get_session()?.user_id)
}
pub fn user_name(&self) -> Result<String, FlowyError> {
Ok(self.get_session()?.name)
}
pub fn token(&self) -> Result<Option<String>, FlowyError> {
Ok(self.get_session()?.token)
}
2021-09-01 08:08:32 +00:00
}
impl UserSession {
async fn update_user(
&self,
_auth_type: &AuthType,
uid: i64,
token: &Option<String>,
params: UpdateUserProfileParams,
) -> Result<(), FlowyError> {
let server = self.cloud_services.get_auth_service()?;
let token = token.to_owned();
let _ = tokio::spawn(async move {
let credentials = UserCredentials::new(token, Some(uid), None);
match server.update_user(credentials, params).await {
Ok(_) => {},
Err(e) => {
tracing::error!("update user profile failed: {:?}", e);
},
}
})
.await;
Ok(())
}
async fn save_user(&self, user: UserTable) -> Result<UserTable, FlowyError> {
let conn = self.db_connection()?;
conn.immediate_transaction(|| {
// delete old user if exists
diesel::delete(dsl::user_table.filter(dsl::id.eq(&user.id))).execute(&*conn)?;
let _ = diesel::insert_into(user_table::table)
.values(user.clone())
.execute(&*conn)?;
Ok::<(), FlowyError>(())
})?;
Ok(user)
}
fn set_session(&self, session: Option<Session>) -> Result<(), FlowyError> {
tracing::debug!("Set user session: {:?}", session);
match &session {
None => KV::remove(&self.session_config.session_cache_key),
Some(session) => {
KV::set_object(&self.session_config.session_cache_key, session.clone())
.map_err(internal_error)?;
},
}
Ok(())
}
fn get_session(&self) -> Result<Session, FlowyError> {
match KV::get_object::<Session>(&self.session_config.session_cache_key) {
None => Err(FlowyError::unauthorized()),
Some(session) => Ok(session),
}
}
2021-08-31 15:01:46 +00:00
}
2021-09-27 15:23:23 +00:00
pub async fn update_user(
_cloud_service: Arc<dyn UserAuthService>,
pool: Arc<ConnectionPool>,
params: UpdateUserProfileParams,
2021-12-14 10:04:51 +00:00
) -> Result<(), FlowyError> {
let changeset = UserTableChangeset::new(params);
let conn = pool.get()?;
diesel_update_table!(user_table, changeset, &*conn);
Ok(())
2021-07-16 15:18:12 +00:00
}
2021-07-11 09:38:03 +00:00
2021-07-13 15:08:20 +00:00
impl UserDatabaseConnection for UserSession {
fn get_connection(&self) -> Result<DBConnection, String> {
self.db_connection().map_err(|e| format!("{:?}", e))
}
2021-07-13 15:08:20 +00:00
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct Session {
user_id: i64,
workspace_id: String,
#[serde(default)]
name: String,
#[serde(default)]
token: Option<String>,
#[serde(default)]
email: Option<String>,
}
2021-12-09 13:39:53 +00:00
impl std::convert::From<SignInResponse> for Session {
fn from(resp: SignInResponse) -> Self {
Session {
user_id: resp.user_id,
token: resp.token,
email: resp.email,
name: resp.name,
workspace_id: resp.workspace_id,
2021-12-09 13:39:53 +00:00
}
}
2021-12-09 13:39:53 +00:00
}
impl std::convert::From<SignUpResponse> for Session {
fn from(resp: SignUpResponse) -> Self {
Session {
user_id: resp.user_id,
token: resp.token,
email: resp.email,
name: resp.name,
workspace_id: resp.workspace_id,
}
}
2021-12-09 13:39:53 +00:00
}
2021-09-07 15:30:43 +00:00
2021-12-09 13:39:53 +00:00
impl Session {
pub fn into_part(self) -> (i64, Option<String>) {
(self.user_id, self.token)
}
}
impl std::convert::From<String> for Session {
fn from(s: String) -> Self {
match serde_json::from_str(&s) {
Ok(s) => s,
Err(e) => {
tracing::error!("Deserialize string to Session failed: {:?}", e);
Session::default()
},
}
}
}
2021-11-27 11:19:41 +00:00
impl std::convert::From<Session> for String {
fn from(session: Session) -> Self {
match serde_json::to_string(&session) {
Ok(s) => s,
Err(e) => {
tracing::error!("Serialize session to string failed: {:?}", e);
"".to_string()
},
}
}
}
#[derive(Debug, Clone, Hash, Serialize_repr, Deserialize_repr, Eq, PartialEq)]
#[repr(u8)]
pub enum AuthType {
/// It's a local server, we do fake sign in default.
Local = 0,
/// Currently not supported. It will be supported in the future when the
/// [AppFlowy-Server](https://github.com/AppFlowy-IO/AppFlowy-Server) ready.
SelfHosted = 1,
/// It uses Supabase as the backend.
Supabase = 2,
}
impl Default for AuthType {
fn default() -> Self {
Self::Local
}
}
impl From<AuthTypePB> for AuthType {
fn from(pb: AuthTypePB) -> Self {
match pb {
AuthTypePB::Supabase => AuthType::Supabase,
AuthTypePB::Local => AuthType::Local,
AuthTypePB::SelfHosted => AuthType::SelfHosted,
}
}
}