use crate::{
api::get_entry::get_entry_result,
error::{ZomeApiError, ZomeApiResult},
Dispatch,
};
use holochain_core_types::{entry::Entry, link::LinkMatch};
use holochain_persistence_api::{cas::content::Address, hash::HashString};
use holochain_wasm_utils::api_serialization::{
get_entry::{GetEntryOptions, GetEntryResult, GetEntryResultItem, GetEntryResultType},
get_links::{GetLinksArgs, GetLinksOptions, GetLinksResult, GetLinksResultCount},
};
pub fn get_links_with_options(
base: &Address,
link_type: LinkMatch<&str>,
tag: LinkMatch<&str>,
options: GetLinksOptions,
) -> ZomeApiResult<GetLinksResult> {
Dispatch::GetLinks.with_input(GetLinksArgs {
entry_address: base.clone(),
link_type: link_type.into(),
tag: tag.into(),
options,
})
}
pub fn get_links_count_with_options(
base: &Address,
link_type: LinkMatch<&str>,
tag: LinkMatch<&str>,
options: GetLinksOptions,
) -> ZomeApiResult<GetLinksResultCount> {
Dispatch::GetLinksCount.with_input(GetLinksArgs {
entry_address: base.clone(),
link_type: link_type.into(),
tag: tag.into(),
options,
})
}
pub fn get_links_count(
base: &Address,
link_type: LinkMatch<&str>,
tag: LinkMatch<&str>,
) -> ZomeApiResult<GetLinksResultCount> {
get_links_count_with_options(base, link_type, tag, GetLinksOptions::default())
}
pub fn get_links(
base: &Address,
link_type: LinkMatch<&str>,
tag: LinkMatch<&str>,
) -> ZomeApiResult<GetLinksResult> {
get_links_with_options(base, link_type, tag, GetLinksOptions::default())
}
pub fn get_links_result(
base: &Address,
link_type: LinkMatch<&str>,
tag: LinkMatch<&str>,
options: GetLinksOptions,
get_entry_options: GetEntryOptions,
) -> ZomeApiResult<Vec<ZomeApiResult<GetEntryResult>>> {
let get_links_result = get_links_with_options(base, link_type, tag, options)?;
let result = get_links_result
.addresses()
.iter()
.map(|address| get_entry_result(&address, get_entry_options.clone()))
.collect();
Ok(result)
}
pub fn get_links_and_load(
base: &HashString,
link_type: LinkMatch<&str>,
tag: LinkMatch<&str>,
) -> ZomeApiResult<Vec<ZomeApiResult<Entry>>> {
println!("get_links_and_load");
let get_links_result = get_links_result(
base,
link_type,
tag,
GetLinksOptions::default(),
GetEntryOptions::default(),
)?;
let entries = get_links_result
.into_iter()
.map(|get_result| {
let get_type = get_result?.result;
match get_type {
GetEntryResultType::Single(GetEntryResultItem{entry: Some(entry), ..}) => Ok(entry),
GetEntryResultType::Single(GetEntryResultItem{entry: None, ..}) => Err(ZomeApiError::Internal("Entry is None so most likely has been marked as deleted".to_string())),
GetEntryResultType::All(_) => Err(ZomeApiError::Internal("Invalid response. get_links_result returned all entries when latest was requested".to_string())),
}
})
.collect();
Ok(entries)
}