From c9a222589730bfca96bee4c4d390d441055a3e93 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Mon, 5 Feb 2024 11:09:36 +0000 Subject: [PATCH] Push `DiamondMembershipPaymentReceived` events to all LocalUserIndexes (#5322) --- .../canisters/local_user_index/CHANGELOG.md | 6 ++++ .../canisters/local_user_index/api/src/lib.rs | 2 ++ .../impl/src/model/local_user_map.rs | 4 +++ .../impl/src/updates/c2c_notify_events.rs | 34 ++++++++++++------- backend/canisters/user_index/CHANGELOG.md | 11 +++++- .../impl/src/lifecycle/post_upgrade.rs | 31 +++++++++-------- .../src/updates/pay_for_diamond_membership.rs | 4 +-- 7 files changed, 61 insertions(+), 31 deletions(-) diff --git a/backend/canisters/local_user_index/CHANGELOG.md b/backend/canisters/local_user_index/CHANGELOG.md index a32edfc53a..d669c3c753 100644 --- a/backend/canisters/local_user_index/CHANGELOG.md +++ b/backend/canisters/local_user_index/CHANGELOG.md @@ -7,6 +7,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed +- Handle `DiamondMembershipPaymentReceived` events from non-local users ([#5322](https://github.com/open-chat-labs/open-chat/pull/5322)) + +## [[2.0.1041](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1041-local_user_index)] - 2024-02-02 + +### Changed + - Add `timestamp` to `chat_events` responses ([#5309](https://github.com/open-chat-labs/open-chat/pull/5309)) ## [[2.0.1031](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1031-local_user_index)] - 2024-01-25 diff --git a/backend/canisters/local_user_index/api/src/lib.rs b/backend/canisters/local_user_index/api/src/lib.rs index 08ceafaade..4cb7bfd8da 100644 --- a/backend/canisters/local_user_index/api/src/lib.rs +++ b/backend/canisters/local_user_index/api/src/lib.rs @@ -31,6 +31,8 @@ pub enum Event { OpenChatBotMessage(Box), ReferralCodeAdded(ReferralCodeAdded), UserPrincipalUpdated(UpdateUserPrincipalArgs), + // Post release - remove this + DiamondMembershipExpiryDate(UserId, TimestampMillis), } #[derive(Serialize, Deserialize, Clone, Debug)] diff --git a/backend/canisters/local_user_index/impl/src/model/local_user_map.rs b/backend/canisters/local_user_index/impl/src/model/local_user_map.rs index adeb308255..a4f9f49edc 100644 --- a/backend/canisters/local_user_index/impl/src/model/local_user_map.rs +++ b/backend/canisters/local_user_index/impl/src/model/local_user_map.rs @@ -21,6 +21,10 @@ impl LocalUserMap { self.users.get_mut(user_id) } + pub fn contains(&self, user_id: &UserId) -> bool { + self.users.contains_key(user_id) + } + pub fn mark_cycles_top_up(&mut self, user_id: &UserId, top_up: CyclesTopUp) -> bool { if let Some(user) = self.users.get_mut(user_id) { user.mark_cycles_top_up(top_up); diff --git a/backend/canisters/local_user_index/impl/src/updates/c2c_notify_events.rs b/backend/canisters/local_user_index/impl/src/updates/c2c_notify_events.rs index ea71df8d59..8442954230 100644 --- a/backend/canisters/local_user_index/impl/src/updates/c2c_notify_events.rs +++ b/backend/canisters/local_user_index/impl/src/updates/c2c_notify_events.rs @@ -130,19 +130,21 @@ fn handle_event(event: Event, state: &mut RuntimeState) { .global_users .set_diamond_membership_expiry_date(ev.user_id, ev.expires_at); - state.push_event_to_user( - ev.user_id, - UserEvent::DiamondMembershipPaymentReceived(Box::new(DiamondMembershipPaymentReceived { - timestamp: ev.timestamp, - expires_at: ev.expires_at, - token: ev.token, - amount_e8s: ev.amount_e8s, - block_index: ev.block_index, - duration: ev.duration, - recurring: ev.recurring, - send_bot_message: ev.send_bot_message, - })), - ); + if state.data.local_users.contains(&ev.user_id) { + state.push_event_to_user( + ev.user_id, + UserEvent::DiamondMembershipPaymentReceived(Box::new(DiamondMembershipPaymentReceived { + timestamp: ev.timestamp, + expires_at: ev.expires_at, + token: ev.token, + amount_e8s: ev.amount_e8s, + block_index: ev.block_index, + duration: ev.duration, + recurring: ev.recurring, + send_bot_message: ev.send_bot_message, + })), + ); + } } Event::OpenChatBotMessage(ev) => { state.push_event_to_user(ev.user_id, UserEvent::OpenChatBotMessage(Box::new(ev.message))); @@ -159,5 +161,11 @@ fn handle_event(event: Event, state: &mut RuntimeState) { .global_users .update_user_principal(update.old_principal, update.new_principal); } + Event::DiamondMembershipExpiryDate(user_id, expires_at) => { + state + .data + .global_users + .set_diamond_membership_expiry_date(user_id, expires_at); + } } } diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index ffcb69791c..cce182865d 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -7,11 +7,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added -- Transfer balance from old Diamond membership payments to treasury ([#5307](https://github.com/open-chat-labs/open-chat/pull/5307)) - Add `c2c_send_openchat_bot_messages` endpoint ([#5319](https://github.com/open-chat-labs/open-chat/pull/5319)) ### Changed +- Push `DiamondMembershipPaymentReceived` events to all LocalUserIndexes ([#5322](https://github.com/open-chat-labs/open-chat/pull/5322)) + +## [[2.0.1040](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1039-user_index)] - 2024-02-02 + +### Added + +- Transfer balance from old Diamond membership payments to treasury ([#5307](https://github.com/open-chat-labs/open-chat/pull/5307)) + +### Changed + - Remove excess 0's from token amount in referral reward messages ([#5308](https://github.com/open-chat-labs/open-chat/pull/5308)) ## [[2.0.1039](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1039-user_index)] - 2024-02-01 diff --git a/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs index 323adf7889..6615fbe256 100644 --- a/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs @@ -1,17 +1,13 @@ use crate::lifecycle::{init_env, init_state}; use crate::memory::get_upgrades_memory; -use crate::model::pending_payments_queue::{PendingPayment, PendingPaymentReason}; use crate::{mutate_state, Data}; use canister_logger::LogEntry; use canister_tracing_macros::trace; use ic_cdk_macros::post_upgrade; -use icrc_ledger_types::icrc1::account::Account; -use rand::Rng; +use local_user_index_canister::Event; use stable_memory::get_reader; use tracing::info; -use types::Cryptocurrency; use user_index_canister::post_upgrade::Args; -use utils::consts::SNS_GOVERNANCE_CANISTER_ID; use utils::cycles::init_cycles_dispenser_client; #[post_upgrade] @@ -42,15 +38,20 @@ fn post_upgrade(args: Args) { crate::jobs::sync_legacy_user_principals::start_job_if_required(state); } - // Transfer previously raised funds to the treasury - state.data.pending_payments_queue.push(PendingPayment { - amount: 365285570000, - currency: Cryptocurrency::InternetComputer, - timestamp: state.env.now_nanos(), - recipient_account: Account::from(SNS_GOVERNANCE_CANISTER_ID), - memo: state.env.rng().gen(), - reason: PendingPaymentReason::Treasury, - }); - crate::jobs::make_pending_payments::start_job_if_required(state); + // Post release - remove this + let now = state.env.now(); + for user in state.data.users.iter() { + if let Some(expires_at) = user.diamond_membership_details.expires_at() { + if expires_at > now { + for local_user_index_canister_id in state.data.local_index_map.canisters() { + state.data.user_index_event_sync_queue.push( + *local_user_index_canister_id, + Event::DiamondMembershipExpiryDate(user.user_id, expires_at), + ); + } + } + } + } + crate::jobs::sync_events_to_local_user_index_canisters::start_job_if_required(state); }); } diff --git a/backend/canisters/user_index/impl/src/updates/pay_for_diamond_membership.rs b/backend/canisters/user_index/impl/src/updates/pay_for_diamond_membership.rs index 24c1a43c18..f857e4e4c3 100644 --- a/backend/canisters/user_index/impl/src/updates/pay_for_diamond_membership.rs +++ b/backend/canisters/user_index/impl/src/updates/pay_for_diamond_membership.rs @@ -117,8 +117,7 @@ fn process_charge( let result = diamond_membership.hydrate(now).unwrap(); state.data.users.mark_updated(&user_id, now); - state.push_event_to_local_user_index( - user_id, + state.push_event_to_all_local_user_indexes( Event::DiamondMembershipPaymentReceived(DiamondMembershipPaymentReceived { user_id, timestamp: now, @@ -130,6 +129,7 @@ fn process_charge( recurring, send_bot_message: true, }), + None, ); if let Some(user) = state.data.users.get_by_user_id(&user_id) {