Skip to content

Commit

Permalink
Push unique person proof from LocalUserIndex to UserIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jul 18, 2024
1 parent 480a596 commit cf8b751
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
25 changes: 21 additions & 4 deletions backend/canisters/local_user_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
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::*, *};

#[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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
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::*, *};

#[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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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::*, *};
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion backend/canisters/user_index/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +22,7 @@ pub enum Event {
OpenChatBotMessage(Box<OpenChatBotMessage>),
OpenChatBotMessageV2(Box<OpenChatBotMessageV2>),
UserDeleted(Box<UserDeleted>),
NotifyUniquePersonProof(Box<(UserId, UniquePersonProof)>),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
}
}

Expand Down

0 comments on commit cf8b751

Please sign in to comment.