2022-01-11 05:34:45 +00:00
|
|
|
use crate::{errors::FlowyError, services::UserSession};
|
2022-01-28 02:56:55 +00:00
|
|
|
use flowy_database::kv::KV;
|
|
|
|
use flowy_user_data_model::entities::{
|
2022-07-03 08:52:06 +00:00
|
|
|
AppearanceSettings, UpdateUserProfileParams, UpdateUserProfilePayload, UserProfile, APPEARANCE_DEFAULT_THEME,
|
2022-01-28 02:56:55 +00:00
|
|
|
};
|
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};
|
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(session))]
|
2022-02-25 14:27:44 +00:00
|
|
|
pub async fn init_user_handler(session: AppData<Arc<UserSession>>) -> Result<(), FlowyError> {
|
2021-09-25 13:47:02 +00:00
|
|
|
let _ = session.init_user().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(session))]
|
2022-02-25 14:27:44 +00:00
|
|
|
pub async fn check_user_handler(session: AppData<Arc<UserSession>>) -> DataResult<UserProfile, FlowyError> {
|
2021-09-25 13:47:02 +00:00
|
|
|
let user_profile = session.check_user().await?;
|
2021-09-17 11:03:46 +00:00
|
|
|
data_result(user_profile)
|
|
|
|
}
|
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(session))]
|
2022-02-25 14:27:44 +00:00
|
|
|
pub async fn get_user_profile_handler(session: AppData<Arc<UserSession>>) -> DataResult<UserProfile, FlowyError> {
|
2022-07-04 02:59:08 +00:00
|
|
|
let user_profile = session.get_user_profile().await?;
|
2021-09-04 08:53:58 +00:00
|
|
|
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-02-25 14:27:44 +00:00
|
|
|
pub async fn sign_out(session: AppData<Arc<UserSession>>) -> Result<(), FlowyError> {
|
2021-08-31 09:25:08 +00:00
|
|
|
let _ = session.sign_out().await?;
|
2021-07-14 13:12:52 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-03 08:52:06 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, session))]
|
|
|
|
pub async fn update_user_profile_handler(
|
|
|
|
data: Data<UpdateUserProfilePayload>,
|
2022-02-25 14:27:44 +00:00
|
|
|
session: AppData<Arc<UserSession>>,
|
2021-12-14 10:04:51 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2022-07-03 08:52:06 +00:00
|
|
|
let params: UpdateUserProfileParams = data.into_inner().try_into()?;
|
|
|
|
session.update_user_profile(params).await?;
|
2021-08-31 15:01:46 +00:00
|
|
|
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)]
|
2022-01-28 10:34:21 +00:00
|
|
|
pub async fn set_appearance_setting(data: Data<AppearanceSettings>) -> Result<(), FlowyError> {
|
2022-01-28 02:56:55 +00:00
|
|
|
let mut setting = data.into_inner();
|
|
|
|
if setting.theme.is_empty() {
|
|
|
|
setting.theme = APPEARANCE_DEFAULT_THEME.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
let s = serde_json::to_string(&setting)?;
|
|
|
|
KV::set_str(APPEARANCE_SETTING_CACHE_KEY, s);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(err)]
|
|
|
|
pub async fn get_appearance_setting() -> DataResult<AppearanceSettings, FlowyError> {
|
|
|
|
match KV::get_str(APPEARANCE_SETTING_CACHE_KEY) {
|
|
|
|
None => data_result(AppearanceSettings::default()),
|
|
|
|
Some(s) => {
|
2022-02-05 13:50:49 +00:00
|
|
|
let setting = match serde_json::from_str(&s) {
|
|
|
|
Ok(setting) => setting,
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Deserialize AppearanceSettings failed: {:?}, fallback to default", e);
|
|
|
|
AppearanceSettings::default()
|
|
|
|
}
|
|
|
|
};
|
2022-01-28 02:56:55 +00:00
|
|
|
data_result(setting)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|