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
use keri_core::{
    oobi::{EndRole, LocationScheme, Role},
    prefix::IdentifierPrefix,
    query::reply_event::ReplyRoute,
};

use crate::{error::ControllerError, identifier::Identifier, known_events::OobiRetrieveError};

impl Identifier {
    pub fn get_location(
        &self,
        identifier: &IdentifierPrefix,
    ) -> Result<Vec<LocationScheme>, OobiRetrieveError> {
        self.known_events.get_loc_schemas(identifier)
    }

    pub fn get_role_location(
        &self,
        id: &IdentifierPrefix,
        role: Role,
    ) -> Result<Vec<LocationScheme>, ControllerError> {
        Ok(self
            .known_events
            .oobi_manager
            .get_end_role(id, role)?
            .unwrap_or_default()
            .into_iter()
            .filter_map(|r| {
                if let ReplyRoute::EndRoleAdd(adds) = r.reply.get_route() {
                    let locations = self
                        .known_events
                        .oobi_manager
                        .get_loc_scheme(&adds.eid)
                        .unwrap_or_default()
                        .unwrap()
                        .into_iter();
                    Some(locations.filter_map(|rep| {
                        if let ReplyRoute::LocScheme(loc) = rep.data.data {
                            Some(loc)
                        } else {
                            None
                        }
                    }))
                } else {
                    None
                }
            })
            .flatten()
            .collect())
    }

    pub fn get_end_role(
        &self,
        id: &IdentifierPrefix,
        role: Role,
    ) -> Result<Vec<EndRole>, ControllerError> {
        let end_roles = self
            .known_events
            .oobi_manager
            .get_end_role(id, role)?
            .unwrap_or_default()
            .into_iter()
            .map(|reply| match reply.reply.data.data {
                ReplyRoute::EndRoleAdd(end_role) => end_role,
                _ => todo!(),
            })
            .collect();
        Ok(end_roles)
    }
}