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-07-05 20:57:09 +08:00
|
|
|
use appflowy_integrate::collab_builder::{CollabStorageProvider, CollabStorageType};
|
2023-08-12 17:36:31 +08:00
|
|
|
use appflowy_integrate::{CollabObject, CollabType, RemoteCollabStorage, YrsDocAction};
|
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-07-29 09:46:24 +08:00
|
|
|
use flowy_database_deps::cloud::*;
|
|
|
|
use flowy_document2::deps::DocumentData;
|
|
|
|
use flowy_document_deps::cloud::{DocumentCloudService, DocumentSnapshot};
|
2023-05-23 23:55:21 +08:00
|
|
|
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
|
2023-07-29 09:46:24 +08:00
|
|
|
use flowy_folder_deps::cloud::*;
|
2023-08-31 16:40:40 +08:00
|
|
|
use flowy_server::af_cloud::configuration::appflowy_cloud_server_configuration;
|
|
|
|
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-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::event_map::UserCloudServiceProvider;
|
|
|
|
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-07-05 20:57:09 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
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
|
|
|
const SERVER_PROVIDER_TYPE_KEY: &str = "server_provider_type";
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum ServerProviderType {
|
|
|
|
/// Local server provider.
|
|
|
|
/// Offline mode, no user authentication and the data is stored locally.
|
|
|
|
Local = 0,
|
|
|
|
/// Self-hosted 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-08-07 22:24:04 +08:00
|
|
|
AppFlowyCloud = 1,
|
2023-05-23 23:55:21 +08:00
|
|
|
/// Supabase server provider.
|
|
|
|
/// It uses supabase's postgresql database to store data and user authentication.
|
|
|
|
Supabase = 2,
|
|
|
|
}
|
|
|
|
|
2023-08-07 22:24:04 +08:00
|
|
|
impl Display for ServerProviderType {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
ServerProviderType::Local => write!(f, "Local"),
|
|
|
|
ServerProviderType::AppFlowyCloud => write!(f, "AppFlowyCloud"),
|
|
|
|
ServerProviderType::Supabase => write!(f, "Supabase"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 18:53:59 +08:00
|
|
|
/// The [AppFlowyServerProvider] provides list of [AppFlowyServer] base on the [AuthType]. Using
|
|
|
|
/// the auth type, the [AppFlowyServerProvider] will create a new [AppFlowyServer] if it doesn't
|
|
|
|
/// exist.
|
2023-08-24 14:00:34 +08:00
|
|
|
/// Each server implements the [AppFlowyServer] trait, which provides the [UserCloudService], etc.
|
2023-05-21 18:53:59 +08:00
|
|
|
pub struct AppFlowyServerProvider {
|
2023-07-14 13:37:13 +08:00
|
|
|
config: AppFlowyCoreConfig,
|
2023-05-23 23:55:21 +08:00
|
|
|
provider_type: RwLock<ServerProviderType>,
|
2023-08-20 14:13:54 +08:00
|
|
|
device_id: Arc<RwLock<String>>,
|
2023-05-23 23:55:21 +08:00
|
|
|
providers: RwLock<HashMap<ServerProviderType, Arc<dyn AppFlowyServer>>>,
|
2023-08-17 23:46:39 +08:00
|
|
|
enable_sync: RwLock<bool>,
|
|
|
|
encryption: RwLock<Arc<dyn AppFlowyEncryption>>,
|
2023-08-06 11:51:03 +08:00
|
|
|
store_preferences: Weak<StorePreferences>,
|
2023-08-24 14:00:34 +08:00
|
|
|
cache_user_service: RwLock<HashMap<ServerProviderType, Arc<dyn UserCloudService>>>,
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppFlowyServerProvider {
|
2023-08-06 11:51:03 +08:00
|
|
|
pub fn new(
|
|
|
|
config: AppFlowyCoreConfig,
|
|
|
|
provider_type: ServerProviderType,
|
|
|
|
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-08-06 11:51:03 +08:00
|
|
|
provider_type: RwLock::new(provider_type),
|
2023-08-12 17:36:31 +08:00
|
|
|
device_id: Default::default(),
|
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-07-14 13:37:13 +08:00
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
2023-08-12 17:36:31 +08:00
|
|
|
pub fn set_sync_device(&self, device_id: &str) {
|
2023-08-20 14:13:54 +08:00
|
|
|
*self.device_id.write() = device_id.to_string();
|
2023-08-12 17:36:31 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
pub fn provider_type(&self) -> ServerProviderType {
|
|
|
|
self.provider_type.read().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a [AppFlowyServer] trait implementation base on the provider_type.
|
|
|
|
fn get_provider(
|
|
|
|
&self,
|
|
|
|
provider_type: &ServerProviderType,
|
|
|
|
) -> FlowyResult<Arc<dyn AppFlowyServer>> {
|
|
|
|
if let Some(provider) = self.providers.read().get(provider_type) {
|
|
|
|
return Ok(provider.clone());
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
let server = match provider_type {
|
|
|
|
ServerProviderType::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-08-07 22:24:04 +08:00
|
|
|
ServerProviderType::AppFlowyCloud => {
|
2023-08-31 16:40:40 +08:00
|
|
|
let config = appflowy_cloud_server_configuration().map_err(|e| {
|
2023-07-14 13:37:13 +08:00
|
|
|
FlowyError::new(
|
|
|
|
ErrorCode::InvalidAuthConfig,
|
|
|
|
format!(
|
|
|
|
"Missing self host config: {:?}. Error: {:?}",
|
|
|
|
provider_type, e
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})?;
|
2023-08-31 16:40:40 +08:00
|
|
|
let server = Arc::new(AFCloudServer::new(config));
|
2023-07-14 13:37:13 +08:00
|
|
|
Ok::<Arc<dyn AppFlowyServer>, FlowyError>(server)
|
|
|
|
},
|
|
|
|
ServerProviderType::Supabase => {
|
2023-08-17 23:46:39 +08:00
|
|
|
let config = SupabaseConfiguration::from_env()?;
|
|
|
|
let encryption = Arc::downgrade(&*self.encryption.read());
|
|
|
|
Ok::<Arc<dyn AppFlowyServer>, FlowyError>(Arc::new(SupabaseServer::new(
|
|
|
|
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()
|
|
|
|
.insert(provider_type.clone(), server.clone());
|
|
|
|
Ok(server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
impl UserCloudServiceProvider for AppFlowyServerProvider {
|
2023-08-17 23:46:39 +08:00
|
|
|
fn set_enable_sync(&self, enable_sync: bool) {
|
|
|
|
match self.get_provider(&self.provider_type.read()) {
|
|
|
|
Ok(server) => {
|
|
|
|
server.set_enable_sync(enable_sync);
|
|
|
|
*self.enable_sync.write() = enable_sync;
|
|
|
|
},
|
|
|
|
Err(e) => tracing::error!("🔴Failed to enable sync: {:?}", e),
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
fn set_encrypt_secret(&self, secret: String) {
|
2023-08-18 22:32:51 +08:00
|
|
|
tracing::info!("🔑Set encrypt secret");
|
2023-08-17 23:46:39 +08:00
|
|
|
self.encryption.write().set_secret(secret);
|
|
|
|
}
|
|
|
|
|
2023-05-31 17:42:14 +08:00
|
|
|
/// When user login, the provider type is set by the [AuthType] and save to disk for next use.
|
|
|
|
///
|
2023-05-23 23:55:21 +08:00
|
|
|
/// Each [AuthType] has a corresponding [ServerProviderType]. The [ServerProviderType] is used
|
|
|
|
/// to create a new [AppFlowyServer] if it doesn't exist. Once the [ServerProviderType] is set,
|
|
|
|
/// it will be used when user open the app again.
|
2023-05-31 17:42:14 +08:00
|
|
|
///
|
2023-05-23 23:55:21 +08:00
|
|
|
fn set_auth_type(&self, auth_type: AuthType) {
|
|
|
|
let provider_type: ServerProviderType = auth_type.into();
|
2023-05-31 17:42:14 +08:00
|
|
|
*self.provider_type.write() = provider_type.clone();
|
|
|
|
|
2023-08-06 11:51:03 +08:00
|
|
|
match self.store_preferences.upgrade() {
|
|
|
|
None => tracing::error!("🔴Failed to update server provider type: store preferences is drop"),
|
|
|
|
Some(store_preferences) => {
|
|
|
|
match store_preferences.set_object(SERVER_PROVIDER_TYPE_KEY, provider_type.clone()) {
|
|
|
|
Ok(_) => tracing::trace!("Update server provider type to: {:?}", provider_type),
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("🔴Failed to update server provider type: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 12:57:59 +08:00
|
|
|
fn set_device_id(&self, device_id: &str) {
|
2023-08-20 14:13:54 +08:00
|
|
|
*self.device_id.write() = device_id.to_string();
|
2023-08-14 12:57:59 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 14:00:34 +08:00
|
|
|
/// Returns the [UserCloudService] base on the current [ServerProviderType].
|
2023-05-21 18:53:59 +08:00
|
|
|
/// Creates a new [AppFlowyServer] if it doesn't exist.
|
2023-08-24 14:00:34 +08:00
|
|
|
fn get_user_service(&self) -> Result<Arc<dyn UserCloudService>, FlowyError> {
|
2023-08-20 14:13:54 +08:00
|
|
|
if let Some(user_service) = self
|
|
|
|
.cache_user_service
|
|
|
|
.read()
|
|
|
|
.get(&self.provider_type.read())
|
|
|
|
{
|
|
|
|
return Ok(user_service.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let provider_type = self.provider_type.read().clone();
|
|
|
|
let user_service = self.get_provider(&provider_type)?.user_service();
|
|
|
|
self
|
|
|
|
.cache_user_service
|
|
|
|
.write()
|
|
|
|
.insert(provider_type, user_service.clone());
|
|
|
|
Ok(user_service)
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
2023-08-07 22:24:04 +08:00
|
|
|
|
|
|
|
fn service_name(&self) -> String {
|
|
|
|
self.provider_type.read().to_string()
|
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
impl FolderCloudService for AppFlowyServerProvider {
|
2023-07-29 09:46:24 +08:00
|
|
|
fn create_workspace(&self, uid: i64, name: &str) -> FutureResult<Workspace, Error> {
|
2023-05-23 23:55:21 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
let name = name.to_string();
|
|
|
|
FutureResult::new(async move { server?.folder_service().create_workspace(uid, &name).await })
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
fn get_folder_data(&self, workspace_id: &str) -> FutureResult<Option<FolderData>, Error> {
|
2023-07-14 13:37:13 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
let workspace_id = workspace_id.to_string();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.folder_service()
|
|
|
|
.get_folder_data(&workspace_id)
|
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
fn get_folder_snapshots(
|
2023-07-05 20:57:09 +08:00
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
2023-08-17 23:46:39 +08:00
|
|
|
limit: usize,
|
|
|
|
) -> FutureResult<Vec<FolderSnapshot>, Error> {
|
2023-07-05 20:57:09 +08:00
|
|
|
let workspace_id = workspace_id.to_string();
|
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.folder_service()
|
2023-08-17 23:46:39 +08:00
|
|
|
.get_folder_snapshots(&workspace_id, limit)
|
2023-07-05 20:57:09 +08:00
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
fn get_folder_updates(&self, workspace_id: &str, uid: i64) -> FutureResult<Vec<Vec<u8>>, Error> {
|
2023-07-05 20:57:09 +08:00
|
|
|
let workspace_id = workspace_id.to_string();
|
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.folder_service()
|
2023-07-14 13:37:13 +08:00
|
|
|
.get_folder_updates(&workspace_id, uid)
|
2023-07-05 20:57:09 +08:00
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
2023-07-14 13:37:13 +08:00
|
|
|
|
|
|
|
fn service_name(&self) -> String {
|
|
|
|
self
|
|
|
|
.get_provider(&self.provider_type.read())
|
|
|
|
.map(|provider| provider.folder_service().service_name())
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DatabaseCloudService for AppFlowyServerProvider {
|
2023-07-29 09:46:24 +08:00
|
|
|
fn get_collab_update(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
object_ty: CollabType,
|
|
|
|
) -> FutureResult<CollabObjectUpdate, Error> {
|
2023-07-05 20:57:09 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
2023-07-14 13:37:13 +08:00
|
|
|
let database_id = object_id.to_string();
|
2023-07-05 20:57:09 +08:00
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.database_service()
|
2023-07-29 09:46:24 +08:00
|
|
|
.get_collab_update(&database_id, object_ty)
|
2023-07-05 20:57:09 +08:00
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
fn batch_get_collab_updates(
|
2023-07-05 20:57:09 +08:00
|
|
|
&self,
|
2023-07-14 13:37:13 +08:00
|
|
|
object_ids: Vec<String>,
|
2023-07-29 09:46:24 +08:00
|
|
|
object_ty: CollabType,
|
|
|
|
) -> FutureResult<CollabObjectUpdateByOid, Error> {
|
2023-07-14 13:37:13 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.database_service()
|
2023-07-29 09:46:24 +08:00
|
|
|
.batch_get_collab_updates(object_ids, object_ty)
|
2023-07-14 13:37:13 +08:00
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
fn get_collab_snapshots(
|
2023-07-14 13:37:13 +08:00
|
|
|
&self,
|
|
|
|
object_id: &str,
|
2023-08-17 23:46:39 +08:00
|
|
|
limit: usize,
|
|
|
|
) -> FutureResult<Vec<DatabaseSnapshot>, Error> {
|
2023-07-05 20:57:09 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
2023-07-14 13:37:13 +08:00
|
|
|
let database_id = object_id.to_string();
|
2023-07-05 20:57:09 +08:00
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.database_service()
|
2023-08-17 23:46:39 +08:00
|
|
|
.get_collab_snapshots(&database_id, limit)
|
2023-07-05 20:57:09 +08:00
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentCloudService for AppFlowyServerProvider {
|
2023-07-29 09:46:24 +08:00
|
|
|
fn get_document_updates(&self, document_id: &str) -> FutureResult<Vec<Vec<u8>>, Error> {
|
2023-07-05 20:57:09 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
let document_id = document_id.to_string();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.document_service()
|
|
|
|
.get_document_updates(&document_id)
|
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
fn get_document_snapshots(
|
2023-07-05 20:57:09 +08:00
|
|
|
&self,
|
|
|
|
document_id: &str,
|
2023-08-17 23:46:39 +08:00
|
|
|
limit: usize,
|
|
|
|
) -> FutureResult<Vec<DocumentSnapshot>, Error> {
|
2023-07-05 20:57:09 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
let document_id = document_id.to_string();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.document_service()
|
2023-08-17 23:46:39 +08:00
|
|
|
.get_document_snapshots(&document_id, limit)
|
2023-07-05 20:57:09 +08:00
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
2023-07-14 13:37:13 +08:00
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
fn get_document_data(&self, document_id: &str) -> FutureResult<Option<DocumentData>, Error> {
|
2023-07-14 13:37:13 +08:00
|
|
|
let server = self.get_provider(&self.provider_type.read());
|
|
|
|
let document_id = document_id.to_string();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
server?
|
|
|
|
.document_service()
|
|
|
|
.get_document_data(&document_id)
|
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CollabStorageProvider for AppFlowyServerProvider {
|
|
|
|
fn storage_type(&self) -> CollabStorageType {
|
|
|
|
self.provider_type().into()
|
|
|
|
}
|
|
|
|
|
2023-08-12 17:36:31 +08:00
|
|
|
fn get_storage(
|
|
|
|
&self,
|
|
|
|
collab_object: &CollabObject,
|
|
|
|
storage_type: &CollabStorageType,
|
|
|
|
) -> Option<Arc<dyn RemoteCollabStorage>> {
|
2023-07-05 20:57:09 +08:00
|
|
|
match storage_type {
|
|
|
|
CollabStorageType::Local => None,
|
|
|
|
CollabStorageType::AWS => None,
|
|
|
|
CollabStorageType::Supabase => self
|
|
|
|
.get_provider(&ServerProviderType::Supabase)
|
|
|
|
.ok()
|
2023-08-12 17:36:31 +08:00
|
|
|
.and_then(|provider| provider.collab_storage(collab_object)),
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
fn is_sync_enabled(&self) -> bool {
|
2023-08-17 23:46:39 +08:00
|
|
|
*self.enable_sync.read()
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
|
|
|
impl From<AuthType> for ServerProviderType {
|
|
|
|
fn from(auth_provider: AuthType) -> Self {
|
|
|
|
match auth_provider {
|
|
|
|
AuthType::Local => ServerProviderType::Local,
|
2023-08-07 22:24:04 +08:00
|
|
|
AuthType::SelfHosted => ServerProviderType::AppFlowyCloud,
|
2023-05-23 23:55:21 +08:00
|
|
|
AuthType::Supabase => ServerProviderType::Supabase,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&AuthType> for ServerProviderType {
|
|
|
|
fn from(auth_provider: &AuthType) -> Self {
|
|
|
|
Self::from(auth_provider.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 11:51:03 +08:00
|
|
|
pub fn current_server_provider(store_preferences: &Arc<StorePreferences>) -> ServerProviderType {
|
|
|
|
match store_preferences.get_object::<ServerProviderType>(SERVER_PROVIDER_TYPE_KEY) {
|
2023-05-23 23:55:21 +08:00
|
|
|
None => ServerProviderType::Local,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|