Skip to content

Commit

Permalink
feat(relay): fill out empty validation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyg committed Oct 27, 2024
1 parent 960c69c commit ab77f8e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 30 deletions.
55 changes: 41 additions & 14 deletions dnas/relay/zomes/integrity/relay/src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,27 @@ pub fn validate_create_contact(
}

pub fn validate_update_contact(
_action: Update,
action: Update,
_contact: Contact,
_original_action: EntryCreationAction,
original_action: EntryCreationAction,
_original_contact: Contact,
) -> ExternResult<ValidateCallbackResult> {
if &action.author != original_action.author() {
return Ok(ValidateCallbackResult::Invalid("Only the contact author can update their contact".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}

pub fn validate_delete_contact(
_action: Delete,
_original_action: EntryCreationAction,
action: Delete,
original_action: EntryCreationAction,
_original_contact: Contact,
) -> ExternResult<ValidateCallbackResult> {
if &action.author != original_action.author() {
return Ok(ValidateCallbackResult::Invalid("Only the contact author can delete their contact".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}

Expand All @@ -60,20 +68,24 @@ pub fn validate_create_link_contact_to_contacts(
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest("Linked action must reference an entry"
WasmErrorInner::Guest("Linked action must reference a Contact entry"
.to_string())
),
)?;
Ok(ValidateCallbackResult::Valid)
}

pub fn validate_delete_link_contact_to_contacts(
_action: DeleteLink,
_original_action: CreateLink,
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 contact author can delete their contact".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}

Expand All @@ -97,7 +109,7 @@ pub fn validate_create_link_contact_updates(
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest("Linked action must reference an entry"
WasmErrorInner::Guest("Linked action must reference a Contact entry"
.to_string())
),
)?;
Expand Down Expand Up @@ -137,8 +149,8 @@ pub fn validate_delete_link_contact_updates(
}

pub fn validate_create_link_all_contacts(
_action: CreateLink,
_base_address: AnyLinkableHash,
action: CreateLink,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Expand All @@ -161,17 +173,32 @@ pub fn validate_create_link_all_contacts(
.to_string())
),
)?;
// TODO: add the appropriate validation rules

// base_address is hash of 'all_contacts'
let base_path = Path::from("all_contacts");
let base_path_hash = base_path.path_entry_hash()?;
if base_address != base_path_hash.into() {
return Ok(ValidateCallbackResult::Invalid("Base address must be hash of 'all_contacts'".to_string()));
}

// action author must be contact author
if &action.author != record.signed_action.hashed.author() {
return Ok(ValidateCallbackResult::Invalid("Only the contact author can create an AllContacts link to their contact".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}

pub fn validate_delete_link_all_contacts(
_action: DeleteLink,
_original_action: CreateLink,
action: DeleteLink,
original_action: CreateLink,
_base: AnyLinkableHash,
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
// TODO: add the appropriate validation rules
if action.author != original_action.author {
return Ok(ValidateCallbackResult::Invalid("Only the orignal author can delete their contacts".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}
31 changes: 28 additions & 3 deletions dnas/relay/zomes/integrity/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,27 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
)
}
EntryTypes::Message(message) => {
validate_update_message(action, message)
let original_app_entry = must_get_valid_record(
action.clone().original_action_address,
)?;
let original_message = match Message::try_from(
original_app_entry,
) {
Ok(entry) => entry,
Err(e) => {
return Ok(
ValidateCallbackResult::Invalid(
format!("Expected to get Message from Record: {e:?}"),
),
);
}
};
validate_update_message(
action,
message,
original_create_action,
original_message
)
}
EntryTypes::Config(config) => {
validate_update_config(action, config)
Expand Down Expand Up @@ -521,7 +541,7 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?;
let _original_message = match original_message {
let original_message = match original_message {
Some(message) => message,
None => {
return Ok(
Expand All @@ -532,7 +552,12 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
);
}
};
validate_update_message(action, message)
validate_update_message(
action,
message,
original_action,
original_message
)
} else {
Ok(result)
}
Expand Down
56 changes: 43 additions & 13 deletions dnas/relay/zomes/integrity/relay/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use hdi::prelude::*;
use crate::messages_path;

#[derive(Serialize, Deserialize, Debug, SerializedBytes, Clone, PartialEq)]
pub struct File {
Expand Down Expand Up @@ -30,16 +31,30 @@ pub fn validate_create_message(
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_update_message(
_action: Update,
_message: Message,
action: Update,
message: Message,
original_action: EntryCreationAction,
original_message: Message,
) -> ExternResult<ValidateCallbackResult> {
if &action.author != original_action.author() {
return Ok(ValidateCallbackResult::Invalid("Only the message author can update their message".to_string()));
}

if message.bucket != original_message.bucket {
return Ok(ValidateCallbackResult::Invalid("Message bucket cannot be updated".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_message(
_action: Delete,
_original_action: EntryCreationAction,
action: Delete,
original_action: EntryCreationAction,
_original_message: Message,
) -> ExternResult<ValidateCallbackResult> {
if &action.author != original_action.author() {
return Ok(ValidateCallbackResult::Invalid("Only the message author can delete the link to their message".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}
pub fn validate_create_link_message_updates(
Expand All @@ -62,7 +77,7 @@ pub fn validate_create_link_message_updates(
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest("Linked action must reference an entry"
WasmErrorInner::Guest("Linked action must reference a Message entry"
.to_string())
),
)?;
Expand All @@ -80,7 +95,7 @@ pub fn validate_create_link_message_updates(
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest("Linked action must reference an entry"
WasmErrorInner::Guest("Linked action must reference a Message entry"
.to_string())
),
)?;
Expand All @@ -100,8 +115,8 @@ pub fn validate_delete_link_message_updates(
)
}
pub fn validate_create_link_all_messages(
_action: CreateLink,
_base_address: AnyLinkableHash,
action: CreateLink,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Expand All @@ -114,7 +129,7 @@ pub fn validate_create_link_all_messages(
),
)?;
let record = must_get_valid_record(action_hash)?;
let _message: crate::Message = record
let message: crate::Message = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
Expand All @@ -124,16 +139,31 @@ pub fn validate_create_link_all_messages(
.to_string())
),
)?;
// TODO: add the appropriate validation rules

// base_address is message_path for this message's bucket
let path = messages_path(message.bucket);
let path_hash = path.path_entry_hash()?;
if base_address != path_hash.into() {
return Ok(ValidateCallbackResult::Invalid("Base address must follow path structure for this message's bucket".to_string()));
}

// action author must be message author
if &action.author != record.signed_action.hashed.author() {
return Ok(ValidateCallbackResult::Invalid("Only the message author can create an AllMessages link to their message".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_all_messages(
_action: DeleteLink,
_original_action: CreateLink,
action: DeleteLink,
original_action: CreateLink,
_base: AnyLinkableHash,
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
// TODO: add the appropriate validation rules
if action.author != original_action.author {
return Ok(ValidateCallbackResult::Invalid("Only the message author can delete the link to their message".to_string()));
}

Ok(ValidateCallbackResult::Valid)
}

0 comments on commit ab77f8e

Please sign in to comment.