2023-07-29 09:46:24 +08:00
|
|
|
use anyhow::Error;
|
2023-10-10 19:05:55 +08:00
|
|
|
use client_api::entity::QueryCollabResult::{Failed, Success};
|
2023-10-23 11:43:31 +08:00
|
|
|
use client_api::entity::{BatchQueryCollab, BatchQueryCollabParams, QueryCollabParams};
|
2023-10-02 17:22:22 +08:00
|
|
|
use client_api::error::ErrorCode::RecordNotFound;
|
2023-10-10 19:05:55 +08:00
|
|
|
use collab_entity::CollabType;
|
|
|
|
|
use tracing::error;
|
2023-07-29 09:46:24 +08:00
|
|
|
|
|
|
|
|
use flowy_database_deps::cloud::{
|
2023-07-14 13:37:13 +08:00
|
|
|
CollabObjectUpdate, CollabObjectUpdateByOid, DatabaseCloudService, DatabaseSnapshot,
|
|
|
|
|
};
|
2023-07-05 20:57:09 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
|
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
use crate::af_cloud::AFServer;
|
2023-07-05 20:57:09 +08:00
|
|
|
|
2023-09-17 17:14:34 +08:00
|
|
|
pub(crate) struct AFCloudDatabaseCloudServiceImpl<T>(pub T);
|
|
|
|
|
|
|
|
|
|
impl<T> DatabaseCloudService for AFCloudDatabaseCloudServiceImpl<T>
|
|
|
|
|
where
|
|
|
|
|
T: AFServer,
|
|
|
|
|
{
|
2023-07-29 09:46:24 +08:00
|
|
|
fn get_collab_update(
|
|
|
|
|
&self,
|
2023-10-02 17:22:22 +08:00
|
|
|
object_id: &str,
|
|
|
|
|
collab_type: CollabType,
|
2023-10-23 11:43:31 +08:00
|
|
|
workspace_id: &str,
|
2023-07-29 09:46:24 +08:00
|
|
|
) -> FutureResult<CollabObjectUpdate, Error> {
|
2023-10-23 11:43:31 +08:00
|
|
|
let workspace_id = workspace_id.to_string();
|
2023-10-02 17:22:22 +08:00
|
|
|
let object_id = object_id.to_string();
|
|
|
|
|
let try_get_client = self.0.try_get_client();
|
|
|
|
|
FutureResult::new(async move {
|
|
|
|
|
let params = QueryCollabParams {
|
2023-10-23 11:43:31 +08:00
|
|
|
workspace_id,
|
2023-10-02 17:22:22 +08:00
|
|
|
object_id,
|
|
|
|
|
collab_type,
|
|
|
|
|
};
|
|
|
|
|
match try_get_client?.get_collab(params).await {
|
|
|
|
|
Ok(data) => Ok(vec![data]),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if err.code == RecordNotFound {
|
|
|
|
|
Ok(vec![])
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::new(err))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
fn batch_get_collab_updates(
|
2023-07-05 20:57:09 +08:00
|
|
|
&self,
|
2023-10-10 19:05:55 +08:00
|
|
|
object_ids: Vec<String>,
|
|
|
|
|
object_ty: CollabType,
|
2023-10-23 11:43:31 +08:00
|
|
|
workspace_id: &str,
|
2023-07-29 09:46:24 +08:00
|
|
|
) -> FutureResult<CollabObjectUpdateByOid, Error> {
|
2023-10-23 11:43:31 +08:00
|
|
|
let workspace_id = workspace_id.to_string();
|
2023-10-10 19:05:55 +08:00
|
|
|
let try_get_client = self.0.try_get_client();
|
|
|
|
|
FutureResult::new(async move {
|
|
|
|
|
let client = try_get_client?;
|
|
|
|
|
let params = BatchQueryCollabParams(
|
|
|
|
|
object_ids
|
|
|
|
|
.into_iter()
|
2023-10-23 11:43:31 +08:00
|
|
|
.map(|object_id| BatchQueryCollab {
|
2023-10-10 19:05:55 +08:00
|
|
|
object_id,
|
|
|
|
|
collab_type: object_ty.clone(),
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
);
|
2023-10-23 11:43:31 +08:00
|
|
|
let results = client.batch_get_collab(&workspace_id, params).await?;
|
2023-10-10 19:05:55 +08:00
|
|
|
Ok(
|
|
|
|
|
results
|
|
|
|
|
.0
|
|
|
|
|
.into_iter()
|
|
|
|
|
.flat_map(|(object_id, result)| match result {
|
|
|
|
|
Success { blob } => Some((object_id, vec![blob])),
|
|
|
|
|
Failed { error } => {
|
|
|
|
|
error!("Failed to get {} update: {}", object_id, error);
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.collect::<CollabObjectUpdateByOid>(),
|
|
|
|
|
)
|
|
|
|
|
})
|
2023-07-14 13:37:13 +08:00
|
|
|
}
|
|
|
|
|
|
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> {
|
|
|
|
|
FutureResult::new(async move { Ok(vec![]) })
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
}
|