From 3bd3182c582fb60b24fd3bf2c0376b2b806b1bff Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Thu, 11 Jul 2024 11:25:01 +0100 Subject: [PATCH] Track event each time a user is deleted (#6025) --- backend/canisters/user_index/CHANGELOG.md | 1 + backend/canisters/user_index/impl/src/lib.rs | 28 ++++++++++++++++++- .../impl/src/updates/c2c_notify_events.rs | 12 ++------ .../impl/src/updates/delete_user.rs | 10 ++----- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index e5bb5da38d..2c1615343b 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Track event each time a proof of uniqueness is submitted ([#6024](https://github.com/open-chat-labs/open-chat/pull/6024)) +- Track event each time a user is deleted ([#6025](https://github.com/open-chat-labs/open-chat/pull/6025)) ## [[2.0.1231](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1231-user_index)] - 2024-07-08 diff --git a/backend/canisters/user_index/impl/src/lib.rs b/backend/canisters/user_index/impl/src/lib.rs index 2163bb8a68..2a165caaf7 100644 --- a/backend/canisters/user_index/impl/src/lib.rs +++ b/backend/canisters/user_index/impl/src/lib.rs @@ -6,7 +6,7 @@ use crate::timer_job_types::TimerJob; use candid::Principal; use canister_state_macros::canister_state; use canister_timer_jobs::TimerJobs; -use event_store_producer::{EventStoreClient, EventStoreClientBuilder, EventStoreClientInfo}; +use event_store_producer::{EventBuilder, EventStoreClient, EventStoreClientBuilder, EventStoreClientInfo}; use event_store_producer_cdk_runtime::CdkRuntime; use fire_and_forget_handler::FireAndForgetHandler; use icrc_ledger_types::icrc1::account::{Account, Subaccount}; @@ -158,6 +158,32 @@ impl RuntimeState { jobs::submit_message_to_modclub::start_job_if_required(self); } + pub fn delete_user(&mut self, user_id: UserId, triggered_by_user: bool) { + let now = self.env.now(); + self.data.users.delete_user(user_id, now); + self.data.local_index_map.remove_user(&user_id); + self.data.empty_users.remove(&user_id); + + #[derive(Serialize)] + struct EventPayload { + triggered_by_user: bool, + } + + self.data.event_store_client.push( + EventBuilder::new("user_deleted", now) + .with_user(user_id.to_string(), true) + .with_source(self.env.canister_id().to_string(), false) + .with_json_payload(&EventPayload { triggered_by_user }) + .build(), + ); + + self.data.deleted_users.push(DeletedUser { + user_id, + triggered_by_user, + timestamp: now, + }); + } + pub fn user_metrics(&self, user_id: UserId) -> Option { self.data.users.get_by_user_id(&user_id).map(|user| { let now = self.env.now(); 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 d67a8fd4c6..f0354341f0 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 @@ -1,6 +1,6 @@ use crate::guards::caller_is_local_user_index_canister; use crate::timer_job_types::{JoinUserToGroup, TimerJob}; -use crate::{mutate_state, DeletedUser, RuntimeState, UserRegisteredEventPayload, ONE_MB}; +use crate::{mutate_state, RuntimeState, UserRegisteredEventPayload, ONE_MB}; use candid::Principal; use canister_api_macros::update_msgpack; use canister_tracing_macros::trace; @@ -88,15 +88,7 @@ fn handle_event(event: Event, state: &mut RuntimeState) { ); } Event::UserDeleted(ev) => { - let now = state.env.now(); - state.data.users.delete_user(ev.user_id, now); - state.data.local_index_map.remove_user(&ev.user_id); - state.data.empty_users.remove(&ev.user_id); - state.data.deleted_users.push(DeletedUser { - user_id: ev.user_id, - triggered_by_user: false, - timestamp: now, - }); + state.delete_user(ev.user_id, false); state.push_event_to_all_local_user_indexes( LocalUserIndexEvent::DeleteUser(DeleteUser { user_id: ev.user_id, diff --git a/backend/canisters/user_index/impl/src/updates/delete_user.rs b/backend/canisters/user_index/impl/src/updates/delete_user.rs index 8542f6d177..36f8352bb3 100644 --- a/backend/canisters/user_index/impl/src/updates/delete_user.rs +++ b/backend/canisters/user_index/impl/src/updates/delete_user.rs @@ -1,4 +1,4 @@ -use crate::{mutate_state, DeletedUser, RuntimeState}; +use crate::{mutate_state, RuntimeState}; use canister_tracing_macros::trace; use ic_cdk::update; use local_user_index_canister::{DeleteUser, Event}; @@ -20,13 +20,7 @@ fn delete_user_impl(args: Args, state: &mut RuntimeState) -> Response { return NotAuthorized; } - let now = state.env.now(); - state.data.users.delete_user(args.user_id, now); - state.data.deleted_users.push(DeletedUser { - user_id: args.user_id, - triggered_by_user: true, - timestamp: now, - }); + state.delete_user(args.user_id, true); state.push_event_to_all_local_user_indexes( Event::DeleteUser(DeleteUser { user_id: args.user_id,