Skip to content

Commit a954b5f

Browse files
authored
fix: create offline document (#4350)
1 parent 36b88c6 commit a954b5f

File tree

8 files changed

+87
-76
lines changed

8 files changed

+87
-76
lines changed

frontend/appflowy_tauri/src-tauri/Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/appflowy_tauri/src-tauri/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "284
6767
# To switch to the local path, run:
6868
# scripts/tool/update_collab_source.sh
6969
# ⚠️⚠️⚠️️
70-
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
71-
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
72-
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
73-
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
74-
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
75-
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
76-
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
70+
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
71+
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
72+
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
73+
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
74+
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
75+
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
76+
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
7777

7878

7979

frontend/rust-lib/Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/rust-lib/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "284
117117
# To switch to the local path, run:
118118
# scripts/tool/update_collab_source.sh
119119
# ⚠️⚠️⚠️️
120-
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
121-
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
122-
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
123-
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
124-
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
125-
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
126-
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
120+
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
121+
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
122+
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
123+
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
124+
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
125+
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }
126+
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "3c2cb055e47ec9d6bff3d3aeb2a476b85d02cb80" }

frontend/rust-lib/event-integration/tests/user/af_cloud_test/import_af_data_folder_test.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ async fn import_appflowy_data_folder_into_new_view_test() {
111111

112112
// In the 040_local, only the first row has a document with content
113113
let row_document_id = database_row_document_id_from_row_id(&rows[0].id);
114+
let row_document_view = test.get_view(&row_document_id).await;
115+
assert_eq!(row_document_view.id, row_document_view.parent_view_id);
116+
114117
let row_document_data = test.get_document_data(&row_document_id).await;
115118
assert_json_include!(actual: json!(row_document_data), expected: expected_row_doc_json());
116-
117119
drop(cleaner);
118120
}
119121

frontend/rust-lib/flowy-ai/tests/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use flowy_ai::config::OpenAISetting;
22

33
// To run the OpenAI test, you need to create a .env file in the flowy-ai folder.
44
// Use the format: OPENAI_API_KEY=your_api_key
5+
#[allow(dead_code)]
56
pub fn get_openai_config() -> Option<OpenAISetting> {
67
dotenv::from_filename(".env").ok()?;
78
OpenAISetting::from_env().ok()

frontend/rust-lib/flowy-document/src/manager.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,14 @@ impl DocumentManager {
110110
format!("document {} already exists", doc_id),
111111
))
112112
} else {
113-
let result: Result<CollabDocState, FlowyError> = self
114-
.cloud_service
115-
.get_document_doc_state(doc_id, &self.user_service.workspace_id()?)
116-
.await;
117-
118-
match result {
119-
Ok(data) => {
120-
let collab = self.collab_for_document(uid, doc_id, data, false).await?;
121-
collab.lock().flush();
122-
},
123-
Err(err) => {
124-
if err.is_record_not_found() {
125-
let doc_state =
126-
doc_state_from_document_data(doc_id, data.unwrap_or_else(default_document_data))?
127-
.doc_state
128-
.to_vec();
129-
let collab = self
130-
.collab_for_document(uid, doc_id, doc_state, false)
131-
.await?;
132-
collab.lock().flush();
133-
} else {
134-
return Err(err);
135-
}
136-
},
137-
}
138-
113+
let doc_state =
114+
doc_state_from_document_data(doc_id, data.unwrap_or_else(default_document_data))?
115+
.doc_state
116+
.to_vec();
117+
let collab = self
118+
.collab_for_document(uid, doc_id, doc_state, false)
119+
.await?;
120+
collab.lock().flush();
139121
Ok(())
140122
}
141123
}

