Skip to content

Commit

Permalink
Don't remove external achievements on expiry (#6588)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Oct 15, 2024
1 parent caacfb5 commit dcdc236
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 4 additions & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Changed

- Don't remove external achievements on expiry ([#6588](https://github.com/open-chat-labs/open-chat/pull/6588))

## [[2.0.1384](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1384-user_index)] - 2024-10-11

### Added
Expand Down
3 changes: 3 additions & 0 deletions backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type ExternalAchievementsArgs = record {
type ExternalAchievementsResponse = variant {
Success : record {
last_updated: TimestampMillis;
added_or_updated : vec ExternalAchievement;
achievements_added : vec ExternalAchievement;
achievements_removed : vec ExternalAchievement;
};
Expand All @@ -157,6 +158,8 @@ type ExternalAchievement = record {
name : text;
url : text;
chit_reward : nat32;
expires : TimestampMillis;
budget_exhausted : bool;
};

type ReferralMetricsResponse = variant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ pub enum Response {
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct SuccessResult {
pub last_updated: TimestampMillis,
pub added_or_updated: Vec<ExternalAchievement>,
// TODO: Remove after FE updated to use added_or_updated
pub achievements_added: Vec<ExternalAchievement>,
// TODO: Remove after FE updated to use added_or_updated
pub achievements_removed: Vec<ExternalAchievement>,
}

#[ts_export(user_index, external_achievements)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub struct ExternalAchievement {
pub id: u32,
pub name: String,
pub url: String,
pub chit_reward: u32,
pub expires: TimestampMillis,
pub budget_exhausted: bool,
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,48 @@ fn external_achievements(args: Args) -> Response {
}

fn external_achievements_impl(args: Args, state: &RuntimeState) -> Response {
let mut added_or_updated = Vec::new();
let mut achievements_added = Vec::new();
let mut achievements_removed = Vec::new();
let mut latest_update: TimestampMillis = 0;
let now = state.env.now();
let mut last_updated: TimestampMillis = 0;

for (id, achievement) in state.data.external_achievements.iter() {
let add = achievement.registered > args.updates_since;
let remove = (achievement.expires <= now && achievement.expires > args.updates_since)
|| achievement.budget_exhausted.map_or(false, |ts| ts > args.updates_since);
let updated = achievement.budget_exhausted.map_or(false, |ts| ts > args.updates_since);

latest_update = max([
latest_update,
last_updated = max([
last_updated,
achievement.registered,
achievement.expires,
achievement.budget_exhausted.unwrap_or_default(),
])
.unwrap();

if add ^ remove {
if add || updated {
let a = ExternalAchievement {
id: *id,
name: achievement.name.clone(),
url: achievement.url.clone(),
chit_reward: achievement.chit_reward,
expires: achievement.expires,
budget_exhausted: achievement.budget_exhausted.is_some(),
};

if add {
achievements_added.push(a);
} else {
achievements_removed.push(a);
achievements_added.push(a.clone());
}

added_or_updated.push(a);
}
}

if achievements_added.is_empty() && achievements_removed.is_empty() {
if added_or_updated.is_empty() {
SuccessNoUpdates
} else {
Success(SuccessResult {
last_updated: latest_update,
last_updated,
added_or_updated,
achievements_added,
achievements_removed,
achievements_removed: Vec::new(),
})
}
}

0 comments on commit dcdc236

Please sign in to comment.