2021-08-20 06:38:03 +00:00
|
|
|
use crate::{
|
2021-09-04 08:53:58 +00:00
|
|
|
entities::{SignInParams, SignUpParams, UpdateUserParams, UserProfile},
|
2021-09-16 04:35:55 +00:00
|
|
|
errors::{ErrorCode, UserError},
|
2021-09-10 10:21:35 +00:00
|
|
|
services::user::database::UserDB,
|
2021-08-20 06:38:03 +00:00
|
|
|
sql_tables::{UserTable, UserTableChangeset},
|
|
|
|
};
|
2021-08-29 14:00:42 +00:00
|
|
|
|
2021-09-10 10:21:35 +00:00
|
|
|
use crate::{
|
|
|
|
observable::*,
|
|
|
|
services::server::{construct_user_server, Server},
|
|
|
|
};
|
2021-07-11 07:33:19 +00:00
|
|
|
use flowy_database::{
|
|
|
|
query_dsl::*,
|
|
|
|
schema::{user_table, user_table::dsl},
|
|
|
|
DBConnection,
|
|
|
|
ExpressionMethods,
|
2021-07-13 15:08:20 +00:00
|
|
|
UserDatabaseConnection,
|
2021-07-10 08:27:20 +00:00
|
|
|
};
|
2021-09-03 04:44:48 +00:00
|
|
|
use flowy_infra::kv::KV;
|
2021-09-27 15:23:23 +00:00
|
|
|
use flowy_net::config::ServerConfig;
|
2021-08-31 15:01:46 +00:00
|
|
|
use flowy_sqlite::ConnectionPool;
|
2021-09-20 07:38:55 +00:00
|
|
|
use flowy_ws::{connect::Retry, WsController, WsMessage, WsMessageHandler, WsSender};
|
2021-09-01 03:21:42 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-09-20 07:38:55 +00:00
|
|
|
use std::{sync::Arc, time::Duration};
|
2021-07-10 08:27:20 +00:00
|
|
|
|
|
|
|
pub struct UserSessionConfig {
|
|
|
|
root_dir: String,
|
2021-09-27 15:23:23 +00:00
|
|
|
server_config: ServerConfig,
|
2021-07-10 08:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserSessionConfig {
|
2021-09-27 15:23:23 +00:00
|
|
|
pub fn new(root_dir: &str, server_config: &ServerConfig) -> Self {
|
2021-07-10 08:27:20 +00:00
|
|
|
Self {
|
|
|
|
root_dir: root_dir.to_owned(),
|
2021-09-27 15:23:23 +00:00
|
|
|
server_config: server_config.clone(),
|
2021-07-10 08:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:03:46 +00:00
|
|
|
pub enum SessionStatus {
|
|
|
|
Login { token: String },
|
|
|
|
Expired { token: String },
|
|
|
|
}
|
|
|
|
pub type SessionStatusCallback = Arc<dyn Fn(SessionStatus) + Send + Sync>;
|
|
|
|
|
2021-07-10 08:27:20 +00:00
|
|
|
pub struct UserSession {
|
|
|
|
database: UserDB,
|
|
|
|
config: UserSessionConfig,
|
2021-08-30 08:18:58 +00:00
|
|
|
#[allow(dead_code)]
|
2021-09-01 08:08:32 +00:00
|
|
|
server: Server,
|
2021-09-01 03:21:42 +00:00
|
|
|
session: RwLock<Option<Session>>,
|
2021-09-19 08:11:02 +00:00
|
|
|
ws_controller: Arc<RwLock<WsController>>,
|
2021-09-17 11:03:46 +00:00
|
|
|
status_callback: SessionStatusCallback,
|
2021-07-10 08:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserSession {
|
2021-09-17 11:03:46 +00:00
|
|
|
pub fn new(config: UserSessionConfig, status_callback: SessionStatusCallback) -> Self {
|
2021-07-10 08:27:20 +00:00
|
|
|
let db = UserDB::new(&config.root_dir);
|
2021-09-27 15:23:23 +00:00
|
|
|
let server = construct_user_server(&config.server_config);
|
2021-09-19 08:11:02 +00:00
|
|
|
let ws_controller = Arc::new(RwLock::new(WsController::new()));
|
2021-09-17 11:03:46 +00:00
|
|
|
let user_session = Self {
|
2021-07-10 08:27:20 +00:00
|
|
|
database: db,
|
|
|
|
config,
|
2021-07-14 15:00:58 +00:00
|
|
|
server,
|
2021-09-01 03:21:42 +00:00
|
|
|
session: RwLock::new(None),
|
2021-09-18 14:32:00 +00:00
|
|
|
ws_controller,
|
2021-09-17 11:03:46 +00:00
|
|
|
status_callback,
|
|
|
|
};
|
|
|
|
user_session
|
2021-07-10 08:27:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 12:09:46 +00:00
|
|
|
pub fn db_connection(&self) -> Result<DBConnection, UserError> {
|
2021-09-01 03:21:42 +00:00
|
|
|
let user_id = self.get_session()?.user_id;
|
2021-07-16 15:18:12 +00:00
|
|
|
self.database.get_connection(&user_id)
|
|
|
|
}
|
|
|
|
|
2021-09-01 03:21:42 +00:00
|
|
|
// 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()?;
|
2021-09-04 09:26:04 +00:00
|
|
|
pub fn db_pool(&self) -> Result<Arc<ConnectionPool>, UserError> {
|
2021-09-01 03:21:42 +00:00
|
|
|
let user_id = self.get_session()?.user_id;
|
2021-08-31 15:01:46 +00:00
|
|
|
self.database.get_pool(&user_id)
|
|
|
|
}
|
|
|
|
|
2021-09-06 08:18:34 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-09-04 08:53:58 +00:00
|
|
|
pub async fn sign_in(&self, params: SignInParams) -> Result<UserProfile, UserError> {
|
2021-09-03 08:43:03 +00:00
|
|
|
if self.is_login(¶ms.email) {
|
2021-09-04 08:53:58 +00:00
|
|
|
self.user_profile().await
|
2021-09-03 08:43:03 +00:00
|
|
|
} else {
|
|
|
|
let resp = self.server.sign_in(params).await?;
|
2021-09-08 10:25:32 +00:00
|
|
|
let session = Session::new(&resp.user_id, &resp.token, &resp.email);
|
2021-09-03 08:43:03 +00:00
|
|
|
let _ = self.set_session(Some(session))?;
|
|
|
|
let user_table = self.save_user(resp.into()).await?;
|
2021-09-04 08:53:58 +00:00
|
|
|
let user_profile = UserProfile::from(user_table);
|
2021-09-17 11:03:46 +00:00
|
|
|
(self.status_callback)(SessionStatus::Login {
|
|
|
|
token: user_profile.token.clone(),
|
|
|
|
});
|
2021-09-04 08:53:58 +00:00
|
|
|
Ok(user_profile)
|
2021-09-03 08:43:03 +00:00
|
|
|
}
|
2021-07-11 07:33:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 08:18:34 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-09-04 08:53:58 +00:00
|
|
|
pub async fn sign_up(&self, params: SignUpParams) -> Result<UserProfile, UserError> {
|
2021-09-04 01:00:15 +00:00
|
|
|
if self.is_login(¶ms.email) {
|
2021-09-04 08:53:58 +00:00
|
|
|
self.user_profile().await
|
2021-09-04 01:00:15 +00:00
|
|
|
} else {
|
|
|
|
let resp = self.server.sign_up(params).await?;
|
|
|
|
let session = Session::new(&resp.user_id, &resp.token, &resp.email);
|
|
|
|
let _ = self.set_session(Some(session))?;
|
|
|
|
let user_table = self.save_user(resp.into()).await?;
|
2021-09-04 08:53:58 +00:00
|
|
|
let user_profile = UserProfile::from(user_table);
|
2021-09-17 11:03:46 +00:00
|
|
|
(self.status_callback)(SessionStatus::Login {
|
|
|
|
token: user_profile.token.clone(),
|
|
|
|
});
|
2021-09-04 08:53:58 +00:00
|
|
|
Ok(user_profile)
|
2021-09-04 01:00:15 +00:00
|
|
|
}
|
2021-07-11 07:33:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 08:18:34 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-08-31 09:25:08 +00:00
|
|
|
pub async fn sign_out(&self) -> Result<(), UserError> {
|
2021-09-01 08:08:32 +00:00
|
|
|
let session = self.get_session()?;
|
2021-09-27 15:23:23 +00:00
|
|
|
let _ =
|
|
|
|
diesel::delete(dsl::user_table.filter(dsl::id.eq(&session.user_id))).execute(&*(self.db_connection()?))?;
|
2021-09-01 08:08:32 +00:00
|
|
|
let _ = self.database.close_user_db(&session.user_id)?;
|
2021-09-01 03:21:42 +00:00
|
|
|
let _ = self.set_session(None)?;
|
2021-09-17 11:03:46 +00:00
|
|
|
(self.status_callback)(SessionStatus::Expired {
|
|
|
|
token: session.token.clone(),
|
|
|
|
});
|
2021-09-04 09:26:04 +00:00
|
|
|
let _ = self.sign_out_on_server(&session.token).await?;
|
2021-07-11 09:38:03 +00:00
|
|
|
|
|
|
|
Ok(())
|
2021-07-11 07:33:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 08:18:34 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-08-31 15:01:46 +00:00
|
|
|
pub async fn update_user(&self, params: UpdateUserParams) -> Result<(), UserError> {
|
2021-09-01 09:57:06 +00:00
|
|
|
let session = self.get_session()?;
|
2021-09-04 09:26:04 +00:00
|
|
|
let changeset = UserTableChangeset::new(params.clone());
|
2021-09-11 12:09:46 +00:00
|
|
|
diesel_update_table!(user_table, changeset, &*self.db_connection()?);
|
2021-09-01 09:57:06 +00:00
|
|
|
|
2021-09-04 09:26:04 +00:00
|
|
|
let _ = self.update_user_on_server(&session.token, params).await?;
|
2021-08-31 15:01:46 +00:00
|
|
|
Ok(())
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 13:47:02 +00:00
|
|
|
pub async fn init_user(&self) -> Result<(), UserError> {
|
|
|
|
let (_, token) = self.get_session()?.into_part();
|
|
|
|
|
|
|
|
let _ = self.start_ws_connection(&token)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn check_user(&self) -> Result<UserProfile, UserError> {
|
2021-09-17 11:03:46 +00:00
|
|
|
let (user_id, token) = self.get_session()?.into_part();
|
|
|
|
|
|
|
|
let user = dsl::user_table
|
|
|
|
.filter(user_table::id.eq(&user_id))
|
|
|
|
.first::<UserTable>(&*(self.db_connection()?))?;
|
|
|
|
|
|
|
|
let _ = self.read_user_profile_on_server(&token)?;
|
|
|
|
Ok(UserProfile::from(user))
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:53:58 +00:00
|
|
|
pub async fn user_profile(&self) -> Result<UserProfile, UserError> {
|
2021-09-07 15:30:43 +00:00
|
|
|
let (user_id, token) = self.get_session()?.into_part();
|
2021-09-01 08:37:46 +00:00
|
|
|
let user = dsl::user_table
|
2021-09-07 15:30:43 +00:00
|
|
|
.filter(user_table::id.eq(&user_id))
|
2021-09-11 12:09:46 +00:00
|
|
|
.first::<UserTable>(&*(self.db_connection()?))?;
|
2021-09-01 08:37:46 +00:00
|
|
|
|
2021-09-17 11:03:46 +00:00
|
|
|
let _ = self.read_user_profile_on_server(&token)?;
|
2021-09-04 08:53:58 +00:00
|
|
|
Ok(UserProfile::from(user))
|
2021-07-11 07:33:19 +00:00
|
|
|
}
|
2021-07-10 08:27:20 +00:00
|
|
|
|
2021-08-29 14:00:42 +00:00
|
|
|
pub fn user_dir(&self) -> Result<String, UserError> {
|
2021-09-01 03:21:42 +00:00
|
|
|
let session = self.get_session()?;
|
|
|
|
Ok(format!("{}/{}", self.config.root_dir, session.user_id))
|
2021-07-23 09:30:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 08:08:32 +00:00
|
|
|
pub fn user_id(&self) -> Result<String, UserError> { Ok(self.get_session()?.user_id) }
|
|
|
|
|
2021-09-01 14:50:22 +00:00
|
|
|
pub fn token(&self) -> Result<String, UserError> { Ok(self.get_session()?.token) }
|
2021-09-18 14:32:00 +00:00
|
|
|
|
2021-09-27 15:23:23 +00:00
|
|
|
pub fn add_ws_handler(&self, handler: Arc<dyn WsMessageHandler>) {
|
|
|
|
let _ = self.ws_controller.write().add_handler(handler);
|
|
|
|
}
|
2021-09-20 07:38:55 +00:00
|
|
|
|
|
|
|
pub fn get_ws_sender(&self) -> Result<Arc<WsSender>, UserError> {
|
|
|
|
match self.ws_controller.try_read_for(Duration::from_millis(300)) {
|
|
|
|
None => Err(UserError::internal().context("Send ws message timeout")),
|
|
|
|
Some(guard) => {
|
|
|
|
let sender = guard.get_sender()?;
|
|
|
|
Ok(sender)
|
|
|
|
},
|
|
|
|
}
|
2021-09-18 14:32:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 07:38:55 +00:00
|
|
|
pub fn send_ws_msg<T: Into<WsMessage>>(&self, msg: T) -> Result<(), UserError> {
|
|
|
|
let sender = self.get_ws_sender()?;
|
|
|
|
let _ = sender.send_msg(msg)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-01 08:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserSession {
|
2021-09-17 11:03:46 +00:00
|
|
|
fn read_user_profile_on_server(&self, token: &str) -> Result<(), UserError> {
|
2021-09-04 09:26:04 +00:00
|
|
|
let server = self.server.clone();
|
|
|
|
let token = token.to_owned();
|
2021-09-07 15:30:43 +00:00
|
|
|
tokio::spawn(async move {
|
2021-09-04 09:26:04 +00:00
|
|
|
match server.get_user(&token).await {
|
|
|
|
Ok(profile) => {
|
2021-09-27 15:23:23 +00:00
|
|
|
notify(&token, UserObservable::UserProfileUpdated)
|
|
|
|
.payload(profile)
|
|
|
|
.send();
|
2021-09-04 09:26:04 +00:00
|
|
|
},
|
|
|
|
Err(e) => {
|
2021-09-11 06:26:30 +00:00
|
|
|
notify(&token, UserObservable::UserProfileUpdated).error(e).send();
|
2021-09-04 09:26:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_user_on_server(&self, token: &str, params: UpdateUserParams) -> Result<(), UserError> {
|
|
|
|
let server = self.server.clone();
|
|
|
|
let token = token.to_owned();
|
2021-09-06 08:18:34 +00:00
|
|
|
let _ = tokio::spawn(async move {
|
2021-09-04 09:26:04 +00:00
|
|
|
match server.update_user(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("update user profile failed: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
2021-09-05 05:50:23 +00:00
|
|
|
})
|
|
|
|
.await;
|
2021-09-04 09:26:04 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn sign_out_on_server(&self, token: &str) -> Result<(), UserError> {
|
|
|
|
let server = self.server.clone();
|
|
|
|
let token = token.to_owned();
|
2021-09-06 08:18:34 +00:00
|
|
|
let _ = tokio::spawn(async move {
|
2021-09-04 09:26:04 +00:00
|
|
|
match server.sign_out(&token).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => log::error!("Sign out failed: {:?}", e),
|
|
|
|
}
|
2021-09-05 05:50:23 +00:00
|
|
|
})
|
|
|
|
.await;
|
2021-09-04 09:26:04 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-01 08:08:32 +00:00
|
|
|
async fn save_user(&self, user: UserTable) -> Result<UserTable, UserError> {
|
2021-09-11 12:09:46 +00:00
|
|
|
let conn = self.db_connection()?;
|
2021-09-27 15:23:23 +00:00
|
|
|
let _ = diesel::insert_into(user_table::table)
|
|
|
|
.values(user.clone())
|
|
|
|
.execute(&*conn)?;
|
2021-09-01 08:08:32 +00:00
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2021-09-01 03:21:42 +00:00
|
|
|
fn set_session(&self, session: Option<Session>) -> Result<(), UserError> {
|
2021-09-07 15:30:43 +00:00
|
|
|
log::debug!("Set user session: {:?}", session);
|
2021-09-01 03:21:42 +00:00
|
|
|
match &session {
|
2021-09-08 05:50:20 +00:00
|
|
|
None => KV::remove(SESSION_CACHE_KEY).map_err(|e| UserError::new(ErrorCode::InternalError, &e))?,
|
2021-09-03 04:44:48 +00:00
|
|
|
Some(session) => KV::set_str(SESSION_CACHE_KEY, session.clone().into()),
|
2021-09-01 03:21:42 +00:00
|
|
|
}
|
2021-09-03 04:44:48 +00:00
|
|
|
*self.session.write() = session;
|
2021-09-01 03:21:42 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-03 08:43:03 +00:00
|
|
|
|
2021-09-01 03:21:42 +00:00
|
|
|
fn get_session(&self) -> Result<Session, UserError> {
|
2021-09-03 04:44:48 +00:00
|
|
|
let mut session = { (*self.session.read()).clone() };
|
2021-09-01 03:21:42 +00:00
|
|
|
if session.is_none() {
|
2021-09-03 04:44:48 +00:00
|
|
|
match KV::get_str(SESSION_CACHE_KEY) {
|
2021-09-01 03:21:42 +00:00
|
|
|
None => {},
|
|
|
|
Some(s) => {
|
|
|
|
session = Some(Session::from(s));
|
|
|
|
let _ = self.set_session(session.clone())?;
|
|
|
|
},
|
|
|
|
}
|
2021-07-18 15:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 03:21:42 +00:00
|
|
|
match session {
|
2021-09-16 04:35:55 +00:00
|
|
|
None => Err(UserError::unauthorized()),
|
2021-09-01 03:21:42 +00:00
|
|
|
Some(session) => Ok(session),
|
2021-07-18 15:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 08:43:03 +00:00
|
|
|
|
|
|
|
fn is_login(&self, email: &str) -> bool {
|
|
|
|
match self.get_session() {
|
|
|
|
Ok(session) => session.email == email,
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 11:03:46 +00:00
|
|
|
|
|
|
|
fn start_ws_connection(&self, token: &str) -> Result<(), UserError> {
|
2021-09-29 09:40:34 +00:00
|
|
|
log::debug!("start_ws_connection");
|
2021-09-27 15:23:23 +00:00
|
|
|
let addr = format!("{}/{}", self.server.ws_addr(), token);
|
2021-09-19 08:11:02 +00:00
|
|
|
let ws_controller = self.ws_controller.clone();
|
|
|
|
let retry = Retry::new(&addr, move |addr| {
|
2021-09-22 06:42:14 +00:00
|
|
|
let _ = ws_controller.write().connect(addr.to_owned());
|
2021-09-19 08:11:02 +00:00
|
|
|
});
|
|
|
|
|
2021-09-22 06:42:14 +00:00
|
|
|
let _ = self.ws_controller.write().connect_with_retry(addr, retry)?;
|
2021-09-17 11:03:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-08-31 15:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:23:23 +00:00
|
|
|
pub async fn update_user(
|
|
|
|
_server: Server,
|
|
|
|
pool: Arc<ConnectionPool>,
|
|
|
|
params: UpdateUserParams,
|
|
|
|
) -> Result<(), UserError> {
|
2021-08-31 15:01:46 +00:00
|
|
|
let changeset = UserTableChangeset::new(params);
|
|
|
|
let conn = pool.get()?;
|
2021-09-07 09:12:03 +00:00
|
|
|
diesel_update_table!(user_table, changeset, &*conn);
|
2021-08-31 15:01:46 +00:00
|
|
|
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 {
|
2021-09-11 12:09:46 +00:00
|
|
|
fn get_connection(&self) -> Result<DBConnection, String> { self.db_connection().map_err(|e| format!("{:?}", e)) }
|
2021-07-13 15:08:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 03:21:42 +00:00
|
|
|
const SESSION_CACHE_KEY: &str = "session_cache_key";
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
|
|
|
struct Session {
|
|
|
|
user_id: String,
|
|
|
|
token: String,
|
2021-09-03 08:43:03 +00:00
|
|
|
email: String,
|
2021-09-01 03:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Session {
|
2021-09-03 08:43:03 +00:00
|
|
|
pub fn new(user_id: &str, token: &str, email: &str) -> Self {
|
2021-09-01 03:21:42 +00:00
|
|
|
Self {
|
|
|
|
user_id: user_id.to_owned(),
|
|
|
|
token: token.to_owned(),
|
2021-09-03 08:43:03 +00:00
|
|
|
email: email.to_owned(),
|
2021-09-01 03:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-07 15:30:43 +00:00
|
|
|
|
2021-09-08 05:50:20 +00:00
|
|
|
pub fn into_part(self) -> (String, String) { (self.user_id, self.token) }
|
2021-09-01 03:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<String> for Session {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
match serde_json::from_str(&s) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
2021-09-01 08:08:32 +00:00
|
|
|
log::error!("Deserialize string to Session failed: {:?}", e);
|
2021-09-01 03:21:42 +00:00
|
|
|
Session::default()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::Into<String> for Session {
|
|
|
|
fn into(self) -> String {
|
|
|
|
match serde_json::to_string(&self) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
2021-09-01 08:08:32 +00:00
|
|
|
log::error!("Serialize session to string failed: {:?}", e);
|
2021-09-01 03:21:42 +00:00
|
|
|
"".to_string()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|