From b0ad75d243f1f0acf45543b57554afb3635edd1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillem=20C=C3=B3rdoba?= Date: Tue, 17 Oct 2023 14:59:15 +0200 Subject: [PATCH] Also emit signal on create cancellation --- zomes/coordinator/cancellations/src/lib.rs | 63 +++++++++++++++++++--- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/zomes/coordinator/cancellations/src/lib.rs b/zomes/coordinator/cancellations/src/lib.rs index 413556a..12d29bc 100644 --- a/zomes/coordinator/cancellations/src/lib.rs +++ b/zomes/coordinator/cancellations/src/lib.rs @@ -17,19 +17,19 @@ pub enum Signal { action: SignedActionHashed, link_type: LinkTypes, }, - // EntryCreated { - // action: SignedActionHashed, - // app_entry: EntryTypes, - // }, + EntryCreated { + action: SignedActionHashed, + app_entry: EntryTypes, + }, // EntryUpdated { // action: SignedActionHashed, // app_entry: EntryTypes, // original_app_entry: EntryTypes, // }, - // EntryDeleted { - // action: SignedActionHashed, - // original_app_entry: EntryTypes, - // }, + EntryDeleted { + action: SignedActionHashed, + original_app_entry: EntryTypes, + }, } #[hdk_extern(infallible)] pub fn post_commit(committed_actions: Vec) { @@ -71,6 +71,53 @@ fn signal_action(action: SignedActionHashed) -> ExternResult<()> { } } } + Action::Create(_create) => { + if let Ok(Some(app_entry)) = get_entry_for_action(&action.hashed.hash) { + + emit_signal(Signal::EntryCreated { action, app_entry })?; + } + Ok(()) + } + Action::Delete(delete) => { + if let Ok(Some(original_app_entry)) = get_entry_for_action(&delete.deletes_address) { + emit_signal(Signal::EntryDeleted { + action, + original_app_entry, + })?; + } + Ok(()) + } + _ => Ok(()), } } + +fn get_entry_for_action(action_hash: &ActionHash) -> ExternResult> { + let record = match get_details(action_hash.clone(), GetOptions::default())? { + Some(Details::Record(record_details)) => record_details.record, + _ => { + return Ok(None); + } + }; + let entry = match record.entry().as_option() { + Some(entry) => entry, + None => { + return Ok(None); + } + }; + let (zome_index, entry_index) = match record.action().entry_type() { + Some(EntryType::App(AppEntryDef { + zome_index, + entry_index, + .. + })) => (zome_index, entry_index), + _ => { + return Ok(None); + } + }; + Ok(EntryTypes::deserialize_from_type( + zome_index.clone(), + entry_index.clone(), + entry, + )?) +}