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};
|
2025-04-19 14:00:51 +08:00
|
|
|
use dashmap::mapref::one::Ref;
|
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::{
|
2025-04-19 14:00:51 +08:00
|
|
|
define::{AIUserServiceImpl, LoggedUser},
|
2025-04-18 15:31:50 +08:00
|
|
|
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-19 14:00:51 +08:00
|
|
|
use std::ops::Deref;
|
2025-04-18 15:31:50 +08:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2025-04-16 21:26:09 +08:00
|
|
|
use std::sync::{Arc, Weak};
|
2025-04-19 14:00:51 +08:00
|
|
|
use tracing::info;
|
2023-07-14 13:37:13 +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:48:17 +08:00
|
|
|
providers: DashMap<AuthType, Arc<dyn AppFlowyServer>>,
|
2025-04-18 15:31:50 +08:00
|
|
|
auth_type: ArcSwap<AuthType>,
|
2025-04-19 14:00:51 +08:00
|
|
|
logged_user: Arc<dyn LoggedUser>,
|
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
|
|
|
}
|
|
|
|
|
2025-04-19 14:00:51 +08:00
|
|
|
// Our little guard wrapper:
|
|
|
|
pub struct ServerHandle<'a>(Ref<'a, AuthType, Arc<dyn AppFlowyServer>>);
|
|
|
|
|
|
|
|
impl<'a> Deref for ServerHandle<'a> {
|
|
|
|
type Target = dyn AppFlowyServer;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
// `self.0.value()` is an `&Arc<dyn AppFlowyServer>`
|
|
|
|
// so `&**` gives us a `&dyn AppFlowyServer`
|
|
|
|
&**self.0.value()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine current server type from ENV
|
|
|
|
pub fn current_server_type() -> AuthType {
|
|
|
|
match AuthenticatorType::from_env() {
|
|
|
|
AuthenticatorType::Local => AuthType::Local,
|
|
|
|
AuthenticatorType::AppFlowyCloud => AuthType::AppFlowyCloud,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
impl ServerProvider {
|
2023-08-06 11:51:03 +08:00
|
|
|
pub fn new(
|
|
|
|
config: AppFlowyCoreConfig,
|
2024-06-30 17:38:39 +08:00
|
|
|
store_preferences: Weak<KVStorePreferences>,
|
2025-04-19 14:00:51 +08:00
|
|
|
user_service: impl LoggedUser + 'static,
|
2023-08-06 11:51:03 +08:00
|
|
|
) -> Self {
|
2025-04-19 14:00:51 +08:00
|
|
|
let initial_auth = current_server_type();
|
|
|
|
let logged_user = Arc::new(user_service) as Arc<dyn LoggedUser>;
|
2025-04-18 15:31:50 +08:00
|
|
|
let auth_type = ArcSwap::from(Arc::new(initial_auth));
|
|
|
|
let encryption = Arc::new(EncryptionImpl::new(None)) as Arc<dyn AppFlowyEncryption>;
|
2025-04-19 14:00:51 +08:00
|
|
|
let ai_user = Arc::new(AIUserServiceImpl(Arc::downgrade(&logged_user)));
|
2025-04-18 15:31:50 +08:00
|
|
|
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,
|
2025-04-19 14:00:51 +08:00
|
|
|
logged_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:48:17 +08:00
|
|
|
pub fn set_auth_type(&self, new_auth_type: AuthType) {
|
|
|
|
let old_type = self.get_auth_type();
|
2025-04-19 14:00:51 +08:00
|
|
|
info!(
|
|
|
|
"ServerProvider: set auth type from {:?} to {:?}",
|
|
|
|
old_type, new_auth_type
|
|
|
|
);
|
|
|
|
|
2025-04-18 15:48:17 +08:00
|
|
|
if old_type != new_auth_type {
|
|
|
|
self.auth_type.store(Arc::new(new_auth_type));
|
2025-04-19 14:00:51 +08:00
|
|
|
if let Some((auth_type, _)) = self.providers.remove(&old_type) {
|
|
|
|
info!("ServerProvider: remove old auth type: {:?}", auth_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
|
2025-04-19 14:00:51 +08:00
|
|
|
pub fn get_server(&self) -> FlowyResult<ServerHandle> {
|
2025-04-18 15:48:17 +08:00
|
|
|
let auth_type = self.get_auth_type();
|
2025-04-19 14:00:51 +08:00
|
|
|
if let Some(r) = self.providers.get(&auth_type) {
|
|
|
|
return Ok(ServerHandle(r));
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
|
2025-04-18 15:48:17 +08:00
|
|
|
let server: Arc<dyn AppFlowyServer> = match auth_type {
|
2025-04-19 14:00:51 +08:00
|
|
|
AuthType::Local => Arc::new(LocalServer::new(
|
|
|
|
self.logged_user.clone(),
|
|
|
|
self.local_ai.clone(),
|
|
|
|
)),
|
2025-04-18 15:48:17 +08:00
|
|
|
AuthType::AppFlowyCloud => {
|
2025-04-18 15:31:50 +08:00
|
|
|
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(),
|
2025-04-19 14:00:51 +08:00
|
|
|
Arc::downgrade(&self.logged_user),
|
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-19 14:00:51 +08:00
|
|
|
self.providers.insert(auth_type, server);
|
|
|
|
let guard = self.providers.get(&auth_type).unwrap();
|
|
|
|
Ok(ServerHandle(guard))
|
2023-12-29 15:03:24 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|