roles/
lib.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
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
use all_role_claims_deleted_proof::create_all_role_claims_deleted_proofs_if_possible;
use hc_zome_trait_notifications::NotificationsZomeTrait;
use hc_zome_traits::implemented_zome_traits;
use hdk::prelude::*;
use linked_devices::get_my_other_devices;
use notifications::{send_roles_notification, RolesNotification, RolesNotifications};
use progenitors::claim_admin_role_as_progenitor;
use remote_signal::RolesRemoteSignal;
use role_claim::{
    assign_my_roles_to_linked_devices_which_havent_claimed_it_yet, claim_roles_assigned_to_me,
};
use roles_integrity::{utils::deserialize_tag, *};
use unassignments::unassign_pending_unassignments;

pub mod all_role_claims_deleted_proof;
pub mod assignees;
pub mod progenitors;
pub mod remote_signal;
pub mod role_claim;
pub mod unassignments;
pub mod utils;

pub mod linked_devices;
pub mod notifications;

#[implemented_zome_traits]
pub enum ZomeTraits {
    Notifications(RolesNotifications),
}

/// initial function called when installing the hApp (if Agent is progenitor then Admin role is claimed)
#[hdk_extern]
pub fn init(_: ()) -> ExternResult<InitCallbackResult> {
    let agent_info = agent_info()?;
    let progenitors = progenitors(())?;

    if progenitors.contains(&agent_info.agent_initial_pubkey) {
        claim_admin_role_as_progenitor()?;
    }

    schedule("scheduled_tasks")?;

    let mut fns: BTreeSet<GrantedFunction> = BTreeSet::new();
    fns.insert((zome_info()?.name, FunctionName::from("recv_remote_signal")));
    let functions = GrantedFunctions::Listed(fns);
    let cap_grant = ZomeCallCapGrant {
        tag: String::from("recv_remote_signal"),
        access: CapAccess::Unrestricted,
        functions,
    };
    create_cap_grant(cap_grant)?;

    Ok(InitCallbackResult::Pass)
}

#[hdk_extern(infallible)]
pub fn scheduled_tasks(_schedule: Option<Schedule>) -> Option<Schedule> {
    // debug!("[scheduled_tasks/begin]");
    if let Err(err) = claim_roles_assigned_to_me() {
        error!("Error calling claim_roles_assigned_to_me: {err:?}");
    }
    if let Err(err) = assign_my_roles_to_linked_devices_which_havent_claimed_it_yet() {
        error!(
            "Error calling assign_my_roles_to_linked_devices_which_havent_claimed_it_yet: {err:?}"
        );
    }
    if let Err(err) = unassign_pending_unassignments() {
        error!("Error calling unassign_pending_unassignments: {err:?}");
    }
    if let Err(err) = create_all_role_claims_deleted_proofs_if_possible(()) {
        error!("Error calling create_all_role_claims_deleted_proofs_if_necessary: {err:?}");
    }

    Some(Schedule::Persisted("*/30 * * * * * *".into()))
}

///Signals available in the module
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Signal {
    EntryCreated {
        action: SignedActionHashed,
        app_entry: EntryTypes,
    },
    EntryUpdated {
        action: SignedActionHashed,
        app_entry: EntryTypes,
        original_app_entry: EntryTypes,
    },
    EntryDeleted {
        action: SignedActionHashed,
        original_app_entry: EntryTypes,
    },
    LinkCreated {
        action: SignedActionHashed,
        link_type: LinkTypes,
    },
    LinkDeleted {
        action: SignedActionHashed,
        create_link_action: SignedActionHashed,
        link_type: LinkTypes,
    },
}

