refactor: deps crates (#4362)

* refactor: rename flowy-folder-deps to flowy-folder-pub

* chore: rename crates

* chore: move flowy-task to lib-infra

* chore: rename crates

* refactor: user manager dir
This commit is contained in:
Nathan.fooo
2024-01-11 14:42:03 +08:00
committed by GitHub
parent dd8b9dd43e
commit 307556b7dd
133 changed files with 362 additions and 382 deletions

View File

@ -0,0 +1,245 @@
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use std::sync::Arc;
use anyhow::Error;
use collab::core::collab::CollabDocState;
use collab_entity::{CollabObject, CollabType};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio_stream::wrappers::WatchStream;
use uuid::Uuid;
use flowy_error::{ErrorCode, FlowyError};
use lib_infra::box_any::BoxAny;
use lib_infra::future::FutureResult;
use crate::entities::{
AuthResponse, Authenticator, Role, UpdateUserProfileParams, UserCredentials, UserProfile,
UserTokenState, UserWorkspace, WorkspaceMember,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserCloudConfig {
pub enable_sync: bool,
pub enable_encrypt: bool,
// The secret used to encrypt the user's data
pub encrypt_secret: String,
}
impl UserCloudConfig {
pub fn new(encrypt_secret: String) -> Self {
Self {
enable_sync: true,
enable_encrypt: false,
encrypt_secret,
}
}
pub fn with_enable_encrypt(mut self, enable_encrypt: bool) -> Self {
self.enable_encrypt = enable_encrypt;
// When the enable_encrypt is true, the encrypt_secret should not be empty
debug_assert!(!self.encrypt_secret.is_empty());
self
}
}
impl Display for UserCloudConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"enable_sync: {}, enable_encrypt: {}",
self.enable_sync, self.enable_encrypt
)
}
}
/// `UserCloudServiceProvider` defines a set of methods for managing user cloud services,
/// including token management, synchronization settings, network reachability, and authentication.
///
/// This trait is intended for implementation by providers that offer cloud-based services for users.
/// It includes methods for handling authentication tokens, enabling/disabling synchronization,
/// setting network reachability, managing encryption secrets, and accessing user-specific cloud services.
pub trait UserCloudServiceProvider: Send + Sync + 'static {
/// Sets the authentication token for the cloud service.
///
/// # Arguments
/// * `token`: A string slice representing the authentication token.
///
/// # Returns
/// A `Result` which is `Ok` if the token is successfully set, or a `FlowyError` otherwise.
fn set_token(&self, token: &str) -> Result<(), FlowyError>;
/// Subscribes to the state of the authentication token.
///
/// # Returns
/// An `Option` containing a `WatchStream<UserTokenState>` if available, or `None` otherwise.
/// The stream allows the caller to watch for changes in the token state.
fn subscribe_token_state(&self) -> Option<WatchStream<UserTokenState>>;
/// Sets the synchronization state for a user.
///
/// # Arguments
/// * `uid`: An i64 representing the user ID.
/// * `enable_sync`: A boolean indicating whether synchronization should be enabled or disabled.
fn set_enable_sync(&self, uid: i64, enable_sync: bool);
/// Sets the authenticator when user sign in or sign up.
///
/// # Arguments
/// * `authenticator`: An `Authenticator` object.
fn set_user_authenticator(&self, authenticator: &Authenticator);
fn get_user_authenticator(&self) -> Authenticator;
/// Sets the network reachability statset_user_authenticatorus.
///
/// # Arguments
/// * `reachable`: A boolean indicating whether the network is reachable.
fn set_network_reachable(&self, reachable: bool);
/// Sets the encryption secret for secure communication.
///
/// # Arguments
/// * `secret`: A `String` representing the encryption secret.
fn set_encrypt_secret(&self, secret: String);
/// Retrieves the user-specific cloud service.
///
/// # Returns
/// A `Result` containing an `Arc<dyn UserCloudService>` if successful, or a `FlowyError` otherwise.
fn get_user_service(&self) -> Result<Arc<dyn UserCloudService>, FlowyError>;
/// Retrieves the service URL.
///
/// # Returns
/// A `String` representing the service URL.
fn service_url(&self) -> String;
}
/// Provide the generic interface for the user cloud service
/// The user cloud service is responsible for the user authentication and user profile management
#[allow(unused_variables)]
pub trait UserCloudService: Send + Sync + 'static {
/// Sign up a new account.
/// The type of the params is defined the this trait's implementation.
/// Use the `unbox_or_error` of the [BoxAny] to get the params.
fn sign_up(&self, params: BoxAny) -> FutureResult<AuthResponse, FlowyError>;
/// Sign in an account
/// The type of the params is defined the this trait's implementation.
fn sign_in(&self, params: BoxAny) -> FutureResult<AuthResponse, FlowyError>;
/// Sign out an account
fn sign_out(&self, token: Option<String>) -> FutureResult<(), FlowyError>;
/// Generate a sign in url for the user with the given email
/// Currently, only use the admin client for testing
fn generate_sign_in_url_with_email(&self, email: &str) -> FutureResult<String, FlowyError>;
/// When the user opens the OAuth URL, it redirects to the corresponding provider's OAuth web page.
/// After the user is authenticated, the browser will open a deep link to the AppFlowy app (iOS, macOS, etc.),
/// which will call [Client::sign_in_with_url] to sign in.
///
/// For example, the OAuth URL on Google looks like `https://appflowy.io/authorize?provider=google`.
fn generate_oauth_url_with_provider(&self, provider: &str) -> FutureResult<String, FlowyError>;
/// Using the user's token to update the user information
fn update_user(
&self,
credential: UserCredentials,
params: UpdateUserProfileParams,
) -> FutureResult<(), FlowyError>;
/// Get the user information using the user's token or uid
/// return None if the user is not found
fn get_user_profile(&self, credential: UserCredentials) -> FutureResult<UserProfile, FlowyError>;
fn open_workspace(&self, workspace_id: &str) -> FutureResult<UserWorkspace, FlowyError>;
/// Return the all the workspaces of the user
fn get_all_workspace(&self, uid: i64) -> FutureResult<Vec<UserWorkspace>, FlowyError>;
fn add_workspace_member(
&self,
user_email: String,
workspace_id: String,
) -> FutureResult<(), Error> {
FutureResult::new(async { Ok(()) })
}
fn remove_workspace_member(
&self,
user_email: String,
workspace_id: String,
) -> FutureResult<(), Error> {
FutureResult::new(async { Ok(()) })
}
fn update_workspace_member(
&self,
user_email: String,
workspace_id: String,
role: Role,
) -> FutureResult<(), Error> {
FutureResult::new(async { Ok(()) })
}
fn get_workspace_members(
&self,
workspace_id: String,
) -> FutureResult<Vec<WorkspaceMember>, Error> {
FutureResult::new(async { Ok(vec![]) })
}
fn get_user_awareness_doc_state(&self, uid: i64) -> FutureResult<CollabDocState, Error>;
fn receive_realtime_event(&self, _json: Value) {}
fn subscribe_user_update(&self) -> Option<UserUpdateReceiver> {
None
}
fn reset_workspace(&self, collab_object: CollabObject) -> FutureResult<(), Error>;
fn create_collab_object(
&self,
collab_object: &CollabObject,
data: Vec<u8>,
override_if_exist: bool,
) -> FutureResult<(), FlowyError>;
fn batch_create_collab_object(
&self,
workspace_id: &str,
objects: Vec<UserCollabParams>,
) -> FutureResult<(), Error>;
}
pub type UserUpdateReceiver = tokio::sync::mpsc::Receiver<UserUpdate>;
pub type UserUpdateSender = tokio::sync::mpsc::Sender<UserUpdate>;
#[derive(Debug, Clone)]
pub struct UserUpdate {
pub uid: i64,
pub name: Option<String>,
pub email: Option<String>,
pub encryption_sign: String,
}
pub fn uuid_from_map(map: &HashMap<String, String>) -> Result<Uuid, Error> {
let uuid = map
.get("uuid")
.ok_or_else(|| FlowyError::new(ErrorCode::MissingAuthField, "Missing uuid field"))?
.as_str();
let uuid = Uuid::from_str(uuid)?;
Ok(uuid)
}
#[derive(Debug)]
pub struct UserCollabParams {
pub object_id: String,
pub encoded_collab: Vec<u8>,
pub collab_type: CollabType,
}

