From a65ef4ffc4bff0a34744284a18bd0ed99b012ef0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 22 Apr 2024 23:20:48 +0200 Subject: [PATCH] added link from developer collective to creator --- .../library/src/developer_collective.rs | 50 ++++++++++++++++- .../src/developer_collective_to_owner.rs | 53 +++++++++++++++++++ dnas/tools/zomes/integrity/library/src/lib.rs | 37 +++++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 dnas/tools/zomes/integrity/library/src/developer_collective_to_owner.rs diff --git a/dnas/tools/zomes/coordinator/library/src/developer_collective.rs b/dnas/tools/zomes/coordinator/library/src/developer_collective.rs index 57217c5..cba1b35 100644 --- a/dnas/tools/zomes/coordinator/library/src/developer_collective.rs +++ b/dnas/tools/zomes/coordinator/library/src/developer_collective.rs @@ -14,6 +14,12 @@ pub fn create_developer_collective( LinkTypes::AllDeveloperCollectives, (), )?; + create_link( + developer_collective_hash.clone(), + agent_info()?.agent_initial_pubkey, + LinkTypes::DeveloperCollectiveToOwner, + (), + )?; let record = get(developer_collective_hash.clone(), GetOptions::default())?.ok_or( wasm_error!(WasmErrorInner::Guest( "Could not find the newly created DeveloperCollective".to_string() @@ -144,11 +150,25 @@ pub fn delete_developer_collective( ))), }?; let path = Path::from("all_developer_collectives"); - let links = get_links( + let all_developer_collective_links = get_links( GetLinksInputBuilder::try_new(path.path_entry_hash()?, LinkTypes::AllDeveloperCollectives)? .build(), )?; - for link in links { + for link in all_developer_collective_links { + if let Some(hash) = link.target.into_action_hash() { + if hash.eq(&original_developer_collective_hash) { + delete_link(link.create_link_hash)?; + } + } + } + let developer_collective_to_owner_links = get_links( + GetLinksInputBuilder::try_new( + path.path_entry_hash()?, + LinkTypes::DeveloperCollectiveToOwner, + )? + .build(), + )?; + for link in developer_collective_to_owner_links { if let Some(hash) = link.target.into_action_hash() { if hash.eq(&original_developer_collective_hash) { delete_link(link.create_link_hash)?; @@ -189,3 +209,29 @@ pub fn get_oldest_delete_for_developer_collective( }); Ok(deletes.first().cloned()) } +#[hdk_extern] +pub fn get_my_developer_collectives(_: ()) -> ExternResult> { + let links = get_links( + GetLinksInputBuilder::try_new( + agent_info()?.agent_initial_pubkey, + LinkTypes::DeveloperCollectiveToOwner, + )? + .build(), + )?; + let get_input: Vec = links + .into_iter() + .map(|link| { + Ok(GetInput::new( + link.target + .into_action_hash() + .ok_or(wasm_error!(WasmErrorInner::Guest( + "No action hash associated with link".to_string() + )))? + .into(), + GetOptions::default(), + )) + }) + .collect::>>()?; + let records = HDK.with(|hdk| hdk.borrow().get(get_input))?; + Ok(records.into_iter().flatten().collect()) +} diff --git a/dnas/tools/zomes/integrity/library/src/developer_collective_to_owner.rs b/dnas/tools/zomes/integrity/library/src/developer_collective_to_owner.rs new file mode 100644 index 0000000..43277c6 --- /dev/null +++ b/dnas/tools/zomes/integrity/library/src/developer_collective_to_owner.rs @@ -0,0 +1,53 @@ +use hdi::prelude::*; +/// Rules: +/// +/// 1. Only the creator (owner) of the developer collective can create a link to their public key +pub fn validate_create_link_developer_collective_to_owner( + action: CreateLink, + base_address: AnyLinkableHash, + target_address: AnyLinkableHash, + _tag: LinkTag, +) -> ExternResult { + let collective_action_hash = + base_address + .into_action_hash() + .ok_or(wasm_error!(WasmErrorInner::Guest( + "No action hash associated with link".to_string() + )))?; + let collective_record = must_get_valid_record(collective_action_hash)?; + + let _developer_collective: crate::DeveloperCollective = collective_record + .entry() + .to_app_option() + .map_err(|e| wasm_error!(e))? + .ok_or(wasm_error!(WasmErrorInner::Guest( + "Linked action must reference an entry".to_string() + )))?; + // Check the entry type for the given action hash + let owner_agent_key = + target_address + .into_agent_pub_key() + .ok_or(wasm_error!(WasmErrorInner::Guest( + "No agent public key associated with link".to_string() + )))?; + + if owner_agent_key != action.author { + return Ok(ValidateCallbackResult::Invalid("Only the creator of a DeveloperCollective can create a DeveloperCollectiveToOwner link from that DeveloperCollective".into())); + } + + Ok(ValidateCallbackResult::Valid) +} +pub fn validate_delete_link_developer_collective_to_owner( + action: DeleteLink, + original_action: CreateLink, + _base: AnyLinkableHash, + _target: AnyLinkableHash, + _tag: LinkTag, +) -> ExternResult { + if action.author != original_action.author { + return Ok(ValidateCallbackResult::Invalid( + "Only the creator of a DeveloperCollectiveToOwner link can delete that link.".into(), + )); + } + Ok(ValidateCallbackResult::Valid) +} diff --git a/dnas/tools/zomes/integrity/library/src/lib.rs b/dnas/tools/zomes/integrity/library/src/lib.rs index 4e28256..7d87f15 100644 --- a/dnas/tools/zomes/integrity/library/src/lib.rs +++ b/dnas/tools/zomes/integrity/library/src/lib.rs @@ -2,6 +2,8 @@ pub mod curator_to_tools; pub use curator_to_tools::*; pub mod curator_to_developer_collectives; pub use curator_to_developer_collectives::*; +pub mod developer_collective_to_owner; +pub use developer_collective_to_owner::*; pub mod tool; pub use tool::*; pub mod contributor_permission; @@ -27,6 +29,7 @@ pub enum LinkTypes { CuratorUpdates, DeveloperCollectiveUpdates, DeveloperCollectiveToContributorPermissions, + DeveloperCollectiveToOwner, ContributorToContributorPermissions, DeveloperCollectiveToTools, ToolUpdates, @@ -185,6 +188,14 @@ pub fn validate(op: Op) -> ExternResult { tag, ) } + LinkTypes::DeveloperCollectiveToOwner => { + validate_create_link_developer_collective_to_owner( + action, + base_address, + target_address, + tag, + ) + } LinkTypes::ContributorToContributorPermissions => { validate_create_link_contributor_to_contributor_permissions( action, @@ -269,6 +280,15 @@ pub fn validate(op: Op) -> ExternResult { tag, ) } + LinkTypes::DeveloperCollectiveToOwner => { + validate_delete_link_developer_collective_to_owner( + action, + original_action, + base_address, + target_address, + tag, + ) + } LinkTypes::ContributorToContributorPermissions => { validate_delete_link_contributor_to_contributor_permissions( action, @@ -608,6 +628,14 @@ pub fn validate(op: Op) -> ExternResult { tag, ) } + LinkTypes::DeveloperCollectiveToOwner => { + validate_create_link_developer_collective_to_owner( + action, + base_address, + target_address, + tag, + ) + } LinkTypes::ContributorToContributorPermissions => { validate_create_link_contributor_to_contributor_permissions( action, @@ -708,6 +736,15 @@ pub fn validate(op: Op) -> ExternResult { create_link.tag, ) } + LinkTypes::DeveloperCollectiveToOwner => { + validate_delete_link_developer_collective_to_owner( + action, + create_link.clone(), + base_address, + create_link.target_address, + create_link.tag, + ) + } LinkTypes::ContributorToContributorPermissions => { validate_delete_link_contributor_to_contributor_permissions( action,