From 1796a360ae215c0c64f26edd386a08910cff6d15 Mon Sep 17 00:00:00 2001 From: mykola2312 Date: Sun, 3 Mar 2024 19:26:30 +0200 Subject: [PATCH] implement request command --- locales/en.yml | 4 +++- src/bot.rs | 7 ++++--- src/bot/bot.rs | 7 ++++++- src/bot/notify.rs | 23 +++++++++++++++++++++++ src/bot/request.rs | 23 +++++++++++++++++------ 5 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/bot/notify.rs diff --git a/locales/en.yml b/locales/en.yml index 2e38028..d7b045f 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -7,4 +7,6 @@ started_public_chat: "For using the bot you may want to request access via /requ request_text_is_too_short: "Fuck off unless you write meaningful request message" request_text_is_too_long: "I ain't reading allat" already_can_download: "You already have permission to download, no need to request it then" -already_has_requested: "Stop spamming requests. That ain't gonna grant ya the download perm" \ No newline at end of file +already_has_requested: "Stop spamming requests. That ain't gonna grant ya the download perm" +request_added: "Your requested has been added. Admins have been notifed" +admin_notify_request: "User %{user} awaits request approval" \ No newline at end of file diff --git a/src/bot.rs b/src/bot.rs index 53d7c23..f5868d0 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,8 +1,9 @@ pub mod bot; pub mod dl; +pub mod notify; pub mod op; -pub mod sanitize; pub mod request; +pub mod sanitize; pub mod start; pub mod types; @@ -11,5 +12,5 @@ macro_rules! reply_i18n_and_return { ($bot:expr, $chat_id:expr, $line:expr) => { $bot.send_message($chat_id, t!($line)).await?; return Ok(()) - } -} \ No newline at end of file + }; +} diff --git a/src/bot/bot.rs b/src/bot/bot.rs index d435bf0..2dd008d 100644 --- a/src/bot/bot.rs +++ b/src/bot/bot.rs @@ -17,6 +17,7 @@ use crate::db::DbPool; use super::dl::cmd_download; use super::op::cmd_op; use super::start::{cmd_start, handle_my_chat_member}; +use super::request::cmd_request; fn parse_env(name: &str) -> T where @@ -61,7 +62,8 @@ fn schema() -> UpdateHandler { .branch(case![Command::Test].endpoint(cmd_test)) .branch(case![Command::Start].endpoint(cmd_start)) .branch(case![Command::Download(url)].endpoint(cmd_download)) - .branch(case![Command::OP].endpoint(cmd_op)); + .branch(case![Command::OP].endpoint(cmd_op)) + .branch(case![Command::Request(text)].endpoint(cmd_request)); let message_handler = Update::filter_message().branch(command_handler); let raw_message_handler = Update::filter_message().branch(dptree::endpoint(handle_message)); @@ -114,6 +116,9 @@ enum Command { #[command(alias = "op")] OP, + + #[command(alias = "request")] + Request(String) } async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult { diff --git a/src/bot/notify.rs b/src/bot/notify.rs new file mode 100644 index 0000000..ce69b14 --- /dev/null +++ b/src/bot/notify.rs @@ -0,0 +1,23 @@ +use teloxide::{prelude::*, types::Recipient}; +use tracing::{event, Level}; + +use crate::db::{DbPool, User}; + +use super::types::HandlerResult; + +pub async fn notify_admins(bot: &Bot, db: &DbPool, message: String) -> HandlerResult { + let admins: Vec = + sqlx::query_as("SELECT * FROM user WHERE is_admin = 1 AND has_private_chat = 1;") + .fetch_all(db) + .await?; + + for admin in admins { + let res = bot + .send_message(Recipient::Id(ChatId(admin.tg_id)), &message) + .await; + if let Err(e) = res { + event!(Level::WARN, "notify admin {} error {}", admin, e); + } + } + Ok(()) +} diff --git a/src/bot/request.rs b/src/bot/request.rs index 7a7ff83..f5e23e5 100644 --- a/src/bot/request.rs +++ b/src/bot/request.rs @@ -1,18 +1,19 @@ -use sqlx::Row; use rust_i18n::t; +use sqlx::Row; use teloxide::prelude::*; use tracing::{event, Level}; +use super::notify::notify_admins; use super::types::HandlerResult; -use crate::reply_i18n_and_return; use crate::db::chat::find_or_create_chat; use crate::db::user::find_or_create_user; use crate::db::DbPool; +use crate::reply_i18n_and_return; pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> HandlerResult { if text.len() < 16 { reply_i18n_and_return!(bot, msg.chat.id, "request_text_is_too_short"); - } else if text.len() > 255 { + } else if text.len() > 100 { reply_i18n_and_return!(bot, msg.chat.id, "request_text_is_too_long"); } @@ -24,7 +25,8 @@ pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> Ha let requests: i64 = sqlx::query("SELECT COUNT(1) FROM request WHERE requested_by = $1;") .bind(user.id) - .fetch_one(&db).await? + .fetch_one(&db) + .await? .get(0); if requests > 0 { reply_i18n_and_return!(bot, msg.chat.id, "already_has_requested"); @@ -35,11 +37,20 @@ pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> Ha .bind(user.id) .bind(text) .bind(0) - .execute(&db).await?; + .execute(&db) + .await?; event!(Level::INFO, "added request for {}", user); // notify admins + notify_admins( + &bot, + &db, + t!("admin_notify_request", user = user.to_string()).to_string(), + ) + .await?; + + bot.send_message(msg.chat.id, t!("request_added")).await?; } todo!() -} \ No newline at end of file +}