Skip to content

Commit

Permalink
Simplify tip_message (#4428)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Sep 25, 2023
1 parent 76cb98a commit ed6a381
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 252 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{ChannelId, CompletedCryptoTransaction, MessageId, MessageIndex, UserId};
use types::{CanisterId, ChannelId, Cryptocurrency, MessageId, MessageIndex, UserId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub recipient: UserId,
pub channel_id: ChannelId,
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub transfer: CompletedCryptoTransaction,
pub ledger: CanisterId,
pub token: Cryptocurrency,
pub amount: u128,
pub username: String,
pub display_name: Option<String>,
}
Expand Down
26 changes: 16 additions & 10 deletions backend/canisters/community/impl/src/updates/c2c_tip_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::activity_notifications::handle_activity_notification;
use crate::{mutate_state, run_regular_jobs, RuntimeState};
use canister_api_macros::update_msgpack;
use canister_tracing_macros::trace;
use chat_events::Reader;
use chat_events::{Reader, TipMessageArgs};
use community_canister::c2c_tip_message::{Response::*, *};
use group_chat_core::TipMessageResult;
use ledger_utils::format_crypto_amount_with_symbol;
Expand All @@ -29,17 +29,19 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {

if let Some(channel) = state.data.channels.get_mut(&args.channel_id) {
let now = state.env.now();
let token = args.transfer.token();
let amount = args.transfer.units();

match channel.chat.tip_message(
let tip_message_args = TipMessageArgs {
user_id,
args.recipient,
args.thread_root_message_index,
args.message_id,
args.transfer,
recipient: args.recipient,
thread_root_message_index: args.thread_root_message_index,
message_id: args.message_id,
ledger: args.ledger,
token: args.token.clone(),
amount: args.amount,
now,
) {
};

match channel.chat.tip_message(tip_message_args) {
TipMessageResult::Success => {
if let Some((message_index, message_event_index)) = channel
.chat
Expand All @@ -61,7 +63,11 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {
tipped_by: user_id,
tipped_by_name: args.username,
tipped_by_display_name: args.display_name,
tip: format_crypto_amount_with_symbol(amount, token.decimals().unwrap_or(8), token.token_symbol()),
tip: format_crypto_amount_with_symbol(
args.amount,
args.token.decimals().unwrap_or(8),
args.token.token_symbol(),
),
community_avatar_id: state.data.avatar.as_ref().map(|a| a.id),
channel_avatar_id: channel.chat.avatar.as_ref().map(|a| a.id),
});
Expand Down
6 changes: 4 additions & 2 deletions backend/canisters/group/api/src/updates/c2c_tip_message.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{CompletedCryptoTransaction, MessageId, MessageIndex, UserId};
use types::{CanisterId, Cryptocurrency, MessageId, MessageIndex, UserId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub recipient: UserId,
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub transfer: CompletedCryptoTransaction,
pub ledger: CanisterId,
pub token: Cryptocurrency,
pub amount: u128,
pub username: String,
pub display_name: Option<String>,
}
Expand Down
26 changes: 16 additions & 10 deletions backend/canisters/group/impl/src/updates/c2c_tip_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::activity_notifications::handle_activity_notification;
use crate::{mutate_state, run_regular_jobs, RuntimeState};
use canister_api_macros::update_msgpack;
use canister_tracing_macros::trace;
use chat_events::Reader;
use chat_events::{Reader, TipMessageArgs};
use group_canister::c2c_tip_message::{Response::*, *};
use group_chat_core::TipMessageResult;
use ledger_utils::format_crypto_amount_with_symbol;
Expand All @@ -23,17 +23,19 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {

let user_id = state.env.caller().into();
let now = state.env.now();
let token = args.transfer.token();
let amount = args.transfer.units();

match state.data.chat.tip_message(
let tip_message_args = TipMessageArgs {
user_id,
args.recipient,
args.thread_root_message_index,
args.message_id,
args.transfer,
recipient: args.recipient,
thread_root_message_index: args.thread_root_message_index,
message_id: args.message_id,
ledger: args.ledger,
token: args.token.clone(),
amount: args.amount,
now,
) {
};

match state.data.chat.tip_message(tip_message_args) {
TipMessageResult::Success => {
if let Some((message_index, message_event_index)) = state
.data
Expand All @@ -56,7 +58,11 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {
tipped_by: user_id,
tipped_by_name: args.username,
tipped_by_display_name: args.display_name,
tip: format_crypto_amount_with_symbol(amount, token.decimals().unwrap_or(8), token.token_symbol()),
tip: format_crypto_amount_with_symbol(
args.amount,
args.token.decimals().unwrap_or(8),
args.token.token_symbol(),
),
group_avatar_id: state.data.chat.avatar.as_ref().map(|a| a.id),
}),
);
Expand Down
6 changes: 5 additions & 1 deletion backend/canisters/user/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ type RemoveReactionResponse = variant {

type TipMessageArgs = record {
chat : Chat;
recipient : UserId;
thread_root_message_index : opt MessageIndex;
message_id : MessageId;
transfer : PendingCryptoTransaction;
ledger : CanisterId;
token : Cryptocurrency;
amount : nat;
fee : nat;
};

type TipMessageResponse = variant {
Expand Down
6 changes: 4 additions & 2 deletions backend/canisters/user/api/src/updates/c2c_tip_message.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{CompletedCryptoTransaction, MessageId, MessageIndex};
use types::{CanisterId, Cryptocurrency, MessageId, MessageIndex};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub transfer: CompletedCryptoTransaction,
pub ledger: CanisterId,
pub token: Cryptocurrency,
pub amount: u128,
pub username: String,
pub display_name: Option<String>,
pub user_avatar_id: Option<u128>,
Expand Down
8 changes: 6 additions & 2 deletions backend/canisters/user/api/src/updates/tip_message.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{Chat, CompletedCryptoTransaction, MessageId, MessageIndex, PendingCryptoTransaction};
use types::{CanisterId, Chat, CompletedCryptoTransaction, Cryptocurrency, MessageId, MessageIndex, UserId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub chat: Chat,
pub recipient: UserId,
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub transfer: PendingCryptoTransaction,
pub ledger: CanisterId,
pub token: Cryptocurrency,
pub amount: u128,
pub fee: u128,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
31 changes: 18 additions & 13 deletions backend/canisters/user/impl/src/updates/c2c_tip_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{mutate_state, run_regular_jobs, RuntimeState};
use canister_api_macros::update_msgpack;
use canister_tracing_macros::trace;
use chat_events::{Reader, TipMessageResult};
use chat_events::{Reader, TipMessageArgs, TipMessageResult};
use ledger_utils::format_crypto_amount_with_symbol;
use types::{DirectMessageTipped, EventIndex, Notification, UserId};
use user_canister::c2c_tip_message::{Response::*, *};
Expand All @@ -19,19 +19,20 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {
if let Some(chat) = state.data.direct_chats.get_mut(&user_id.into()) {
let now = state.env.now();
let my_user_id = state.env.canister_id().into();
let token = args.transfer.token();
let amount = args.transfer.units();

let tip_message_args = TipMessageArgs {
user_id,
recipient: my_user_id,
thread_root_message_index: args.thread_root_message_index,
message_id: args.message_id,
ledger: args.ledger,
token: args.token.clone(),
amount: args.amount,
now,
};

if matches!(
chat.events.tip_message(
user_id,
my_user_id,
EventIndex::default(),
args.thread_root_message_index,
args.message_id,
args.transfer,
now,
),
chat.events.tip_message(tip_message_args, EventIndex::default(),),
TipMessageResult::Success
) {
if let Some(event) = chat
Expand All @@ -46,7 +47,11 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {
message_event_index: event.index,
username: args.username,
display_name: args.display_name,
tip: format_crypto_amount_with_symbol(amount, token.decimals().unwrap_or(8), token.token_symbol()),
tip: format_crypto_amount_with_symbol(
args.amount,
args.token.decimals().unwrap_or(8),
args.token.token_symbol(),
),
user_avatar_id: args.user_avatar_id,
});
state.push_notification(my_user_id, notification);
Expand Down
Loading

0 comments on commit ed6a381

Please sign in to comment.