comments_integrity/
comment.rsuse hdi::prelude::*;
#[derive(Clone, PartialEq)]
#[hdk_entry_helper]
pub struct Comment {
pub commented_hash: AnyDhtHash,
pub content: String,
pub reply_to: Option<ActionHash>,
}
pub fn validate_create_comment(
_action: EntryCreationAction,
comment: Comment,
) -> ExternResult<ValidateCallbackResult> {
if let Some(action_hash) = comment.reply_to.clone() {
let record = must_get_valid_record(action_hash)?;
let _comment: crate::Comment = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Dependant action must be accompanied by an entry"
))))?;
}
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_update_comment(
_action: Update,
_comment: Comment,
_original_action: EntryCreationAction,
_original_comment: Comment,
) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_comment(
_action: Delete,
_original_action: EntryCreationAction,
_original_comment: Comment,
) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_create_link_comment_to_comments(
_action: CreateLink,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let action_hash = base_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(
"No action hash associated with link".to_string()
)))?;
let record = must_get_valid_record(action_hash)?;
let _comment: crate::Comment = 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()
)))?;
let action_hash =
target_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(
"No action hash associated with link".to_string()
)))?;
let record = must_get_valid_record(action_hash)?;
let _comment: crate::Comment = 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()
)))?;
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_comment_to_comments(
_action: DeleteLink,
_original_action: CreateLink,
_base: AnyLinkableHash,
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_create_link_comment_updates(
_action: CreateLink,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let action_hash = base_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(
"No action hash associated with link".to_string()
)))?;
let record = must_get_valid_record(action_hash)?;
let _comment: crate::Comment = 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()
)))?;
let action_hash =
target_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(
"No action hash associated with link".to_string()
)))?;
let record = must_get_valid_record(action_hash)?;
let _comment: crate::Comment = 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()
)))?;
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_comment_updates(
_action: DeleteLink,
_original_action: CreateLink,
_base: AnyLinkableHash,
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Invalid(
"CommentUpdates links cannot be deleted".to_string(),
))
}