From cf8b7511f2ddbb2c54bc4b8e67f8326254f7169c Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Thu, 18 Jul 2024 21:36:21 +0100 Subject: [PATCH] Push unique person proof from LocalUserIndex to UserIndex --- .../local_user_index/impl/src/lib.rs | 25 ++++++++++++++++--- .../impl/src/queries/chat_events.rs | 2 +- .../impl/src/updates/join_channel.rs | 9 ++++--- .../impl/src/updates/join_community.rs | 9 ++++--- .../impl/src/updates/join_group.rs | 9 ++++--- backend/canisters/user_index/api/src/lib.rs | 3 ++- .../impl/src/updates/c2c_notify_events.rs | 8 ++++++ 7 files changed, 50 insertions(+), 15 deletions(-) diff --git a/backend/canisters/local_user_index/impl/src/lib.rs b/backend/canisters/local_user_index/impl/src/lib.rs index d83b5cb265..8fecf1ec3c 100644 --- a/backend/canisters/local_user_index/impl/src/lib.rs +++ b/backend/canisters/local_user_index/impl/src/lib.rs @@ -58,16 +58,33 @@ impl RuntimeState { self.data.global_users.get(&caller).unwrap().user_id } - pub fn calling_user(&self, credential_jwts: Option<&[String]>) -> GlobalUser { + pub fn calling_user(&self) -> GlobalUser { let caller = self.env.caller(); - let mut user_details = self.data.global_users.get(&caller).unwrap(); + self.data.global_users.get(&caller).unwrap() + } + + pub fn get_calling_user_and_process_credentials(&mut self, credential_jwts: Option<&[String]>) -> GlobalUser { + let mut user_details = self.calling_user(); if user_details.unique_person_proof.is_none() { - user_details.unique_person_proof = credential_jwts.as_ref().and_then(|jwts| { + if let Some(unique_person_proof) = credential_jwts.as_ref().and_then(|jwts| { let now = self.env.now(); self.data .extract_proof_of_unique_personhood(user_details.principal, jwts, now) - }); + }) { + let user_id = user_details.user_id; + self.push_event_to_user_index(UserIndexEvent::NotifyUniquePersonProof(Box::new(( + user_id, + unique_person_proof.clone(), + )))); + if self.data.local_users.contains(&user_id) { + self.push_event_to_user( + user_id, + UserEvent::NotifyUniquePersonProof(Box::new(unique_person_proof.clone())), + ); + } + user_details.unique_person_proof = Some(unique_person_proof); + } } user_details diff --git a/backend/canisters/local_user_index/impl/src/queries/chat_events.rs b/backend/canisters/local_user_index/impl/src/queries/chat_events.rs index f560109b61..9158972259 100644 --- a/backend/canisters/local_user_index/impl/src/queries/chat_events.rs +++ b/backend/canisters/local_user_index/impl/src/queries/chat_events.rs @@ -8,7 +8,7 @@ use types::UserId; #[query(composite = true, guard = "caller_is_openchat_user")] async fn chat_events(args: Args) -> Response { - let (user, now) = read_state(|state| (state.calling_user(None), state.env.now())); + let (user, now) = read_state(|state| (state.calling_user(), state.env.now())); let futures: Vec<_> = args .requests diff --git a/backend/canisters/local_user_index/impl/src/updates/join_channel.rs b/backend/canisters/local_user_index/impl/src/updates/join_channel.rs index 71a3852083..62e6c39890 100644 --- a/backend/canisters/local_user_index/impl/src/updates/join_channel.rs +++ b/backend/canisters/local_user_index/impl/src/updates/join_channel.rs @@ -1,5 +1,5 @@ use crate::guards::caller_is_openchat_user; -use crate::{mutate_state, read_state}; +use crate::mutate_state; use canister_tracing_macros::trace; use ic_cdk::update; use local_user_index_canister::join_channel::{Response::*, *}; @@ -7,8 +7,11 @@ use local_user_index_canister::join_channel::{Response::*, *}; #[update(guard = "caller_is_openchat_user")] #[trace] async fn join_channel(args: Args) -> Response { - let user_details = - read_state(|state| state.calling_user(args.verified_credential_args.as_ref().map(|c| c.credential_jwts.as_slice()))); + let user_details = mutate_state(|state| { + state.get_calling_user_and_process_credentials( + args.verified_credential_args.as_ref().map(|c| c.credential_jwts.as_slice()), + ) + }); let c2c_args = community_canister::c2c_join_channel::Args { user_id: user_details.user_id, diff --git a/backend/canisters/local_user_index/impl/src/updates/join_community.rs b/backend/canisters/local_user_index/impl/src/updates/join_community.rs index a20b522425..91e4443869 100644 --- a/backend/canisters/local_user_index/impl/src/updates/join_community.rs +++ b/backend/canisters/local_user_index/impl/src/updates/join_community.rs @@ -1,5 +1,5 @@ use crate::guards::caller_is_openchat_user; -use crate::{mutate_state, read_state}; +use crate::mutate_state; use canister_tracing_macros::trace; use ic_cdk::update; use local_user_index_canister::join_community::{Response::*, *}; @@ -7,8 +7,11 @@ use local_user_index_canister::join_community::{Response::*, *}; #[update(guard = "caller_is_openchat_user")] #[trace] async fn join_community(args: Args) -> Response { - let user_details = - read_state(|state| state.calling_user(args.verified_credential_args.as_ref().map(|c| c.credential_jwts.as_slice()))); + let user_details = mutate_state(|state| { + state.get_calling_user_and_process_credentials( + args.verified_credential_args.as_ref().map(|c| c.credential_jwts.as_slice()), + ) + }); let c2c_args = community_canister::c2c_join_community::Args { user_id: user_details.user_id, diff --git a/backend/canisters/local_user_index/impl/src/updates/join_group.rs b/backend/canisters/local_user_index/impl/src/updates/join_group.rs index 442b487491..133bcda189 100644 --- a/backend/canisters/local_user_index/impl/src/updates/join_group.rs +++ b/backend/canisters/local_user_index/impl/src/updates/join_group.rs @@ -1,5 +1,5 @@ use crate::guards::caller_is_openchat_user; -use crate::{mutate_state, read_state, RuntimeState}; +use crate::{mutate_state, RuntimeState}; use canister_tracing_macros::trace; use ic_cdk::update; use local_user_index_canister::join_group::{Response::*, *}; @@ -10,8 +10,11 @@ use user_index_canister::Event as UserIndexEvent; #[update(guard = "caller_is_openchat_user")] #[trace] async fn join_group(args: Args) -> Response { - let user_details = - read_state(|state| state.calling_user(args.verified_credential_args.as_ref().map(|c| c.credential_jwts.as_slice()))); + let user_details = mutate_state(|state| { + state.get_calling_user_and_process_credentials( + args.verified_credential_args.as_ref().map(|c| c.credential_jwts.as_slice()), + ) + }); let c2c_args = group_canister::c2c_join_group::Args { user_id: user_details.user_id, diff --git a/backend/canisters/user_index/api/src/lib.rs b/backend/canisters/user_index/api/src/lib.rs index cc593674e7..0c75efcd69 100644 --- a/backend/canisters/user_index/api/src/lib.rs +++ b/backend/canisters/user_index/api/src/lib.rs @@ -2,7 +2,7 @@ use candid::Principal; use serde::{Deserialize, Serialize}; use types::{ CanisterId, ChannelLatestMessageIndex, ChatId, CommunityId, MessageContent, MessageContentInitial, MessageId, MessageIndex, - User, UserId, + UniquePersonProof, User, UserId, }; mod lifecycle; @@ -22,6 +22,7 @@ pub enum Event { OpenChatBotMessage(Box), OpenChatBotMessageV2(Box), UserDeleted(Box), + NotifyUniquePersonProof(Box<(UserId, UniquePersonProof)>), } #[derive(Serialize, Deserialize, Clone, Debug)] diff --git a/backend/canisters/user_index/impl/src/updates/c2c_notify_events.rs b/backend/canisters/user_index/impl/src/updates/c2c_notify_events.rs index f66ea73df0..423a3d8255 100644 --- a/backend/canisters/user_index/impl/src/updates/c2c_notify_events.rs +++ b/backend/canisters/user_index/impl/src/updates/c2c_notify_events.rs @@ -97,6 +97,14 @@ fn handle_event(event: Event, state: &mut RuntimeState) { Some(caller), ); } + Event::NotifyUniquePersonProof(ev) => { + let (user_id, proof) = *ev; + state.data.users.record_proof_of_unique_personhood(user_id, proof.clone()); + state.push_event_to_all_local_user_indexes( + LocalUserIndexEvent::NotifyUniquePersonProof(user_id, proof), + Some(caller), + ); + } } }