Compare commits

...

4 Commits

Author SHA1 Message Date
Akulij
85f16311fa set mongodb's index to clear callback info after a day 2025-04-30 14:37:45 +03:00
Akulij
f3ec4188bb add created_at field to CallbackInfo 2025-04-30 14:37:12 +03:00
Akulij
2d5e2ed34e fix: wrongly spelled serde's rename_all attribute 2025-04-30 14:36:22 +03:00
Akulij
53edb54118 make more_info and show_projects on separate lines 2025-04-30 13:56:29 +03:00
3 changed files with 34 additions and 8 deletions

View File

@ -1,6 +1,9 @@
use crate::query_call_consume;
use crate::CallDB;
use bson::oid::ObjectId;
use chrono::DateTime;
use chrono::FixedOffset;
use chrono::Local;
use serde::{Deserialize, Serialize};
use super::DbResult;
@ -12,6 +15,7 @@ where
C: Serialize,
{
pub _id: bson::oid::ObjectId,
pub created_at: DateTime<FixedOffset>,
#[serde(flatten)]
pub callback: C,
}
@ -23,6 +27,7 @@ where
pub fn new(callback: C) -> Self {
Self {
_id: Default::default(),
created_at: Local::now().into(),
callback,
}
}

View File

@ -1,5 +1,7 @@
pub mod callback_info;
use std::time::Duration;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use enum_stringify::EnumStringify;
@ -129,6 +131,25 @@ impl DB {
.build(),
)
.await?;
// clear callbacks after a day because otherwise database will contain so much data
// for just button clicks
let callback_info = self
.get_database()
.await
.collection::<Event>("callback_info");
callback_info
.create_index(
IndexModel::builder()
.keys(doc! {"created_at": 1})
.options(
IndexOptions::builder()
.expire_after(Duration::from_secs(60 * 60 * 24 /* 1 day */))
.build(),
)
.build(),
)
.await?;
Ok(())
}

View File

@ -75,7 +75,7 @@ pub enum State {
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum Callback {
MoreInfo,
ProjectPage { id: u32 },
@ -611,14 +611,14 @@ async fn make_start_buttons(db: &mut DB) -> BotResult<InlineKeyboardMarkup> {
)]
})
.collect();
buttons.push(vec![InlineKeyboardButton::callback(
"More info",
CallbackStore::new(Callback::MoreInfo)
.store(db)
.await?
.get_id(),
)]);
buttons.push(vec![
InlineKeyboardButton::callback(
"More info",
CallbackStore::new(Callback::MoreInfo)
.store(db)
.await?
.get_id(),
),
create_callback_button(
"show_projects",
CallbackStore::new(Callback::ProjectPage { id: 1 }),