Skip to content

Commit

Permalink
Push DiamondMembershipPaymentReceived events to all LocalUserIndexes (
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Feb 5, 2024
1 parent 14e8455 commit c9a2225
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 31 deletions.
6 changes: 6 additions & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions backend/canisters/local_user_index/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub enum Event {
OpenChatBotMessage(Box<OpenChatBotMessage>),
ReferralCodeAdded(ReferralCodeAdded),
UserPrincipalUpdated(UpdateUserPrincipalArgs),
// Post release - remove this
DiamondMembershipExpiryDate(UserId, TimestampMillis),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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);
}
}
}
11 changes: 10 additions & 1 deletion backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 16 additions & 15 deletions backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit c9a2225

Please sign in to comment.