125 lines
3.2 KiB
Rust
Raw Normal View History

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 std::fmt;
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use chrono::prelude::*;
use serde::de::{Deserializer, MapAccess, Visitor};
use serde::Deserialize;
use serde::Serialize;
use flowy_user_deps::entities::{SignInResponse, UserWorkspace};
#[derive(Debug, Clone, Serialize)]
pub struct Session {
pub user_id: i64,
pub user_workspace: UserWorkspace,
}
struct SessionVisitor;
impl<'de> Visitor<'de> for SessionVisitor {
type Value = Session;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Session")
}
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut user_id = None;
// For historical reasons, the session used to contain a workspace_id field.
// This field is no longer used, and is replaced by user_workspace.
let mut workspace_id = None;
let mut user_workspace = None;
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"user_id" => {
user_id = Some(map.next_value()?);
},
"workspace_id" => {
workspace_id = Some(map.next_value()?);
},
"user_workspace" => {
user_workspace = Some(map.next_value()?);
},
_ => {},
}
}
let user_id = user_id.ok_or(serde::de::Error::missing_field("user_id"))?;
if user_workspace.is_none() {
if let Some(workspace_id) = workspace_id {
user_workspace = Some(UserWorkspace {
id: workspace_id,
name: "My Workspace".to_string(),
created_at: Utc::now(),
// For historical reasons, the database_storage_id is constructed by the user_id.
database_storage_id: STANDARD.encode(format!("{}:user:database", user_id)),
})
}
}
let session = Session {
user_id,
user_workspace: user_workspace.ok_or(serde::de::Error::missing_field("user_workspace"))?,
};
Ok(session)
}
}
impl<'de> Deserialize<'de> for Session {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(SessionVisitor)
}
}
impl std::convert::From<SignInResponse> for Session {
fn from(resp: SignInResponse) -> Self {
Session {
user_id: resp.user_id,
user_workspace: resp.latest_workspace,
}
}
}
impl std::convert::From<Session> for String {
fn from(session: Session) -> Self {
match serde_json::to_string(&session) {
Ok(s) => s,
Err(e) => {
tracing::error!("Serialize session to string failed: {:?}", e);
"".to_string()
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(serde::Serialize)]
struct OldSession {
user_id: i64,
workspace_id: String,
}
#[test]
fn deserialize_user_workspace_from_workspace_id() {
// For historical reasons, the session used to contain a workspace_id field.
let old = OldSession {
user_id: 1,
workspace_id: uuid::Uuid::new_v4().to_string(),
};
let s = serde_json::to_string(&old).unwrap();
let new = serde_json::from_str::<Session>(&s).unwrap();
assert_eq!(old.user_id, new.user_id);
assert_eq!(old.workspace_id, new.user_workspace.id);
}
}