2023-09-17 09:14:34 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2023-05-21 10:53:59 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-09-17 09:14:34 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use client_api::notify::{TokenState, TokenStateReceiver};
|
2023-10-02 09:22:22 +00:00
|
|
|
use client_api::ws::{
|
|
|
|
BusinessID, WSClient, WSClientConfig, WSConnectStateReceiver, WebSocketChannel,
|
|
|
|
};
|
2023-09-17 09:14:34 +00:00
|
|
|
use client_api::Client;
|
2023-10-07 01:58:44 +00:00
|
|
|
use tracing::info;
|
2023-07-05 12:57:09 +00:00
|
|
|
|
2023-07-29 01:46:24 +00:00
|
|
|
use flowy_database_deps::cloud::DatabaseCloudService;
|
|
|
|
use flowy_document_deps::cloud::DocumentCloudService;
|
2023-09-17 09:14:34 +00:00
|
|
|
use flowy_error::{ErrorCode, FlowyError};
|
2023-07-29 01:46:24 +00:00
|
|
|
use flowy_folder_deps::cloud::FolderCloudService;
|
2023-10-02 09:22:22 +00:00
|
|
|
use flowy_server_config::af_cloud_config::AFCloudConfiguration;
|
2023-09-01 14:27:29 +00:00
|
|
|
use flowy_storage::FileStorageService;
|
2023-08-24 06:00:34 +00:00
|
|
|
use flowy_user_deps::cloud::UserCloudService;
|
2023-09-17 09:14:34 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2023-05-21 10:53:59 +00:00
|
|
|
|
2023-08-31 08:40:40 +00:00
|
|
|
use crate::af_cloud::impls::{
|
2023-10-02 09:22:22 +00:00
|
|
|
AFCloudDatabaseCloudServiceImpl, AFCloudDocumentCloudServiceImpl, AFCloudFileStorageServiceImpl,
|
|
|
|
AFCloudFolderCloudServiceImpl, AFCloudUserAuthServiceImpl,
|
2023-05-23 15:55:21 +00:00
|
|
|
};
|
2023-05-21 10:53:59 +00:00
|
|
|
use crate::AppFlowyServer;
|
|
|
|
|
2023-10-02 09:22:22 +00:00
|
|
|
pub(crate) type AFCloudClient = client_api::Client;
|
2023-09-17 09:14:34 +00:00
|
|
|
|
2023-08-31 08:40:40 +00:00
|
|
|
pub struct AFCloudServer {
|
2023-09-17 09:14:34 +00:00
|
|
|
#[allow(dead_code)]
|
2023-08-31 08:40:40 +00:00
|
|
|
pub(crate) config: AFCloudConfiguration,
|
2023-09-17 09:14:34 +00:00
|
|
|
pub(crate) client: Arc<AFCloudClient>,
|
|
|
|
enable_sync: AtomicBool,
|
|
|
|
#[allow(dead_code)]
|
|
|
|
device_id: Arc<parking_lot::RwLock<String>>,
|
2023-10-07 01:58:44 +00:00
|
|
|
ws_client: Arc<WSClient>,
|
2023-05-21 10:53:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 08:40:40 +00:00
|
|
|
impl AFCloudServer {
|
2023-09-17 09:14:34 +00:00
|
|
|
pub fn new(
|
|
|
|
config: AFCloudConfiguration,
|
|
|
|
enable_sync: bool,
|
|
|
|
device_id: Arc<parking_lot::RwLock<String>>,
|
|
|
|
) -> Self {
|
2023-10-07 01:58:44 +00:00
|
|
|
let api_client =
|
|
|
|
client_api::Client::new(&config.base_url, &config.ws_base_url, &config.gotrue_url);
|
2023-09-17 09:14:34 +00:00
|
|
|
let token_state_rx = api_client.subscribe_token_state();
|
|
|
|
let enable_sync = AtomicBool::new(enable_sync);
|
|
|
|
|
|
|
|
let ws_client = WSClient::new(WSClientConfig {
|
|
|
|
buffer_capacity: 100,
|
2023-10-02 09:22:22 +00:00
|
|
|
ping_per_secs: 8,
|
2023-09-17 09:14:34 +00:00
|
|
|
retry_connect_per_pings: 5,
|
|
|
|
});
|
2023-10-07 01:58:44 +00:00
|
|
|
let ws_client = Arc::new(ws_client);
|
2023-10-02 09:22:22 +00:00
|
|
|
let api_client = Arc::new(api_client);
|
2023-09-17 09:14:34 +00:00
|
|
|
|
|
|
|
spawn_ws_conn(&device_id, token_state_rx, &ws_client, &api_client);
|
|
|
|
Self {
|
|
|
|
config,
|
|
|
|
client: api_client,
|
|
|
|
enable_sync,
|
|
|
|
device_id,
|
|
|
|
ws_client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_client(&self) -> Option<Arc<AFCloudClient>> {
|
|
|
|
if self.enable_sync.load(Ordering::SeqCst) {
|
|
|
|
Some(self.client.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2023-05-21 10:53:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:40:40 +00:00
|
|
|
impl AppFlowyServer for AFCloudServer {
|
2023-09-17 09:14:34 +00:00
|
|
|
fn set_enable_sync(&self, uid: i64, enable: bool) {
|
2023-10-07 01:58:44 +00:00
|
|
|
info!("{} cloud sync: {}", uid, enable);
|
2023-09-17 09:14:34 +00:00
|
|
|
self.enable_sync.store(enable, Ordering::SeqCst);
|
|
|
|
}
|
2023-08-24 06:00:34 +00:00
|
|
|
fn user_service(&self) -> Arc<dyn UserCloudService> {
|
2023-09-17 09:14:34 +00:00
|
|
|
let server = AFServerImpl(self.get_client());
|
|
|
|
Arc::new(AFCloudUserAuthServiceImpl::new(server))
|
2023-05-21 10:53:59 +00:00
|
|
|
}
|
2023-05-23 15:55:21 +00:00
|
|
|
|
|
|
|
fn folder_service(&self) -> Arc<dyn FolderCloudService> {
|
2023-09-17 09:14:34 +00:00
|
|
|
let server = AFServerImpl(self.get_client());
|
|
|
|
Arc::new(AFCloudFolderCloudServiceImpl(server))
|
2023-05-23 15:55:21 +00:00
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
|
|
|
|
fn database_service(&self) -> Arc<dyn DatabaseCloudService> {
|
2023-09-17 09:14:34 +00:00
|
|
|
let server = AFServerImpl(self.get_client());
|
|
|
|
Arc::new(AFCloudDatabaseCloudServiceImpl(server))
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn document_service(&self) -> Arc<dyn DocumentCloudService> {
|
2023-09-17 09:14:34 +00:00
|
|
|
let server = AFServerImpl(self.get_client());
|
|
|
|
Arc::new(AFCloudDocumentCloudServiceImpl(server))
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-17 09:14:34 +00:00
|
|
|
fn collab_ws_channel(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
2023-10-02 09:22:22 +00:00
|
|
|
) -> FutureResult<Option<(Arc<WebSocketChannel>, WSConnectStateReceiver)>, anyhow::Error> {
|
2023-09-17 09:14:34 +00:00
|
|
|
if self.enable_sync.load(Ordering::SeqCst) {
|
|
|
|
let object_id = object_id.to_string();
|
|
|
|
let weak_ws_client = Arc::downgrade(&self.ws_client);
|
|
|
|
FutureResult::new(async move {
|
|
|
|
match weak_ws_client.upgrade() {
|
2023-10-02 09:22:22 +00:00
|
|
|
None => Ok(None),
|
|
|
|
Some(ws_client) => {
|
2023-10-07 01:58:44 +00:00
|
|
|
let channel = ws_client.subscribe(BusinessID::CollabId, object_id).ok();
|
|
|
|
let connect_state_recv = ws_client.subscribe_connect_state();
|
2023-10-02 09:22:22 +00:00
|
|
|
Ok(channel.map(|c| (c, connect_state_recv)))
|
|
|
|
},
|
2023-09-17 09:14:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
FutureResult::new(async { Ok(None) })
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
2023-09-01 14:27:29 +00:00
|
|
|
|
|
|
|
fn file_storage(&self) -> Option<Arc<dyn FileStorageService>> {
|
2023-10-02 09:22:22 +00:00
|
|
|
let client = AFServerImpl(self.get_client());
|
|
|
|
Some(Arc::new(AFCloudFileStorageServiceImpl::new(client)))
|
2023-09-01 14:27:29 +00:00
|
|
|
}
|
2023-05-21 10:53:59 +00:00
|
|
|
}
|
2023-09-17 09:14:34 +00:00
|
|
|
|
|
|
|
/// Spawns a new asynchronous task to handle WebSocket connections based on token state.
|
|
|
|
///
|
|
|
|
/// This function listens to the `token_state_rx` channel for token state updates. Depending on the
|
|
|
|
/// received state, it either refreshes the WebSocket connection or disconnects from it.
|
|
|
|
fn spawn_ws_conn(
|
|
|
|
device_id: &Arc<parking_lot::RwLock<String>>,
|
|
|
|
mut token_state_rx: TokenStateReceiver,
|
2023-10-07 01:58:44 +00:00
|
|
|
ws_client: &Arc<WSClient>,
|
2023-10-02 09:22:22 +00:00
|
|
|
api_client: &Arc<Client>,
|
2023-09-17 09:14:34 +00:00
|
|
|
) {
|
2023-10-02 09:22:22 +00:00
|
|
|
let weak_device_id = Arc::downgrade(device_id);
|
|
|
|
let weak_ws_client = Arc::downgrade(ws_client);
|
|
|
|
let weak_api_client = Arc::downgrade(api_client);
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
if let Some(ws_client) = weak_ws_client.upgrade() {
|
2023-10-07 01:58:44 +00:00
|
|
|
let mut state_recv = ws_client.subscribe_connect_state();
|
2023-10-02 09:22:22 +00:00
|
|
|
while let Ok(state) = state_recv.recv().await {
|
|
|
|
if !state.is_timeout() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to reconnect if the connection is timed out.
|
|
|
|
if let (Some(api_client), Some(device_id)) =
|
|
|
|
(weak_api_client.upgrade(), weak_device_id.upgrade())
|
|
|
|
{
|
|
|
|
let device_id = device_id.read().clone();
|
|
|
|
if let Ok(ws_addr) = api_client.ws_url(&device_id) {
|
2023-10-07 01:58:44 +00:00
|
|
|
info!("🟢WebSocket Reconnecting");
|
|
|
|
let _ = ws_client.connect(ws_addr).await;
|
2023-10-02 09:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-09-17 09:14:34 +00:00
|
|
|
let weak_device_id = Arc::downgrade(device_id);
|
|
|
|
let weak_ws_client = Arc::downgrade(ws_client);
|
|
|
|
let weak_api_client = Arc::downgrade(api_client);
|
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Ok(token_state) = token_state_rx.recv().await {
|
2023-10-07 01:58:44 +00:00
|
|
|
info!("🟢Token state: {:?}", token_state);
|
2023-09-17 09:14:34 +00:00
|
|
|
match token_state {
|
|
|
|
TokenState::Refresh => {
|
|
|
|
if let (Some(api_client), Some(ws_client), Some(device_id)) = (
|
|
|
|
weak_api_client.upgrade(),
|
|
|
|
weak_ws_client.upgrade(),
|
|
|
|
weak_device_id.upgrade(),
|
|
|
|
) {
|
|
|
|
let device_id = device_id.read().clone();
|
2023-10-02 09:22:22 +00:00
|
|
|
if let Ok(ws_addr) = api_client.ws_url(&device_id) {
|
2023-10-07 01:58:44 +00:00
|
|
|
let _ = ws_client.connect(ws_addr).await;
|
2023-09-17 09:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
TokenState::Invalid => {
|
|
|
|
if let Some(ws_client) = weak_ws_client.upgrade() {
|
2023-10-07 01:58:44 +00:00
|
|
|
info!("🟡WebSocket Disconnecting");
|
|
|
|
ws_client.disconnect().await;
|
2023-09-17 09:14:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait AFServer: Send + Sync + 'static {
|
|
|
|
fn get_client(&self) -> Option<Arc<AFCloudClient>>;
|
|
|
|
fn try_get_client(&self) -> Result<Arc<AFCloudClient>, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AFServerImpl(pub Option<Arc<AFCloudClient>>);
|
|
|
|
|
|
|
|
impl AFServer for AFServerImpl {
|
|
|
|
fn get_client(&self) -> Option<Arc<AFCloudClient>> {
|
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_get_client(&self) -> Result<Arc<AFCloudClient>, Error> {
|
|
|
|
match self.0.clone() {
|
|
|
|
None => Err(
|
|
|
|
FlowyError::new(
|
|
|
|
ErrorCode::DataSyncRequired,
|
|
|
|
"Data Sync is disabled, please enable it first",
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
Some(client) => Ok(client),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|