2024-01-16 18:59:15 +00:00
|
|
|
use flowy_storage::ObjectStorageService;
|
2023-07-29 01:46:24 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-04-15 06:50:28 +00:00
|
|
|
use collab::core::collab::{DataSource, MutexCollab};
|
2023-08-17 15:46:39 +00:00
|
|
|
use collab::core::origin::CollabOrigin;
|
2024-04-15 06:50:28 +00:00
|
|
|
use collab::preclude::Collab;
|
2023-07-29 01:46:24 +00:00
|
|
|
use collab_plugins::cloud_storage::RemoteCollabStorage;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2024-01-11 06:42:03 +00:00
|
|
|
use flowy_database_pub::cloud::DatabaseCloudService;
|
2023-09-01 14:27:29 +00:00
|
|
|
use flowy_error::FlowyError;
|
2024-01-11 06:42:03 +00:00
|
|
|
use flowy_folder_pub::cloud::{Folder, FolderCloudService};
|
2023-07-29 01:46:24 +00:00
|
|
|
use flowy_server::supabase::api::{
|
2023-08-14 04:57:59 +00:00
|
|
|
RESTfulPostgresServer, SupabaseCollabStorageImpl, SupabaseDatabaseServiceImpl,
|
|
|
|
SupabaseFolderServiceImpl, SupabaseServerServiceImpl, SupabaseUserServiceImpl,
|
2023-07-29 01:46:24 +00:00
|
|
|
};
|
2023-08-17 15:46:39 +00:00
|
|
|
use flowy_server::supabase::define::{USER_DEVICE_ID, USER_EMAIL, USER_UUID};
|
2023-08-31 08:40:40 +00:00
|
|
|
use flowy_server::supabase::file_storage::core::SupabaseFileStorage;
|
2023-08-17 15:46:39 +00:00
|
|
|
use flowy_server::{AppFlowyEncryption, EncryptionImpl};
|
2024-01-11 06:42:03 +00:00
|
|
|
use flowy_server_pub::supabase_config::SupabaseConfiguration;
|
2024-01-16 18:59:15 +00:00
|
|
|
use flowy_storage::{FileStoragePlan, StorageObject};
|
2024-01-11 06:42:03 +00:00
|
|
|
use flowy_user_pub::cloud::UserCloudService;
|
2023-09-01 14:27:29 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2023-07-29 01:46:24 +00:00
|
|
|
|
|
|
|
use crate::setup_log;
|
|
|
|
|
2023-08-17 15:46:39 +00:00
|
|
|
pub fn get_supabase_ci_config() -> Option<SupabaseConfiguration> {
|
|
|
|
dotenv::from_filename("./.env.ci").ok()?;
|
|
|
|
setup_log();
|
|
|
|
SupabaseConfiguration::from_env().ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn get_supabase_dev_config() -> Option<SupabaseConfiguration> {
|
|
|
|
dotenv::from_filename("./.env.dev").ok()?;
|
2023-07-29 01:46:24 +00:00
|
|
|
setup_log();
|
|
|
|
SupabaseConfiguration::from_env().ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collab_service() -> Arc<dyn RemoteCollabStorage> {
|
2023-10-02 09:22:22 +00:00
|
|
|
let (server, encryption_impl) = supabase_server_service(None);
|
2023-08-05 07:02:05 +00:00
|
|
|
Arc::new(SupabaseCollabStorageImpl::new(
|
2023-08-17 15:46:39 +00:00
|
|
|
server,
|
2023-08-12 09:36:31 +00:00
|
|
|
None,
|
2023-08-17 15:46:39 +00:00
|
|
|
Arc::downgrade(&encryption_impl),
|
2023-07-29 01:46:24 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn database_service() -> Arc<dyn DatabaseCloudService> {
|
2023-10-02 09:22:22 +00:00
|
|
|
let (server, _encryption_impl) = supabase_server_service(None);
|
2023-08-17 15:46:39 +00:00
|
|
|
Arc::new(SupabaseDatabaseServiceImpl::new(server))
|
2023-07-29 01:46:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 06:00:34 +00:00
|
|
|
pub fn user_auth_service() -> Arc<dyn UserCloudService> {
|
2023-10-02 09:22:22 +00:00
|
|
|
let (server, _encryption_impl) = supabase_server_service(None);
|
2023-08-20 06:13:54 +00:00
|
|
|
Arc::new(SupabaseUserServiceImpl::new(server, vec![], None))
|
2023-07-29 01:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn folder_service() -> Arc<dyn FolderCloudService> {
|
2023-10-02 09:22:22 +00:00
|
|
|
let (server, _encryption_impl) = supabase_server_service(None);
|
2023-08-17 15:46:39 +00:00
|
|
|
Arc::new(SupabaseFolderServiceImpl::new(server))
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:59:15 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn file_storage_service() -> Arc<dyn ObjectStorageService> {
|
2023-09-01 14:27:29 +00:00
|
|
|
let encryption_impl: Arc<dyn AppFlowyEncryption> = Arc::new(EncryptionImpl::new(None));
|
2023-08-31 08:40:40 +00:00
|
|
|
let config = SupabaseConfiguration::from_env().unwrap();
|
2023-09-01 14:27:29 +00:00
|
|
|
Arc::new(
|
|
|
|
SupabaseFileStorage::new(
|
|
|
|
&config,
|
|
|
|
Arc::downgrade(&encryption_impl),
|
|
|
|
Arc::new(TestFileStoragePlan),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
2023-08-31 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:46:39 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn encryption_folder_service(
|
|
|
|
secret: Option<String>,
|
|
|
|
) -> (Arc<dyn FolderCloudService>, Arc<dyn AppFlowyEncryption>) {
|
2023-10-02 09:22:22 +00:00
|
|
|
let (server, encryption_impl) = supabase_server_service(secret);
|
2023-08-17 15:46:39 +00:00
|
|
|
let service = Arc::new(SupabaseFolderServiceImpl::new(server));
|
|
|
|
(service, encryption_impl)
|
|
|
|
}
|
|
|
|
|
2023-08-18 07:13:34 +00:00
|
|
|
#[allow(dead_code)]
|
2023-08-17 15:46:39 +00:00
|
|
|
pub fn encryption_collab_service(
|
|
|
|
secret: Option<String>,
|
|
|
|
) -> (Arc<dyn RemoteCollabStorage>, Arc<dyn AppFlowyEncryption>) {
|
2023-10-02 09:22:22 +00:00
|
|
|
let (server, encryption_impl) = supabase_server_service(secret);
|
2023-08-17 15:46:39 +00:00
|
|
|
let service = Arc::new(SupabaseCollabStorageImpl::new(
|
|
|
|
server,
|
|
|
|
None,
|
|
|
|
Arc::downgrade(&encryption_impl),
|
|
|
|
));
|
|
|
|
(service, encryption_impl)
|
|
|
|
}
|
|
|
|
|
2023-08-18 07:13:34 +00:00
|
|
|
#[allow(dead_code)]
|
2023-11-01 03:45:35 +00:00
|
|
|
pub async fn print_encryption_folder(
|
|
|
|
uid: &i64,
|
|
|
|
folder_id: &str,
|
|
|
|
encryption_secret: Option<String>,
|
|
|
|
) {
|
2023-08-17 15:46:39 +00:00
|
|
|
let (cloud_service, _encryption) = encryption_folder_service(encryption_secret);
|
2023-11-01 03:45:35 +00:00
|
|
|
let folder_data = cloud_service.get_folder_data(folder_id, uid).await.unwrap();
|
2023-08-17 15:46:39 +00:00
|
|
|
let json = serde_json::to_value(folder_data).unwrap();
|
|
|
|
println!("{}", serde_json::to_string_pretty(&json).unwrap());
|
|
|
|
}
|
|
|
|
|
2023-08-18 07:13:34 +00:00
|
|
|
#[allow(dead_code)]
|
2023-11-01 03:45:35 +00:00
|
|
|
pub async fn print_encryption_folder_snapshot(
|
|
|
|
uid: &i64,
|
|
|
|
folder_id: &str,
|
|
|
|
encryption_secret: Option<String>,
|
|
|
|
) {
|
2023-08-17 15:46:39 +00:00
|
|
|
let (cloud_service, _encryption) = encryption_collab_service(encryption_secret);
|
|
|
|
let snapshot = cloud_service
|
|
|
|
.get_snapshots(folder_id, 1)
|
|
|
|
.await
|
|
|
|
.pop()
|
|
|
|
.unwrap();
|
2024-04-15 06:50:28 +00:00
|
|
|
let collab = Arc::new(MutexCollab::new(
|
|
|
|
Collab::new_with_source(
|
2024-03-23 01:18:47 +00:00
|
|
|
CollabOrigin::Empty,
|
|
|
|
folder_id,
|
2024-04-15 06:50:28 +00:00
|
|
|
DataSource::DocStateV1(snapshot.blob),
|
2024-03-23 01:18:47 +00:00
|
|
|
vec![],
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2024-04-15 06:50:28 +00:00
|
|
|
));
|
2023-11-01 03:45:35 +00:00
|
|
|
let folder_data = Folder::open(uid, collab, None)
|
|
|
|
.unwrap()
|
|
|
|
.get_folder_data()
|
|
|
|
.unwrap();
|
2023-08-17 15:46:39 +00:00
|
|
|
let json = serde_json::to_value(folder_data).unwrap();
|
|
|
|
println!("{}", serde_json::to_string_pretty(&json).unwrap());
|
|
|
|
}
|
|
|
|
|
2023-10-02 09:22:22 +00:00
|
|
|
pub fn supabase_server_service(
|
2023-08-17 15:46:39 +00:00
|
|
|
encryption_secret: Option<String>,
|
|
|
|
) -> (SupabaseServerServiceImpl, Arc<dyn AppFlowyEncryption>) {
|
2023-07-29 01:46:24 +00:00
|
|
|
let config = SupabaseConfiguration::from_env().unwrap();
|
2023-08-17 15:46:39 +00:00
|
|
|
let encryption_impl: Arc<dyn AppFlowyEncryption> =
|
|
|
|
Arc::new(EncryptionImpl::new(encryption_secret));
|
|
|
|
let encryption = Arc::downgrade(&encryption_impl);
|
|
|
|
let server = Arc::new(RESTfulPostgresServer::new(config, encryption));
|
|
|
|
(SupabaseServerServiceImpl::new(server), encryption_impl)
|
2023-07-29 01:46:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:46:39 +00:00
|
|
|
pub fn third_party_sign_up_param(uuid: String) -> HashMap<String, String> {
|
2023-07-29 01:46:24 +00:00
|
|
|
let mut params = HashMap::new();
|
|
|
|
params.insert(USER_UUID.to_string(), uuid);
|
|
|
|
params.insert(
|
|
|
|
USER_EMAIL.to_string(),
|
|
|
|
format!("{}@test.com", Uuid::new_v4()),
|
|
|
|
);
|
2023-08-17 15:46:39 +00:00
|
|
|
params.insert(USER_DEVICE_ID.to_string(), Uuid::new_v4().to_string());
|
2023-07-29 01:46:24 +00:00
|
|
|
params
|
|
|
|
}
|
2023-09-01 14:27:29 +00:00
|
|
|
|
|
|
|
pub struct TestFileStoragePlan;
|
|
|
|
|
|
|
|
impl FileStoragePlan for TestFileStoragePlan {
|
|
|
|
fn storage_size(&self) -> FutureResult<u64, FlowyError> {
|
|
|
|
// 1 GB
|
|
|
|
FutureResult::new(async { Ok(1024 * 1024 * 1024) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maximum_file_size(&self) -> FutureResult<u64, FlowyError> {
|
|
|
|
// 5 MB
|
|
|
|
FutureResult::new(async { Ok(5 * 1024 * 1024) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_upload_object(&self, _object: &StorageObject) -> FutureResult<(), FlowyError> {
|
|
|
|
FutureResult::new(async { Ok(()) })
|
|
|
|
}
|
|
|
|
}
|