refactor: remove server type

This commit is contained in:
Nathan 2025-04-18 15:48:17 +08:00
parent 394ac85c32
commit 0906febe95
5 changed files with 49 additions and 94 deletions

View file

@ -1,4 +1,4 @@
use crate::server_layer::{ServerProvider, ServerType};
use crate::server_layer::ServerProvider;
use client_api::collab_sync::{SinkConfig, SyncObject, SyncPlugin};
use client_api::entity::ai_dto::RepeatedRelatedQuestion;
use client_api::entity::workspace_dto::PublishInfoView;
@ -188,8 +188,8 @@ impl UserCloudServiceProvider for ServerProvider {
/// When user login, the provider type is set by the [AuthType] and save to disk for next use.
///
/// Each [AuthType] has a corresponding [ServerType]. The [ServerType] is used
/// to create a new [AppFlowyServer] if it doesn't exist. Once the [ServerType] is set,
/// Each [AuthType] has a corresponding [AuthType]. The [AuthType] is used
/// to create a new [AppFlowyServer] if it doesn't exist. Once the [AuthType] is set,
/// it will be used when user open the app again.
///
fn set_auth_type(&self, auth_type: &AuthType) {
@ -211,7 +211,7 @@ impl UserCloudServiceProvider for ServerProvider {
self.encryption.set_secret(secret);
}
/// Returns the [UserCloudService] base on the current [ServerType].
/// Returns the [UserCloudService] base on the current [AuthType].
/// Creates a new [AppFlowyServer] if it doesn't exist.
fn get_user_service(&self) -> Result<Arc<dyn UserCloudService>, FlowyError> {
let user_service = self.get_server()?.user_service();
@ -219,9 +219,9 @@ impl UserCloudServiceProvider for ServerProvider {
}
fn service_url(&self) -> String {
match self.get_server_type() {
ServerType::Local => "".to_string(),
ServerType::AppFlowyCloud => AFCloudConfiguration::from_env()
match self.get_auth_type() {
AuthType::Local => "".to_string(),
AuthType::AppFlowyCloud => AFCloudConfiguration::from_env()
.map(|config| config.base_url)
.unwrap_or_default(),
}
@ -578,12 +578,15 @@ impl DocumentCloudService for ServerProvider {
impl CollabCloudPluginProvider for ServerProvider {
fn provider_type(&self) -> CollabPluginProviderType {
self.get_server_type().into()
match self.get_auth_type() {
AuthType::Local => CollabPluginProviderType::Local,
AuthType::AppFlowyCloud => CollabPluginProviderType::AppFlowyCloud,
}
}
fn get_plugins(&self, context: CollabPluginProviderContext) -> Vec<Box<dyn CollabPlugin>> {
// If the user is local, we don't need to create a sync plugin.
if self.get_server_type().is_local() {
if self.get_auth_type().is_local() {
debug!(
"User authenticator is local, skip create sync plugin for: {}",
context

View file

@ -1,6 +1,6 @@
#![allow(unused_doc_comments)]
use collab_integrate::collab_builder::{AppFlowyCollabBuilder, CollabPluginProviderType};
use collab_integrate::collab_builder::AppFlowyCollabBuilder;
use flowy_ai::ai_manager::AIManager;
use flowy_database2::DatabaseManager;
use flowy_document::manager::DocumentManager;
@ -34,7 +34,7 @@ use crate::config::AppFlowyCoreConfig;
use crate::deps_resolve::file_storage_deps::FileStorageResolver;
use crate::deps_resolve::*;
use crate::log_filter::init_log;
use crate::server_layer::{current_server_type, ServerProvider, ServerType};
use crate::server_layer::{current_server_type, ServerProvider};
use deps_resolve::reminder_deps::CollabInteractImpl;
use flowy_sqlite::DBConnection;
use lib_infra::async_trait::async_trait;
@ -131,12 +131,12 @@ impl AppFlowyCore {
store_preference.clone(),
));
let server_type = current_server_type();
debug!("🔥runtime:{}, server:{}", runtime, server_type);
let auth_type = current_server_type();
debug!("🔥runtime:{}, server:{}", runtime, auth_type);
let server_provider = Arc::new(ServerProvider::new(
config.clone(),
server_type,
auth_type,
Arc::downgrade(&store_preference),
ServerUserImpl(Arc::downgrade(&authenticate_user)),
));
@ -314,15 +314,6 @@ impl AppFlowyCore {
}
}
impl From<ServerType> for CollabPluginProviderType {
fn from(server_type: ServerType) -> Self {
match server_type {
ServerType::Local => CollabPluginProviderType::Local,
ServerType::AppFlowyCloud => CollabPluginProviderType::AppFlowyCloud,
}
}
}
struct ServerUserImpl(Weak<AuthenticateUser>);
impl ServerUserImpl {

View file

@ -13,52 +13,12 @@ use flowy_server::{AppFlowyEncryption, AppFlowyServer, EncryptionImpl};
use flowy_server_pub::AuthenticatorType;
use flowy_sqlite::kv::KVStorePreferences;
use flowy_user_pub::entities::*;
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak};
/// ServerType: local or cloud
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum ServerType {
Local = 0,
AppFlowyCloud = 1,
}
impl ServerType {
pub fn is_local(&self) -> bool {
matches!(self, Self::Local)
}
}
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,
}
}
}
pub struct ServerProvider {
config: AppFlowyCoreConfig,
providers: DashMap<ServerType, Arc<dyn AppFlowyServer>>,
providers: DashMap<AuthType, Arc<dyn AppFlowyServer>>,
auth_type: ArcSwap<AuthType>,
user: Arc<dyn LoginUserService>,
pub local_ai: Arc<LocalAIController>,
@ -70,12 +30,11 @@ pub struct ServerProvider {
impl ServerProvider {
pub fn new(
config: AppFlowyCoreConfig,
initial: ServerType,
initial_auth: AuthType,
store_preferences: Weak<KVStorePreferences>,
user_service: impl LoginUserService + 'static,
) -> Self {
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()));
@ -98,17 +57,10 @@ impl ServerProvider {
}
}
/// Reads current type
pub fn get_server_type(&self) -> ServerType {
let auth_type = self.auth_type.load_full();
ServerType::from(auth_type.as_ref())
}
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 {
pub fn set_auth_type(&self, new_auth_type: AuthType) {
let old_type = self.get_auth_type();
if old_type != new_auth_type {
self.auth_type.store(Arc::new(new_auth_type));
self.providers.remove(&old_type);
}
}
@ -119,14 +71,14 @@ impl ServerProvider {
/// Lazily create or fetch an AppFlowyServer instance
pub fn get_server(&self) -> FlowyResult<Arc<dyn AppFlowyServer>> {
let key = self.get_server_type();
if let Some(entry) = self.providers.get(&key) {
let auth_type = self.get_auth_type();
if let Some(entry) = self.providers.get(&auth_type) {
return Ok(entry.clone());
}
let server: Arc<dyn AppFlowyServer> = match key {
ServerType::Local => Arc::new(LocalServer::new(self.user.clone(), self.local_ai.clone())),
ServerType::AppFlowyCloud => {
let server: Arc<dyn AppFlowyServer> = match auth_type {
AuthType::Local => Arc::new(LocalServer::new(self.user.clone(), self.local_ai.clone())),
AuthType::AppFlowyCloud => {
let cfg = self
.config
.cloud_config
@ -142,15 +94,15 @@ impl ServerProvider {
},
};
self.providers.insert(key.clone(), server.clone());
self.providers.insert(auth_type, server.clone());
Ok(server)
}
}
/// Determine current server type from ENV
pub fn current_server_type() -> ServerType {
pub fn current_server_type() -> AuthType {
match AuthenticatorType::from_env() {
AuthenticatorType::Local => ServerType::Local,
AuthenticatorType::AppFlowyCloud => ServerType::AppFlowyCloud,
AuthenticatorType::Local => AuthType::Local,
AuthenticatorType::AppFlowyCloud => AuthType::AppFlowyCloud,
}
}

View file

@ -18,7 +18,7 @@ use flowy_user_pub::entities::{AuthType, UserProfile, UserWorkspace};
use lib_dispatch::runtime::AFPluginRuntime;
use lib_infra::async_trait::async_trait;
use crate::server_layer::{ServerProvider, ServerType};
use crate::server_layer::ServerProvider;
pub(crate) struct UserStatusCallbackImpl {
pub(crate) collab_builder: Arc<AppFlowyCollabBuilder>,
@ -128,7 +128,6 @@ impl UserStatusCallback for UserStatusCallbackImpl {
auth_type: &AuthType,
) -> FlowyResult<()> {
self.server_provider.set_auth_type(*auth_type);
let server_type = self.server_provider.get_server_type();
event!(
tracing::Level::TRACE,
@ -154,17 +153,17 @@ impl UserStatusCallback for UserStatusCallbackImpl {
)
.await
{
Ok(doc_state) => match server_type {
ServerType::Local => FolderInitDataSource::LocalDisk {
Ok(doc_state) => match auth_type {
AuthType::Local => FolderInitDataSource::LocalDisk {
create_if_not_exist: true,
},
ServerType::AppFlowyCloud => FolderInitDataSource::Cloud(doc_state),
AuthType::AppFlowyCloud => FolderInitDataSource::Cloud(doc_state),
},
Err(err) => match server_type {
ServerType::Local => FolderInitDataSource::LocalDisk {
Err(err) => match auth_type {
AuthType::Local => FolderInitDataSource::LocalDisk {
create_if_not_exist: true,
},
ServerType::AppFlowyCloud => {
AuthType::AppFlowyCloud => {
return Err(err);
},
},

View file

@ -1,3 +1,4 @@
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use chrono::{DateTime, Utc};
@ -359,6 +360,15 @@ pub enum AuthType {
AppFlowyCloud = 1,
}
impl Display for AuthType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
AuthType::Local => write!(f, "Local"),
AuthType::AppFlowyCloud => write!(f, "AppFlowyCloud"),
}
}
}
impl Default for AuthType {
fn default() -> Self {
Self::Local