2022-07-04 06:53:35 +00:00
|
|
|
use crate::entities::{
|
2023-02-13 01:29:49 +00:00
|
|
|
AppearanceSettingsPB, UpdateUserProfilePayloadPB, UserProfilePB, UserSettingPB,
|
|
|
|
APPEARANCE_DEFAULT_THEME,
|
2022-01-28 02:56:55 +00:00
|
|
|
};
|
2022-07-04 06:53:35 +00:00
|
|
|
use crate::{errors::FlowyError, services::UserSession};
|
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::*;
|
2021-07-14 13:12:52 +00:00
|
|
|
use std::{convert::TryInto, sync::Arc};
|
2023-01-30 03:11:19 +00:00
|
|
|
use user_model::UpdateUserProfileParams;
|
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(
|
|
|
|
session: AFPluginState<Arc<UserSession>>,
|
|
|
|
) -> DataResult<UserProfilePB, FlowyError> {
|
|
|
|
let user_profile: UserProfilePB = session.check_user().await?.into();
|
|
|
|
data_result(user_profile)
|
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();
|
|
|
|
data_result(user_profile)
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", name = "sign_out", skip(session))]
|
2022-12-01 00:35:50 +00:00
|
|
|
pub async fn sign_out(session: AFPluginState<Arc<UserSession>>) -> Result<(), FlowyError> {
|
2023-02-13 01:29:49 +00:00
|
|
|
session.sign_out().await?;
|
|
|
|
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-02-13 01:29:49 +00:00
|
|
|
let s = serde_json::to_string(&setting)?;
|
|
|
|
KV::set_str(APPEARANCE_SETTING_CACHE_KEY, s);
|
|
|
|
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) {
|
|
|
|
None => data_result(AppearanceSettingsPB::default()),
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
data_result(setting)
|
|
|
|
},
|
|
|
|
}
|
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()?;
|
|
|
|
data_result(user_setting)
|
2022-11-11 09:24:10 +00:00
|
|
|
}
|