notes/
incremental_changes.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use hdk::prelude::*;
use notes_integrity::*;

#[derive(Serialize, Deserialize, Debug)]
pub struct AddIncrementalChangesForNoteInput {
    pub note_hash: ActionHash,
    pub incremental_changes: String,
}

#[hdk_extern]
pub fn add_incremental_changes_for_note(
    input: AddIncrementalChangesForNoteInput,
) -> ExternResult<()> {
    create_link(
        input.note_hash.clone(),
        input.note_hash,
        LinkTypes::IncrementalChanges,
        input.incremental_changes,
    )?;
    Ok(())
}

#[hdk_extern]
pub fn get_incremental_changes_for_note(note_hash: ActionHash) -> ExternResult<Vec<String>> {
    let links = get_links(
        GetLinksInputBuilder::try_new(note_hash, LinkTypes::IncrementalChanges)?.build(),
    )?;
    let incremental_changes = links
        .into_iter()
        .map(|link| {
            String::from_utf8(link.tag.into_inner()).map_err(|e| {
                wasm_error!(WasmErrorInner::Guest(format!(
                    "Error converting link tag to string: {:?}",
                    e
                )))
            })
        })
        .collect::<ExternResult<Vec<String>>>()?;
    Ok(incremental_changes)
}