2025-04-16 21:26:09 +08:00
|
|
|
use crate::AppFlowyCoreConfig;
|
|
|
|
use af_plugin::manager::PluginManager;
|
2025-04-18 15:31:50 +08:00
|
|
|
use arc_swap::{ArcSwap, ArcSwapOption};
|
2024-08-18 05:16:42 +02:00
|
|
|
use dashmap::DashMap;
|
2025-04-16 21:26:09 +08:00
|
|
|
use flowy_ai::local_ai::controller::LocalAIController;
|
2023-10-02 17:22:22 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2025-04-18 15:31:50 +08:00
|
|
|
use flowy_server::af_cloud::{
|
|
|
|
define::{AIUserServiceImpl, LoginUserService},
|
|
|
|
AppFlowyCloudServer,
|
|
|
|
};
|
2025-04-16 20:59:25 +08:00
|
|
|
use flowy_server::local_server::LocalServer;
|
2023-08-17 23:46:39 +08:00
|
|
|
use flowy_server::{AppFlowyEncryption, AppFlowyServer, EncryptionImpl};
|
2024-01-11 14:42:03 +08:00
|
|
|
use flowy_server_pub::AuthenticatorType;
|
2024-06-30 17:38:39 +08:00
|
|
|
use flowy_sqlite::kv::KVStorePreferences;
|
2024-01-11 14:42:03 +08:00
|
|
|
use flowy_user_pub::entities::*;
|
2025-04-18 15:31:50 +08:00
|
|
|
use serde_repr::{Deserialize_repr, Serialize_repr};
|
|
|
|
use std::fmt::{Display, Formatter, Result as FmtResult};
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2025-04-16 21:26:09 +08:00
|
|
|
use std::sync::{Arc, Weak};
|
2023-07-14 13:37:13 +08:00
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
/// ServerType: local or cloud
|
2023-05-23 23:55:21 +08:00
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
|
|
|
|
#[repr(u8)]
|
2025-04-18 15:31:50 +08:00
|
|
|
pub enum ServerType {
|
2023-05-23 23:55:21 +08:00
|
|
|
Local = 0,
|
2023-12-26 02:03:42 +08:00
|
|
|
AppFlowyCloud = 1,
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
impl ServerType {
|
2024-01-04 08:02:12 +08:00
|
|
|
pub fn is_local(&self) -> bool {
|
2025-04-18 15:31:50 +08:00
|
|
|
matches!(self, Self::Local)
|
2024-01-04 08:02:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
impl Display for ServerType {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Conversion between AuthType and ServerType
|
|
|
|
impl From<&AuthType> for ServerType {
|
|
|
|
fn from(a: &AuthType) -> Self {
|
|
|
|
match a {
|
|
|
|
AuthType::Local => ServerType::Local,
|
|
|
|
AuthType::AppFlowyCloud => ServerType::AppFlowyCloud,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<ServerType> for AuthType {
|
|
|
|
fn from(s: ServerType) -> Self {
|
|
|
|
match s {
|
|
|
|
ServerType::Local => AuthType::Local,
|
|
|
|
ServerType::AppFlowyCloud => AuthType::AppFlowyCloud,
|
2023-08-07 22:24:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
pub struct ServerProvider {
|
2023-07-14 13:37:13 +08:00
|
|
|
config: AppFlowyCoreConfig,
|
2025-04-18 15:31:50 +08:00
|
|
|
providers: DashMap<ServerType, Arc<dyn AppFlowyServer>>,
|
|
|
|
auth_type: ArcSwap<AuthType>,
|
2025-04-18 13:20:05 +08:00
|
|
|
user: Arc<dyn LoginUserService>,
|
2025-04-17 11:11:54 +08:00
|
|
|
pub local_ai: Arc<LocalAIController>,
|
2025-04-18 15:31:50 +08:00
|
|
|
pub uid: Arc<ArcSwapOption<i64>>,
|
|
|
|
pub user_enable_sync: Arc<AtomicBool>,
|
|
|
|
pub encryption: Arc<dyn AppFlowyEncryption>,
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
impl ServerProvider {
|
2023-08-06 11:51:03 +08:00
|
|
|
pub fn new(
|
|
|
|
config: AppFlowyCoreConfig,
|
2025-04-18 15:31:50 +08:00
|
|
|
initial: ServerType,
|
2024-06-30 17:38:39 +08:00
|
|
|
store_preferences: Weak<KVStorePreferences>,
|
2025-04-18 15:31:50 +08:00
|
|
|
user_service: impl LoginUserService + 'static,
|
2023-08-06 11:51:03 +08:00
|
|
|
) -> Self {
|
2025-04-18 15:31:50 +08:00
|
|
|
let user = Arc::new(user_service);
|
|
|
|
let initial_auth = AuthType::from(initial);
|
|
|
|
let auth_type = ArcSwap::from(Arc::new(initial_auth));
|
|
|
|
let encryption = Arc::new(EncryptionImpl::new(None)) as Arc<dyn AppFlowyEncryption>;
|
|
|
|
let ai_user = Arc::new(AIUserServiceImpl(user.clone()));
|
|
|
|
let plugins = Arc::new(PluginManager::new());
|
2025-04-16 21:26:09 +08:00
|
|
|
let local_ai = Arc::new(LocalAIController::new(
|
2025-04-18 15:31:50 +08:00
|
|
|
plugins,
|
|
|
|
store_preferences,
|
|
|
|
ai_user.clone(),
|
2025-04-16 21:26:09 +08:00
|
|
|
));
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
ServerProvider {
|
2023-07-14 13:37:13 +08:00
|
|
|
config,
|
2024-08-18 05:16:42 +02:00
|
|
|
providers: DashMap::new(),
|
2025-04-18 15:31:50 +08:00
|
|
|
encryption,
|
|
|
|
user_enable_sync: Arc::new(AtomicBool::new(true)),
|
|
|
|
auth_type,
|
2024-04-26 09:44:07 +08:00
|
|
|
user,
|
2025-04-18 15:31:50 +08:00
|
|
|
uid: Default::default(),
|
2025-04-17 11:11:54 +08:00
|
|
|
local_ai,
|
2023-07-14 13:37:13 +08:00
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
/// Reads current type
|
|
|
|
pub fn get_server_type(&self) -> ServerType {
|
|
|
|
let auth_type = self.auth_type.load_full();
|
|
|
|
ServerType::from(auth_type.as_ref())
|
2023-09-17 17:14:34 +08:00
|
|
|
}
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
pub fn set_auth_type(&self, a: AuthType) {
|
|
|
|
let old_type = self.get_server_type();
|
|
|
|
self.auth_type.store(Arc::new(a));
|
|
|
|
let new_type = self.get_server_type();
|
|
|
|
if old_type != new_type {
|
|
|
|
self.providers.remove(&old_type);
|
2023-10-24 23:13:51 +08:00
|
|
|
}
|
2023-12-30 13:44:09 +08:00
|
|
|
}
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
pub fn get_auth_type(&self) -> AuthType {
|
|
|
|
*self.auth_type.load_full().as_ref()
|
2023-12-27 11:42:39 +08:00
|
|
|
}
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
/// Lazily create or fetch an AppFlowyServer instance
|
2024-01-04 08:02:12 +08:00
|
|
|
pub fn get_server(&self) -> FlowyResult<Arc<dyn AppFlowyServer>> {
|
2025-04-18 15:31:50 +08:00
|
|
|
let key = self.get_server_type();
|
|
|
|
if let Some(entry) = self.providers.get(&key) {
|
|
|
|
return Ok(entry.clone());
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
let server: Arc<dyn AppFlowyServer> = match key {
|
|
|
|
ServerType::Local => Arc::new(LocalServer::new(self.user.clone(), self.local_ai.clone())),
|
|
|
|
ServerType::AppFlowyCloud => {
|
|
|
|
let cfg = self
|
|
|
|
.config
|
|
|
|
.cloud_config
|
|
|
|
.clone()
|
|
|
|
.ok_or_else(|| FlowyError::internal().with_context("Missing cloud config"))?;
|
|
|
|
Arc::new(AppFlowyCloudServer::new(
|
|
|
|
cfg,
|
2024-08-18 05:16:42 +02:00
|
|
|
self.user_enable_sync.load(Ordering::Acquire),
|
2023-11-20 20:54:47 +08:00
|
|
|
self.config.device_id.clone(),
|
2024-05-22 14:24:11 +08:00
|
|
|
self.config.app_version.clone(),
|
2024-04-26 09:44:07 +08:00
|
|
|
self.user.clone(),
|
2025-04-18 15:31:50 +08:00
|
|
|
))
|
2023-07-14 13:37:13 +08:00
|
|
|
},
|
2025-04-18 15:31:50 +08:00
|
|
|
};
|
2023-07-14 13:37:13 +08:00
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
self.providers.insert(key.clone(), server.clone());
|
2023-05-23 23:55:21 +08:00
|
|
|
Ok(server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-18 15:31:50 +08:00
|
|
|
/// Determine current server type from ENV
|
|
|
|
pub fn current_server_type() -> ServerType {
|
2023-12-29 15:03:24 +08:00
|
|
|
match AuthenticatorType::from_env() {
|
2025-04-18 15:31:50 +08:00
|
|
|
AuthenticatorType::Local => ServerType::Local,
|
|
|
|
AuthenticatorType::AppFlowyCloud => ServerType::AppFlowyCloud,
|
2023-12-29 15:03:24 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|