Skip to content

Commit

Permalink
Post release (#5987)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Jul 3, 2024
1 parent 31b8738 commit bcf2535
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 106 deletions.
2 changes: 2 additions & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
18 changes: 0 additions & 18 deletions backend/canisters/user/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down
86 changes: 2 additions & 84 deletions backend/canisters/user/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -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());
})
}
4 changes: 0 additions & 4 deletions backend/canisters/user/impl/src/model/chit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ impl ChitEarnedEvents {
(page, self.events.len() as u32)
}

pub fn iter(&self) -> impl Iterator<Item = &ChitEarned> {
self.events.iter()
}

pub fn achievements(&self, since: Option<TimestampMillis>) -> Vec<ChitEarned> {
self.events
.iter()
Expand Down
2 changes: 2 additions & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit bcf2535

Please sign in to comment.