Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardcode airdrop_bot username #6122

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
};
Expand All @@ -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(),
Expand Down
10 changes: 2 additions & 8 deletions backend/canisters/airdrop_bot/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ thread_local! {

canister_state!(RuntimeState);

pub const USERNAME: &str = "AirdropBot";

struct RuntimeState {
pub env: Box<dyn Environment>,
pub data: Data,
Expand Down Expand Up @@ -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,
Expand All @@ -70,8 +70,6 @@ struct Data {
pub chat_ledger_canister_id: CanisterId,
pub admins: HashSet<Principal>,
pub avatar: Timestamped<Option<Document>>,
pub username: String,
pub display_name: Option<String>,
pub airdrops: Airdrops,
pub channels_joined: HashSet<(CommunityId, ChannelId)>,
pub pending_actions_queue: PendingActionsQueue,
Expand All @@ -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(),
Expand All @@ -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<String>,
pub initialized: bool,
pub canister_ids: CanisterIds,
pub airdrops: AirdropsMetrics,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
Loading