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-09-17 17:14:34 +08:00
|
|
|
use collab_integrate::YrsDocAction;
|
2023-10-02 17:22:22 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2023-08-31 16:40:40 +08:00
|
|
|
use flowy_server::af_cloud::AFCloudServer;
|
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};
|
2023-10-02 17:22:22 +08:00
|
|
|
use flowy_server_config::af_cloud_config::AFCloudConfiguration;
|
2023-07-14 13:37:13 +08:00
|
|
|
use flowy_server_config::supabase_config::SupabaseConfiguration;
|
2023-08-06 11:51:03 +08:00
|
|
|
use flowy_sqlite::kv::StorePreferences;
|
2023-07-29 09:46:24 +08:00
|
|
|
use flowy_user::services::database::{
|
|
|
|
get_user_profile, get_user_workspace, open_collab_db, open_user_db,
|
|
|
|
};
|
2023-08-24 14:00:34 +08:00
|
|
|
use flowy_user_deps::cloud::UserCloudService;
|
2023-07-29 09:46:24 +08:00
|
|
|
use flowy_user_deps::entities::*;
|
2023-05-23 23:55:21 +08:00
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
use crate::AppFlowyCoreConfig;
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
pub(crate) const SERVER_PROVIDER_TYPE_KEY: &str = "server_provider_type";
|
2023-05-23 23:55:21 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
|
|
|
|
#[repr(u8)]
|
2023-09-17 17:14:34 +08:00
|
|
|
pub enum ServerType {
|
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-10-02 17:22:22 +08:00
|
|
|
AFCloud = 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,
|
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
impl Display for ServerType {
|
2023-08-07 22:24:04 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2023-09-17 17:14:34 +08:00
|
|
|
ServerType::Local => write!(f, "Local"),
|
2023-10-02 17:22:22 +08:00
|
|
|
ServerType::AFCloud => write!(f, "AppFlowyCloud"),
|
2023-09-17 17:14:34 +08:00
|
|
|
ServerType::Supabase => write!(f, "Supabase"),
|
2023-08-07 22:24:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
/// The [ServerProvider] provides list of [AppFlowyServer] base on the [AuthType]. Using
|
|
|
|
/// 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-09-17 17:14:34 +08:00
|
|
|
server_type: RwLock<ServerType>,
|
|
|
|
providers: RwLock<HashMap<ServerType, Arc<dyn AppFlowyServer>>>,
|
|
|
|
pub(crate) encryption: RwLock<Arc<dyn AppFlowyEncryption>>,
|
|
|
|
pub(crate) store_preferences: Weak<StorePreferences>,
|
|
|
|
pub(crate) cache_user_service: RwLock<HashMap<ServerType, Arc<dyn UserCloudService>>>,
|
|
|
|
|
|
|
|
pub(crate) device_id: Arc<RwLock<String>>,
|
|
|
|
pub(crate) enable_sync: RwLock<bool>,
|
|
|
|
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-09-17 17:14:34 +08:00
|
|
|
provider_type: ServerType,
|
2023-08-06 11:51:03 +08:00
|
|
|
store_preferences: Weak<StorePreferences>,
|
|
|
|
) -> Self {
|
2023-08-18 22:32:51 +08:00
|
|
|
let encryption = EncryptionImpl::new(None);
|
2023-07-14 13:37:13 +08:00
|
|
|
Self {
|
|
|
|
config,
|
2023-09-17 17:14:34 +08:00
|
|
|
server_type: RwLock::new(provider_type),
|
|
|
|
device_id: Arc::new(RwLock::new(uuid::Uuid::new_v4().to_string())),
|
2023-07-14 13:37:13 +08:00
|
|
|
providers: RwLock::new(HashMap::new()),
|
2023-08-18 22:32:51 +08:00
|
|
|
enable_sync: RwLock::new(true),
|
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-08-20 14:13:54 +08:00
|
|
|
cache_user_service: Default::default(),
|
2023-09-01 22:27:29 +08:00
|
|
|
uid: Default::default(),
|
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-09-17 17:14:34 +08:00
|
|
|
pub fn get_server_type(&self) -> ServerType {
|
|
|
|
self.server_type.read().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_server_type(&self, server_type: ServerType) {
|
|
|
|
*self.server_type.write() = server_type;
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a [AppFlowyServer] trait implementation base on the provider_type.
|
2023-09-17 17:14:34 +08:00
|
|
|
pub(crate) fn get_server(
|
2023-05-23 23:55:21 +08:00
|
|
|
&self,
|
2023-09-17 17:14:34 +08:00
|
|
|
server_type: &ServerType,
|
2023-05-23 23:55:21 +08:00
|
|
|
) -> FlowyResult<Arc<dyn AppFlowyServer>> {
|
2023-09-17 17:14:34 +08:00
|
|
|
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 {
|
|
|
|
ServerType::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-10-02 17:22:22 +08:00
|
|
|
ServerType::AFCloud => {
|
|
|
|
let config = AFCloudConfiguration::from_env()?;
|
2023-09-17 17:14:34 +08:00
|
|
|
tracing::trace!("🔑AppFlowy cloud config: {:?}", config);
|
|
|
|
let server = Arc::new(AFCloudServer::new(
|
|
|
|
config,
|
|
|
|
*self.enable_sync.read(),
|
|
|
|
self.device_id.clone(),
|
|
|
|
));
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
Ok::<Arc<dyn AppFlowyServer>, FlowyError>(server)
|
|
|
|
},
|
2023-09-17 17:14:34 +08:00
|
|
|
ServerType::Supabase => {
|
2023-09-01 11:32:45 +08:00
|
|
|
let config = match SupabaseConfiguration::from_env() {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(e) => {
|
|
|
|
*self.enable_sync.write() = false;
|
|
|
|
return Err(e);
|
|
|
|
},
|
|
|
|
};
|
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,
|
|
|
|
*self.enable_sync.read(),
|
2023-08-20 14:13:54 +08:00
|
|
|
self.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-09-17 17:14:34 +08:00
|
|
|
impl From<AuthType> for ServerType {
|
2023-05-23 23:55:21 +08:00
|
|
|
fn from(auth_provider: AuthType) -> Self {
|
|
|
|
match auth_provider {
|
2023-09-17 17:14:34 +08:00
|
|
|
AuthType::Local => ServerType::Local,
|
2023-10-02 17:22:22 +08:00
|
|
|
AuthType::AFCloud => ServerType::AFCloud,
|
2023-09-17 17:14:34 +08:00
|
|
|
AuthType::Supabase => ServerType::Supabase,
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
impl From<&AuthType> for ServerType {
|
2023-05-23 23:55:21 +08:00
|
|
|
fn from(auth_provider: &AuthType) -> Self {
|
|
|
|
Self::from(auth_provider.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
pub fn current_server_provider(store_preferences: &Arc<StorePreferences>) -> ServerType {
|
|
|
|
match store_preferences.get_object::<ServerType>(SERVER_PROVIDER_TYPE_KEY) {
|
|
|
|
None => ServerType::Local,
|
2023-05-23 23:55:21 +08:00
|
|
|
Some(provider_type) => provider_type,
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 09:46:24 +08:00
|
|
|
|
|
|
|
struct LocalServerDBImpl {
|
|
|
|
storage_path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalServerDB for LocalServerDBImpl {
|
|
|
|
fn get_user_profile(&self, uid: i64) -> Result<Option<UserProfile>, FlowyError> {
|
|
|
|
let sqlite_db = open_user_db(&self.storage_path, uid)?;
|
|
|
|
let user_profile = get_user_profile(&sqlite_db, uid).ok();
|
|
|
|
Ok(user_profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_user_workspace(&self, uid: i64) -> Result<Option<UserWorkspace>, FlowyError> {
|
|
|
|
let sqlite_db = open_user_db(&self.storage_path, uid)?;
|
|
|
|
let user_workspace = get_user_workspace(&sqlite_db, uid)?;
|
|
|
|
Ok(user_workspace)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_collab_updates(&self, uid: i64, object_id: &str) -> Result<Vec<Vec<u8>>, FlowyError> {
|
|
|
|
let collab_db = open_collab_db(&self.storage_path, uid)?;
|
|
|
|
let read_txn = collab_db.read_txn();
|
2023-08-22 00:19:15 +08:00
|
|
|
let updates = read_txn.get_all_updates(uid, object_id).map_err(|e| {
|
|
|
|
FlowyError::internal().with_context(format!("Failed to open collab db: {:?}", e))
|
|
|
|
})?;
|
2023-07-29 09:46:24 +08:00
|
|
|
|
|
|
|
Ok(updates)
|
|
|
|
}
|
|
|
|
}
|