ignore too old message(1 day before)

This commit is contained in:
ihciah 2022-06-18 21:55:51 +08:00
parent e7a5ad13af
commit 283cd046a8
No known key found for this signature in database
GPG Key ID: 97CE6E121061F3BA
3 changed files with 47 additions and 3 deletions

27
Cargo.lock generated
View File

@ -94,6 +94,7 @@ name = "bot"
version = "0.1.4"
dependencies = [
"anyhow",
"chrono",
"clap",
"dptree",
"eh2telegraph",
@ -103,7 +104,7 @@ dependencies = [
"serde",
"singleflight-async",
"teloxide",
"time",
"time 0.3.9",
"tokio",
"tracing",
"tracing-subscriber",
@ -140,8 +141,11 @@ version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time 0.1.44",
"winapi",
]
[[package]]
@ -1645,6 +1649,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]]
name = "time"
version = "0.3.9"
@ -1830,7 +1845,7 @@ dependencies = [
"sharded-slab",
"smallvec",
"thread_local",
"time",
"time 0.3.9",
"tracing-core",
"tracing-log",
]
@ -1924,7 +1939,7 @@ dependencies = [
"rustc_version",
"rustversion",
"thiserror",
"time",
"time 0.3.9",
]
[[package]]
@ -1949,6 +1964,12 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View File

@ -8,6 +8,7 @@ eh2telegraph = {path = "../eh2telegraph"}
anyhow = "1"
clap = {version = "3", features = ["derive"]}
chrono = "0.4"
dptree = "0.2"
once_cell = "1"
regex = "1"

View File

@ -11,6 +11,7 @@ use eh2telegraph::{
use clap::Parser;
use once_cell::sync::OnceCell;
use teloxide::{
adaptors::DefaultParseMode,
dispatching::update_listeners,
@ -52,6 +53,8 @@ struct Args {
config: Option<String>,
}
static PROCESS_MESSAGE_DATE: OnceCell<chrono::DateTime<chrono::Utc>> = OnceCell::new();
#[tokio::main]
async fn main() {
let args = Args::parse();
@ -59,6 +62,14 @@ async fn main() {
let timer = tracing_subscriber::fmt::time::LocalTime::new(time::macros::format_description!(
"[month]-[day] [hour]:[minute]:[second]"
));
// We will only process messages from 1 day earlier.
PROCESS_MESSAGE_DATE
.set(
chrono::Utc::now()
.checked_sub_signed(chrono::Duration::days(1))
.expect("illegal current date"),
)
.expect("unable to set global date");
tracing_subscriber::fmt().with_timer(timer).init();
tracing::info!("initializing...");
@ -126,6 +137,16 @@ async fn main() {
Some(message)
}
};
let time_filter = |message: Message| async move {
// Ignore old message.
// # Safety:
// We already set PROCESS_MESSAGE_DATE.
if &message.date > unsafe { PROCESS_MESSAGE_DATE.get_unchecked() } {
Some(message)
} else {
None
}
};
let bot = Bot::new(base_config.bot_token)
.parse_mode(ParseMode::MarkdownV2)
@ -139,6 +160,7 @@ async fn main() {
_ => None,
}
}))
.chain(dptree::filter_map_async(time_filter))
.chain(dptree::filter_map_async(permission_filter))
.branch(
dptree::entry()