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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
pub use hdi_extensions::hdi;
pub use hdi_extensions::holo_hash;
pub use hdk;
pub use hdi_extensions;

use core::convert::{ TryFrom, TryInto };
use hdi_extensions::{
    summon_action,
    summon_entry,
};
use hdk::prelude::{
    get, get_details, agent_info,
    debug, wasm_error,
    Serialize, Deserialize,
    ExternResult, WasmError, WasmErrorInner, GetOptions,
    Record, Action, Details, RecordDetails, SignedHashed,
    LinkTypeFilter, LinkTypeFilterExt, LinkTag,
};
use holo_hash::{
    AgentPubKey, ActionHash, AnyDhtHash, AnyLinkableHash,
    AnyDhtHashPrimitive, AnyLinkableHashPrimitive,
};
use thiserror::Error;
use hdi_extensions::*;



//
// General Structs
//
/// A distinct state within the context of a life-cycle
///
/// A [`MorphAddr`] and its entry content represent a chain in the entity's life-cycle.
///
/// ##### Example: Basic Usage
/// ```
/// # use hdk::prelude::{
///     ActionHash, TryFrom,
/// };
/// # use hdk_extensions::{
///     Entity, MorphAddr,
/// };
///
/// #[derive(Clone)]
/// struct Content {
///     pub message: String,
/// }
///
/// let identity_addr = "uhCkkrVjqWkvcFoq2Aw4LOSe6Yx9OgQLMNG-DiXqtT0nLx8uIM2j7";
/// let revision_addr = "uhCkknDrZjzEgzf8iIQ6aEzbqEYrYBBg1pv_iTNUGAFJovhxOJqu0";
///
/// Entity::<Content>(
///     MorphAddr(
///         ActionHash::try_from(identity_addr).unwrap(),
///         ActionHash::try_from(revision_addr).unwrap(),
///     ),
///     Content {
///         message: String::from("Hello world"),
///     }
/// );
/// ```
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Entity<T>(
    /// The Metamorphic address relevant to `T` (the content)
    pub MorphAddr,
    /// The content that belong's to the [`MorphAddr`]'s revision address
    pub T,
)
where
    T: Clone;

impl<T> Entity<T>
where
    T: Clone,
{
    /// See [`MorphAddr::is_origin`]
    pub fn identity(&self) -> &ActionHash {
        self.0.identity()
    }

    /// See [`MorphAddr::is_origin`]
    pub fn revision(&self) -> &ActionHash {
        self.0.revision()
    }

    /// See [`MorphAddr::is_origin`]
    pub fn is_origin(&self) -> bool {
        self.0.is_origin()
    }
}

/// An address representing a precise phase in an entities life-cycle (short for: Metamorphic
/// Address)
///
/// Together the pair of identity/revision addresses act as coordinates that can be used to
/// determine the entity's identity (identity addr) and a phase in its life-cycle (revision addr).
///
/// ##### Example: Basic Usage
/// ```
/// # use hdk::prelude::{
///     ActionHash, TryFrom,
/// };
/// # use hdk_extensions::{
///     Entity, MorphAddr,
/// };
///
/// let identity_addr = "uhCkkrVjqWkvcFoq2Aw4LOSe6Yx9OgQLMNG-DiXqtT0nLx8uIM2j7";
/// let revision_addr = "uhCkknDrZjzEgzf8iIQ6aEzbqEYrYBBg1pv_iTNUGAFJovhxOJqu0";
///
/// MorphAddr(
///     ActionHash::try_from(identity_addr).unwrap(),
///     ActionHash::try_from(revision_addr).unwrap(),
/// );
/// ```
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct MorphAddr(
    /// The create action of the entities life-cycle
    pub ActionHash,
    /// Any entry creation action in the entity's life-cycle
    pub ActionHash,
);

impl MorphAddr {
    /// A reference to the tuple's index 0
    pub fn identity(&self) -> &ActionHash {
        &self.0
    }

    /// A reference to the tuple's index 1
    pub fn revision(&self) -> &ActionHash {
        &self.1
    }

