notes_integrity/
note.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// use automerge::AutoCommit;
// use autosurgeon::{hydrate, Hydrate};
use hdi::prelude::*;

#[derive(Clone, PartialEq)]
#[hdk_entry_helper]
pub struct Note {
    pub data: Vec<u8>,
    pub previous_hashes: Vec<ActionHash>,
}

// #[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
// pub struct Document(pub Vec<u8>);
// #[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
// pub struct HydratedHoloHash<T: HashType>(pub HoloHash<T>);
// impl<T: HashType> Hydrate for HydratedHoloHash<T> {
//     fn hydrate_bytes(bytes: &[u8]) -> Result<HydratedHoloHash<T>, autosurgeon::HydrateError> {
//         let hash: HoloHash<T> = HoloHash::from_raw_39(bytes.to_vec());
//         Ok(HydratedHoloHash(hash))
//     }
// }
// pub type HydrateEntryHash = HydratedHoloHash<hash_type::Entry>;

// #[derive(Clone, Hydrate, PartialEq, Serialize, Deserialize, Debug)]
// pub struct NoteContent {
//     pub title: String,
//     pub body: String,

//     pub images_hashes: Vec<HydrateEntryHash>,
// }

pub fn validate_create_note(
    _action: EntryCreationAction,
    note: Note,
) -> ExternResult<ValidateCallbackResult> {
    // let autocommit =
    //     AutoCommit::load(note.0.as_slice()).map_err(|err| wasm_error!("Load error {:?}", err))?;
    // let _note: NoteContent =
    //     hydrate(&autocommit).map_err(|err| wasm_error!("Invalid data: {:?}", err))?;

    for previous_hash in note.previous_hashes {
        let record = must_get_valid_record(previous_hash)?;

        let _note: crate::Note = record
            .entry()
            .to_app_option()
            .map_err(|e| wasm_error!(e))?
            .ok_or(wasm_error!(WasmErrorInner::Guest(
                "Linked action must reference an entry".to_string()
            )))?;
        // TODO: crawl back til the start of the history
    }

    Ok(ValidateCallbackResult::Valid)
}

pub fn validate_update_note(
    _action: Update,
    _note: Note,
    _original_action: EntryCreationAction,
    _original_note: Note,
) -> ExternResult<ValidateCallbackResult> {
    // TODO: add the appropriate validation rules
    Ok(ValidateCallbackResult::Valid)
}

pub fn validate_delete_note(
    _action: Delete,
    _original_action: EntryCreationAction,
    _original_note: Note,
) -> ExternResult<ValidateCallbackResult> {
    // TODO: add the appropriate validation rules
    Ok(ValidateCallbackResult::Valid)
}

pub fn validate_create_link_note_updates(
    _action: CreateLink,
    base_address: AnyLinkableHash,
    target_address: AnyLinkableHash,
    _tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
    let action_hash = base_address
        .into_action_hash()
        .ok_or(wasm_error!(WasmErrorInner::Guest(
            "No action hash associated with link".to_string()
        )))?;
    let record = must_get_valid_record(action_hash)?;
    let _note: crate::Note = record
        .entry()
        .to_app_option()
        .map_err(|e| wasm_error!(e))?
        .ok_or(wasm_error!(WasmErrorInner::Guest(
            "Linked action must reference an entry".to_string()
        )))?;
    let action_hash =
        target_address
            .into_action_hash()
            .ok_or(wasm_error!(WasmErrorInner::Guest(
                "No action hash associated with link".to_string()
            )))?;
    let record = must_get_valid_record(action_hash)?;
    let _note: crate::Note = record
        .entry()
        .to_app_option()
        .map_err(|e| wasm_error!(e))?
        .ok_or(wasm_error!(WasmErrorInner::Guest(
            "Linked action must reference an entry".to_string()
        )))?;
    // TODO: add the appropriate validation rules
    Ok(ValidateCallbackResult::Valid)
}

pub fn validate_delete_link_note_updates(
    _action: DeleteLink,
    _original_action: CreateLink,
    _base: AnyLinkableHash,
    _target: AnyLinkableHash,
    _tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
    Ok(ValidateCallbackResult::Invalid(
        "NoteUpdates links cannot be deleted".to_string(),
    ))
}

pub fn validate_create_link_all_notes(
    _action: CreateLink,
    _base_address: AnyLinkableHash,
    target_address: AnyLinkableHash,
    _tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
    let action_hash =
        target_address
            .into_action_hash()
            .ok_or(wasm_error!(WasmErrorInner::Guest(
                "No action hash associated with link".to_string()
            )))?;
    let record = must_get_valid_record(action_hash)?;
    let _note: crate::Note = record
        .entry()
        .to_app_option()
        .map_err(|e| wasm_error!(e))?
        .ok_or(wasm_error!(WasmErrorInner::Guest(
            "Linked action must reference an entry".to_string()
        )))?;
    // TODO: add the appropriate validation rules
    Ok(ValidateCallbackResult::Valid)
}

pub fn validate_delete_link_all_notes(
    _action: DeleteLink,
    _original_action: CreateLink,
    _base: AnyLinkableHash,
    _target: AnyLinkableHash,
    _tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
    // TODO: add the appropriate validation rules
    Ok(ValidateCallbackResult::Valid)
}