From c641a872b4aeb079dceb34043eb726a9a73e3cde Mon Sep 17 00:00:00 2001 From: mykola2312 Date: Sun, 3 Mar 2024 22:26:58 +0200 Subject: [PATCH] begin implementing request listing --- locales/en.yml | 5 +++-- src/bot/bot.rs | 10 +++++++--- src/bot/request.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/locales/en.yml b/locales/en.yml index d7b045f..eda5bda 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -8,5 +8,6 @@ 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" -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 +request_added: "Your requested has been added. Admins have been notifed. You should DM bot with /start to enable personal notifications" +admin_notify_request: "User %{user} awaits request approval" +not_an_admin: "You are not an admin. Back off" \ No newline at end of file diff --git a/src/bot/bot.rs b/src/bot/bot.rs index b6399f8..ee62534 100644 --- a/src/bot/bot.rs +++ b/src/bot/bot.rs @@ -18,7 +18,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; +use super::request::{cmd_request, cmd_listrequests}; fn parse_env(name: &str) -> T where @@ -64,7 +64,8 @@ fn schema() -> UpdateHandler { .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::Request(text)].endpoint(cmd_request)); + .branch(case![Command::Request(text)].endpoint(cmd_request)) + .branch(case![Command::ListRequests].endpoint(cmd_listrequests)); let message_handler = Update::filter_message().branch(command_handler); let raw_message_handler = Update::filter_message().branch(dptree::endpoint(handle_message)); @@ -119,7 +120,10 @@ enum Command { OP, #[command(alias = "request")] - Request(String) + Request(String), + + #[command(alias = "listrequests")] + ListRequests } async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult { diff --git a/src/bot/request.rs b/src/bot/request.rs index 05697c4..28d6806 100644 --- a/src/bot/request.rs +++ b/src/bot/request.rs @@ -7,7 +7,7 @@ use super::notify::notify_admins; use super::types::HandlerResult; use crate::db::chat::find_or_create_chat; use crate::db::user::find_or_create_user; -use crate::db::DbPool; +use crate::db::{DbPool, User}; use crate::reply_i18n_and_return; pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> HandlerResult { @@ -54,3 +54,30 @@ pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> Ha Ok(()) } + +#[derive(sqlx::FromRow, Debug)] +struct RequestWithUser { + pub request_id: i64, + pub message: String, + #[sqlx(flatten)] + pub user: User +} + +pub async fn cmd_listrequests(bot: Bot, msg: Message, db: DbPool) -> HandlerResult { + if let Some(user) = msg.from() { + let user = find_or_create_user(&db, user).await?; + if user.is_admin != 1 { + reply_i18n_and_return!(bot, msg.chat.id, "not_an_admin"); + } + + let mut list = String::new(); + let requests: Vec = sqlx::query_as( + "SELECT request.id AS request_id, request.message, user.*FROM request + INNER JOIN user ON request.requested_by = user.id + WHERE request.is_approved = 0;") + .fetch_all(&db).await?; + dbg!(requests); + } + + Ok(()) +} \ No newline at end of file