///Commiting an action to the source chain
#[hdk_extern(infallible)]
pub fn post_commit(committed_actions: Vec<SignedActionHashed>) {
    debug!(
        "[post_commit/begin] with {:?} actions.",
        committed_actions.len()
    );
    for action in committed_actions {
        if let Action::CreateLink(create_link) = &action.hashed.content {
            if let Ok(Some(LinkTypes::RoleToAssignee)) =
                LinkTypes::from_type(create_link.zome_index, create_link.link_type)
            {
                if let Err(err) = notify_new_role_assigned(&action.hashed.hash, create_link) {
                    error!("Error calling notify_new_role_assigned: {:?}", err);
                }
            }
            if let Ok(Some(LinkTypes::AssigneeToRole)) =
                LinkTypes::from_type(create_link.zome_index, create_link.link_type)
            {
                if let Err(err) = send_try_claim_new_role_signal(&action.hashed.hash, create_link) {
                    error!("Error calling send_try_claim_new_role_signal: {:?}", err);
                }
            }
            if let Ok(Some(LinkTypes::PendingUnassignments)) =
                LinkTypes::from_type(create_link.zome_index, create_link.link_type)
            {
                if let Err(err) = notify_pending_unassignment(&action.hashed.hash, create_link) {
                    error!("Error calling notify_pending_unassignment: {:?}", err);
                }
            }
        }
        if let Action::DeleteLink(delete_link) = action.action() {
            if let Ok(LinkTypes::AssigneeRoleClaim) = get_deleted_link_type(delete_link) {
                if let Err(err) =
                    attempt_create_all_role_claims_deleted_proof(action.hashed.hash.clone())
                {
                    error!("Could not create an AllRoleClaimsDeletedProof: {:?}", err);
                }
            }
        }
        if let Err(err) = signal_action(action) {
            error!("Error signaling new action: {:?}", err);
        }
    }
}

pub fn attempt_create_all_role_claims_deleted_proof(
    deleted_role_claim_hash: ActionHash,
) -> ExternResult<()> {
    let agent_info = agent_info()?;
    let zome_info = zome_info()?;
    info!("An AssingeeRoleClaim link was just deleted. Attempting to create an AllRoleClaimsDeletedProof.");
    if let Err(err) = call_remote(
        agent_info.agent_initial_pubkey,
        zome_info.name,
        "create_all_role_claims_deleted_proofs_if_possible".into(),
        None,
        (),
    ) {
        error!("Error calling create_all_role_claims_deleted_proofs_if_possible: {err:?}");
    }

    let my_other_devices = get_my_other_devices()?;

    send_remote_signal(
        RolesRemoteSignal::TryCreateAllRoleClaimsDeletedProof {
            deleted_role_claim_hash,
        },
        my_other_devices,
    )?;

    Ok(())
}

fn get_deleted_link_type(delete_link: &DeleteLink) -> ExternResult<LinkTypes> {
    let Some(Details::Record(record_details)) =
        get_details(delete_link.link_add_address.clone(), GetOptions::default())?
    else {
        return Err(wasm_error!("Invalid get details return value"));
    };
    match record_details.record.action() {
        Action::CreateLink(create_link) => {
            if let Ok(Some(link_type)) =
                LinkTypes::from_type(create_link.zome_index, create_link.link_type)
            {
                return Ok(link_type);
            }
            return Err(wasm_error!("Invalid link type"));
        }
        _ => Err(wasm_error!("Create Link should exist")),
    }
}

fn notify_new_role_assigned(
    action_hash: &ActionHash,
    role_to_assignee_create_link: &CreateLink,
) -> ExternResult<()> {
    let Some(assignee) = role_to_assignee_create_link
        .target_address
        .clone()
        .into_agent_pub_key()
    else {
        return Err(wasm_error!(
            "Unreachable: RoleToAssignee link does not have an AgentPubKey as its target"
        ));
    };

    let tag: RoleToAssigneeLinkTag = deserialize_tag(role_to_assignee_create_link.tag.clone())?;

    send_roles_notification(
        assignee,
        RolesNotification::AssignedRole {
            role: tag.role,
            assign_role_create_link_hash: action_hash.clone(),
        },
    )?;
    Ok(())
}

fn send_try_claim_new_role_signal(
    action_hash: &ActionHash,
    assignee_to_role_create_link: &CreateLink,
) -> ExternResult<()> {
    let Some(assignee) = assignee_to_role_create_link
        .base_address
        .clone()
        .into_agent_pub_key()
    else {
        return Err(wasm_error!(
            "Unreachable: AssigneeToRole link does not have an AgentPubKey as its base"
        ));
    };

    let tag: AssigneeToRoleLinkTag = deserialize_tag(assignee_to_role_create_link.tag.clone())?;

    info!("Notifying of a new role assigned to them to assignee {assignee}");

    send_remote_signal(
        RolesRemoteSignal::TryClaimNewRole {
            role: tag.role,
            assignee_to_role_create_link_hash: action_hash.clone(),
        },
        vec![assignee],
    )?;

    Ok(())
}

