rave_engine 0.8.0

A secure and efficient JSON Schema validation and Rhai script execution engine
Documentation
use hdi::prelude::*;
mod hdk_utils;
pub use hdk_utils::*;

// todo: optimization: we could fetch based on what exactly is required or at the least get_valid_record only if we need to read it
/// Returns the chain of records for a given agent and action
/// A `Vec` of `Record`s representing the chain of records for the given agent and action.
pub fn get_agent_app_entries_activity(
    action: Action,
    exclude_latest: bool,
) -> ExternResult<(Vec<Record>, Vec<RegisterAgentActivity>)> {
    let mut action_hash: ActionHash = hash_action(action.clone())?;
    if let Some(l) = action.prev_action() {
        if exclude_latest {
            action_hash = l.to_owned();
        }
    }
    inner_get_agent_app_entries_activity(action.author().to_owned(), action_hash)
}

pub fn inner_get_agent_app_entries_activity(
    author: AgentPubKey,
    action_hash: ActionHash,
) -> ExternResult<(Vec<Record>, Vec<RegisterAgentActivity>)> {
    let agent_activity = must_get_agent_activity(author, ChainFilter::new(action_hash))?;

    let filtered_agent_activity = agent_activity
        .iter()
        .filter(|activity| {
            matches!(
                activity.action.action(),
                Action::Create(_) | Action::Update(_) | Action::CreateLink(_)
            )
        })
        .collect::<Vec<_>>();

    let agents_chain_entry_records = get_record_from_activity(filtered_agent_activity)?;
    // agent_activity includes the links
    Ok((agents_chain_entry_records, agent_activity))
}

fn get_record_from_activity(
    agent_activity: Vec<&RegisterAgentActivity>,
) -> ExternResult<Vec<Record>> {
    // todo: Future optimized with async must_get_valid_records loop
    // let mut action_hashes: Vec<MustGetValidRecordInput> = Vec::new();
    // // getting valid record is series so that an error is thrown as soon as one record is unable to be retrieved
    // for activity in agent_activity {
    //     let action = activity.action.action_address().clone();
    //     // get record for links
    //     if let ActionType::CreateLink = activity.action.action().action_type() {
    //         action_hashes.push(MustGetValidRecordInput::new(action));
    //     }
    //     // Get entries for only AgentPubKey and App Entries
    //     else if let Some(EntryType::App(_)) = activity.action.action().entry_type() {
    //         action_hashes.push(MustGetValidRecordInput::new(action));
    //     }
    // }
    // let agents_chain_entry_records = HDI.with(|hdi| hdi.borrow().must_get_entry(action_hashes))?;

    let mut agents_chain_entry_records = Vec::new();
    // getting valid record is series so that an error is thrown as soon as one record is unable to be retrieved
    for activity in agent_activity {
        let action = activity.action.action_address().clone();
        // get record for links
        if let ActionType::CreateLink = activity.action.action().action_type() {
            agents_chain_entry_records.push(must_get_valid_record(action)?);
        }
        // Get entries for only AgentPubKey and App Entries
        else if let Some(EntryType::App(_)) = activity.action.action().entry_type() {
            agents_chain_entry_records.push(must_get_valid_record(action)?);
        }
    }
    Ok(agents_chain_entry_records)
}