refactor(utils): sort, rename and refactor utils

This commit is contained in:
doryan 2024-07-09 22:57:37 +04:00
parent e0a5e3a0bb
commit ec2ff44e36
15 changed files with 116 additions and 60 deletions

View File

@ -0,0 +1,11 @@
pub fn cast_box<'a>(boxed: Box<str>) -> &'a str {
unsafe { &*Box::into_raw(boxed) }
}
#[allow(clippy::boxed_local)]
pub fn cast_boxed<'a>(boxed_array: Box<[Box<str>]>) -> Vec<&'a str> {
boxed_array
.iter()
.map(|arg| cast_box(arg.to_owned()))
.collect()
}

View File

@ -3,7 +3,7 @@ use chrono::{Duration, Local, NaiveDateTime};
use crate::types::{enums::time_metrics::TimeMetrics, TimeValues};
#[inline]
pub fn unrestrict_date(duration: TimeValues) -> NaiveDateTime {
pub fn expiration_date(duration: TimeValues) -> NaiveDateTime {
let mute_duration = match duration.0 {
TimeMetrics::Minutes(min) => Duration::minutes(min),
TimeMetrics::Hours(hrs) => Duration::hours(hrs),

View File

@ -4,13 +4,13 @@ use crate::types::{
structs::countable_time::CountableTime, traits::countable_interface::ICountable, TimeValues,
};
use super::unrestrict_date::unrestrict_date;
use super::expiration_date::expiration_date;
pub type ExtractedDuration = (NaiveDateTime, String, i64);
pub fn get_duration(time: TimeValues) -> ExtractedDuration {
pub fn get_expiration_time(time: TimeValues) -> ExtractedDuration {
let time_duration = time.0.extract();
let unmute_date = unrestrict_date(time);
let unmute_date = expiration_date(time);
let postfix = CountableTime::from_value(time_duration)
.get_postfix(time.0)
.unwrap();

View File

@ -1,3 +1,3 @@
pub mod get_duration;
pub mod parse_boxed_array;
pub mod unrestrict_date;
pub mod expiration_date;
pub mod get_expiration_time;
pub mod cast_boxed_array;

View File

@ -1,11 +0,0 @@
pub fn parse_box<'a>(boxed: Box<str>) -> &'a str {
unsafe { &*Box::into_raw(boxed) }
}
#[allow(clippy::boxed_local)]
pub fn parse_boxed<'a>(boxed_array: Box<[Box<str>]>) -> Vec<&'a str> {
boxed_array
.iter()
.map(|arg| parse_box(arg.to_owned()))
.collect()
}

View File

@ -1,6 +1,7 @@
use telers::types::chat_member::ChatMember;
pub fn is_admin(all_admin_members: &Vec<ChatMember>, user_id: i64) -> bool {
//fix: moderators with non full rights can use admin commands now.
all_admin_members
.iter()
.any(|admin: &ChatMember| match admin {
@ -9,10 +10,7 @@ pub fn is_admin(all_admin_members: &Vec<ChatMember>, user_id: i64) -> bool {
&& admin.can_change_info
&& admin.can_delete_messages
&& admin.can_promote_members
&& admin.can_manage_chat
&& admin.can_restrict_members
&& admin.can_invite_users
&& admin.can_manage_topics.unwrap()
}
ChatMember::Owner(owner) => owner.user.id == user_id,
_ => false,

View File

@ -0,0 +1,28 @@
use crate::types::{
enums::target_user::TargetUser,
structs::handler_entity::HandlerEntity
};
pub fn get_user(
handler_entity: HandlerEntity,
arg: Option<&str>,
arg_pos: &mut usize,
) -> TargetUser {
match (
handler_entity.message_reciever.reply_to_message(),
arg,
) {
(Some(msg), _) => TargetUser::CurrentMessage(msg.clone()),
(None, Some(raw_id)) => {
*arg_pos += 1;
if let Ok(id) = raw_id.parse::<i64>() {
TargetUser::Id(id)
} else {
TargetUser::None
}
}
(None, None) => {
TargetUser::None
}
}
}

View File

@ -1,6 +0,0 @@
use telers::{errors::session::ErrorKind, methods::BanChatMember, Bot};
#[inline]
pub async fn ban_chat_member(bot: &Bot, chat_id: i64, user_id: i64) -> Result<bool, ErrorKind> {
bot.send(BanChatMember::new(chat_id, user_id)).await
}

View File

@ -0,0 +1,22 @@
use telers::{
methods::GetChatAdministrators,
types::{ChatMember, Message},
Bot,
errors::SessionErrorKind
};
use crate::types::structs::{handler_entity::HandlerEntity, message_sender::MessageSender};
pub fn get_chat_data(bot: &Bot, message: &Message) -> (i64, HandlerEntity) {
let (message_id, chat_id): (i64, i64) = (message.id(), message.chat().id());
let sender = MessageSender::builder(chat_id).reply_to(message_id);
let handler_entity: HandlerEntity = HandlerEntity::new(bot.clone(), message.clone(), sender);
(chat_id, handler_entity)
}
#[inline]
pub async fn get_all_admins(bot: &Bot, chat_id: i64) -> Result<Vec<ChatMember>, SessionErrorKind> {
bot.send(GetChatAdministrators::new(chat_id))
.await
}

View File

@ -1,21 +0,0 @@
use telers::{errors::session::ErrorKind, methods::promote_chat_member::PromoteChatMember, Bot};
#[inline]
pub async fn demote_user(bot: &Bot, user_id: i64, chat_id: i64) -> Result<bool, ErrorKind> {
bot.send(
PromoteChatMember::new(chat_id, user_id)
.can_manage_topics(false)
.can_pin_messages(false)
.can_invite_users(false)
.can_change_info(false)
.can_promote_members(false)
.can_restrict_members(false)
.can_manage_voice_chats(false)
.can_delete_messages(false)
.can_edit_messages(false)
.can_post_messages(false)
.can_manage_chat(false)
.is_anonymous(false),
)
.await
}

View File

@ -1,7 +0,0 @@
use telers::{errors::SessionErrorKind, methods::get_chat_administrators, types::ChatMember, Bot};
#[inline]
pub async fn get_all_admins(bot: &Bot, chat_id: i64) -> Result<Vec<ChatMember>, SessionErrorKind> {
bot.send(get_chat_administrators::GetChatAdministrators::new(chat_id))
.await
}

View File

@ -1,6 +1,6 @@
pub mod admin_check;
pub mod ban_member;
pub mod demote;
pub mod get_all_admins;
pub mod restrict;
pub mod args_parsers;
pub mod data_getters;
pub mod rights_control;
pub mod senders;
pub mod try_do;

View File

@ -31,3 +31,23 @@ pub async fn restrict(
)
.await
}
#[inline]
pub async fn demote_user(bot: &Bot, user_id: i64, chat_id: i64) -> Result<bool, ErrorKind> {
bot.send(
PromoteChatMember::new(chat_id, user_id)
.can_manage_topics(false)
.can_pin_messages(false)
.can_invite_users(false)
.can_change_info(false)
.can_promote_members(false)
.can_restrict_members(false)
.can_manage_voice_chats(false)
.can_delete_messages(false)
.can_edit_messages(false)
.can_post_messages(false)
.can_manage_chat(false)
.is_anonymous(false),
)
.await
}

View File

@ -0,0 +1,22 @@
use telers::{
event::{
telegram::HandlerResult,
EventReturn
},
enums::ParseMode,
types::Message,
Bot
};
use crate::types::structs::message_sender::MessageSender;
pub async fn send_html(bot: Bot, message: Message, info_text: &str) -> HandlerResult {
MessageSender::builder(message.chat().id())
.text(info_text)
.parse_mode(ParseMode::HTML)
.build()
.send(&bot)
.await?;
Ok(EventReturn::Finish)
}

View File

@ -3,7 +3,7 @@ use telers::{errors::SessionErrorKind as ErrorKind, event::EventReturn, types::C
use crate::types::structs::message_sender::MessageSender;
use std::future::Future;
use super::{admin_check::is_admin, demote::demote_user, get_all_admins::get_all_admins};
use super::{admin_check::is_admin, rights_control::demote_user, data_getters::get_all_admins};
const DEMOTE_FAILURE_MESSAGE: &str = "Команда не может быть выполнена: \
не удалось удалить административные привилегии пользователя.";