2023-07-29 09:46:24 +08:00
|
|
|
use anyhow::Error;
|
2023-09-13 22:11:51 +08:00
|
|
|
use collab_define::CollabType;
|
2023-07-29 09:46:24 +08:00
|
|
|
use tokio::sync::oneshot::channel;
|
|
|
|
|
|
|
|
use flowy_database_deps::cloud::{
|
|
|
|
CollabObjectUpdate, CollabObjectUpdateByOid, DatabaseCloudService, DatabaseSnapshot,
|
|
|
|
};
|
|
|
|
use lib_infra::future::FutureResult;
|
|
|
|
|
|
|
|
use crate::supabase::api::request::{
|
2023-08-17 23:46:39 +08:00
|
|
|
get_snapshots_from_server, BatchFetchObjectUpdateAction, FetchObjectUpdateAction,
|
2023-07-29 09:46:24 +08:00
|
|
|
};
|
|
|
|
use crate::supabase::api::SupabaseServerService;
|
|
|
|
|
2023-08-05 15:02:05 +08:00
|
|
|
pub struct SupabaseDatabaseServiceImpl<T> {
|
2023-07-29 09:46:24 +08:00
|
|
|
server: T,
|
|
|
|
}
|
|
|
|
|
2023-08-05 15:02:05 +08:00
|
|
|
impl<T> SupabaseDatabaseServiceImpl<T> {
|
2023-07-29 09:46:24 +08:00
|
|
|
pub fn new(server: T) -> Self {
|
|
|
|
Self { server }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 15:02:05 +08:00
|
|
|
impl<T> DatabaseCloudService for SupabaseDatabaseServiceImpl<T>
|
2023-07-29 09:46:24 +08:00
|
|
|
where
|
|
|
|
T: SupabaseServerService,
|
|
|
|
{
|
|
|
|
fn get_collab_update(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
2023-10-02 17:22:22 +08:00
|
|
|
collab_type: CollabType,
|
2023-07-29 09:46:24 +08:00
|
|
|
) -> FutureResult<CollabObjectUpdate, Error> {
|
|
|
|
let try_get_postgrest = self.server.try_get_weak_postgrest();
|
|
|
|
let object_id = object_id.to_string();
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
tx.send(
|
|
|
|
async move {
|
|
|
|
let postgrest = try_get_postgrest?;
|
2023-10-02 17:22:22 +08:00
|
|
|
let updates = FetchObjectUpdateAction::new(object_id.to_string(), collab_type, postgrest)
|
2023-07-29 09:46:24 +08:00
|
|
|
.run_with_fix_interval(5, 10)
|
2023-08-18 22:32:51 +08:00
|
|
|
.await?;
|
|
|
|
Ok(updates)
|
2023-07-29 09:46:24 +08:00
|
|
|
}
|
|
|
|
.await,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
FutureResult::new(async { rx.await? })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn batch_get_collab_updates(
|
|
|
|
&self,
|
|
|
|
object_ids: Vec<String>,
|
|
|
|
object_ty: CollabType,
|
|
|
|
) -> FutureResult<CollabObjectUpdateByOid, Error> {
|
|
|
|
let try_get_postgrest = self.server.try_get_weak_postgrest();
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
tx.send(
|
|
|
|
async move {
|
|
|
|
let postgrest = try_get_postgrest?;
|
|
|
|
BatchFetchObjectUpdateAction::new(object_ids, object_ty, postgrest)
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
.await,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
FutureResult::new(async { rx.await? })
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
fn get_collab_snapshots(
|
2023-07-29 09:46:24 +08:00
|
|
|
&self,
|
|
|
|
object_id: &str,
|
2023-08-17 23:46:39 +08:00
|
|
|
limit: usize,
|
|
|
|
) -> FutureResult<Vec<DatabaseSnapshot>, Error> {
|
2023-07-29 09:46:24 +08:00
|
|
|
let try_get_postgrest = self.server.try_get_postgrest();
|
|
|
|
let object_id = object_id.to_string();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let postgrest = try_get_postgrest?;
|
2023-08-17 23:46:39 +08:00
|
|
|
let snapshots = get_snapshots_from_server(&object_id, postgrest, limit)
|
2023-07-29 09:46:24 +08:00
|
|
|
.await?
|
2023-08-17 23:46:39 +08:00
|
|
|
.into_iter()
|
2023-07-29 09:46:24 +08:00
|
|
|
.map(|snapshot| DatabaseSnapshot {
|
|
|
|
snapshot_id: snapshot.sid,
|
|
|
|
database_id: snapshot.oid,
|
|
|
|
data: snapshot.blob,
|
|
|
|
created_at: snapshot.created_at,
|
2023-08-17 23:46:39 +08:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(snapshots)
|
2023-07-29 09:46:24 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|