From bcf25359a81d7d46664eb74df66ca12f48e8b184 Mon Sep 17 00:00:00 2001 From: Matt Grogan Date: Wed, 3 Jul 2024 17:11:52 +0100 Subject: [PATCH] Post release (#5987) --- backend/canisters/user/CHANGELOG.md | 2 + backend/canisters/user/impl/src/lib.rs | 18 ---- .../user/impl/src/lifecycle/post_upgrade.rs | 86 +------------------ backend/canisters/user/impl/src/model/chit.rs | 4 - backend/canisters/user_index/CHANGELOG.md | 2 + 5 files changed, 6 insertions(+), 106 deletions(-) diff --git a/backend/canisters/user/CHANGELOG.md b/backend/canisters/user/CHANGELOG.md index 4d29816768..5bb478c4f7 100644 --- a/backend/canisters/user/CHANGELOG.md +++ b/backend/canisters/user/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [unreleased] +## [[2.0.1222](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1222-user)] - 2024-07-03 + ### Changed - Added `achievements` ([#5962](https://github.com/open-chat-labs/open-chat/pull/5962)) diff --git a/backend/canisters/user/impl/src/lib.rs b/backend/canisters/user/impl/src/lib.rs index f48fd87c6e..540d9f26eb 100644 --- a/backend/canisters/user/impl/src/lib.rs +++ b/backend/canisters/user/impl/src/lib.rs @@ -22,7 +22,6 @@ use notifications_canister::c2c_push_notification; use serde::{Deserialize, Serialize}; use serde_bytes::ByteBuf; use std::cell::RefCell; -use std::cmp::max; use std::collections::HashSet; use std::ops::Deref; use std::time::Duration; @@ -371,23 +370,6 @@ impl Data { msgpack::serialize_then_unwrap(args), ); } - - pub fn init_streak_and_chit_balance(&mut self, now: TimestampMillis) -> u16 { - let mut max_streak: u16 = 0; - self.chit_balance = Timestamped::new(0, now); - - for event in self.chit_events.iter() { - self.chit_balance = Timestamped::new(self.chit_balance.value + event.amount, now); - - let is_daily_claim = matches!(event.reason, ChitEarnedReason::DailyClaim); - - if is_daily_claim && self.streak.claim(event.timestamp) { - max_streak = max(max_streak, self.streak.days(event.timestamp)) - } - } - - max_streak - } } #[derive(Serialize, Debug)] diff --git a/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs index c6099ff8f5..00406c3999 100644 --- a/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs @@ -1,14 +1,13 @@ use crate::lifecycle::{init_env, init_state}; use crate::memory::get_upgrades_memory; -use crate::{mutate_state, Data, RuntimeState}; +use crate::{mutate_state, Data}; use canister_logger::LogEntry; use canister_tracing_macros::trace; -use chat_events::Reader; use ic_cdk::post_upgrade; use stable_memory::get_reader; use std::time::Duration; use tracing::info; -use types::{Achievement, Empty, Milliseconds, UserId}; +use types::{Empty, Milliseconds}; use user_canister::post_upgrade::Args; use utils::time::DAY_IN_MS; @@ -38,8 +37,6 @@ fn post_upgrade(args: Args) { ic_cdk_timers::set_timer(Duration::ZERO, mark_user_canister_empty); } }); - - mutate_state(initialize_chit_and_achievements); } fn mark_user_canister_empty() { @@ -52,82 +49,3 @@ fn mark_user_canister_empty() { ); }) } - -fn initialize_chit_and_achievements(state: &mut RuntimeState) { - let me: UserId = state.env.canister_id().into(); - let now = state.env.now(); - - let longest_streak = state.data.init_streak_and_chit_balance(now); - - if state.data.group_chats.len() > 0 { - state.data.award_achievement(Achievement::JoinedGroup, now); - } - - if state.data.communities.len() > 0 { - state.data.award_achievement(Achievement::JoinedCommunity, now); - } - - if state.data.direct_chats.iter().any(|c| { - c.events - .main_events_reader() - .iter_latest_messages(None) - .any(|m| m.event.sender == me) - }) { - state.data.award_achievement(Achievement::SentDirectMessage, now); - } - - if state.data.direct_chats.iter().any(|c| { - c.events - .main_events_reader() - .iter_latest_messages(None) - .any(|m| m.event.sender == c.them) - }) { - state.data.award_achievement(Achievement::ReceivedDirectMessage, now); - } - - if state.data.avatar.value.is_some() { - state.data.award_achievement(Achievement::SetAvatar, now); - } - - if !state.data.bio.value.is_empty() { - state.data.award_achievement(Achievement::SetBio, now); - } - - if state.data.display_name.value.is_some() { - state.data.award_achievement(Achievement::SetDisplayName, now); - } - - if let Some(diamond_expires) = state.data.diamond_membership_expires_at { - state.data.award_achievement(Achievement::UpgradedToDiamond, now); - - if (diamond_expires - now) > (5 * 365 * DAY_IN_MS) { - state.data.award_achievement(Achievement::UpgradedToGoldDiamond, now); - } - } - - if longest_streak >= 3 { - state.data.award_achievement(Achievement::Streak3, now); - } - - if longest_streak >= 7 { - state.data.award_achievement(Achievement::Streak7, now); - } - - if longest_streak >= 14 { - state.data.award_achievement(Achievement::Streak14, now); - } - - if longest_streak >= 30 { - state.data.award_achievement(Achievement::Streak30, now); - } - - if state.data.chit_balance.value > 0 { - 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()); - }) -} diff --git a/backend/canisters/user/impl/src/model/chit.rs b/backend/canisters/user/impl/src/model/chit.rs index b7b464ba29..0dcf4a8b03 100644 --- a/backend/canisters/user/impl/src/model/chit.rs +++ b/backend/canisters/user/impl/src/model/chit.rs @@ -52,10 +52,6 @@ impl ChitEarnedEvents { (page, self.events.len() as u32) } - pub fn iter(&self) -> impl Iterator { - self.events.iter() - } - pub fn achievements(&self, since: Option) -> Vec { self.events .iter() diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index 6fdb6e7117..a253caa5d8 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [unreleased] +## [[2.0.1221](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1221-user_index)] - 2024-07-03 + ### Added - Added `c2c_notify_chit` to sync chit balance and streak from user canister ([#5972](https://github.com/open-chat-labs/open-chat/pull/5972))