fn notify_pending_unassignment(
    action_hash: &ActionHash,
    pending_unassignment_create_link: &CreateLink,
) -> ExternResult<()> {
    let Some(role_to_assignee_create_link_hash) = pending_unassignment_create_link
        .target_address
        .clone()
        .into_action_hash()
    else {
        return Err(wasm_error!(
            "Unreachable: RoleToAssignee link does not point to an ActionHash."
        ));
    };
    let tag: PendingUnassignmentLinkTag =
        deserialize_tag(pending_unassignment_create_link.tag.clone())?;
    let agents: Vec<AgentPubKey> = tag
        .all_agents_for_assignee
        .into_iter()
        .map(|(agent, _)| agent.clone())
        .collect();

    info!("Notifying of a pending unassigment to agents {agents:?}");

    send_remote_signal(
        RolesRemoteSignal::NewPendingUnassignment {
            role: tag.role.clone(),
            pending_unassignment_create_link_hash: action_hash.clone(),
        },
        agents.clone(),
    )?;
    send_roles_notification(
        agents[0].clone(),
        RolesNotification::UnassignedRole {
            role: tag.role,
            assign_role_create_link_hash: role_to_assignee_create_link_hash,
        },
    )?;

    Ok(())
}

///Generate signals to handle all the actions made with module
fn signal_action(action: SignedActionHashed) -> ExternResult<()> {
    match action.hashed.content.clone() {
        Action::Create(_create) => {
            if let Ok(Some(app_entry)) = get_entry_for_action(&action.hashed.hash) {
                emit_signal(Signal::EntryCreated { action, app_entry })?;
            }
            Ok(())
        }
        Action::Update(update) => {
            if let Ok(Some(app_entry)) = get_entry_for_action(&action.hashed.hash) {
                if let Ok(Some(original_app_entry)) =
                    get_entry_for_action(&update.original_action_address)
                {
                    emit_signal(Signal::EntryUpdated {
                        action,
                        app_entry,
                        original_app_entry,
                    })?;
                }
            }
            Ok(())
        }
        Action::Delete(delete) => {
            if let Ok(Some(original_app_entry)) = get_entry_for_action(&delete.deletes_address) {
                emit_signal(Signal::EntryDeleted {
                    action,
                    original_app_entry,
                })?;
            }
            Ok(())
        }
        Action::CreateLink(create_link) => {
            if let Ok(Some(link_type)) =
                LinkTypes::from_type(create_link.zome_index, create_link.link_type)
            {
                emit_signal(Signal::LinkCreated { action, link_type })?;
            }
            Ok(())
        }
        Action::DeleteLink(delete_link) => {
            let record = get(delete_link.link_add_address.clone(), GetOptions::default())?
                .ok_or(wasm_error!("Failed to fetch CreateLink action."))?;
            match record.action() {
                Action::CreateLink(create_link) => {
                    if let Ok(Some(link_type)) =
                        LinkTypes::from_type(create_link.zome_index, create_link.link_type)
                    {
                        emit_signal(Signal::LinkDeleted {
                            action,
                            link_type,
                            create_link_action: record.signed_action.clone(),
                        })?;
                    }
                    Ok(())
                }
                _ => Err(wasm_error!("Create Link should exist")),
            }
        }
        _ => Ok(()),
    }
}

///Retrieve entry for a specific action
fn get_entry_for_action(action_hash: &ActionHash) -> ExternResult<Option<EntryTypes>> {
    let record = match get_details(action_hash.clone(), GetOptions::default())? {
        Some(Details::Record(record_details)) => record_details.record,
        _ => return Ok(None),
    };
    let entry = match record.entry().as_option() {
        Some(entry) => entry,
        None => return Ok(None),
    };
    let (zome_index, entry_index) = match record.action().entry_type() {
        Some(EntryType::App(AppEntryDef {
            zome_index,
            entry_index,
            ..
        })) => (zome_index, entry_index),
        _ => return Ok(None),
    };
    EntryTypes::deserialize_from_type(*zome_index, *entry_index, entry)
}