From 4d63cfc2de7fc6ba2bc992369ef0c5af6fe10c9d Mon Sep 17 00:00:00 2001 From: megrogan Date: Fri, 26 Jul 2024 14:30:46 +0100 Subject: [PATCH] Hardcode airdrop_bot username --- .../impl/src/jobs/process_pending_actions.rs | 40 ++++++++----------- backend/canisters/airdrop_bot/impl/src/lib.rs | 10 +---- .../impl/src/updates/handle_direct_message.rs | 8 +--- 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/backend/canisters/airdrop_bot/impl/src/jobs/process_pending_actions.rs b/backend/canisters/airdrop_bot/impl/src/jobs/process_pending_actions.rs index 792018b13c..139e5670d0 100644 --- a/backend/canisters/airdrop_bot/impl/src/jobs/process_pending_actions.rs +++ b/backend/canisters/airdrop_bot/impl/src/jobs/process_pending_actions.rs @@ -1,5 +1,5 @@ use crate::model::pending_actions_queue::{Action, AirdropMessage, AirdropTransfer, AirdropType, LotteryAirdrop, MainAidrop}; -use crate::{mutate_state, read_state, RuntimeState}; +use crate::{mutate_state, read_state, RuntimeState, USERNAME}; use candid::Principal; use ic_cdk_timers::TimerId; use icrc_ledger_types::icrc1::account::Account; @@ -185,24 +185,19 @@ async fn handle_main_message_action(action: AirdropMessage) { return; }; - let Some((username, display_name, month)) = read_state(|state| { - state - .data - .airdrops - .current(state.env.now()) - .and_then(|c| { - let date = time::OffsetDateTime::from_unix_timestamp((c.start / 1000) as i64).unwrap(); - let format = format_description!("[month repr:long]"); - date.format(format).ok() - }) - .map(|m| (state.data.username.clone(), state.data.display_name.clone(), m)) + let Some(month) = read_state(|state| { + state.data.airdrops.current(state.env.now()).and_then(|c| { + let date = time::OffsetDateTime::from_unix_timestamp((c.start / 1000) as i64).unwrap(); + let format = format_description!("[month repr:long]"); + date.format(format).ok() + }) }) else { return; }; let args = user_canister::c2c_handle_bot_messages::Args { - bot_name: username, - bot_display_name: display_name, + bot_name: USERNAME.to_string(), + bot_display_name: None, messages: vec![BotMessage { thread_root_message_id: None, content: MessageContentInitial::Crypto(CryptoContent { @@ -230,15 +225,12 @@ async fn handle_lottery_message_action(action: AirdropMessage) { return; }; - let Some((username, community_id, channel_id, message_id)) = mutate_state(|state| { - state.data.airdrops.current(state.env.now()).map(|c| { - ( - state.data.username.clone(), - c.community_id, - c.channel_id, - state.env.rng().gen(), - ) - }) + let Some((community_id, channel_id, message_id)) = mutate_state(|state| { + state + .data + .airdrops + .current(state.env.now()) + .map(|c| (c.community_id, c.channel_id, state.env.rng().gen())) }) else { return; }; @@ -264,7 +256,7 @@ async fn handle_lottery_message_action(action: AirdropMessage) { "Congratulations! You have won {position} prize in the CHIT for CHAT airdrop lottery!" )), }), - sender_name: username, + sender_name: USERNAME.to_string(), sender_display_name: None, replies_to: None, mentioned: Vec::new(), diff --git a/backend/canisters/airdrop_bot/impl/src/lib.rs b/backend/canisters/airdrop_bot/impl/src/lib.rs index 492554daae..de9fef61af 100644 --- a/backend/canisters/airdrop_bot/impl/src/lib.rs +++ b/backend/canisters/airdrop_bot/impl/src/lib.rs @@ -22,6 +22,8 @@ thread_local! { canister_state!(RuntimeState); +pub const USERNAME: &str = "AirdropBot"; + struct RuntimeState { pub env: Box, pub data: Data, @@ -50,8 +52,6 @@ impl RuntimeState { cycles_balance: self.env.cycles_balance(), wasm_version: WASM_VERSION.with_borrow(|v| **v), git_commit_id: utils::git::git_commit_id().to_string(), - username: self.data.username.clone(), - display_name: self.data.display_name.clone(), initialized: self.data.initialized, canister_ids: CanisterIds { user_index: self.data.user_index_canister_id, @@ -70,8 +70,6 @@ struct Data { pub chat_ledger_canister_id: CanisterId, pub admins: HashSet, pub avatar: Timestamped>, - pub username: String, - pub display_name: Option, pub airdrops: Airdrops, pub channels_joined: HashSet<(CommunityId, ChannelId)>, pub pending_actions_queue: PendingActionsQueue, @@ -94,8 +92,6 @@ impl Data { chat_ledger_canister_id, admins, avatar: Timestamped::default(), - username: "".to_string(), - display_name: None, airdrops: Airdrops::default(), channels_joined: HashSet::default(), pending_actions_queue: PendingActionsQueue::default(), @@ -114,8 +110,6 @@ pub struct Metrics { pub cycles_balance: Cycles, pub wasm_version: BuildVersion, pub git_commit_id: String, - pub username: String, - pub display_name: Option, pub initialized: bool, pub canister_ids: CanisterIds, pub airdrops: AirdropsMetrics, diff --git a/backend/canisters/airdrop_bot/impl/src/updates/handle_direct_message.rs b/backend/canisters/airdrop_bot/impl/src/updates/handle_direct_message.rs index 3e11d66f6a..de3d1ec6b3 100644 --- a/backend/canisters/airdrop_bot/impl/src/updates/handle_direct_message.rs +++ b/backend/canisters/airdrop_bot/impl/src/updates/handle_direct_message.rs @@ -1,4 +1,4 @@ -use crate::{mutate_state, RuntimeState}; +use crate::USERNAME; use airdrop_bot_canister::handle_direct_message::*; use canister_api_macros::update; use canister_tracing_macros::trace; @@ -7,13 +7,9 @@ use types::{BotMessage, MessageContentInitial, TextContent}; #[update(msgpack = true)] #[trace] fn handle_direct_message(_args: Args) -> Response { - mutate_state(handle_message) -} - -fn handle_message(state: &mut RuntimeState) -> Response { let text = "Hi, I am the bot which conducts the CHIT for CHAT airdrops. For information about CHIT and the aridrops please read [this blog post](https://oc.app/blog/chit).".to_string(); Success(SuccessResult { - bot_name: state.data.username.clone(), + bot_name: USERNAME.to_string(), bot_display_name: None, messages: vec![BotMessage { thread_root_message_id: None,