gluon/src/middlewares/admin_check_middleware.rs

56 lines
1.7 KiB
Rust

use async_trait::async_trait;
use telers::{
errors::{EventErrorKind, HandlerError},
event::telegram::{HandlerRequest, HandlerResponse},
middlewares::{InnerMiddleware, Next}, types::Message,
};
use anyhow::Error as Reject;
use crate::{
types::structs::message_sender::MessageSender,
utils::telegram::{admin_check::is_admin, data_getters::get_all_admins},
};
#[derive(Default)]
pub struct AdminCheck {}
#[async_trait]
impl InnerMiddleware for AdminCheck {
async fn call(
&self,
request: HandlerRequest,
next: Next,
) -> Result<HandlerResponse, EventErrorKind> {
let (bot, message) = (request.clone().bot, request.clone().update);
let is_replying_dice: Option<Message> = match message.kind().message().unwrap() {
Message::Dice(dice) => dice.reply_to_message.clone(),
_ => None
};
let admins_list = get_all_admins(&bot, message.chat().unwrap().id())
.await
.unwrap();
let chat_id: i64 = message.chat_id().unwrap();
if is_admin(&admins_list, message.from().unwrap().id) {
let response = next(request).await?;
return Ok(response);
} else {
if is_replying_dice.is_some() {
MessageSender::builder(chat_id)
.text("Недостаточно прав для использования данной команды.")
.build().send(&bot)
.await
.unwrap();
}
return Err(EventErrorKind::Handler(HandlerError::new(Reject::msg(
"User isn't admin.".to_string(),
))));
}
}
}