Hi, I am trying to implement a zome call that lists all locally authored elements of a certain entry type (using hdk::chain::query
) and excludes obsolete ones that have been updated or deleted (updates and deletes are only allowed by the original author, so local source chain should be sufficient). Has anyone done this before?
I was planning on using ChainQueryFilter::entry_type
to do the filtering, but I looked at the structure of Header::Delete
and it doesn’t contain an entry_type
, so I expect that deletes won’t show up in my chain query.
Here is my attempt that I expect to fail:
fn list_my_messages() -> Result<Vec<Element>, MyError> {
let message_elements =
hdk::chain::query(ChainQueryFilter::new()
.entry_type(/* message entry type omitted for brevity */)
.include_entries(true))?;
let obsolete_message_header_hashes: HashSet<_> = message_elements
.iter()
.filter_map(|element| match element.header() {
Header::Update(Update {
original_header_address,
..
}) => Some(original_header_address.clone()),
// oops! this match arm will never be reached, because Delete headers don't carry entry types.
Header::Delete(Delete {
deletes_address, ..
}) => Some(deletes_address.clone()),
_ => None,
})
.collect();
Ok(message_elements
.into_iter()
.filter(|element| !obsolete_message_header_hashes.contains(element.header_address()))
.collect())
}
I haven’t checked for certain that Deletes don’t show up in the chain query. So let me know if my assumption is just wrong.