From cd9946ddc692d1cf2d615f5b003843bbee540c37 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Wed, 20 Nov 2024 09:29:36 +0000 Subject: [PATCH] Avoid double iteration when updating member expiry (#6851) --- backend/canisters/community/CHANGELOG.md | 1 + backend/canisters/community/impl/src/lib.rs | 29 ++++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/backend/canisters/community/CHANGELOG.md b/backend/canisters/community/CHANGELOG.md index 30af957d47..3f542cfdb0 100644 --- a/backend/canisters/community/CHANGELOG.md +++ b/backend/canisters/community/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Add the `BotCommand` access token type ([#6830](https://github.com/open-chat-labs/open-chat/pull/6830)) - Add `bot_api_gateway` canisterId to the canister state ([#6842](https://github.com/open-chat-labs/open-chat/pull/6842)) - Simplify `inspect_message` ([#6847](https://github.com/open-chat-labs/open-chat/pull/6847)) +- Avoid double iteration when updating member expiry ([#6851](https://github.com/open-chat-labs/open-chat/pull/6851)) ### Removed diff --git a/backend/canisters/community/impl/src/lib.rs b/backend/canisters/community/impl/src/lib.rs index 07e90b6eda..19b2d136ad 100644 --- a/backend/canisters/community/impl/src/lib.rs +++ b/backend/canisters/community/impl/src/lib.rs @@ -15,7 +15,7 @@ use fire_and_forget_handler::FireAndForgetHandler; use gated_groups::GatePayment; use group_chat_core::AccessRulesInternal; use group_community_common::{ - Achievements, ExpiringMember, ExpiringMemberActions, ExpiringMembers, Members, PaymentReceipts, PaymentRecipient, + Achievements, ExpiringMember, ExpiringMemberActions, ExpiringMembers, Member, Members, PaymentReceipts, PaymentRecipient, PendingPayment, PendingPaymentReason, PendingPaymentsQueue, UserCache, }; use instruction_counts_log::{InstructionCountEntry, InstructionCountFunctionId, InstructionCountsLog}; @@ -638,20 +638,29 @@ impl Data { if let Some(channel_id) = channel_id { if let Some(channel) = self.channels.get_mut(&channel_id) { - user_ids = channel.chat.members.iter().map(|m| m.user_id).collect(); + user_ids = channel + .chat + .members + .iter() + .filter(|m| m.can_member_lapse()) + .map(|m| m.user_id) + .collect(); } } else { - user_ids = self.members.iter().map(|m| m.user_id).collect(); + user_ids = self + .members + .iter() + .filter(|m| m.can_member_lapse()) + .map(|m| m.user_id) + .collect(); } for user_id in user_ids { - if self.can_member_lapse(&user_id, channel_id) { - self.expiring_members.push(ExpiringMember { - expires: now + new_gate_expiry, - channel_id, - user_id, - }); - } + self.expiring_members.push(ExpiringMember { + expires: now + new_gate_expiry, + channel_id, + user_id, + }); } } }