2023-05-21 18:53:59 +08:00
|
|
|
use std::collections::HashMap;
|
2023-08-07 22:24:04 +08:00
|
|
|
use std::fmt::{Display, Formatter};
|
2023-08-06 11:51:03 +08:00
|
|
|
use std::sync::{Arc, Weak};
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-08-20 14:13:54 +08:00
|
|
|
use parking_lot::RwLock;
|
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-07-14 13:37:13 +08:00
|
|
|
use flowy_server::supabase::SupabaseServer;
|
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::supabase_config::SupabaseConfiguration;
|
|
|
|
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.
|
2023-08-07 22:24:04 +08:00
|
|
|
/// The [AppFlowy-Server](https://github.com/AppFlowy-IO/AppFlowy-Cloud) is still a work in
|
2023-05-23 23:55:21 +08:00
|
|
|
/// progress.
|
2023-12-26 02:03:42 +08:00
|
|
|
AppFlowyCloud = 1,
|
2023-05-23 23:55:21 +08:00
|
|
|
/// Supabase server provider.
|
2023-09-17 17:14:34 +08:00
|
|
|
/// It uses supabase postgresql database to store data and user authentication.
|
2023-05-23 23:55:21 +08:00
|
|
|
Supabase = 2,
|
|
|
|
}
|
|
|
|
|
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"),
|
|
|
|
Server::Supabase => write!(f, "Supabase"),
|
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,
|
2023-12-26 02:03:42 +08:00
|
|
|
providers: RwLock<HashMap<Server, Arc<dyn AppFlowyServer>>>,
|
2023-09-17 17:14:34 +08:00
|
|
|
pub(crate) encryption: RwLock<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>,
|
2023-12-30 07:05:26 +08:00
|
|
|
pub(crate) user_enable_sync: RwLock<bool>,
|
|
|
|
|
|
|
|
/// The authenticator type of the user.
|
2024-01-04 08:02:12 +08:00
|
|
|
authenticator: RwLock<Authenticator>,
|
2024-04-26 09:44:07 +08:00
|
|
|
user: Arc<dyn ServerUser>,
|
2023-09-17 17:14:34 +08:00
|
|
|
pub(crate) uid: Arc<RwLock<Option<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,
|
|
|
|
providers: RwLock::new(HashMap::new()),
|
2023-12-30 07:05:26 +08:00
|
|
|
user_enable_sync: RwLock::new(true),
|
2024-01-04 08:02:12 +08:00
|
|
|
authenticator: RwLock::new(Authenticator::from(server)),
|
2023-08-17 23:46:39 +08:00
|
|
|
encryption: RwLock::new(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-01-04 08:02:12 +08:00
|
|
|
match &*self.authenticator.read() {
|
|
|
|
Authenticator::Local => Server::Local,
|
|
|
|
Authenticator::AppFlowyCloud => Server::AppFlowyCloud,
|
|
|
|
Authenticator::Supabase => Server::Supabase,
|
|
|
|
}
|
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();
|
|
|
|
*self.authenticator.write() = authenticator;
|
|
|
|
let new_server_type = self.get_server_type();
|
|
|
|
|
|
|
|
if old_server_type != new_server_type {
|
2023-10-24 23:13:51 +08:00
|
|
|
self.providers.write().remove(&old_server_type);
|
|
|
|
}
|
2023-12-30 13:44:09 +08:00
|
|
|
}
|
|
|
|
|
2024-01-04 08:02:12 +08:00
|
|
|
pub fn get_authenticator(&self) -> Authenticator {
|
|
|
|
self.authenticator.read().clone()
|
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();
|
|
|
|
|
|
|
|
if let Some(provider) = self.providers.read().get(&server_type) {
|
2023-05-23 23:55:21 +08:00
|
|
|
return Ok(provider.clone());
|
|
|
|
}
|
|
|
|
|
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,
|
2023-12-30 07:05:26 +08:00
|
|
|
*self.user_enable_sync.read(),
|
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)
|
|
|
|
},
|
2023-12-26 02:03:42 +08:00
|
|
|
Server::Supabase => {
|
2023-11-17 15:38:56 +08:00
|
|
|
let config = SupabaseConfiguration::from_env()?;
|
2023-09-01 22:27:29 +08:00
|
|
|
let uid = self.uid.clone();
|
2023-09-01 11:32:45 +08:00
|
|
|
tracing::trace!("🔑Supabase config: {:?}", config);
|
2023-08-17 23:46:39 +08:00
|
|
|
let encryption = Arc::downgrade(&*self.encryption.read());
|
|
|
|
Ok::<Arc<dyn AppFlowyServer>, FlowyError>(Arc::new(SupabaseServer::new(
|
2023-09-01 22:27:29 +08:00
|
|
|
uid,
|
2023-08-17 23:46:39 +08:00
|
|
|
config,
|
2023-12-30 07:05:26 +08:00
|
|
|
*self.user_enable_sync.read(),
|
2023-11-20 20:54:47 +08:00
|
|
|
self.config.device_id.clone(),
|
2023-08-17 23:46:39 +08:00
|
|
|
encryption,
|
|
|
|
)))
|
2023-07-14 13:37:13 +08:00
|
|
|
},
|
|
|
|
}?;
|
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
self
|
|
|
|
.providers
|
|
|
|
.write()
|
2023-09-17 17:14:34 +08:00
|
|
|
.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,
|
|
|
|
Authenticator::Supabase => Server::Supabase,
|
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,
|
|
|
|
Server::Supabase => Authenticator::Supabase,
|
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::Supabase => Server::Supabase,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|