hc_utils/
get_details.rs

1use hdk::prelude::*;
2
3/// optimized get details by links
4pub fn get_details(
5    links: Vec<Link>,
6    option: GetOptions,
7) -> ExternResult<Vec<(Details, Link, ActionHash)>> {
8    let links: Vec<(AnyDhtHash, Link)> = links
9        .iter()
10        .filter_map(|l| match l.clone().target.into_entry_hash() {
11            Some(e) => Some((e.into(), l.clone())),
12            None => match l.clone().target.into_action_hash() {
13                Some(a) => Some((a.into(), l.clone())),
14                None => None,
15            },
16        })
17        .collect();
18    let msg_results_input: Vec<GetInput> = links
19        .clone()
20        .into_iter()
21        .map(|link| (GetInput::new(link.0, option.clone())))
22        .collect();
23    let list = HDK.with(|hdk| hdk.borrow().get_details(msg_results_input))?;
24
25    Ok(list
26        .iter()
27        .filter_map(|d| match d {
28            Some(details) => {
29                if let Details::Record(r) = details {
30                    let action_hash = r.record.action_address().clone();
31                    let any_hash: AnyDhtHash = action_hash.clone().into();
32                    let found = links
33                        .iter()
34                        .find_map(|(h, l)| if h == &any_hash { Some(l) } else { None });
35                    match found {
36                        Some(l) => Some((details.clone(), l.clone(), action_hash)),
37                        None => None,
38                    }
39                } else {
40                    //  Details::Entry(e): this is not needed for our app, but thats because we will alway link to ActionHash
41                    None
42                }
43            }
44            None => None,
45        })
46        .collect())
47}