mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Refactor
This commit is contained in:
49
src/bots/approved_bot/modules/settings/callback_data.rs
Normal file
49
src/bots/approved_bot/modules/settings/callback_data.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use regex::Regex;
|
||||
use smartstring::alias::String as SmartString;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum SettingsCallbackData {
|
||||
Settings,
|
||||
On { code: SmartString },
|
||||
Off { code: SmartString },
|
||||
}
|
||||
|
||||
impl FromStr for SettingsCallbackData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s == SettingsCallbackData::Settings.to_string().as_str() {
|
||||
return Ok(SettingsCallbackData::Settings);
|
||||
}
|
||||
|
||||
let re = Regex::new(r"^lang_(?P<action>(off)|(on))_(?P<code>[a-zA-z]+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let action = &caps["action"];
|
||||
let code = caps["code"].to_string();
|
||||
|
||||
match action {
|
||||
"on" => Ok(SettingsCallbackData::On { code: code.into() }),
|
||||
"off" => Ok(SettingsCallbackData::Off { code: code.into() }),
|
||||
_ => Err(strum::ParseError::VariantNotFound),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for SettingsCallbackData {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
SettingsCallbackData::Settings => "lang_settings".to_string(),
|
||||
SettingsCallbackData::On { code } => format!("lang_on_{code}"),
|
||||
SettingsCallbackData::Off { code } => format!("lang_off_{code}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/bots/approved_bot/modules/settings/commands.rs
Normal file
8
src/bots/approved_bot/modules/settings/commands.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use teloxide::macros::BotCommands;
|
||||
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename_rule = "lowercase")]
|
||||
pub enum SettingsCommand {
|
||||
Settings,
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
use std::{collections::HashSet, str::FromStr};
|
||||
pub mod commands;
|
||||
pub mod callback_data;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use smartstring::alias::String as SmartString;
|
||||
|
||||
@@ -12,64 +15,13 @@ use crate::bots::{
|
||||
BotHandlerInternal,
|
||||
};
|
||||
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup, Me},
|
||||
utils::command::BotCommands, adaptors::{Throttle, CacheMe},
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup, Me}, adaptors::{Throttle, CacheMe},
|
||||
};
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename_rule = "lowercase")]
|
||||
enum SettingsCommand {
|
||||
Settings,
|
||||
}
|
||||
use self::{commands::SettingsCommand, callback_data::SettingsCallbackData};
|
||||
|
||||
#[derive(Clone)]
|
||||
enum SettingsCallbackData {
|
||||
Settings,
|
||||
On { code: SmartString },
|
||||
Off { code: SmartString },
|
||||
}
|
||||
|
||||
impl FromStr for SettingsCallbackData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s == SettingsCallbackData::Settings.to_string().as_str() {
|
||||
return Ok(SettingsCallbackData::Settings);
|
||||
}
|
||||
|
||||
let re = Regex::new(r"^lang_(?P<action>(off)|(on))_(?P<code>[a-zA-z]+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let action = &caps["action"];
|
||||
let code = caps["code"].to_string();
|
||||
|
||||
match action {
|
||||
"on" => Ok(SettingsCallbackData::On { code: code.into() }),
|
||||
"off" => Ok(SettingsCallbackData::Off { code: code.into() }),
|
||||
_ => Err(strum::ParseError::VariantNotFound),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for SettingsCallbackData {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
SettingsCallbackData::Settings => "lang_settings".to_string(),
|
||||
SettingsCallbackData::On { code } => format!("lang_on_{code}"),
|
||||
SettingsCallbackData::Off { code } => format!("lang_off_{code}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn settings_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
let keyboard = InlineKeyboardMarkup {
|
||||
@@ -64,9 +64,7 @@ The Open Network - TON:
|
||||
pub fn get_support_handler() -> crate::bots::BotHandler {
|
||||
dptree::entry().branch(
|
||||
Update::filter_message().branch(
|
||||
dptree::entry().filter_command::<SupportCommand>().endpoint(
|
||||
|message, bot| async move { support_command_handler(message, bot).await },
|
||||
),
|
||||
dptree::entry().filter_command::<SupportCommand>().endpoint(support_command_handler),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use dateparser::parse;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::bots::approved_bot::modules::utils::GetPaginationCallbackData;
|
||||
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct UpdateLogCallbackData {
|
||||
pub from: NaiveDate,
|
||||
pub to: NaiveDate,
|
||||
pub page: u32,
|
||||
}
|
||||
|
||||
impl FromStr for UpdateLogCallbackData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(
|
||||
r"^update_log_(?P<from>\d{4}-\d{2}-\d{2})_(?P<to>\d{4}-\d{2}-\d{2})_(?P<page>\d+)$",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let from: NaiveDate = parse(&caps["from"]).unwrap().date_naive();
|
||||
let to: NaiveDate = parse(&caps["to"]).unwrap().date_naive();
|
||||
let page: u32 = caps["page"].parse().unwrap();
|
||||
|
||||
Ok(UpdateLogCallbackData { from, to, page })
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for UpdateLogCallbackData {
|
||||
fn to_string(&self) -> String {
|
||||
let date_format = "%Y-%m-%d";
|
||||
|
||||
let from = self.from.format(date_format);
|
||||
let to = self.to.format(date_format);
|
||||
let page = self.page;
|
||||
|
||||
format!("update_log_{from}_{to}_{page}")
|
||||
}
|
||||
}
|
||||
|
||||
impl GetPaginationCallbackData for UpdateLogCallbackData {
|
||||
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
||||
let UpdateLogCallbackData { from, to, .. } = self;
|
||||
UpdateLogCallbackData {
|
||||
from: *from,
|
||||
to: *to,
|
||||
page: target_page,
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
8
src/bots/approved_bot/modules/update_history/commands.rs
Normal file
8
src/bots/approved_bot/modules/update_history/commands.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use teloxide::macros::BotCommands;
|
||||
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename_rule = "snake_case")]
|
||||
pub enum UpdateLogCommand {
|
||||
UpdateLog,
|
||||
}
|
||||
@@ -1,81 +1,23 @@
|
||||
use chrono::{prelude::*, Duration};
|
||||
use dateparser::parse;
|
||||
pub mod commands;
|
||||
pub mod callback_data;
|
||||
|
||||
use std::{str::FromStr, vec};
|
||||
use chrono::{prelude::*, Duration};
|
||||
|
||||
use crate::bots::{
|
||||
approved_bot::{services::book_library::get_uploaded_books, tools::filter_callback_query},
|
||||
BotHandlerInternal,
|
||||
};
|
||||
|
||||
use regex::Regex;
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup},
|
||||
utils::command::BotCommands, adaptors::{Throttle, CacheMe},
|
||||
adaptors::{Throttle, CacheMe},
|
||||
};
|
||||
|
||||
use super::utils::{generic_get_pagination_keyboard, GetPaginationCallbackData};
|
||||
use self::{commands::UpdateLogCommand, callback_data::UpdateLogCallbackData};
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename_rule = "snake_case")]
|
||||
enum UpdateLogCommand {
|
||||
UpdateLog,
|
||||
}
|
||||
use super::utils::generic_get_pagination_keyboard;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct UpdateLogCallbackData {
|
||||
from: NaiveDate,
|
||||
to: NaiveDate,
|
||||
page: u32,
|
||||
}
|
||||
|
||||
impl FromStr for UpdateLogCallbackData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(
|
||||
r"^update_log_(?P<from>\d{4}-\d{2}-\d{2})_(?P<to>\d{4}-\d{2}-\d{2})_(?P<page>\d+)$",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let from: NaiveDate = parse(&caps["from"]).unwrap().date_naive();
|
||||
let to: NaiveDate = parse(&caps["to"]).unwrap().date_naive();
|
||||
let page: u32 = caps["page"].parse().unwrap();
|
||||
|
||||
Ok(UpdateLogCallbackData { from, to, page })
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for UpdateLogCallbackData {
|
||||
fn to_string(&self) -> String {
|
||||
let date_format = "%Y-%m-%d";
|
||||
|
||||
let from = self.from.format(date_format);
|
||||
let to = self.to.format(date_format);
|
||||
let page = self.page;
|
||||
|
||||
format!("update_log_{from}_{to}_{page}")
|
||||
}
|
||||
}
|
||||
|
||||
impl GetPaginationCallbackData for UpdateLogCallbackData {
|
||||
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
||||
let UpdateLogCallbackData { from, to, .. } = self;
|
||||
UpdateLogCallbackData {
|
||||
from: *from,
|
||||
to: *to,
|
||||
page: target_page,
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
async fn update_log_command(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
let now = Utc::now().date_naive();
|
||||
Reference in New Issue
Block a user