From 99fdb66176a989d8334e7adc7edd817467100bc6 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Tue, 15 Oct 2024 12:15:38 +0100 Subject: [PATCH] Remove `transaction` from serialized PrizeWinner messages (#6578) --- .../impl/src/jobs/execute_airdrop.rs | 2 +- backend/canisters/community/CHANGELOG.md | 1 + .../src/queries/selected_channel_initial.rs | 17 +++- backend/canisters/group/CHANGELOG.md | 1 + .../libraries/chat_events/src/chat_events.rs | 2 +- .../src/message_content_internal.rs | 93 ++++++++++++++++--- 6 files changed, 100 insertions(+), 16 deletions(-) diff --git a/backend/canisters/airdrop_bot/impl/src/jobs/execute_airdrop.rs b/backend/canisters/airdrop_bot/impl/src/jobs/execute_airdrop.rs index 8fc70a36c1..683afa2638 100644 --- a/backend/canisters/airdrop_bot/impl/src/jobs/execute_airdrop.rs +++ b/backend/canisters/airdrop_bot/impl/src/jobs/execute_airdrop.rs @@ -98,7 +98,7 @@ async fn prepare_airdrop(config: AirdropConfig, user_index_canister_id: Canister ) .await { - Ok(community_canister::selected_channel_initial::Response::Success(success)) => success.members, + Ok(community_canister::selected_channel_initial::Response::Success(success)) => success.members(), Ok(resp) => { error!(?resp, "Failed to get channel members"); return; diff --git a/backend/canisters/community/CHANGELOG.md b/backend/canisters/community/CHANGELOG.md index e8fa30e211..ac2e675fa1 100644 --- a/backend/canisters/community/CHANGELOG.md +++ b/backend/canisters/community/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Reduce size of some message types when serialized ([#6559](https://github.com/open-chat-labs/open-chat/pull/6559)) - Log details whenever a prize claim results in a ledger error ([#6560](https://github.com/open-chat-labs/open-chat/pull/6560)) - Reduce size of responses by only returning UserIds for basic members ([#6577](https://github.com/open-chat-labs/open-chat/pull/6577)) +- Remove `transaction` from serialized PrizeWinner messages ([#6578](https://github.com/open-chat-labs/open-chat/pull/6578)) ## [[2.0.1378](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1378-community)] - 2024-10-10 diff --git a/backend/canisters/community/api/src/queries/selected_channel_initial.rs b/backend/canisters/community/api/src/queries/selected_channel_initial.rs index e7a74148ee..685df43c34 100644 --- a/backend/canisters/community/api/src/queries/selected_channel_initial.rs +++ b/backend/canisters/community/api/src/queries/selected_channel_initial.rs @@ -1,7 +1,7 @@ use candid::CandidType; use serde::{Deserialize, Serialize}; use ts_export::ts_export; -use types::{ChannelId, EventIndex, GroupMember, MessageIndex, TimestampMillis, UserId, VersionedRules}; +use types::{ChannelId, EventIndex, GroupMember, GroupRole, MessageIndex, TimestampMillis, UserId, VersionedRules}; #[ts_export(community, selected_channel_initial)] #[derive(CandidType, Serialize, Deserialize, Debug)] @@ -31,3 +31,18 @@ pub struct SuccessResult { pub pinned_messages: Vec, pub chat_rules: VersionedRules, } + +impl SuccessResult { + pub fn members(&self) -> Vec { + self.members + .iter() + .cloned() + .chain(self.basic_members.iter().map(|u| GroupMember { + user_id: *u, + date_added: 0, + role: GroupRole::Participant, + lapsed: false, + })) + .collect() + } +} diff --git a/backend/canisters/group/CHANGELOG.md b/backend/canisters/group/CHANGELOG.md index 1b800e764c..8cb03f8324 100644 --- a/backend/canisters/group/CHANGELOG.md +++ b/backend/canisters/group/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Reduce size of responses by only returning UserIds for basic members ([#6577](https://github.com/open-chat-labs/open-chat/pull/6577)) +- Remove `transaction` from serialized PrizeWinner messages ([#6578](https://github.com/open-chat-labs/open-chat/pull/6578)) ## [[2.0.1380](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1380-group)] - 2024-10-11 diff --git a/backend/libraries/chat_events/src/chat_events.rs b/backend/libraries/chat_events/src/chat_events.rs index f6c93f6052..6ef2322e54 100644 --- a/backend/libraries/chat_events/src/chat_events.rs +++ b/backend/libraries/chat_events/src/chat_events.rs @@ -959,10 +959,10 @@ impl ChatEvents { content: MessageContentInternal::PrizeWinner(PrizeWinnerContentInternal { winner, ledger: transaction.ledger_canister_id(), + token_symbol: transaction.token().token_symbol().to_string(), amount: transaction.units(), fee: transaction.fee(), block_index: transaction.index(), - transaction, prize_message: message_index, }), mentioned: Vec::new(), diff --git a/backend/libraries/chat_events/src/message_content_internal.rs b/backend/libraries/chat_events/src/message_content_internal.rs index d0adca602a..009243afca 100644 --- a/backend/libraries/chat_events/src/message_content_internal.rs +++ b/backend/libraries/chat_events/src/message_content_internal.rs @@ -1,4 +1,5 @@ use crate::DeletedByInternal; +use candid::Principal; use ledger_utils::{create_pending_transaction, format_crypto_amount}; use search::Document; use serde::{Deserialize, Serialize}; @@ -227,8 +228,8 @@ impl MessageContentInternal { diamond_only: c.diamond_only, }), MessageContentInternal::PrizeWinner(c) => MessageContentEventPayload::PrizeWinner(PrizeWinnerContentEventPayload { - token: c.transaction.token().token_symbol().to_string(), - amount: c.transaction.units(), + token: c.token_symbol.clone(), + amount: c.amount, }), MessageContentInternal::MessageReminderCreated(c) => { MessageContentEventPayload::MessageReminderCreated(MessageReminderContentEventPayload { @@ -319,7 +320,7 @@ impl From<&MessageContentInternal> for Document { try_add_caption(&mut document, c.caption.as_ref()) } MessageContentInternal::PrizeWinner(c) => { - document.add_field(c.transaction.token().token_symbol().to_string(), 1.0, false); + document.add_field(c.token_symbol.clone(), 1.0, false); } MessageContentInternal::MessageReminderCreated(r) => try_add_caption(&mut document, r.notes.as_ref()), MessageContentInternal::MessageReminder(r) => try_add_caption(&mut document, r.notes.as_ref()), @@ -1256,32 +1257,80 @@ impl MessageContentInternalSubtype for PrizeContentInternal { } #[derive(Serialize, Deserialize, Clone, Debug)] -#[serde(from = "PrizeWinnerContentInternalPrevious")] +#[serde(from = "PrizeWinnerContentInternalCombined")] pub struct PrizeWinnerContentInternal { #[serde(rename = "w")] pub winner: UserId, #[serde(rename = "l")] pub ledger: CanisterId, + #[serde(rename = "t")] + pub token_symbol: String, #[serde(rename = "a")] pub amount: u128, #[serde(rename = "f")] pub fee: u128, #[serde(rename = "i")] pub block_index: u64, - #[serde(rename = "t")] - pub transaction: CompletedCryptoTransaction, #[serde(rename = "m")] pub prize_message: MessageIndex, } +#[allow(clippy::large_enum_variant)] +#[derive(Serialize, Deserialize, Clone, Debug)] +#[serde(untagged)] +enum PrizeWinnerContentInternalCombined { + Old(PrizeWinnerContentInternalPrevious), + New { + #[serde(rename = "w")] + winner: UserId, + #[serde(rename = "l")] + ledger: CanisterId, + #[serde(rename = "t")] + token_symbol: String, + #[serde(rename = "a")] + amount: u128, + #[serde(rename = "f")] + fee: u128, + #[serde(rename = "i")] + block_index: u64, + #[serde(rename = "m")] + prize_message: MessageIndex, + }, +} + +impl From for PrizeWinnerContentInternal { + fn from(value: PrizeWinnerContentInternalCombined) -> Self { + match value { + PrizeWinnerContentInternalCombined::Old(p) => p.into(), + PrizeWinnerContentInternalCombined::New { + winner, + ledger, + token_symbol, + amount, + fee, + block_index, + prize_message, + } => PrizeWinnerContentInternal { + winner, + ledger, + token_symbol, + amount, + fee, + block_index, + prize_message, + }, + } + } +} + #[derive(Serialize, Deserialize, Clone, Debug)] -pub struct PrizeWinnerContentInternalPrevious { +struct PrizeWinnerContentInternalPrevious { #[serde(rename = "w")] - pub winner: UserId, + winner: UserId, #[serde(rename = "t")] - pub transaction: CompletedCryptoTransaction, + transaction: CompletedCryptoTransaction, #[serde(rename = "m")] - pub prize_message: MessageIndex, + prize_message: MessageIndex, } impl From for PrizeWinnerContentInternal { @@ -1289,10 +1338,10 @@ impl From for PrizeWinnerContentInternal { PrizeWinnerContentInternal { winner: value.winner, ledger: value.transaction.ledger_canister_id(), + token_symbol: value.transaction.token().token_symbol().to_string(), amount: value.transaction.units(), fee: value.transaction.fee(), block_index: value.transaction.index(), - transaction: value.transaction, prize_message: value.prize_message, } } @@ -1301,10 +1350,28 @@ impl From for PrizeWinnerContentInternal { impl MessageContentInternalSubtype for PrizeWinnerContentInternal { type ContentType = PrizeWinnerContent; - fn hydrate(self, _my_user_id: Option) -> Self::ContentType { + fn hydrate(self, my_user_id: Option) -> Self::ContentType { PrizeWinnerContent { winner: self.winner, - transaction: self.transaction, + transaction: CompletedCryptoTransaction::ICRC1(types::icrc1::CompletedCryptoTransaction { + ledger: self.ledger, + token: Cryptocurrency::Other(self.token_symbol.clone()), + amount: self.amount, + from: types::icrc1::Account { + owner: Principal::anonymous(), + subaccount: None, + } + .into(), + to: types::icrc1::Account { + owner: my_user_id.map(|u| u.into()).unwrap_or(Principal::anonymous()), + subaccount: None, + } + .into(), + fee: self.fee, + memo: None, + created: 0, + block_index: self.block_index, + }), prize_message: self.prize_message, } }