Skip to content

Commit

Permalink
Track event each time a user is deleted (#6025)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jul 11, 2024
1 parent b3e1d1c commit 3bd3182
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 27 additions & 1 deletion backend/canisters/user_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<UserMetrics> {
self.data.users.get_by_user_id(&user_id).map(|user| {
let now = self.env.now();
Expand Down
12 changes: 2 additions & 10 deletions backend/canisters/user_index/impl/src/updates/c2c_notify_events.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions backend/canisters/user_index/impl/src/updates/delete_user.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand Down

0 comments on commit 3bd3182

Please sign in to comment.