View File

@ -0,0 +1,393 @@
use std::str::FromStr;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_repr::*;
use uuid::Uuid;
pub const USER_METADATA_OPEN_AI_KEY: &str = "openai_key";
pub const USER_METADATA_STABILITY_AI_KEY: &str = "stability_ai_key";
pub const USER_METADATA_ICON_URL: &str = "icon_url";
pub const USER_METADATA_UPDATE_AT: &str = "updated_at";
pub trait UserAuthResponse {
fn user_id(&self) -> i64;
fn user_uuid(&self) -> &Uuid;
fn user_name(&self) -> &str;
fn latest_workspace(&self) -> &UserWorkspace;
fn user_workspaces(&self) -> &[UserWorkspace];
fn user_token(&self) -> Option<String>;
fn user_email(&self) -> Option<String>;
fn encryption_type(&self) -> EncryptionType;
fn metadata(&self) -> &Option<serde_json::Value>;
fn updated_at(&self) -> i64;
}
#[derive(Default, Serialize, Deserialize, Debug)]
pub struct SignInParams {
pub email: String,
pub password: String,
pub name: String,
pub auth_type: Authenticator,
}
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct SignUpParams {
pub email: String,
pub name: String,
pub password: String,
pub auth_type: Authenticator,
pub device_id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AuthResponse {
pub user_id: i64,
pub user_uuid: Uuid,
pub name: String,
pub latest_workspace: UserWorkspace,
pub user_workspaces: Vec<UserWorkspace>,
pub is_new_user: bool,
pub email: Option<String>,
pub token: Option<String>,
pub encryption_type: EncryptionType,
pub updated_at: i64,
pub metadata: Option<serde_json::Value>,
}
impl UserAuthResponse for AuthResponse {
fn user_id(&self) -> i64 {
self.user_id
}
fn user_uuid(&self) -> &Uuid {
&self.user_uuid
}
fn user_name(&self) -> &str {
&self.name
}
fn latest_workspace(&self) -> &UserWorkspace {
&self.latest_workspace
}
fn user_workspaces(&self) -> &[UserWorkspace] {
&self.user_workspaces
}
fn user_token(&self) -> Option<String> {
self.token.clone()
}
fn user_email(&self) -> Option<String> {
self.email.clone()
}
fn encryption_type(&self) -> EncryptionType {
self.encryption_type.clone()
}
fn metadata(&self) -> &Option<Value> {
&self.metadata
}
fn updated_at(&self) -> i64 {
self.updated_at
}
}
#[derive(Clone, Debug)]
pub struct UserCredentials {
/// Currently, the token is only used when the [Authenticator] is AFCloud
pub token: Option<String>,
/// The user id
pub uid: Option<i64>,
/// The user id
pub uuid: Option<String>,
}
impl UserCredentials {
pub fn from_uid(uid: i64) -> Self {
Self {
token: None,
uid: Some(uid),
uuid: None,
}
}
pub fn from_uuid(uuid: String) -> Self {
Self {
token: None,
uid: None,
uuid: Some(uuid),
}
}
pub fn new(token: Option<String>, uid: Option<i64>, uuid: Option<String>) -> Self {
Self { token, uid, uuid }
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserWorkspace {
pub id: String,
pub name: String,
pub created_at: DateTime<Utc>,
/// The database storage id is used indexing all the database views in current workspace.
#[serde(rename = "database_storage_id")]
pub database_view_tracker_id: String,
}
impl UserWorkspace {
pub fn new(workspace_id: &str, _uid: i64) -> Self {
Self {
id: workspace_id.to_string(),
name: "".to_string(),
created_at: Utc::now(),
database_view_tracker_id: Uuid::new_v4().to_string(),
}
}
}
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct UserProfile {
#[serde(rename = "id")]
pub uid: i64,
pub email: String,
pub name: String,
pub token: String,
pub icon_url: String,
pub openai_key: String,
pub stability_ai_key: String,
pub workspace_id: String,
pub authenticator: Authenticator,
// If the encryption_sign is not empty, which means the user has enabled the encryption.
pub encryption_type: EncryptionType,
pub updated_at: i64,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, Eq, PartialEq)]
pub enum EncryptionType {
#[default]
NoEncryption,
SelfEncryption(String),
}
impl EncryptionType {
pub fn from_sign(sign: &str) -> Self {
if sign.is_empty() {
EncryptionType::NoEncryption
} else {
EncryptionType::SelfEncryption(sign.to_owned())
}
}
pub fn require_encrypt_secret(&self) -> bool {
match self {
EncryptionType::NoEncryption => false,
EncryptionType::SelfEncryption(sign) => !sign.is_empty(),
}
}
pub fn sign(&self) -> String {
match self {
EncryptionType::NoEncryption => "".to_owned(),
EncryptionType::SelfEncryption(sign) => sign.to_owned(),
}
}
}
impl FromStr for EncryptionType {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
impl<T> From<(&T, &Authenticator)> for UserProfile
where
T: UserAuthResponse,
{
fn from(params: (&T, &Authenticator)) -> Self {
let (value, auth_type) = params;
let (icon_url, openai_key, stability_ai_key) = {
value
.metadata()
.as_ref()
.map(|m| {
(
m.get(USER_METADATA_ICON_URL)
.map(|v| v.as_str().map(|s| s.to_string()).unwrap_or_default())
.unwrap_or_default(),
m.get(USER_METADATA_OPEN_AI_KEY)
.map(|v| v.as_str().map(|s| s.to_string()).unwrap_or_default())
.unwrap_or_default(),
m.get(USER_METADATA_STABILITY_AI_KEY)
.map(|v| v.as_str().map(|s| s.to_string()).unwrap_or_default())
.unwrap_or_default(),
)
})
.unwrap_or_default()
};
Self {
uid: value.user_id(),
email: value.user_email().unwrap_or_default(),
name: value.user_name().to_owned(),
token: value.user_token().unwrap_or_default(),
icon_url,
openai_key,
workspace_id: value.latest_workspace().id.to_owned(),
authenticator: auth_type.clone(),
encryption_type: value.encryption_type(),
stability_ai_key,
updated_at: value.updated_at(),
}
}
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct UpdateUserProfileParams {
pub uid: i64,
pub name: Option<String>,
pub email: Option<String>,
pub password: Option<String>,
pub icon_url: Option<String>,
pub openai_key: Option<String>,
pub stability_ai_key: Option<String>,
pub encryption_sign: Option<String>,
pub token: Option<String>,
}
impl UpdateUserProfileParams {
pub fn new(uid: i64) -> Self {
Self {
uid,
..Default::default()
}
}
pub fn with_token(mut self, token: String) -> Self {
self.token = Some(token);
self
}
pub fn with_name<T: ToString>(mut self, name: T) -> Self {
self.name = Some(name.to_string());
self
}
pub fn with_email<T: ToString>(mut self, email: T) -> Self {
self.email = Some(email.to_string());
self
}
pub fn with_password<T: ToString>(mut self, password: T) -> Self {
self.password = Some(password.to_string());
self
}
pub fn with_icon_url<T: ToString>(mut self, icon_url: T) -> Self {
self.icon_url = Some(icon_url.to_string());
self
}
pub fn with_openai_key(mut self, openai_key: &str) -> Self {
self.openai_key = Some(openai_key.to_owned());
self
}
pub fn with_stability_ai_key(mut self, stability_ai_key: &str) -> Self {
self.stability_ai_key = Some(stability_ai_key.to_owned());
self
}
pub fn with_encryption_type(mut self, encryption_type: EncryptionType) -> Self {
let sign = match encryption_type {
EncryptionType::NoEncryption => "".to_string(),
EncryptionType::SelfEncryption(sign) => sign,
};
self.encryption_sign = Some(sign);
self
}
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()
&& self.encryption_sign.is_none()
&& self.stability_ai_key.is_none()
}
}
#[derive(Debug, Clone, Hash, Serialize_repr, Deserialize_repr, Eq, PartialEq)]
#[repr(u8)]
pub enum Authenticator {
/// It's a local server, we do fake sign in default.
Local = 0,
/// Currently not supported. It will be supported in the future when the
/// [AppFlowy-Server](https://github.com/AppFlowy-IO/AppFlowy-Server) ready.
AppFlowyCloud = 1,
/// It uses Supabase as the backend.
Supabase = 2,
}
impl Default for Authenticator {
fn default() -> Self {
Self::Local
}
}
impl Authenticator {
pub fn is_local(&self) -> bool {
matches!(self, Authenticator::Local)
}
pub fn is_appflowy_cloud(&self) -> bool {
matches!(self, Authenticator::AppFlowyCloud)
}
}
impl From<i32> for Authenticator {
fn from(value: i32) -> Self {
match value {
0 => Authenticator::Local,
1 => Authenticator::AppFlowyCloud,
2 => Authenticator::Supabase,
_ => Authenticator::Local,
}
}
}
pub struct SupabaseOAuthParams {
pub uuid: Uuid,
pub email: String,
}
pub struct AFCloudOAuthParams {
pub sign_in_url: String,
}
#[derive(Clone, Debug)]
pub enum UserTokenState {
Refresh { token: String },
Invalid,
}
#[derive(Clone, Debug)]
pub enum Role {
Owner,
Member,
Guest,
}
pub struct WorkspaceMember {
pub email: String,
pub role: Role,
pub name: String,
}

View File

@ -0,0 +1,4 @@
pub mod cloud;
pub mod entities;
pub const DEFAULT_USER_NAME: fn() -> String = || "Me".to_string();