frontend/rust-lib/flowy-user/src/services/data_import/appflowy_data_import.rs

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use crate::migrations::session_migration::migrate_session_with_user_uuid;
2-
31
use crate::manager::run_collab_data_migration;
4-
2+
use crate::migrations::session_migration::migrate_session_with_user_uuid;
53
use crate::services::data_import::importer::load_collab_by_oid;
64
use crate::services::db::UserDBPath;
75
use crate::services::entities::{Session, UserPaths};
@@ -102,6 +100,7 @@ pub(crate) fn import_appflowy_data_folder(
102100

103101
let mut database_view_ids_by_database_id: HashMap<String, Vec<String>> = HashMap::new();
104102
let row_object_ids = Mutex::new(HashSet::new());
103+
let row_document_object_ids = Mutex::new(HashSet::new());
105104
let document_object_ids = Mutex::new(HashSet::new());
106105
let database_object_ids = Mutex::new(HashSet::new());
107106
let import_container_view_id = match &container_name {
@@ -156,8 +155,17 @@ pub(crate) fn import_appflowy_data_folder(
156155
&mut all_imported_object_ids,
157156
&imported_collab_by_oid,
158157
&row_object_ids,
158+
&row_document_object_ids,
159159
)?;
160160

161+
debug!(
162+
"import row document ids: {:?}",
163+
row_document_object_ids
164+
.lock()
165+
.iter()
166+
.collect::<Vec<&String>>()
167+
);
168+
161169
// the object ids now only contains the document collab object ids
162170
for object_id in &all_imported_object_ids {
163171
if let Some(imported_collab) = imported_collab_by_oid.get(object_id) {
@@ -174,15 +182,18 @@ pub(crate) fn import_appflowy_data_folder(
174182
}
175183

176184
// create a root view that contains all the views
177-
let child_views = import_workspace_views(
185+
let (mut child_views, orphan_views) = import_workspace_views(
178186
&import_container_view_id,
179187
&mut old_to_new_id_map.lock(),
180188
&imported_session,
181189
&imported_collab_read_txn,
182190
)?;
183191

184192
match container_name {
185-
None => Ok(child_views),
193+
None => {
194+
child_views.extend(orphan_views);
195+
Ok(child_views)
196+
},
186197
Some(container_name) => {
187198
let name = if container_name.is_empty() {
188199
format!(
@@ -207,15 +218,18 @@ pub(crate) fn import_appflowy_data_folder(
207218
document_object_ids
208219
.lock()
209220
.insert(import_container_view_id.clone());
210-
let import_container_view =
211-
ViewBuilder::new(session.user_id, session.user_workspace.id.clone())
212-
.with_view_id(import_container_view_id)
213-
.with_layout(ViewLayout::Document)
214-
.with_name(name)
215-
.with_child_views(child_views)
216-
.build();
217-
218-
Ok(vec![import_container_view])
221+
let mut import_container_views =
222+
vec![
223+
ViewBuilder::new(session.user_id, session.user_workspace.id.clone())
224+
.with_view_id(import_container_view_id)
225+
.with_layout(ViewLayout::Document)
226+
.with_name(name)
227+
.with_child_views(child_views)
228+
.build(),
229+
];
230+
231+
import_container_views.extend(orphan_views);
232+
Ok(import_container_views)
219233
},
220234
}
221235
})?;
@@ -280,6 +294,7 @@ fn migrate_databases<'a, W>(
280294
imported_object_ids: &mut Vec<String>,
281295
imported_collab_by_oid: &HashMap<String, Collab>,
282296
row_object_ids: &Mutex<HashSet<String>>,
297+
row_document_object_ids: &Mutex<HashSet<String>>,
283298
) -> Result<(), PersistenceError>
284299
where
285300
W: CollabKVAction<'a>,
@@ -312,6 +327,8 @@ where
312327
let old_row_id = String::from(row_order.id.clone());
313328
let old_row_document_id = database_row_document_id_from_row_id(&old_row_id);
314329
let new_row_id = old_to_new_id_map.lock().renew_id(&old_row_id);
330+
// The row document might not exist in the database row. But by querying the old_row_document_id,
331+
// we can know the document of the row is exist or not.
315332
let new_row_document_id = database_row_document_id_from_row_id(&new_row_id);
316333

317334
old_to_new_id_map
@@ -328,7 +345,6 @@ where
328345
.iter()
329346
.map(|order| order.id.clone().into_inner())
330347
.collect::<Vec<String>>();
331-
332348
row_object_ids.lock().extend(new_row_ids);
333349
});
334350

@@ -369,18 +385,18 @@ where
369385
);
370386
}
371387

388+
// imported_collab_by_oid contains all the collab object ids, including the row document collab object ids.
389+
// So, if the id exist in the imported_collab_by_oid, it means the row document collab object is exist.
372390
let imported_row_document_id = database_row_document_id_from_row_id(imported_row_id);
373391
if imported_collab_by_oid
374392
.get(&imported_row_document_id)
375393
.is_some()
376394
{
377395
let new_row_document_id = old_to_new_id_map.lock().renew_id(&imported_row_document_id);
378-
info!(
379-
"map row document from: {}, to: {}",
380-
imported_row_document_id, new_row_document_id,
381-
);
396+
row_document_object_ids.lock().insert(new_row_document_id);
382397
}
383398
}
399+
384400
Ok(())
385401
}
386402

@@ -436,7 +452,7 @@ fn import_workspace_views<'a, W>(
436452
old_to_new_id_map: &mut OldToNewIdMap,
437453
other_session: &Session,
438454
other_collab_read_txn: &W,
439-
) -> Result<Vec<ParentChildViews>, PersistenceError>
455+
) -> Result<(Vec<ParentChildViews>, Vec<ParentChildViews>), PersistenceError>
440456
where
441457
W: CollabKVAction<'a>,
442458
PersistenceError: From<W::Error>,
@@ -523,7 +539,17 @@ where
523539
)
524540
.collect::<Vec<ParentChildViews>>();
525541

526-
Ok(parent_views)
542+
// the views in the all_views_map now, should be the orphan views
543+
debug!("create orphan views: {:?}", all_views_map.keys());
544+
let mut orphan_views = vec![];
545+
for orphan_view in all_views_map.into_values() {
546+
orphan_views.push(ParentChildViews {
547+
parent_view: orphan_view,
548+
child_views: vec![],
549+
});
550+
}
551+
552+
Ok((parent_views, orphan_views))
527553
}
528554

529555
fn parent_view_from_view(

0 commit comments

Comments
 (0)