Skip to content

Commit

Permalink
Fix LifetimeDiamondMembership achievement (#5995)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Jul 5, 2024
1 parent 824b2c9 commit 44b04d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added

- Add `LifetimeDiamondMembership` access gate ([#5986](https://github.com/open-chat-labs/open-chat/pull/5986))
- Fix `LifetimeDiamondMembership` achievement ([#5995](https://github.com/open-chat-labs/open-chat/pull/5995))

## [[2.0.1222](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1222-user)] - 2024-07-03

Expand Down
24 changes: 22 additions & 2 deletions backend/canisters/user/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::{mutate_state, Data};
use crate::{mutate_state, Data, RuntimeState};
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use ic_cdk::post_upgrade;
use stable_memory::get_reader;
use std::time::Duration;
use tracing::info;
use types::{Empty, Milliseconds};
use types::{Achievement, Empty, Milliseconds};
use user_canister::post_upgrade::Args;
use utils::time::DAY_IN_MS;

Expand Down Expand Up @@ -37,6 +37,8 @@ fn post_upgrade(args: Args) {
ic_cdk_timers::set_timer(Duration::ZERO, mark_user_canister_empty);
}
});

mutate_state(fix_achievements);
}

fn mark_user_canister_empty() {
Expand All @@ -49,3 +51,21 @@ fn mark_user_canister_empty() {
);
})
}

fn fix_achievements(state: &mut RuntimeState) {
let now = state.env.now();

if let Some(diamond_expires) = state.data.diamond_membership_expires_at {
let lifetime_diamond = (diamond_expires - now) > (5 * 365 * DAY_IN_MS);

if lifetime_diamond && state.data.award_achievement(Achievement::UpgradedToGoldDiamond, now) {
ic_cdk_timers::set_timer(Duration::ZERO, notify_user_index_of_chit);
}
}
}

fn notify_user_index_of_chit() {
mutate_state(|state| {
state.data.notify_user_index_of_chit(state.env.now());
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn process_event(event: Event, state: &mut RuntimeState) {
let mut awarded = state.data.award_achievement(Achievement::UpgradedToDiamond, now);

if matches!(ev.duration, DiamondMembershipPlanDuration::Lifetime) {
awarded = awarded || state.data.award_achievement(Achievement::UpgradedToGoldDiamond, now);
awarded |= state.data.award_achievement(Achievement::UpgradedToGoldDiamond, now);
}

if awarded {
Expand Down

0 comments on commit 44b04d8

Please sign in to comment.