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:
62
Cargo.lock
generated
62
Cargo.lock
generated
@@ -97,7 +97,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -365,9 +365,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ctrlc"
|
name = "ctrlc"
|
||||||
version = "3.4.0"
|
version = "3.4.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2a011bbe2c35ce9c1f143b7af6f94f29a167beb4cd1d29e6740ce836f723120e"
|
checksum = "82e95fbd621905b854affdc67943b043a0fbb6ed7385fd5a25650d19a8a6cfdf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"nix",
|
"nix",
|
||||||
"windows-sys",
|
"windows-sys",
|
||||||
@@ -647,7 +647,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1005,9 +1005,9 @@ checksum = "ed1202b2a6f884ae56f04cff409ab315c5ce26b5e58d7412e484f01fd52f52ef"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.6.2"
|
version = "2.6.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5486aed0026218e61b8a01d5fbd5a0a134649abb71a0e53b7bc088529dced86e"
|
checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memoffset"
|
name = "memoffset"
|
||||||
@@ -1105,11 +1105,11 @@ checksum = "c96aba5aa877601bb3f6dd6a63a969e1f82e60646e81e71b14496995e9853c91"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nix"
|
name = "nix"
|
||||||
version = "0.26.4"
|
version = "0.27.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
|
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 2.4.0",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
@@ -1145,9 +1145,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "object"
|
name = "object"
|
||||||
version = "0.32.0"
|
version = "0.32.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "77ac5bbd07aea88c60a577a1ce218075ffd59208b2d7ca97adf9bfc5aeb21ebe"
|
checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
@@ -1181,7 +1181,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1271,7 +1271,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1442,13 +1442,13 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex"
|
name = "regex"
|
||||||
version = "1.9.4"
|
version = "1.9.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29"
|
checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
"regex-automata 0.3.7",
|
"regex-automata 0.3.8",
|
||||||
"regex-syntax 0.7.5",
|
"regex-syntax 0.7.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1463,9 +1463,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-automata"
|
name = "regex-automata"
|
||||||
version = "0.3.7"
|
version = "0.3.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629"
|
checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
@@ -1768,7 +1768,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1933,7 +1933,7 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"rustversion",
|
"rustversion",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1949,9 +1949,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.29"
|
version = "2.0.31"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a"
|
checksum = "718fa2415bcb8d8bd775917a1bf12a7931b6dfa890753378538118181e0cb398"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -2079,22 +2079,22 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.47"
|
version = "1.0.48"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f"
|
checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl",
|
"thiserror-impl",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror-impl"
|
name = "thiserror-impl"
|
||||||
version = "1.0.47"
|
version = "1.0.48"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b"
|
checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2187,7 +2187,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2313,7 +2313,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2535,7 +2535,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -2569,7 +2569,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.29",
|
"syn 2.0.31",
|
||||||
"wasm-bindgen-backend",
|
"wasm-bindgen-backend",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|||||||
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;
|
use smartstring::alias::String as SmartString;
|
||||||
|
|
||||||
@@ -12,64 +15,13 @@ use crate::bots::{
|
|||||||
BotHandlerInternal,
|
BotHandlerInternal,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
use regex::Regex;
|
|
||||||
|
|
||||||
use teloxide::{
|
use teloxide::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
types::{InlineKeyboardButton, InlineKeyboardMarkup, Me},
|
types::{InlineKeyboardButton, InlineKeyboardMarkup, Me}, adaptors::{Throttle, CacheMe},
|
||||||
utils::command::BotCommands, adaptors::{Throttle, CacheMe},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(BotCommands, Clone)]
|
use self::{commands::SettingsCommand, callback_data::SettingsCallbackData};
|
||||||
#[command(rename_rule = "lowercase")]
|
|
||||||
enum SettingsCommand {
|
|
||||||
Settings,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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 {
|
async fn settings_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||||
let keyboard = InlineKeyboardMarkup {
|
let keyboard = InlineKeyboardMarkup {
|
||||||
@@ -64,9 +64,7 @@ The Open Network - TON:
|
|||||||
pub fn get_support_handler() -> crate::bots::BotHandler {
|
pub fn get_support_handler() -> crate::bots::BotHandler {
|
||||||
dptree::entry().branch(
|
dptree::entry().branch(
|
||||||
Update::filter_message().branch(
|
Update::filter_message().branch(
|
||||||
dptree::entry().filter_command::<SupportCommand>().endpoint(
|
dptree::entry().filter_command::<SupportCommand>().endpoint(support_command_handler),
|
||||||
|message, bot| async move { support_command_handler(message, bot).await },
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -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};
|
pub mod commands;
|
||||||
use dateparser::parse;
|
pub mod callback_data;
|
||||||
|
|
||||||
use std::{str::FromStr, vec};
|
use chrono::{prelude::*, Duration};
|
||||||
|
|
||||||
use crate::bots::{
|
use crate::bots::{
|
||||||
approved_bot::{services::book_library::get_uploaded_books, tools::filter_callback_query},
|
approved_bot::{services::book_library::get_uploaded_books, tools::filter_callback_query},
|
||||||
BotHandlerInternal,
|
BotHandlerInternal,
|
||||||
};
|
};
|
||||||
|
|
||||||
use regex::Regex;
|
|
||||||
use teloxide::{
|
use teloxide::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
types::{InlineKeyboardButton, InlineKeyboardMarkup},
|
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)]
|
use super::utils::generic_get_pagination_keyboard;
|
||||||
#[command(rename_rule = "snake_case")]
|
|
||||||
enum UpdateLogCommand {
|
|
||||||
UpdateLog,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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 {
|
async fn update_log_command(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||||
let now = Utc::now().date_naive();
|
let now = Utc::now().date_naive();
|
||||||
Reference in New Issue
Block a user