mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
test: Import folder test (#4321)
* fix: import old version appflowy data * chore: add 037 test * chore: add default appflowy cloud url * chore: bump collab
This commit is contained in:
@ -383,21 +383,30 @@ impl TryInto<ConvertDataParams> for ConvertDataPayloadPB {
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, ProtoBuf)]
|
||||
pub struct RepeatedDocumentSnapshotPB {
|
||||
pub struct RepeatedDocumentSnapshotMetaPB {
|
||||
#[pb(index = 1)]
|
||||
pub items: Vec<DocumentSnapshotPB>,
|
||||
pub items: Vec<DocumentSnapshotMetaPB>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, ProtoBuf)]
|
||||
pub struct DocumentSnapshotMetaPB {
|
||||
#[pb(index = 1)]
|
||||
pub snapshot_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub object_id: String,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub created_at: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, ProtoBuf)]
|
||||
pub struct DocumentSnapshotPB {
|
||||
#[pb(index = 1)]
|
||||
pub snapshot_id: i64,
|
||||
pub object_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub snapshot_desc: String,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub created_at: i64,
|
||||
pub encoded_v1: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, ProtoBuf)]
|
||||
@ -452,3 +461,14 @@ impl TryInto<TextDeltaParams> for TextDeltaPayloadPB {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DocumentSnapshotMeta {
|
||||
pub snapshot_id: String,
|
||||
pub object_id: String,
|
||||
pub created_at: i64,
|
||||
}
|
||||
|
||||
pub struct DocumentSnapshotData {
|
||||
pub object_id: String,
|
||||
pub encoded_v1: Vec<u8>,
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ pub(crate) async fn create_document_handler(
|
||||
) -> FlowyResult<()> {
|
||||
let manager = upgrade_document(manager)?;
|
||||
let params: CreateDocumentParams = data.into_inner().try_into()?;
|
||||
let uid = manager.user.user_id()?;
|
||||
let uid = manager.user_service.user_id()?;
|
||||
manager
|
||||
.create_document(uid, ¶ms.document_id, params.initial_data)
|
||||
.await?;
|
||||
@ -208,15 +208,25 @@ pub(crate) async fn can_undo_redo_handler(
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn get_snapshot_handler(
|
||||
pub(crate) async fn get_snapshot_meta_handler(
|
||||
data: AFPluginData<OpenDocumentPayloadPB>,
|
||||
manager: AFPluginState<Weak<DocumentManager>>,
|
||||
) -> DataResult<RepeatedDocumentSnapshotPB, FlowyError> {
|
||||
) -> DataResult<RepeatedDocumentSnapshotMetaPB, FlowyError> {
|
||||
let manager = upgrade_document(manager)?;
|
||||
let params: OpenDocumentParams = data.into_inner().try_into()?;
|
||||
let doc_id = params.document_id;
|
||||
let snapshots = manager.get_document_snapshots(&doc_id, 10).await?;
|
||||
data_result_ok(RepeatedDocumentSnapshotPB { items: snapshots })
|
||||
let snapshots = manager.get_document_snapshot_meta(&doc_id, 10).await?;
|
||||
data_result_ok(RepeatedDocumentSnapshotMetaPB { items: snapshots })
|
||||
}
|
||||
|
||||
pub(crate) async fn get_snapshot_data_handler(
|
||||
data: AFPluginData<DocumentSnapshotMetaPB>,
|
||||
manager: AFPluginState<Weak<DocumentManager>>,
|
||||
) -> DataResult<DocumentSnapshotPB, FlowyError> {
|
||||
let manager = upgrade_document(manager)?;
|
||||
let params = data.into_inner();
|
||||
let snapshot = manager.get_document_snapshot(¶ms.snapshot_id).await?;
|
||||
data_result_ok(snapshot)
|
||||
}
|
||||
|
||||
impl From<BlockActionPB> for BlockAction {
|
||||
|
@ -5,7 +5,7 @@ use strum_macros::Display;
|
||||
use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
|
||||
use lib_dispatch::prelude::AFPlugin;
|
||||
|
||||
use crate::event_handler::get_snapshot_handler;
|
||||
use crate::event_handler::get_snapshot_meta_handler;
|
||||
use crate::{event_handler::*, manager::DocumentManager};
|
||||
|
||||
pub fn init(document_manager: Weak<DocumentManager>) -> AFPlugin {
|
||||
@ -24,7 +24,14 @@ pub fn init(document_manager: Weak<DocumentManager>) -> AFPlugin {
|
||||
.event(DocumentEvent::Redo, redo_handler)
|
||||
.event(DocumentEvent::Undo, undo_handler)
|
||||
.event(DocumentEvent::CanUndoRedo, can_undo_redo_handler)
|
||||
.event(DocumentEvent::GetDocumentSnapshots, get_snapshot_handler)
|
||||
.event(
|
||||
DocumentEvent::GetDocumentSnapshotMeta,
|
||||
get_snapshot_meta_handler,
|
||||
)
|
||||
.event(
|
||||
DocumentEvent::GetDocumentSnapshot,
|
||||
get_snapshot_data_handler,
|
||||
)
|
||||
.event(DocumentEvent::CreateText, create_text_handler)
|
||||
.event(DocumentEvent::ApplyTextDeltaEvent, apply_text_delta_handler)
|
||||
.event(DocumentEvent::ConvertDocument, convert_document_handler)
|
||||
@ -73,8 +80,11 @@ pub enum DocumentEvent {
|
||||
)]
|
||||
CanUndoRedo = 8,
|
||||
|
||||
#[event(input = "OpenDocumentPayloadPB", output = "RepeatedDocumentSnapshotPB")]
|
||||
GetDocumentSnapshots = 9,
|
||||
#[event(
|
||||
input = "OpenDocumentPayloadPB",
|
||||
output = "RepeatedDocumentSnapshotMetaPB"
|
||||
)]
|
||||
GetDocumentSnapshotMeta = 9,
|
||||
|
||||
#[event(input = "TextDeltaPayloadPB")]
|
||||
CreateText = 10,
|
||||
@ -95,4 +105,7 @@ pub enum DocumentEvent {
|
||||
output = "ConvertDataToJsonResponsePB"
|
||||
)]
|
||||
ConvertDataToJSON = 13,
|
||||
|
||||
#[event(input = "DocumentSnapshotMetaPB", output = "DocumentSnapshotPB")]
|
||||
GetDocumentSnapshot = 14,
|
||||
}
|
||||
|
@ -21,39 +21,51 @@ use flowy_error::{internal_error, ErrorCode, FlowyError, FlowyResult};
|
||||
use flowy_storage::FileStorageService;
|
||||
|
||||
use crate::document::MutexDocument;
|
||||
use crate::entities::DocumentSnapshotPB;
|
||||
use crate::entities::{
|
||||
DocumentSnapshotData, DocumentSnapshotMeta, DocumentSnapshotMetaPB, DocumentSnapshotPB,
|
||||
};
|
||||
use crate::reminder::DocumentReminderAction;
|
||||
|
||||
pub trait DocumentUser: Send + Sync {
|
||||
pub trait DocumentUserService: Send + Sync {
|
||||
fn user_id(&self) -> Result<i64, FlowyError>;
|
||||
fn workspace_id(&self) -> Result<String, FlowyError>;
|
||||
fn token(&self) -> Result<Option<String>, FlowyError>; // unused now.
|
||||
fn collab_db(&self, uid: i64) -> Result<Weak<CollabKVDB>, FlowyError>;
|
||||
}
|
||||
|
||||
pub trait DocumentSnapshotService: Send + Sync {
|
||||
fn get_document_snapshot_metas(
|
||||
&self,
|
||||
document_id: &str,
|
||||
) -> FlowyResult<Vec<DocumentSnapshotMeta>>;
|
||||
fn get_document_snapshot(&self, snapshot_id: &str) -> FlowyResult<DocumentSnapshotData>;
|
||||
}
|
||||
|
||||
pub struct DocumentManager {
|
||||
pub user: Arc<dyn DocumentUser>,
|
||||
pub user_service: Arc<dyn DocumentUserService>,
|
||||
collab_builder: Arc<AppFlowyCollabBuilder>,
|
||||
documents: Arc<Mutex<LruCache<String, Arc<MutexDocument>>>>,
|
||||
#[allow(dead_code)]
|
||||
cloud_service: Arc<dyn DocumentCloudService>,
|
||||
storage_service: Weak<dyn FileStorageService>,
|
||||
snapshot_service: Arc<dyn DocumentSnapshotService>,
|
||||
}
|
||||
|
||||
impl DocumentManager {
|
||||
pub fn new(
|
||||
user: Arc<dyn DocumentUser>,
|
||||
user_service: Arc<dyn DocumentUserService>,
|
||||
collab_builder: Arc<AppFlowyCollabBuilder>,
|
||||
cloud_service: Arc<dyn DocumentCloudService>,
|
||||
storage_service: Weak<dyn FileStorageService>,
|
||||
snapshot_service: Arc<dyn DocumentSnapshotService>,
|
||||
) -> Self {
|
||||
let documents = Arc::new(Mutex::new(LruCache::new(NonZeroUsize::new(10).unwrap())));
|
||||
Self {
|
||||
user,
|
||||
user_service,
|
||||
collab_builder,
|
||||
documents,
|
||||
cloud_service,
|
||||
storage_service,
|
||||
snapshot_service,
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,7 +112,7 @@ impl DocumentManager {
|
||||
} else {
|
||||
let result: Result<CollabDocState, FlowyError> = self
|
||||
.cloud_service
|
||||
.get_document_doc_state(doc_id, &self.user.workspace_id()?)
|
||||
.get_document_doc_state(doc_id, &self.user_service.workspace_id()?)
|
||||
.await;
|
||||
|
||||
match result {
|
||||
@ -140,11 +152,11 @@ impl DocumentManager {
|
||||
// Try to get the document from the cloud service
|
||||
doc_state = self
|
||||
.cloud_service
|
||||
.get_document_doc_state(doc_id, &self.user.workspace_id()?)
|
||||
.get_document_doc_state(doc_id, &self.user_service.workspace_id()?)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let uid = self.user.user_id()?;
|
||||
let uid = self.user_service.user_id()?;
|
||||
event!(tracing::Level::DEBUG, "Initialize document: {}", doc_id);
|
||||
let collab = self
|
||||
.collab_for_document(uid, doc_id, doc_state, true)
|
||||
@ -165,10 +177,10 @@ impl DocumentManager {
|
||||
if !self.is_doc_exist(doc_id)? {
|
||||
updates = self
|
||||
.cloud_service
|
||||
.get_document_doc_state(doc_id, &self.user.workspace_id()?)
|
||||
.get_document_doc_state(doc_id, &self.user_service.workspace_id()?)
|
||||
.await?;
|
||||
}
|
||||
let uid = self.user.user_id()?;
|
||||
let uid = self.user_service.user_id()?;
|
||||
let collab = self
|
||||
.collab_for_document(uid, doc_id, updates, false)
|
||||
.await?;
|
||||
@ -190,8 +202,8 @@ impl DocumentManager {
|
||||
}
|
||||
|
||||
pub fn delete_document(&self, doc_id: &str) -> FlowyResult<()> {
|
||||
let uid = self.user.user_id()?;
|
||||
if let Some(db) = self.user.collab_db(uid)?.upgrade() {
|
||||
let uid = self.user_service.user_id()?;
|
||||
if let Some(db) = self.user_service.collab_db(uid)?.upgrade() {
|
||||
let _ = db.with_write_txn(|txn| {
|
||||
txn.delete_doc(uid, &doc_id)?;
|
||||
Ok(())
|
||||
@ -204,25 +216,46 @@ impl DocumentManager {
|
||||
}
|
||||
|
||||
/// Return the list of snapshots of the document.
|
||||
pub async fn get_document_snapshots(
|
||||
pub async fn get_document_snapshot_meta(
|
||||
&self,
|
||||
document_id: &str,
|
||||
limit: usize,
|
||||
) -> FlowyResult<Vec<DocumentSnapshotPB>> {
|
||||
let workspace_id = self.user.workspace_id()?;
|
||||
let snapshots = self
|
||||
.cloud_service
|
||||
.get_document_snapshots(document_id, limit, &workspace_id)
|
||||
.await?
|
||||
_limit: usize,
|
||||
) -> FlowyResult<Vec<DocumentSnapshotMetaPB>> {
|
||||
let metas = self
|
||||
.snapshot_service
|
||||
.get_document_snapshot_metas(document_id)?
|
||||
.into_iter()
|
||||
.map(|snapshot| DocumentSnapshotPB {
|
||||
snapshot_id: snapshot.snapshot_id,
|
||||
snapshot_desc: "".to_string(),
|
||||
created_at: snapshot.created_at,
|
||||
.map(|meta| DocumentSnapshotMetaPB {
|
||||
snapshot_id: meta.snapshot_id,
|
||||
object_id: meta.object_id,
|
||||
created_at: meta.created_at,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(snapshots)
|
||||
// let snapshots = self
|
||||
// .cloud_service
|
||||
// .get_document_snapshots(document_id, limit, &workspace_id)
|
||||
// .await?
|
||||
// .into_iter()
|
||||
// .map(|snapshot| DocumentSnapshotPB {
|
||||
// snapshot_id: snapshot.snapshot_id,
|
||||
// snapshot_desc: "".to_string(),
|
||||
// created_at: snapshot.created_at,
|
||||
// })
|
||||
// .collect::<Vec<_>>();
|
||||
|
||||
Ok(metas)
|
||||
}
|
||||
|
||||
pub async fn get_document_snapshot(&self, snapshot_id: &str) -> FlowyResult<DocumentSnapshotPB> {
|
||||
let snapshot = self
|
||||
.snapshot_service
|
||||
.get_document_snapshot(snapshot_id)
|
||||
.map(|snapshot| DocumentSnapshotPB {
|
||||
object_id: snapshot.object_id,
|
||||
encoded_v1: snapshot.encoded_v1,
|
||||
})?;
|
||||
Ok(snapshot)
|
||||
}
|
||||
|
||||
async fn collab_for_document(
|
||||
@ -232,7 +265,7 @@ impl DocumentManager {
|
||||
doc_state: CollabDocState,
|
||||
sync_enable: bool,
|
||||
) -> FlowyResult<Arc<MutexCollab>> {
|
||||
let db = self.user.collab_db(uid)?;
|
||||
let db = self.user_service.collab_db(uid)?;
|
||||
let collab = self
|
||||
.collab_builder
|
||||
.build_with_config(
|
||||
@ -249,8 +282,8 @@ impl DocumentManager {
|
||||
}
|
||||
|
||||
fn is_doc_exist(&self, doc_id: &str) -> FlowyResult<bool> {
|
||||
let uid = self.user.user_id()?;
|
||||
if let Some(collab_db) = self.user.collab_db(uid)?.upgrade() {
|
||||
let uid = self.user_service.user_id()?;
|
||||
if let Some(collab_db) = self.user_service.collab_db(uid)?.upgrade() {
|
||||
let read_txn = collab_db.read_txn();
|
||||
Ok(read_txn.is_exist(uid, doc_id))
|
||||
} else {
|
||||
|
@ -14,7 +14,11 @@ async fn undo_redo_test() {
|
||||
|
||||
// create a document
|
||||
_ = test
|
||||
.create_document(test.user.user_id().unwrap(), &doc_id, Some(data.clone()))
|
||||
.create_document(
|
||||
test.user_service.user_id().unwrap(),
|
||||
&doc_id,
|
||||
Some(data.clone()),
|
||||
)
|
||||
.await;
|
||||
|
||||
// open a document
|
||||
|
@ -13,7 +13,7 @@ async fn restore_document() {
|
||||
// create a document
|
||||
let doc_id: String = gen_document_id();
|
||||
let data = default_document_data();
|
||||
let uid = test.user.user_id().unwrap();
|
||||
let uid = test.user_service.user_id().unwrap();
|
||||
test
|
||||
.create_document(uid, &doc_id, Some(data.clone()))
|
||||
.await
|
||||
@ -51,7 +51,7 @@ async fn restore_document() {
|
||||
#[tokio::test]
|
||||
async fn document_apply_insert_action() {
|
||||
let test = DocumentTest::new();
|
||||
let uid = test.user.user_id().unwrap();
|
||||
let uid = test.user_service.user_id().unwrap();
|
||||
let doc_id: String = gen_document_id();
|
||||
let data = default_document_data();
|
||||
|
||||
@ -105,7 +105,7 @@ async fn document_apply_insert_action() {
|
||||
async fn document_apply_update_page_action() {
|
||||
let test = DocumentTest::new();
|
||||
let doc_id: String = gen_document_id();
|
||||
let uid = test.user.user_id().unwrap();
|
||||
let uid = test.user_service.user_id().unwrap();
|
||||
let data = default_document_data();
|
||||
|
||||
// create a document
|
||||
@ -147,7 +147,7 @@ async fn document_apply_update_page_action() {
|
||||
#[tokio::test]
|
||||
async fn document_apply_update_action() {
|
||||
let test = DocumentTest::new();
|
||||
let uid = test.user.user_id().unwrap();
|
||||
let uid = test.user_service.user_id().unwrap();
|
||||
let doc_id: String = gen_document_id();
|
||||
let data = default_document_data();
|
||||
|
||||
|
@ -19,9 +19,10 @@ use collab_integrate::collab_builder::{
|
||||
};
|
||||
use collab_integrate::CollabKVDB;
|
||||
use flowy_document::document::MutexDocument;
|
||||
use flowy_document::manager::{DocumentManager, DocumentUser};
|
||||
use flowy_document::entities::{DocumentSnapshotData, DocumentSnapshotMeta};
|
||||
use flowy_document::manager::{DocumentManager, DocumentSnapshotService, DocumentUserService};
|
||||
use flowy_document_deps::cloud::*;
|
||||
use flowy_error::{ErrorCode, FlowyError};
|
||||
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
|
||||
use flowy_storage::{FileStorageService, StorageObject};
|
||||
use lib_infra::async_trait::async_trait;
|
||||
use lib_infra::future::{to_fut, Fut, FutureResult};
|
||||
@ -35,11 +36,13 @@ impl DocumentTest {
|
||||
let user = FakeUser::new();
|
||||
let cloud_service = Arc::new(LocalTestDocumentCloudServiceImpl());
|
||||
let file_storage = Arc::new(DocumentTestFileStorageService) as Arc<dyn FileStorageService>;
|
||||
let document_snapshot = Arc::new(DocumentTestSnapshot);
|
||||
let manager = DocumentManager::new(
|
||||
Arc::new(user),
|
||||
default_collab_builder(),
|
||||
cloud_service,
|
||||
Arc::downgrade(&file_storage),
|
||||
document_snapshot,
|
||||
);
|
||||
Self { inner: manager }
|
||||
}
|
||||
@ -69,7 +72,7 @@ impl FakeUser {
|
||||
}
|
||||
}
|
||||
|
||||
impl DocumentUser for FakeUser {
|
||||
impl DocumentUserService for FakeUser {
|
||||
fn user_id(&self) -> Result<i64, FlowyError> {
|
||||
Ok(1)
|
||||
}
|
||||
@ -110,7 +113,7 @@ pub async fn create_and_open_empty_document() -> (DocumentTest, Arc<MutexDocumen
|
||||
let test = DocumentTest::new();
|
||||
let doc_id: String = gen_document_id();
|
||||
let data = default_document_data();
|
||||
let uid = test.user.user_id().unwrap();
|
||||
let uid = test.user_service.user_id().unwrap();
|
||||
// create a document
|
||||
test
|
||||
.create_document(uid, &doc_id, Some(data.clone()))
|
||||
@ -196,3 +199,17 @@ impl CollabCloudPluginProvider for DefaultCollabStorageProvider {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
struct DocumentTestSnapshot;
|
||||
impl DocumentSnapshotService for DocumentTestSnapshot {
|
||||
fn get_document_snapshot_metas(
|
||||
&self,
|
||||
_document_id: &str,
|
||||
) -> FlowyResult<Vec<DocumentSnapshotMeta>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_document_snapshot(&self, _snapshot_id: &str) -> FlowyResult<DocumentSnapshotData> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user