2024-08-18 05:16:42 +02:00
|
|
|
use arc_swap::ArcSwapOption;
|
|
|
|
use dashmap::DashMap;
|
2023-08-07 22:24:04 +08:00
|
|
|
use std::fmt::{Display, Formatter};
|
2024-08-18 05:16:42 +02:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
2023-08-06 11:51:03 +08:00
|
|
|
use std::sync::{Arc, Weak};
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-07-05 20:57:09 +08:00
|
|
|
use serde_repr::*;
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-10-02 17:22:22 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2024-04-26 09:44:07 +08:00
|
|
|
use flowy_server::af_cloud::define::ServerUser;
|
2023-11-20 20:54:47 +08:00
|
|
|
use flowy_server::af_cloud::AppFlowyCloudServer;
|
2023-07-29 09:46:24 +08:00
|
|
|
use flowy_server::local_server::{LocalServer, LocalServerDB};
|
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::af_cloud_config::AFCloudConfiguration;
|
|
|
|
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::*;
|
2023-05-23 23:55:21 +08:00
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
use crate::AppFlowyCoreConfig;
|
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
|
|
|
|
#[repr(u8)]
|
2023-12-26 02:03:42 +08:00
|
|
|
pub enum Server {
|
2023-05-23 23:55:21 +08:00
|
|
|
/// Local server provider.
|
|
|
|
/// Offline mode, no user authentication and the data is stored locally.
|
|
|
|
Local = 0,
|
2023-10-02 17:22:22 +08:00
|
|
|
/// AppFlowy Cloud server provider.
|
2024-08-18 05:16:42 +02:00
|
|
|
/// See: https://github.com/AppFlowy-IO/AppFlowy-Cloud
|
2023-12-26 02:03:42 +08:00
|
|
|
AppFlowyCloud = 1,
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
|
2024-01-04 08:02:12 +08:00
|
|
|
impl Server {
|
|
|
|
pub fn is_local(&self) -> bool {
|
|
|
|
matches!(self, Server::Local)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 02:03:42 +08:00
|
|
|
impl Display for Server {
|
2023-08-07 22:24:04 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2023-12-26 02:03:42 +08:00
|
|
|
Server::Local => write!(f, "Local"),
|
|
|
|
Server::AppFlowyCloud => write!(f, "AppFlowyCloud"),
|
2023-08-07 22:24:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:00:07 +08:00
|
|
|
/// The [ServerProvider] provides list of [AppFlowyServer] base on the [Authenticator]. Using
|
2023-09-17 17:14:34 +08:00
|
|
|
/// the auth type, the [ServerProvider] will create a new [AppFlowyServer] if it doesn't
|
2023-05-21 18:53:59 +08:00
|
|
|
/// exist.
|
2023-08-24 14:00:34 +08:00
|
|
|
/// Each server implements the [AppFlowyServer] trait, which provides the [UserCloudService], etc.
|
2023-09-17 17:14:34 +08:00
|
|
|
pub struct ServerProvider {
|
2023-07-14 13:37:13 +08:00
|
|
|
config: AppFlowyCoreConfig,
|
2024-08-18 05:16:42 +02:00
|
|
|
providers: DashMap<Server, Arc<dyn AppFlowyServer>>,
|
|
|
|
pub(crate) encryption: Arc<dyn AppFlowyEncryption>,
|
2023-12-29 15:03:24 +08:00
|
|
|
#[allow(dead_code)]
|
2024-06-30 17:38:39 +08:00
|
|
|
pub(crate) store_preferences: Weak<KVStorePreferences>,
|
2024-08-18 05:16:42 +02:00
|
|
|
pub(crate) user_enable_sync: AtomicBool,
|
2023-12-30 07:05:26 +08:00
|
|
|
|
|
|
|
/// The authenticator type of the user.
|
2024-08-18 05:16:42 +02:00
|
|
|
authenticator: AtomicU8,
|
2024-04-26 09:44:07 +08:00
|
|
|
user: Arc<dyn ServerUser>,
|
2024-08-18 05:16:42 +02:00
|
|
|
pub(crate) uid: Arc<ArcSwapOption<i64>>,
|
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,
|
2023-12-26 02:03:42 +08:00
|
|
|
server: Server,
|
2024-06-30 17:38:39 +08:00
|
|
|
store_preferences: Weak<KVStorePreferences>,
|
2024-04-26 09:44:07 +08:00
|
|
|
server_user: impl ServerUser + 'static,
|
2023-08-06 11:51:03 +08:00
|
|
|
) -> Self {
|
2024-04-26 09:44:07 +08:00
|
|
|
let user = Arc::new(server_user);
|
2023-08-18 22:32:51 +08:00
|
|
|
let encryption = EncryptionImpl::new(None);
|
2023-07-14 13:37:13 +08:00
|
|
|
Self {
|
|
|
|
config,
|
2024-08-18 05:16:42 +02:00
|
|
|
providers: DashMap::new(),
|
|
|
|
user_enable_sync: AtomicBool::new(true),
|
|
|
|
authenticator: AtomicU8::new(Authenticator::from(server) as u8),
|
|
|
|
encryption: Arc::new(encryption),
|
2023-08-06 11:51:03 +08:00
|
|
|
store_preferences,
|
2023-09-01 22:27:29 +08:00
|
|
|
uid: Default::default(),
|
2024-04-26 09:44:07 +08:00
|
|
|
user,
|
2023-07-14 13:37:13 +08:00
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
2023-12-26 02:03:42 +08:00
|
|
|
pub fn get_server_type(&self) -> Server {
|
2024-08-18 05:16:42 +02:00
|
|
|
match Authenticator::from(self.authenticator.load(Ordering::Acquire) as i32) {
|
2024-01-04 08:02:12 +08:00
|
|
|
Authenticator::Local => Server::Local,
|
|
|
|
Authenticator::AppFlowyCloud => Server::AppFlowyCloud,
|
|
|
|
}
|
2023-09-17 17:14:34 +08:00
|
|
|
}
|
|
|
|
|
2024-01-04 08:02:12 +08:00
|
|
|
pub fn set_authenticator(&self, authenticator: Authenticator) {
|
|
|
|
let old_server_type = self.get_server_type();
|
2024-08-18 05:16:42 +02:00
|
|
|
self
|
|
|
|
.authenticator
|
|
|
|
.store(authenticator as u8, Ordering::Release);
|
2024-01-04 08:02:12 +08:00
|
|
|
let new_server_type = self.get_server_type();
|
|
|
|
|
|
|
|
if old_server_type != new_server_type {
|
2024-08-18 05:16:42 +02:00
|
|
|
self.providers.remove(&old_server_type);
|
2023-10-24 23:13:51 +08:00
|
|
|
}
|
2023-12-30 13:44:09 +08:00
|
|
|
}
|
|
|
|
|
2024-01-04 08:02:12 +08:00
|
|
|
pub fn get_authenticator(&self) -> Authenticator {
|
2024-08-18 05:16:42 +02:00
|
|
|
Authenticator::from(self.authenticator.load(Ordering::Acquire) as i32)
|
2023-12-27 11:42:39 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
/// Returns a [AppFlowyServer] trait implementation base on the provider_type.
|
2024-01-04 08:02:12 +08:00
|
|
|
pub fn get_server(&self) -> FlowyResult<Arc<dyn AppFlowyServer>> {
|
|
|
|
let server_type = self.get_server_type();
|
|
|
|
|
2024-08-18 05:16:42 +02:00
|
|
|
if let Some(provider) = self.providers.get(&server_type) {
|
|
|
|
return Ok(provider.value().clone());
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
let server = match server_type {
|
2023-12-26 02:03:42 +08:00
|
|
|
Server::Local => {
|
2023-07-29 09:46:24 +08:00
|
|
|
let local_db = Arc::new(LocalServerDBImpl {
|
|
|
|
storage_path: self.config.storage_path.clone(),
|
|
|
|
});
|
|
|
|
let server = Arc::new(LocalServer::new(local_db));
|
2023-07-14 13:37:13 +08:00
|
|
|
Ok::<Arc<dyn AppFlowyServer>, FlowyError>(server)
|
|
|
|
},
|
2023-12-26 02:03:42 +08:00
|
|
|
Server::AppFlowyCloud => {
|
2023-10-02 17:22:22 +08:00
|
|
|
let config = AFCloudConfiguration::from_env()?;
|
2023-11-20 20:54:47 +08:00
|
|
|
let server = Arc::new(AppFlowyCloudServer::new(
|
2023-09-17 17:14:34 +08:00
|
|
|
config,
|
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(),
|
2023-09-17 17:14:34 +08:00
|
|
|
));
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
Ok::<Arc<dyn AppFlowyServer>, FlowyError>(server)
|
|
|
|
},
|
|
|
|
}?;
|
|
|
|
|
2024-08-18 05:16:42 +02:00
|
|
|
self.providers.insert(server_type.clone(), server.clone());
|
2023-05-23 23:55:21 +08:00
|
|
|
Ok(server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 02:03:42 +08:00
|
|
|
impl From<Authenticator> for Server {
|
2023-11-12 18:00:07 +08:00
|
|
|
fn from(auth_provider: Authenticator) -> Self {
|
2023-05-23 23:55:21 +08:00
|
|
|
match auth_provider {
|
2023-12-26 02:03:42 +08:00
|
|
|
Authenticator::Local => Server::Local,
|
|
|
|
Authenticator::AppFlowyCloud => Server::AppFlowyCloud,
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 02:03:42 +08:00
|
|
|
impl From<Server> for Authenticator {
|
|
|
|
fn from(ty: Server) -> Self {
|
2023-11-12 18:00:07 +08:00
|
|
|
match ty {
|
2023-12-26 02:03:42 +08:00
|
|
|
Server::Local => Authenticator::Local,
|
|
|
|
Server::AppFlowyCloud => Authenticator::AppFlowyCloud,
|
2023-11-12 18:00:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-26 02:03:42 +08:00
|
|
|
impl From<&Authenticator> for Server {
|
2023-11-12 18:00:07 +08:00
|
|
|
fn from(auth_provider: &Authenticator) -> Self {
|
2023-05-23 23:55:21 +08:00
|
|
|
Self::from(auth_provider.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 15:03:24 +08:00
|
|
|
pub fn current_server_type() -> Server {
|
|
|
|
match AuthenticatorType::from_env() {
|
|
|
|
AuthenticatorType::Local => Server::Local,
|
|
|
|
AuthenticatorType::AppFlowyCloud => Server::AppFlowyCloud,
|
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
2023-07-29 09:46:24 +08:00
|
|
|
|
|
|
|
struct LocalServerDBImpl {
|
2023-12-27 11:42:39 +08:00
|
|
|
#[allow(dead_code)]
|
2023-07-29 09:46:24 +08:00
|
|
|
storage_path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalServerDB for LocalServerDBImpl {
|
2023-12-27 11:42:39 +08:00
|
|
|
fn get_user_profile(&self, _uid: i64) -> Result<UserProfile, FlowyError> {
|
|
|
|
Err(
|
|
|
|
FlowyError::local_version_not_support()
|
|
|
|
.with_context("LocalServer doesn't support get_user_profile"),
|
|
|
|
)
|
2023-07-29 09:46:24 +08:00
|
|
|
}
|
|
|
|
|
2023-12-27 11:42:39 +08:00
|
|
|
fn get_user_workspace(&self, _uid: i64) -> Result<Option<UserWorkspace>, FlowyError> {
|
|
|
|
Err(
|
|
|
|
FlowyError::local_version_not_support()
|
|
|
|
.with_context("LocalServer doesn't support get_user_workspace"),
|
|
|
|
)
|
2023-07-29 09:46:24 +08:00
|
|
|
}
|
|
|
|
}
|