Skip to content

Commit

Permalink
added link from developer collective to creator
Browse files Browse the repository at this point in the history
  • Loading branch information
matthme committed Apr 22, 2024
1 parent 899451f commit a65ef4f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
50 changes: 48 additions & 2 deletions dnas/tools/zomes/coordinator/library/src/developer_collective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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<Vec<Record>> {
let links = get_links(
GetLinksInputBuilder::try_new(
agent_info()?.agent_initial_pubkey,
LinkTypes::DeveloperCollectiveToOwner,
)?
.build(),
)?;
let get_input: Vec<GetInput> = 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::<ExternResult<Vec<GetInput>>>()?;
let records = HDK.with(|hdk| hdk.borrow().get(get_input))?;
Ok(records.into_iter().flatten().collect())
}
Original file line number Diff line number Diff line change
@@ -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<ValidateCallbackResult> {
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<ValidateCallbackResult> {
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)
}
37 changes: 37 additions & 0 deletions dnas/tools/zomes/integrity/library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +29,7 @@ pub enum LinkTypes {
CuratorUpdates,
DeveloperCollectiveUpdates,
DeveloperCollectiveToContributorPermissions,
DeveloperCollectiveToOwner,
ContributorToContributorPermissions,
DeveloperCollectiveToTools,
ToolUpdates,
Expand Down Expand Up @@ -185,6 +188,14 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
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,
Expand Down Expand Up @@ -269,6 +280,15 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
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,
Expand Down Expand Up @@ -608,6 +628,14 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
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,
Expand Down Expand Up @@ -708,6 +736,15 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
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,
Expand Down

0 comments on commit a65ef4f

Please sign in to comment.