    /// This is an origin metamorphic address if the identity and revision are the same
    pub fn is_origin(&self) -> bool {
        self.0 == self.1
    }
}



//
// Custom Errors
//
#[derive(Debug, Error)]
pub enum HdkExtError<'a> {
    #[error("Record not found @ address {0}")]
    RecordNotFound(&'a AnyDhtHash),
    #[error("No entry in record ({0})")]
    RecordHasNoEntry(&'a ActionHash),
    #[error("Expected an action hash, not an entry hash: {0}")]
    ExpectedRecordNotEntry(&'a ActionHash),
}

impl<'a> From<HdkExtError<'a>> for WasmError {
    fn from(error: HdkExtError) -> Self {
        wasm_error!(WasmErrorInner::Guest( format!("{}", error ) ))
    }
}



//
// Agent
//
/// Get this Agent's initial pubkey from zome info
pub fn agent_id() -> ExternResult<AgentPubKey> {
    Ok( agent_info()?.agent_initial_pubkey )
}



//
// Get Helpers
//
/// Get a [`Record`] or return a "not found" error
///
/// The difference between this `must_get` and `hdk`'s `get` is that this one replaces a `None` response
/// with [`HdkExtError::RecordNotFound`] so that an ok result will always be a [`Record`].
///
/// **NOTE:** Not to be confused with the `hdi`'s meaning of 'must'.  This 'must' will not retrieve
/// deleted records.
pub fn must_get<T>(addr: &T) -> ExternResult<Record>
where
    T: Clone + std::fmt::Debug,
    AnyDhtHash: From<T>,
{
    Ok(
        get( addr.to_owned(), GetOptions::network() )?
            .ok_or(HdkExtError::RecordNotFound(&addr.to_owned().into()))?
    )
}


/// Get the [`RecordDetails`] for a given [`ActionHash`]
///
/// This method provides a more deterministic result by unwrapping the [`get_details`] result.
pub fn must_get_record_details(action: &ActionHash) -> ExternResult<RecordDetails> {
    let details = get_details( action.to_owned(), GetOptions::network() )?
        .ok_or(HdkExtError::RecordNotFound(&action.to_owned().into()))?;

    match details {
        Details::Record(record_details) => Ok( record_details ),
        Details::Entry(_) => Err(HdkExtError::ExpectedRecordNotEntry(action))?,
    }
}


/// Check if a DHT address can be fetched
pub fn exists<T>(addr: &T) -> ExternResult<bool>
where
    T: Clone + std::fmt::Debug,
    AnyDhtHash: From<T>,
{
    debug!("Checking if address {:?} exists", addr );
    Ok(
        match AnyDhtHash::from(addr.to_owned()).into_primitive() {
            AnyDhtHashPrimitive::Action(addr) => summon_action( &addr ).is_ok(),
            AnyDhtHashPrimitive::Entry(addr) => summon_entry( &addr ).is_ok(),
        }
    )
}


/// Check if a DHT address can be fetched and is not deleted
pub fn available<T>(addr: &T) -> ExternResult<bool>
where
    T: Clone + std::fmt::Debug,
    AnyDhtHash: From<T>,
{
    debug!("Checking if address {:?} is available", addr );
    Ok( get( addr.to_owned(), GetOptions::network() )?.is_some() )
}



//
// Tracing Actions
//
/// Resolve an [`AnyLinkableHash`] into an [`ActionHash`]
///
/// If the linkable's primitive is a
/// - `Action` - the action hash is simply returned
/// - `Entry` - the action hash is pulled from the result of a `get`
/// - `External` - results in an error
pub fn resolve_action_addr<T>(addr: &T) -> ExternResult<ActionHash>
where
    T: Into<AnyLinkableHash> + Clone,
{
    let addr : AnyLinkableHash = addr.to_owned().into();
    match addr.into_primitive() {
        AnyLinkableHashPrimitive::Entry(entry_hash) => {
            Ok(
                must_get( &entry_hash )?.action_address().to_owned()
            )
        },
        AnyLinkableHashPrimitive::Action(action_hash) => Ok( action_hash ),
        AnyLinkableHashPrimitive::External(external_hash) => Err(guest_error!(
            format!("External hash ({}) will not have a corresponding action", external_hash )
        )),
    }
}


/// Collect the chain of evolutions forward
///
/// When there are multiple updates the lowest action's timestamp is selected.
///
/// The first item of the returned [`Vec`] will always be the given [`ActionHash`]
pub fn follow_evolutions(action_address: &ActionHash) -> ExternResult<Vec<ActionHash>> {
    let mut evolutions = vec![];
    let mut next_addr = Some(action_address.to_owned());

    while let Some(addr) = next_addr {
        let details = must_get_record_details( &addr )?;
        let maybe_next_update = details.updates.iter()
            .min_by_key(|sa| sa.action().timestamp() );

        next_addr = match maybe_next_update {
            Some(signed_action) => Some(signed_action.hashed.hash.to_owned()),
            None => None,
        };

        evolutions.push( addr );
    }

    Ok( evolutions )
}


/// Collect the chain of evolutions forward filtering updates
pub fn follow_evolutions_selector<F>(
    action_address: &ActionHash,
    selector: F
) -> ExternResult<Vec<ActionHash>>
where
    F: Fn(Vec<SignedHashed<Action>>) -> ExternResult<Option<ActionHash>>,
{
    let mut evolutions = vec![];
    let mut next_addr = Some(action_address.to_owned());

    while let Some(addr) = next_addr {
        let details = must_get_record_details( &addr )?;
        next_addr = selector( details.updates )?;

        evolutions.push( addr );
    }

    Ok( evolutions )
}


/// Collect the chain of evolutions forward filtering authorized updates
pub fn follow_evolutions_using_authorities(
    action_address: &ActionHash,
    authors: &Vec<AgentPubKey>
) -> ExternResult<Vec<ActionHash>> {
    let evolutions = follow_evolutions_selector( action_address, |updates| {
        let updates_count = updates.len();
        let valid_updates : Vec<SignedHashed<Action>> = updates
            .into_iter()
            .filter(|sa| {
                debug!(
                    "Checking authorities for author '{}': {:?}",
                    sa.action().author(),
                    authors
                );
                authors.contains( sa.action().author() )
            })
            .collect();

        debug!(
            "Filtered {}/{} updates",
            updates_count - valid_updates.len(),
            updates_count
        );
        let maybe_next_update = valid_updates.iter()
            .min_by_key(|sa| sa.action().timestamp() );

        Ok(
            match maybe_next_update {
                Some(signed_action) => Some(signed_action.hashed.hash.to_owned()),
                None => None,
            }
        )
    })?;

    Ok( evolutions )
}


/// Collect the chain of evolutions forward filtering authorized updates with exceptions
pub fn follow_evolutions_using_authorities_with_exceptions(
    action_address: &ActionHash,
    authors: &Vec<AgentPubKey>,
    exceptions: &Vec<ActionHash>
) -> ExternResult<Vec<ActionHash>> {
    let evolutions = follow_evolutions_selector( action_address, |updates| {
        let updates_count = updates.len();
        let valid_updates : Vec<SignedHashed<Action>> = updates
            .into_iter()
            .filter(|sa| {
                debug!(
                    "Checking authorities for author '{}' or an action exception '{}'",
                    sa.action().author(),
                    sa.action_address()
                );
                authors.contains( sa.action().author() ) || exceptions.contains( sa.action_address() )
            })
            .collect();

        debug!(
            "Filtered {}/{} updates",
            updates_count - valid_updates.len(),
            updates_count
        );
        let maybe_next_update = valid_updates.iter()
            .min_by_key(|sa| sa.action().timestamp() );

        Ok(
            match maybe_next_update {
                Some(signed_action) => Some(signed_action.hashed.hash.to_owned()),
                None => None,
            }
        )
    })?;

    Ok( evolutions )
}


/// Indicates the update filtering pattern when following content evolution
#[derive(Clone, Serialize, Debug)]
#[serde(untagged)]
pub enum EvolutionFilteringStrategy {
    /// Variant used for [`follow_evolutions`]
    Unfiltered,
    /// Variant used for [`follow_evolutions_using_authorities`]
    AuthoritiesFilter(Vec<AgentPubKey>),
    /// Variant used for [`follow_evolutions_using_authorities_with_exceptions`]
    AuthoritiesExceptionsFilter(Vec<AgentPubKey>, Vec<ActionHash>),
    /// Not used yet
    ExceptionsFilter(Vec<ActionHash>),
}

impl Default for EvolutionFilteringStrategy {
    fn default() -> Self {
        EvolutionFilteringStrategy::Unfiltered
    }
}

impl<'de> serde::Deserialize<'de> for EvolutionFilteringStrategy {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let buffer : FollowEvolutionsInputBuffer = Deserialize::deserialize(deserializer)?;

        Ok( buffer.into() )
    }
}

/// A deserializing buffer for [`EvolutionFilteringStrategy`]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct FollowEvolutionsInputBuffer {
    pub authors: Option<Vec<AgentPubKey>>,
    pub exceptions: Option<Vec<ActionHash>>,
}

