2023-05-21 18:53:59 +08:00
|
|
|
use std::collections::HashMap;
|
2022-07-04 14:53:35 +08:00
|
|
|
use std::convert::TryInto;
|
2023-05-17 09:49:39 +08:00
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2023-05-21 18:53:59 +08:00
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
2023-05-17 09:49:39 +08:00
|
|
|
|
|
|
|
use crate::entities::parser::*;
|
|
|
|
use crate::errors::ErrorCode;
|
2023-05-21 18:53:59 +08:00
|
|
|
use crate::services::AuthType;
|
2021-11-07 21:45:18 +08:00
|
|
|
|
2021-08-31 17:25:08 +08:00
|
|
|
#[derive(ProtoBuf, Default)]
|
2022-07-19 14:40:56 +08:00
|
|
|
pub struct SignInPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 1)]
|
|
|
|
pub email: String,
|
2021-08-31 17:25:08 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 2)]
|
|
|
|
pub password: String,
|
2021-11-06 22:35:45 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 3)]
|
|
|
|
pub name: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
pub auth_type: AuthTypePB,
|
2021-08-31 17:25:08 +08:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:40:56 +08:00
|
|
|
impl TryInto<SignInParams> for SignInPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<SignInParams, Self::Error> {
|
|
|
|
let email = UserEmail::parse(self.email)?;
|
|
|
|
let password = UserPassword::parse(self.password)?;
|
|
|
|
|
|
|
|
Ok(SignInParams {
|
|
|
|
email: email.0,
|
|
|
|
password: password.0,
|
|
|
|
name: self.name,
|
2023-05-21 18:53:59 +08:00
|
|
|
auth_type: self.auth_type.into(),
|
2023-02-13 09:29:49 +08:00
|
|
|
})
|
|
|
|
}
|
2021-08-31 17:25:08 +08:00
|
|
|
}
|
|
|
|
|
2021-07-05 21:23:13 +08:00
|
|
|
#[derive(ProtoBuf, Default)]
|
2022-07-19 14:40:56 +08:00
|
|
|
pub struct SignUpPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 1)]
|
|
|
|
pub email: String,
|
2021-07-05 21:23:13 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 2)]
|
|
|
|
pub name: String,
|
2021-07-05 21:23:13 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 3)]
|
|
|
|
pub password: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
pub auth_type: AuthTypePB,
|
2021-07-05 21:23:13 +08:00
|
|
|
}
|
2022-07-19 14:40:56 +08:00
|
|
|
impl TryInto<SignUpParams> for SignUpPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<SignUpParams, Self::Error> {
|
|
|
|
let email = UserEmail::parse(self.email)?;
|
|
|
|
let password = UserPassword::parse(self.password)?;
|
|
|
|
let name = UserName::parse(self.name)?;
|
|
|
|
|
|
|
|
Ok(SignUpParams {
|
|
|
|
email: email.0,
|
|
|
|
name: name.0,
|
|
|
|
password: password.0,
|
2023-05-21 18:53:59 +08:00
|
|
|
auth_type: self.auth_type.into(),
|
2023-02-13 09:29:49 +08:00
|
|
|
})
|
|
|
|
}
|
2021-07-05 21:23:13 +08:00
|
|
|
}
|
2023-05-17 09:49:39 +08:00
|
|
|
|
|
|
|
#[derive(Default, Serialize, Deserialize, Debug)]
|
|
|
|
pub struct SignInParams {
|
|
|
|
pub email: String,
|
|
|
|
pub password: String,
|
|
|
|
pub name: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
pub auth_type: AuthType,
|
2023-05-17 09:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct SignInResponse {
|
|
|
|
pub user_id: i64,
|
|
|
|
pub name: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
pub workspace_id: String,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub token: Option<String>,
|
2023-05-17 09:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
|
|
|
pub struct SignUpParams {
|
|
|
|
pub email: String,
|
|
|
|
pub name: String,
|
|
|
|
pub password: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
pub auth_type: AuthType,
|
2023-05-17 09:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
|
|
|
pub struct SignUpResponse {
|
|
|
|
pub user_id: i64,
|
|
|
|
pub name: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
pub workspace_id: String,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub token: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(ProtoBuf, Default)]
|
|
|
|
pub struct ThirdPartyAuthPB {
|
|
|
|
/// Use this field to store the third party auth information.
|
|
|
|
/// Different auth type has different fields.
|
|
|
|
/// Supabase:
|
|
|
|
/// - map: { "uuid": "xxx" }
|
|
|
|
///
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub map: HashMap<String, String>,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub auth_type: AuthTypePB,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(ProtoBuf_Enum, Debug, Clone)]
|
|
|
|
pub enum AuthTypePB {
|
|
|
|
Local = 0,
|
|
|
|
SelfHosted = 1,
|
|
|
|
Supabase = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AuthTypePB {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Local
|
|
|
|
}
|
2023-05-17 09:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
|
|
|
|
pub struct UserProfile {
|
|
|
|
pub id: i64,
|
|
|
|
pub email: String,
|
|
|
|
pub name: String,
|
|
|
|
pub token: String,
|
|
|
|
pub icon_url: String,
|
|
|
|
pub openai_key: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
pub workspace_id: String,
|
2023-05-17 09:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
|
|
|
|
pub struct UpdateUserProfileParams {
|
|
|
|
pub id: i64,
|
2023-05-21 18:53:59 +08:00
|
|
|
pub auth_type: AuthType,
|
2023-05-17 09:49:39 +08:00
|
|
|
pub name: Option<String>,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub password: Option<String>,
|
|
|
|
pub icon_url: Option<String>,
|
|
|
|
pub openai_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateUserProfileParams {
|
|
|
|
pub fn name(mut self, name: &str) -> Self {
|
|
|
|
self.name = Some(name.to_owned());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn email(mut self, email: &str) -> Self {
|
|
|
|
self.email = Some(email.to_owned());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn password(mut self, password: &str) -> Self {
|
|
|
|
self.password = Some(password.to_owned());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn icon_url(mut self, icon_url: &str) -> Self {
|
|
|
|
self.icon_url = Some(icon_url.to_owned());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn openai_key(mut self, openai_key: &str) -> Self {
|
|
|
|
self.openai_key = Some(openai_key.to_owned());
|
|
|
|
self
|
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.name.is_none()
|
|
|
|
&& self.email.is_none()
|
|
|
|
&& self.password.is_none()
|
|
|
|
&& self.icon_url.is_none()
|
|
|
|
&& self.openai_key.is_none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(ProtoBuf, Default)]
|
|
|
|
pub struct SignOutPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub auth_type: AuthTypePB,
|
2023-05-17 09:49:39 +08:00
|
|
|
}
|