2023-05-21 18:53:59 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-07-05 20:57:09 +08:00
|
|
|
use appflowy_integrate::RemoteCollabStorage;
|
2023-07-14 13:37:13 +08:00
|
|
|
use collab_document::YrsDocAction;
|
2023-05-21 18:53:59 +08:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
2023-07-05 20:57:09 +08:00
|
|
|
use flowy_database2::deps::DatabaseCloudService;
|
|
|
|
use flowy_document2::deps::DocumentCloudService;
|
2023-07-14 13:37:13 +08:00
|
|
|
use flowy_error::FlowyError;
|
2023-07-05 20:57:09 +08:00
|
|
|
use flowy_folder2::deps::FolderCloudService;
|
2023-07-14 13:37:13 +08:00
|
|
|
use flowy_user::entities::UserProfile;
|
2023-05-21 18:53:59 +08:00
|
|
|
use flowy_user::event_map::UserAuthService;
|
2023-07-14 13:37:13 +08:00
|
|
|
use flowy_user::services::database::{get_user_profile, open_collab_db, open_user_db};
|
2023-05-21 18:53:59 +08:00
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
use crate::local_server::impls::{
|
2023-07-05 20:57:09 +08:00
|
|
|
LocalServerDatabaseCloudServiceImpl, LocalServerDocumentCloudServiceImpl,
|
2023-05-23 23:55:21 +08:00
|
|
|
LocalServerFolderCloudServiceImpl, LocalServerUserAuthServiceImpl,
|
|
|
|
};
|
2023-05-21 18:53:59 +08:00
|
|
|
use crate::AppFlowyServer;
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
pub trait LocalServerDB: Send + Sync + 'static {
|
|
|
|
fn get_user_profile(&self, uid: i64) -> Result<Option<UserProfile>, FlowyError>;
|
|
|
|
fn get_collab_updates(&self, uid: i64, object_id: &str) -> Result<Vec<Vec<u8>>, FlowyError>;
|
|
|
|
}
|
|
|
|
|
2023-05-21 18:53:59 +08:00
|
|
|
pub struct LocalServer {
|
2023-07-14 13:37:13 +08:00
|
|
|
storage_path: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
stop_tx: RwLock<Option<mpsc::Sender<()>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalServer {
|
2023-07-14 13:37:13 +08:00
|
|
|
pub fn new(storage_path: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
storage_path: storage_path.to_string(),
|
|
|
|
stop_tx: Default::default(),
|
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn stop(&self) {
|
|
|
|
let sender = self.stop_tx.read().clone();
|
|
|
|
if let Some(stop_tx) = sender {
|
|
|
|
let _ = stop_tx.send(()).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AppFlowyServer for LocalServer {
|
|
|
|
fn user_service(&self) -> Arc<dyn UserAuthService> {
|
2023-07-14 13:37:13 +08:00
|
|
|
let db = LocalServerDBImpl {
|
|
|
|
storage_path: self.storage_path.clone(),
|
|
|
|
};
|
|
|
|
Arc::new(LocalServerUserAuthServiceImpl { db: Arc::new(db) })
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
|
|
|
fn folder_service(&self) -> Arc<dyn FolderCloudService> {
|
2023-07-14 13:37:13 +08:00
|
|
|
let db = LocalServerDBImpl {
|
|
|
|
storage_path: self.storage_path.clone(),
|
|
|
|
};
|
|
|
|
Arc::new(LocalServerFolderCloudServiceImpl { db: Arc::new(db) })
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
fn database_service(&self) -> Arc<dyn DatabaseCloudService> {
|
|
|
|
Arc::new(LocalServerDatabaseCloudServiceImpl())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn document_service(&self) -> Arc<dyn DocumentCloudService> {
|
|
|
|
Arc::new(LocalServerDocumentCloudServiceImpl())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collab_storage(&self) -> Option<Arc<dyn RemoteCollabStorage>> {
|
|
|
|
None
|
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
2023-07-14 13:37:13 +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_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();
|
|
|
|
let updates = read_txn
|
|
|
|
.get_all_updates(uid, object_id)
|
|
|
|
.map_err(|e| FlowyError::internal().context(format!("Failed to open collab db: {:?}", e)))?;
|
|
|
|
|
|
|
|
Ok(updates)
|
|
|
|
}
|
|
|
|
}
|