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-12-26 02:03:42 +08:00
|
|
|
use client_api::entity::{QueryCollab, QueryCollabParams};
|
2023-10-02 17:22:22 +08:00
|
|
|
use client_api::error::ErrorCode::RecordNotFound;
|
2023-12-29 13:02:27 +08:00
|
|
|
use collab::core::collab::CollabDocState;
|
2024-01-03 11:41:29 +08:00
|
|
|
use collab::core::collab_plugin::EncodedCollab;
|
2023-10-10 19:05:55 +08:00
|
|
|
use collab_entity::CollabType;
|
|
|
|
|
use tracing::error;
|
2023-07-29 09:46:24 +08:00
|
|
|
|
2024-01-11 14:42:03 +08:00
|
|
|
use flowy_database_pub::cloud::{CollabDocStateByOid, 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,
|
|
|
|
|
{
|
2024-02-04 05:49:45 +08:00
|
|
|
fn get_database_object_doc_state(
|
2023-07-29 09:46:24 +08:00
|
|
|
&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-12-29 13:02:27 +08:00
|
|
|
) -> FutureResult<CollabDocState, 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-12-26 02:03:42 +08:00
|
|
|
inner: QueryCollab {
|
|
|
|
|
object_id,
|
|
|
|
|
collab_type,
|
|
|
|
|
},
|
2023-10-02 17:22:22 +08:00
|
|
|
};
|
|
|
|
|
match try_get_client?.get_collab(params).await {
|
2023-12-29 13:02:27 +08:00
|
|
|
Ok(data) => Ok(data.doc_state.to_vec()),
|
2023-10-02 17:22:22 +08:00
|
|
|
Err(err) => {
|
|
|
|
|
if err.code == RecordNotFound {
|
|
|
|
|
Ok(vec![])
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::new(err))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 05:49:45 +08:00
|
|
|
fn batch_get_database_object_doc_state(
|
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-12-29 13:02:27 +08:00
|
|
|
) -> FutureResult<CollabDocStateByOid, 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?;
|
2023-12-26 02:03:42 +08:00
|
|
|
let params = object_ids
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|object_id| QueryCollab {
|
|
|
|
|
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 {
|
2023-11-06 00:47:20 +08:00
|
|
|
Success { encode_collab_v1 } => {
|
2024-01-03 11:41:29 +08:00
|
|
|
match EncodedCollab::decode_from_bytes(&encode_collab_v1) {
|
2023-12-29 13:02:27 +08:00
|
|
|
Ok(encode) => Some((object_id, encode.doc_state.to_vec())),
|
2023-11-06 00:47:20 +08:00
|
|
|
Err(err) => {
|
|
|
|
|
error!("Failed to decode collab: {}", err);
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-10-10 19:05:55 +08:00
|
|
|
Failed { error } => {
|
|
|
|
|
error!("Failed to get {} update: {}", object_id, error);
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
})
|
2023-12-29 13:02:27 +08:00
|
|
|
.collect::<CollabDocStateByOid>(),
|
2023-10-10 19:05:55 +08:00
|
|
|
)
|
|
|
|
|
})
|
2023-07-14 13:37:13 +08:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 05:49:45 +08:00
|
|
|
fn get_database_collab_object_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
|
|
|
}
|
|
|
|
|
}
|