impl From<FollowEvolutionsInputBuffer> for EvolutionFilteringStrategy {
    fn from(buffer: FollowEvolutionsInputBuffer) -> Self {
        match (buffer.authors, buffer.exceptions) {
            (None, None) => Self::Unfiltered,
            (Some(authors), None) => Self::AuthoritiesFilter(authors),
            (None, Some(exceptions)) => Self::ExceptionsFilter(exceptions),
            (Some(authors), Some(exceptions)) => Self::AuthoritiesExceptionsFilter(authors, exceptions),
        }
    }
}


//
// Standard Inputs
//
/// Input required for calling the [`follow_evolutions`] method
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct GetEntityInput {
    pub id: ActionHash,
    #[serde(default)]
    pub follow_strategy: EvolutionFilteringStrategy,
}

/// Input required for calling the [`hdk::prelude::update_entry`] method
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct UpdateEntryInput<T> {
    pub base: ActionHash,
    pub entry: T,
}

/// A simpler deserializable buffer for [`GetLinksInput`]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct GetLinksInputBuffer {
    pub base: AnyLinkableHash,
    pub target: AnyLinkableHash,
    pub link_type: String,
    pub tag: Option<String>,
}

/// Input required for calling the [`hdk::prelude::get_links`] method
#[derive(Clone, Serialize, Debug)]
pub struct GetLinksInput<T>
where
    T: LinkTypeFilterExt + TryFrom<String, Error = WasmError> + Clone,
{
    pub base: AnyLinkableHash,
    pub target: AnyLinkableHash,
    pub link_type_filter: LinkTypeFilter,
    pub tag: Option<LinkTag>,
    pub link_type: Option<T>,
}

impl<T> TryFrom<GetLinksInputBuffer> for GetLinksInput<T>
where
    T: LinkTypeFilterExt + TryFrom<String, Error = WasmError> + Clone,
{
    type Error = WasmError;

    fn try_from(buffer: GetLinksInputBuffer) -> Result<Self, Self::Error> {
        let (link_type, link_type_filter) = match buffer.link_type.as_str() {
            ".." => ( None, (..).try_into_filter()? ),
            name => {
                let link_type = T::try_from( name.to_string() )?;
                ( Some(link_type.clone()), link_type.try_into_filter()? )
            },
        };

        Ok(Self {
            base: buffer.base,
            target: buffer.target,
            tag: buffer.tag.map(|text| text.into_bytes().into() ),
            link_type,
            link_type_filter,
        })
    }
}

impl<'de,T> serde::Deserialize<'de> for GetLinksInput<T>
where
    T: LinkTypeFilterExt + TryFrom<String, Error = WasmError> + Clone,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let buffer : GetLinksInputBuffer = Deserialize::deserialize(deserializer)?;
        let error_msg = format!("Buffer could not be converted: {:#?}", buffer );

        Ok(
            buffer.try_into()
                .or(Err(serde::de::Error::custom(error_msg)))?
        )
    }
}