2023-05-17 01:49:39 +00:00
|
|
|
use std::{convert::TryInto, sync::Arc};
|
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
use flowy_error::FlowyError;
|
2023-01-31 00:28:31 +00:00
|
|
|
use flowy_sqlite::kv::KV;
|
2021-11-19 06:38:11 +00:00
|
|
|
use lib_dispatch::prelude::*;
|
2023-05-21 10:53:59 +00:00
|
|
|
use lib_infra::box_any::BoxAny;
|
2023-05-17 01:49:39 +00:00
|
|
|
|
|
|
|
use crate::entities::*;
|
|
|
|
use crate::entities::{SignInParams, SignUpParams, UpdateUserProfileParams};
|
2023-07-05 12:57:09 +00:00
|
|
|
use crate::event_map::UserCredentials;
|
2023-05-21 10:53:59 +00:00
|
|
|
use crate::services::{AuthType, UserSession};
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", name = "sign_in", skip(data, session), fields(email = %data.email), err)]
|
|
|
|
pub async fn sign_in(
|
|
|
|
data: AFPluginData<SignInPayloadPB>,
|
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
|
|
|
) -> DataResult<UserProfilePB, FlowyError> {
|
|
|
|
let params: SignInParams = data.into_inner().try_into()?;
|
2023-05-21 10:53:59 +00:00
|
|
|
let auth_type = params.auth_type.clone();
|
2023-05-23 15:55:21 +00:00
|
|
|
|
2023-05-21 10:53:59 +00:00
|
|
|
let user_profile: UserProfilePB = session
|
|
|
|
.sign_in(&auth_type, BoxAny::new(params))
|
|
|
|
.await?
|
|
|
|
.into();
|
2023-04-04 00:41:16 +00:00
|
|
|
data_result_ok(user_profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(
|
|
|
|
level = "debug",
|
|
|
|
name = "sign_up",
|
|
|
|
skip(data, session),
|
|
|
|
fields(
|
|
|
|
email = %data.email,
|
|
|
|
name = %data.name,
|
|
|
|
),
|
|
|
|
err
|
|
|
|
)]
|
|
|
|
pub async fn sign_up(
|
|
|
|
data: AFPluginData<SignUpPayloadPB>,
|
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
|
|
|
) -> DataResult<UserProfilePB, FlowyError> {
|
|
|
|
let params: SignUpParams = data.into_inner().try_into()?;
|
2023-05-21 10:53:59 +00:00
|
|
|
let auth_type = params.auth_type.clone();
|
|
|
|
let user_profile: UserProfilePB = session
|
|
|
|
.sign_up(&auth_type, BoxAny::new(params))
|
|
|
|
.await?
|
|
|
|
.into();
|
2023-04-04 00:41:16 +00:00
|
|
|
data_result_ok(user_profile)
|
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(session))]
|
2022-12-01 00:35:50 +00:00
|
|
|
pub async fn init_user_handler(session: AFPluginState<Arc<UserSession>>) -> Result<(), FlowyError> {
|
2023-02-13 01:29:49 +00:00
|
|
|
session.init_user().await?;
|
|
|
|
Ok(())
|
2021-09-25 13:47:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(session))]
|
2023-02-13 01:29:49 +00:00
|
|
|
pub async fn check_user_handler(
|
2023-07-05 12:57:09 +00:00
|
|
|
data: AFPluginData<UserCredentialsPB>,
|
2023-02-13 01:29:49 +00:00
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
2023-07-05 12:57:09 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let credential = UserCredentials::from(data.into_inner());
|
|
|
|
session.check_user(credential).await?;
|
|
|
|
Ok(())
|
2021-09-17 11:03:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(session))]
|
2022-12-01 00:35:50 +00:00
|
|
|
pub async fn get_user_profile_handler(
|
2023-02-13 01:29:49 +00:00
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
2022-12-01 00:35:50 +00:00
|
|
|
) -> DataResult<UserProfilePB, FlowyError> {
|
2023-02-13 01:29:49 +00:00
|
|
|
let user_profile: UserProfilePB = session.get_user_profile().await?.into();
|
2023-02-26 08:27:17 +00:00
|
|
|
data_result_ok(user_profile)
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 10:53:59 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, session))]
|
|
|
|
pub async fn sign_out(
|
|
|
|
data: AFPluginData<SignOutPB>,
|
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let auth_type: AuthType = data.into_inner().auth_type.into();
|
|
|
|
session.sign_out(&auth_type).await?;
|
2023-02-13 01:29:49 +00:00
|
|
|
Ok(())
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 08:52:06 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, session))]
|
|
|
|
pub async fn update_user_profile_handler(
|
2023-02-13 01:29:49 +00:00
|
|
|
data: AFPluginData<UpdateUserProfilePayloadPB>,
|
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
2021-12-14 10:04:51 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2023-02-13 01:29:49 +00:00
|
|
|
let params: UpdateUserProfileParams = data.into_inner().try_into()?;
|
|
|
|
session.update_user_profile(params).await?;
|
|
|
|
Ok(())
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
2022-01-28 02:56:55 +00:00
|
|
|
|
|
|
|
const APPEARANCE_SETTING_CACHE_KEY: &str = "appearance_settings";
|
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data), err)]
|
2023-02-13 01:29:49 +00:00
|
|
|
pub async fn set_appearance_setting(
|
|
|
|
data: AFPluginData<AppearanceSettingsPB>,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let mut setting = data.into_inner();
|
|
|
|
if setting.theme.is_empty() {
|
|
|
|
setting.theme = APPEARANCE_DEFAULT_THEME.to_string();
|
|
|
|
}
|
2022-01-28 02:56:55 +00:00
|
|
|
|
2023-05-21 10:53:59 +00:00
|
|
|
KV::set_object(APPEARANCE_SETTING_CACHE_KEY, setting)?;
|
2023-02-13 01:29:49 +00:00
|
|
|
Ok(())
|
2022-01-28 02:56:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 03:09:51 +00:00
|
|
|
#[tracing::instrument(level = "debug", err)]
|
2022-07-19 06:40:56 +00:00
|
|
|
pub async fn get_appearance_setting() -> DataResult<AppearanceSettingsPB, FlowyError> {
|
2023-02-13 01:29:49 +00:00
|
|
|
match KV::get_str(APPEARANCE_SETTING_CACHE_KEY) {
|
2023-02-26 08:27:17 +00:00
|
|
|
None => data_result_ok(AppearanceSettingsPB::default()),
|
2023-02-13 01:29:49 +00:00
|
|
|
Some(s) => {
|
|
|
|
let setting = match serde_json::from_str(&s) {
|
|
|
|
Ok(setting) => setting,
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(
|
|
|
|
"Deserialize AppearanceSettings failed: {:?}, fallback to default",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
AppearanceSettingsPB::default()
|
|
|
|
},
|
|
|
|
};
|
2023-02-26 08:27:17 +00:00
|
|
|
data_result_ok(setting)
|
2023-02-13 01:29:49 +00:00
|
|
|
},
|
|
|
|
}
|
2022-01-28 02:56:55 +00:00
|
|
|
}
|
2022-11-11 09:24:10 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip_all, err)]
|
2023-02-13 01:29:49 +00:00
|
|
|
pub async fn get_user_setting(
|
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
|
|
|
) -> DataResult<UserSettingPB, FlowyError> {
|
|
|
|
let user_setting = session.user_setting()?;
|
2023-02-26 08:27:17 +00:00
|
|
|
data_result_ok(user_setting)
|
2022-11-11 09:24:10 +00:00
|
|
|
}
|
2023-05-21 10:53:59 +00:00
|
|
|
|
|
|
|
/// Only used for third party auth.
|
|
|
|
/// Use [UserEvent::SignIn] or [UserEvent::SignUp] If the [AuthType] is Local or SelfHosted
|
|
|
|
#[tracing::instrument(level = "debug", skip(data, session), err)]
|
|
|
|
pub async fn third_party_auth_handler(
|
|
|
|
data: AFPluginData<ThirdPartyAuthPB>,
|
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
|
|
|
) -> DataResult<UserProfilePB, FlowyError> {
|
|
|
|
let params = data.into_inner();
|
|
|
|
let auth_type: AuthType = params.auth_type.into();
|
|
|
|
let user_profile: UserProfilePB = session
|
|
|
|
.sign_up(&auth_type, BoxAny::new(params.map))
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
data_result_ok(user_profile)
|
|
|
|
}
|