|
@@ -1,3 +1,5 @@
|
|
|
+use std::convert::TryFrom;
|
|
|
+use std::string::ToString;
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
|
|
use appflowy_integrate::RocksCollabDB;
|
|
@@ -152,27 +154,30 @@ impl UserSession {
|
|
|
params: BoxAny,
|
|
|
auth_type: AuthType,
|
|
|
) -> Result<UserProfile, FlowyError> {
|
|
|
- let resp: SignInResponse = self
|
|
|
+ let response: SignInResponse = self
|
|
|
.cloud_services
|
|
|
.get_user_service()?
|
|
|
.sign_in(params)
|
|
|
.await?;
|
|
|
-
|
|
|
- let session: Session = resp.clone().into();
|
|
|
+ let session: Session = response.clone().into();
|
|
|
let uid = session.user_id;
|
|
|
- self.set_session(Some(session))?;
|
|
|
- self.log_user(uid, self.user_dir(uid));
|
|
|
+ self.set_current_session(Some(session))?;
|
|
|
+
|
|
|
+ self.log_user(uid, response.name.clone(), &auth_type, self.user_dir(uid));
|
|
|
|
|
|
- let user_workspace = resp.latest_workspace.clone();
|
|
|
+ let user_workspace = response.latest_workspace.clone();
|
|
|
save_user_workspaces(
|
|
|
self.db_pool(uid)?,
|
|
|
- resp
|
|
|
+ response
|
|
|
.user_workspaces
|
|
|
.iter()
|
|
|
- .map(|user_workspace| UserWorkspaceTable::from((uid, user_workspace)))
|
|
|
+ .flat_map(|user_workspace| UserWorkspaceTable::try_from((uid, user_workspace)).ok())
|
|
|
.collect(),
|
|
|
)?;
|
|
|
- let user_profile: UserProfile = self.save_user(uid, (resp, auth_type).into()).await?.into();
|
|
|
+ let user_profile: UserProfile = self
|
|
|
+ .save_user(uid, (response, auth_type).into())
|
|
|
+ .await?
|
|
|
+ .into();
|
|
|
if let Err(e) = self
|
|
|
.user_status_callback
|
|
|
.read()
|
|
@@ -226,19 +231,16 @@ impl UserSession {
|
|
|
is_new: response.is_new,
|
|
|
local_folder: None,
|
|
|
};
|
|
|
- let new_session = Session {
|
|
|
- user_id: response.user_id,
|
|
|
- user_workspace: response.latest_workspace.clone(),
|
|
|
- };
|
|
|
- let uid = new_session.user_id;
|
|
|
- self.set_session(Some(new_session.clone()))?;
|
|
|
- self.log_user(uid, self.user_dir(uid));
|
|
|
+ let new_session = Session::from(&response);
|
|
|
+ self.set_current_session(Some(new_session.clone()))?;
|
|
|
+ let uid = response.user_id;
|
|
|
+ self.log_user(uid, response.name.clone(), &auth_type, self.user_dir(uid));
|
|
|
save_user_workspaces(
|
|
|
self.db_pool(uid)?,
|
|
|
response
|
|
|
.user_workspaces
|
|
|
.iter()
|
|
|
- .map(|user_workspace| UserWorkspaceTable::from((uid, user_workspace)))
|
|
|
+ .flat_map(|user_workspace| UserWorkspaceTable::try_from((uid, user_workspace)).ok())
|
|
|
.collect(),
|
|
|
)?;
|
|
|
let user_table = self
|
|
@@ -289,7 +291,7 @@ impl UserSession {
|
|
|
pub async fn sign_out(&self) -> Result<(), FlowyError> {
|
|
|
let session = self.get_session()?;
|
|
|
self.database.close(session.user_id)?;
|
|
|
- self.set_session(None)?;
|
|
|
+ self.set_current_session(None)?;
|
|
|
|
|
|
let server = self.cloud_services.get_user_service()?;
|
|
|
tokio::spawn(async move {
|
|
@@ -513,7 +515,7 @@ impl UserSession {
|
|
|
pool,
|
|
|
new_user_workspaces
|
|
|
.iter()
|
|
|
- .map(|user_workspace| UserWorkspaceTable::from((uid, user_workspace)))
|
|
|
+ .flat_map(|user_workspace| UserWorkspaceTable::try_from((uid, user_workspace)).ok())
|
|
|
.collect(),
|
|
|
);
|
|
|
|
|
@@ -561,8 +563,8 @@ impl UserSession {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- fn set_session(&self, session: Option<Session>) -> Result<(), FlowyError> {
|
|
|
- tracing::debug!("Set user session: {:?}", session);
|
|
|
+ fn set_current_session(&self, session: Option<Session>) -> Result<(), FlowyError> {
|
|
|
+ tracing::debug!("Set current user: {:?}", session);
|
|
|
match &session {
|
|
|
None => self
|
|
|
.store_preferences
|
|
@@ -577,13 +579,15 @@ impl UserSession {
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
- fn log_user(&self, uid: i64, storage_path: String) {
|
|
|
+ fn log_user(&self, uid: i64, user_name: String, auth_type: &AuthType, storage_path: String) {
|
|
|
let mut logger_users = self
|
|
|
.store_preferences
|
|
|
.get_object::<HistoricalUsers>(HISTORICAL_USER)
|
|
|
.unwrap_or_default();
|
|
|
logger_users.add_user(HistoricalUser {
|
|
|
user_id: uid,
|
|
|
+ user_name,
|
|
|
+ auth_type: auth_type.clone(),
|
|
|
sign_in_timestamp: timestamp(),
|
|
|
storage_path,
|
|
|
});
|
|
@@ -593,11 +597,27 @@ impl UserSession {
|
|
|
}
|
|
|
|
|
|
pub fn get_historical_users(&self) -> Vec<HistoricalUser> {
|
|
|
- self
|
|
|
+ let mut users = self
|
|
|
.store_preferences
|
|
|
.get_object::<HistoricalUsers>(HISTORICAL_USER)
|
|
|
.unwrap_or_default()
|
|
|
- .users
|
|
|
+ .users;
|
|
|
+ users.sort_by(|a, b| b.sign_in_timestamp.cmp(&a.sign_in_timestamp));
|
|
|
+ users
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn open_historical_user(&self, uid: i64) -> FlowyResult<()> {
|
|
|
+ let conn = self.db_connection(uid)?;
|
|
|
+ let row = user_workspace_table::dsl::user_workspace_table
|
|
|
+ .filter(user_workspace_table::uid.eq(uid))
|
|
|
+ .first::<UserWorkspaceTable>(&*conn)?;
|
|
|
+ let user_workspace = UserWorkspace::from(row);
|
|
|
+ let session = Session {
|
|
|
+ user_id: uid,
|
|
|
+ user_workspace,
|
|
|
+ };
|
|
|
+ self.set_current_session(Some(session))?;
|
|
|
+ Ok(())
|
|
|
}
|
|
|
|
|
|
/// Returns the current user session.
|
|
@@ -691,6 +711,12 @@ impl HistoricalUsers {
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
|
|
pub struct HistoricalUser {
|
|
|
pub user_id: i64,
|
|
|
+ #[serde(default = "flowy_user_deps::DEFAULT_USER_NAME")]
|
|
|
+ pub user_name: String,
|
|
|
+ #[serde(default = "DEFAULT_AUTH_TYPE")]
|
|
|
+ pub auth_type: AuthType,
|
|
|
pub sign_in_timestamp: i64,
|
|
|
pub storage_path: String,
|
|
|
}
|
|
|
+
|
|
|
+const DEFAULT_AUTH_TYPE: fn() -> AuthType = || AuthType::Local;
|