178 lines
3.4 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2022-07-04 14:53:35 +08:00
use std::convert::TryInto;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
feat: migrate user data to cloud (#3078) * refactor: weak passed-in params in handler * refactor: rename struct * chore: update tables * chore: update schema * chore: add permission * chore: update tables * chore: support transaction mode * chore: workspace database id * chore: add user workspace * feat: return list of workspaces * chore: add user to workspace * feat: separate database row table * refactor: update schema * chore: partition table * chore: use transaction * refactor: dir * refactor: collab db ref * fix: collab db lock * chore: rename files * chore: add tables descriptions * chore: update readme * docs: update documentation * chore: rename crate * chore: update ref * chore: update tests * chore: update tests * refactor: crate deps * chore: update crate ref * chore: remove unused deps * chore: remove unused deps * chore: update collab crate refs * chore: replace client with transaction in pooler * refactor: return error type * refactor: use anyhow error in deps * feat: supabase postgrest user signin (wip) * fix: Cargo.toml source git deps, changed Error to anyhow::Error * fix: uuid serialization * chore: fix conflict * chore: extend the response * feat: add implementation place holders * feat: impl get_user_workspaces * feat: impl get_user_profile * test: create workspace * fix: postgrest: field names and alias * chore: implement folder restful api * chore: implement collab storate with restful api * feat: added placeholders for impl: update_user_profile, check_user * feat: impl: update_user_profile * feat: impl: check_user * fix: use UidResponse, add more debug info for serde serialization error * fix: get_user_profile: use Optional<UserProfileResponse> * chore: imple init sync * chore: support soft delete * feat: postgresql: add migration test * feat: postgresql migration test: added UID display and colored output * feat: postgresql migration test: workspace role * feat: postgresql migration test: create shared common utils * feat: postgresql migration test: fixed shebang * chore: add flush_collab_update pg function * chore: implement datbaase and document restful api * chore: migrate to use restful api * chore: update table schema * chore: fix tests * chore: remove unused code * chore: format code * chore: remove unused env * fix: tauri build * fix: tauri build --------- Co-authored-by: Fu Zi Xiang <speed2exe@live.com.sg>
2023-07-29 09:46:24 +08:00
use flowy_user_deps::entities::*;
use crate::entities::parser::*;
use crate::errors::ErrorCode;
2021-08-31 17:25:08 +08:00
#[derive(ProtoBuf, Default)]
2022-07-19 14:40:56 +08:00
pub struct SignInPayloadPB {
#[pb(index = 1)]
pub email: String,
2021-08-31 17:25:08 +08:00
#[pb(index = 2)]
pub password: String,
2021-11-06 22:35:45 +08:00
#[pb(index = 3)]
pub name: String,
#[pb(index = 4)]
pub auth_type: AuthTypePB,
#[pb(index = 5)]
pub device_id: String,
2021-08-31 17:25:08 +08:00
}
2022-07-19 14:40:56 +08:00
impl TryInto<SignInParams> for SignInPayloadPB {
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,
auth_type: self.auth_type.into(),
device_id: self.device_id,
})
}
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 {
#[pb(index = 1)]
pub email: String,
2021-07-05 21:23:13 +08:00
#[pb(index = 2)]
pub name: String,
2021-07-05 21:23:13 +08:00
#[pb(index = 3)]
pub password: String,
#[pb(index = 4)]
pub auth_type: AuthTypePB,
#[pb(index = 5)]
pub device_id: String,
2021-07-05 21:23:13 +08:00
}
2022-07-19 14:40:56 +08:00
impl TryInto<SignUpParams> for SignUpPayloadPB {
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,
auth_type: self.auth_type.into(),
device_id: self.device_id,
})
}
2021-07-05 21:23:13 +08:00
}
#[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, Eq, PartialEq, Debug, Clone)]
pub enum AuthTypePB {
Local = 0,
SelfHosted = 1,
Supabase = 2,
}
impl Default for AuthTypePB {
fn default() -> Self {
Self::Local
}
}
#[derive(Debug, ProtoBuf, Default)]
pub struct UserCredentialsPB {
#[pb(index = 1, one_of)]
pub uid: Option<i64>,
#[pb(index = 2, one_of)]
pub uuid: Option<String>,
#[pb(index = 3, one_of)]
pub token: Option<String>,
}
impl UserCredentialsPB {
pub fn from_uid(uid: i64) -> Self {
Self {
uid: Some(uid),
uuid: None,
token: None,
}
}
pub fn from_token(token: &str) -> Self {
Self {
uid: None,
uuid: None,
token: Some(token.to_owned()),
}
}
pub fn from_uuid(uuid: &str) -> Self {
Self {
uid: None,
uuid: Some(uuid.to_owned()),
token: None,
}
}
}
impl From<UserCredentialsPB> for UserCredentials {
fn from(value: UserCredentialsPB) -> Self {
Self::new(value.token, value.uid, value.uuid)
}
}
#[derive(Default, ProtoBuf)]
pub struct UserStatePB {
#[pb(index = 1)]
pub auth_type: AuthTypePB,
}
#[derive(ProtoBuf, Debug, Default, Clone)]
pub struct AuthStateChangedPB {
#[pb(index = 1)]
pub state: AuthStatePB,
}
#[derive(ProtoBuf_Enum, Debug, Clone)]
pub enum AuthStatePB {
// adding AuthState prefix to avoid conflict with other enums
AuthStateUnknown = 0,
AuthStateSignIn = 1,
AuthStateSignOut = 2,
AuthStateForceSignOut = 3,
}
impl Default for AuthStatePB {
fn default() -> Self {
Self::AuthStateUnknown
}
}