diff --git a/src/api.rs b/src/api.rs index edbde60..35b6302 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1896,3 +1896,96 @@ pub fn roles_contains_member(roles: String, member_str: Vec) -> ApiResul Ok(()) } + +#[wasm_bindgen] +pub fn members_in_same_roles_me(roles: String) -> ApiResult> { + let roles: Value = serde_json::from_str(&roles)?; + + let json = json!({ + "roles": roles + }); + + let roles = json.extract_roles()?; + + let device = lock_local_device()?; + + let us = device.to_member(); + + let mut res: Vec = vec![]; + + for (_, role) in roles { + if role.members.contains(&us) { + let to_add: Vec = role.members + .iter() + .filter_map(|m| { + if *m != us { + Some(serde_json::to_string(m).unwrap()) + } else { + None + } + }).collect(); + res.extend(to_add); + } + } + + Ok(res) +} + +#[wasm_bindgen] +pub fn sync_process_from_relay(process_id: String, process_str: String) -> ApiResult { + let outpoint = OutPoint::from_str(&process_id)?; + + let process: Process = serde_json::from_str(&process_str)?; + + // Are we part of this process? + let last_state = process.get_latest_commited_state().ok_or(ApiError::new("Process doesn't have a commited state".to_owned()))?; + + let roles = last_state.encrypted_pcd.extract_roles()?; + + let us = lock_local_device()?.to_member(); + + let mut contains_us = false; + for (_, role) in &roles { + if role.members.contains(&us) { + contains_us = true; + break; + } + } + + let mut processes = lock_processes()?; + if !contains_us { + // If we're not part of this process, we just keep the minimum of public informations + processes.insert(outpoint, process.clone()); + + let updated_process = Some(UpdatedProcess { + process_id: outpoint, + current_process: process, + up_to_date_roles: roles, + ..Default::default() + }); + + let api_return = ApiReturn { + updated_process, + ..Default::default() + }; + Ok(api_return) + } else { + // If we're part of this process, we want to complete what we are missing + let api_return: ApiReturn = if let Some(in_memory_process) = processes.get(&outpoint) { + debug!("We already know about this process"); + // We already know about this process + // For each state, do we have the keys? the encrypted values? + for state in in_memory_process.get_all_states_iter() { + debug!("parsing state {:?}", state); + } + ApiReturn::default() + } else { + debug!("We don't know about this process"); + // We didn't know about this process + // We simply request all the attributes from each state + ApiReturn::default() + }; + + Ok(api_return) + } +}