Skip to content

Commit

Permalink
Remove transaction from serialized PrizeWinner messages (#6578)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 15, 2024
1 parent d23c441 commit 99fdb66
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -31,3 +31,18 @@ pub struct SuccessResult {
pub pinned_messages: Vec<MessageIndex>,
pub chat_rules: VersionedRules,
}

impl SuccessResult {
pub fn members(&self) -> Vec<GroupMember> {
self.members
.iter()
.cloned()
.chain(self.basic_members.iter().map(|u| GroupMember {
user_id: *u,
date_added: 0,
role: GroupRole::Participant,
lapsed: false,
}))
.collect()
}
}
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion backend/libraries/chat_events/src/chat_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
93 changes: 80 additions & 13 deletions backend/libraries/chat_events/src/message_content_internal.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -1256,43 +1257,91 @@ 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<PrizeWinnerContentInternalCombined> 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<PrizeWinnerContentInternalPrevious> for PrizeWinnerContentInternal {
fn from(value: PrizeWinnerContentInternalPrevious) -> Self {
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,
}
}
Expand All @@ -1301,10 +1350,28 @@ impl From<PrizeWinnerContentInternalPrevious> for PrizeWinnerContentInternal {
impl MessageContentInternalSubtype for PrizeWinnerContentInternal {
type ContentType = PrizeWinnerContent;

fn hydrate(self, _my_user_id: Option<UserId>) -> Self::ContentType {
fn hydrate(self, my_user_id: Option<UserId>) -> 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,
}
}
Expand Down

0 comments on commit 99fdb66

Please sign in to comment.