use hdi::prelude::*;
mod hdk_utils;
pub use hdk_utils::*;
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)?;
Ok((agents_chain_entry_records, agent_activity))
}
fn get_record_from_activity(
agent_activity: Vec<&RegisterAgentActivity>,
) -> ExternResult<Vec<Record>> {
let mut agents_chain_entry_records = Vec::new();
for activity in agent_activity {
let action = activity.action.action_address().clone();
if let ActionType::CreateLink = activity.action.action().action_type() {
agents_chain_entry_records.push(must_get_valid_record(action)?);
}
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)
}