feat: add attachment count

This commit is contained in:
Zack Fu Zi Xiang 2024-09-10 23:27:19 +08:00
parent bcb2f520a2
commit 8766344073
No known key found for this signature in database
1 changed files with 66 additions and 67 deletions

View File

@ -809,90 +809,89 @@ impl PublishCollabDuplicator {
} }
{ {
// handle document in database row // handle row meta
let row_meta: MapRef = db_row_collab let row_meta: MapRef = db_row_collab
.data .data
.get(&txn, "meta") .get(&txn, "meta")
.ok_or_else(|| { .ok_or_else(|| {
AppError::RecordNotFound(format!( AppError::RecordNotFound(format!(
"no data found in database row collab: {}", "no meta found in database row collab: {}",
pub_row_id pub_row_id
)) ))
})? })?
.cast() .cast()
.map_err(|err| AppError::Unhandled(format!("not a map: {:?}", err)))?; .map_err(|err| AppError::Unhandled(format!("not a map: {:?}", err)))?;
// is document empty {
let pub_is_doc_empty_key = // handle document in database row
meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::IsDocumentEmpty); let pub_is_doc_empty_key =
let pub_is_doc_empty = row_meta.get(&txn, &pub_is_doc_empty_key); meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::IsDocumentEmpty);
let pub_is_doc_empty = row_meta.get(&txn, &pub_is_doc_empty_key);
if let Some(Out::Any(any::Any::Bool(is_doc_empty))) = pub_is_doc_empty {
row_meta.remove(&mut txn, &pub_is_doc_empty_key);
if !is_doc_empty {
let pub_row_doc_id =
meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::DocumentId);
let dup_row_doc_id =
meta_id_from_row_id(&dup_row_id.parse()?, RowMetaKey::DocumentId);
row_meta.insert(&mut txn, dup_row_doc_id, any::Any::Bool(false));
// icon id let row_doc_doc_state = published_db
let pub_icon_id_key = meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::IconId); .database_row_document_collabs
let pub_icon_id = row_meta.get(&txn, &pub_icon_id_key); .get(&pub_row_doc_id)
.ok_or_else(|| {
AppError::RecordNotFound(format!("doc not found: {}", pub_row_doc_id))
})?;
// cover id let doc_collab =
let pub_cover_id_key = meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::CoverId); collab_from_doc_state(row_doc_doc_state.to_vec(), &pub_row_doc_id)?;
let pub_cover_id = row_meta.get(&txn, &pub_cover_id_key); let doc =
Document::open(doc_collab).map_err(|e| AppError::Unhandled(e.to_string()))?;
// TODO: attachment_count let mut new_doc_view = Box::pin(self.deep_copy_doc(
// // attachment_count &pub_row_doc_id,
// let pub_attach_count_key = meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::CoverId); dup_row_doc_id.clone(),
// let pub_attachment_count = row_meta.get(&txn, &pub_attach_count_key); doc,
PublishViewMetaData::default(),
// clear the meta ))
row_meta.clear(&mut txn); .await?;
new_doc_view.parent_view_id.clone_from(&dup_row_doc_id); // orphan folder view
// if doc exists, duplicate it! self
if let Some(Out::Any(any::Any::Bool(is_doc_empty))) = pub_is_doc_empty { .views_to_add
if !is_doc_empty { .insert(dup_row_doc_id.clone(), new_doc_view);
let pub_row_doc_id = row_meta.insert(&mut txn, dup_row_doc_id, any::Any::Bool(false));
meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::DocumentId); }
let dup_row_doc_id =
meta_id_from_row_id(&dup_row_id.parse()?, RowMetaKey::DocumentId);
let row_doc_doc_state = published_db
.database_row_document_collabs
.get(&pub_row_doc_id)
.ok_or_else(|| {
AppError::RecordNotFound(format!("doc not found: {}", pub_row_doc_id))
})?;
let doc_collab = collab_from_doc_state(row_doc_doc_state.to_vec(), &pub_row_doc_id)?;
let doc =
Document::open(doc_collab).map_err(|e| AppError::Unhandled(e.to_string()))?;
let mut new_doc_view = Box::pin(self.deep_copy_doc(
&pub_row_doc_id,
dup_row_doc_id.clone(),
doc,
PublishViewMetaData::default(),
))
.await?;
new_doc_view.parent_view_id.clone_from(&dup_row_doc_id); // orphan folder view
self
.views_to_add
.insert(dup_row_doc_id.clone(), new_doc_view);
row_meta.insert(&mut txn, dup_row_doc_id, any::Any::Bool(false));
} }
} }
{
// if pub_icon_id exists, duplicate it! // handle icon id
if let Some(Out::Any(any::Any::String(icon_id))) = pub_icon_id { let pub_icon_id_key = meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::IconId);
let dup_icon_id_key = meta_id_from_row_id(&dup_row_id.parse()?, RowMetaKey::IconId); let pub_icon_id = row_meta.get(&txn, &pub_icon_id_key);
row_meta.insert(&mut txn, dup_icon_id_key, icon_id); if let Some(Out::Any(any::Any::String(icon_id))) = pub_icon_id {
row_meta.remove(&mut txn, &pub_icon_id_key);
let dup_icon_id_key = meta_id_from_row_id(&dup_row_id.parse()?, RowMetaKey::IconId);
row_meta.insert(&mut txn, dup_icon_id_key, icon_id);
}
} }
{
// if pub_cover_id exists, duplicate it! // handle cover id
if let Some(Out::Any(any::Any::String(cover_id))) = pub_cover_id { let pub_cover_id_key = meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::CoverId);
let dup_cover_id_key = meta_id_from_row_id(&dup_row_id.parse()?, RowMetaKey::IconId); let pub_cover_id = row_meta.get(&txn, &pub_cover_id_key);
row_meta.insert(&mut txn, dup_cover_id_key, cover_id); if let Some(Out::Any(any::Any::String(cover_id))) = pub_cover_id {
let dup_cover_id_key = meta_id_from_row_id(&dup_row_id.parse()?, RowMetaKey::CoverId);
row_meta.insert(&mut txn, dup_cover_id_key, cover_id);
}
}
{
// handle attachment_count
let pub_attach_count_key =
meta_id_from_row_id(&pub_row_id.parse()?, RowMetaKey::CoverId);
let pub_attachment_count = row_meta.get(&txn, &pub_attach_count_key);
if let Some(Out::Any(any::Any::BigInt(attachment_count))) = pub_attachment_count {
let dup_ac_key =
meta_id_from_row_id(&new_row_id.parse()?, RowMetaKey::AttachmentCount);
row_meta.insert(&mut txn, dup_ac_key, attachment_count);
}
} }
// TODO: add attachment_count
// // if attachment_count exists, duplicate it!
// if let Some(Out::Any(any::Any::BigInt(attachment_count))) = pub_attachment_count {
// let dup_ac_key = meta_id_from_row_id(&new_row_id.parse()?, RowMetaKey::AttachmentCount);
// row_meta.insert(&mut txn, dup_ac_key, attachment_count);
// }
} }
} }