Added event cleanup
parent
8fb01ba1a4
commit
7c441028a7
File diff suppressed because it is too large
Load Diff
|
@ -11,15 +11,16 @@ clap = "2.33.0"
|
|||
serde = "1.0.106"
|
||||
serde_derive = "1.0.104"
|
||||
config = "0.9"
|
||||
chrono = "0.4.11"
|
||||
chrono = { version = "0.4.19", default-features = false, features = ["clock", "std"] }
|
||||
chrono-tz = "0.4"
|
||||
diesel = { version = "1.4.0", features = ["mysql", "chrono"] }
|
||||
diesel = { version = "1.4.4", features = ["mysql", "chrono"] }
|
||||
diesel_migrations = "1.4.0"
|
||||
log = "0.4.8"
|
||||
log4rs = "0.11.0"
|
||||
strfmt = "0.1.6"
|
||||
url = "2.1.1"
|
||||
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
|
||||
percent-encoding = "2.1.0"
|
||||
|
||||
[dependencies.serenity]
|
||||
version = "0.10.2"
|
||||
|
|
|
@ -5,6 +5,7 @@ use diesel::prelude::*;
|
|||
use diesel::result::Error;
|
||||
use models::{Event, NewEvent};
|
||||
use std::vec::Vec;
|
||||
use chrono::{Utc, DateTime};
|
||||
|
||||
/// Establish a connection to the database
|
||||
pub fn establish_connection(database_url: String) -> MysqlConnection {
|
||||
|
@ -58,11 +59,11 @@ pub fn get_event_by_msg_id(database_url: String, msg_id: String) -> Result<Event
|
|||
|
||||
/// Get all events
|
||||
pub fn get_all_events(database_url: String) -> Result<Vec<Event>, Error> {
|
||||
use schema::events::dsl::{event_time, events};
|
||||
use schema::events::dsl::{events};
|
||||
|
||||
let connection = establish_connection(database_url);
|
||||
|
||||
events.order(event_time).load(&connection)
|
||||
events.load(&connection)
|
||||
}
|
||||
|
||||
/// Get event with id
|
||||
|
@ -73,3 +74,11 @@ pub fn get_event_by_id(database_url: String, event_id: i32) -> Result<Event, Err
|
|||
|
||||
events.filter(id.eq(event_id)).first::<Event>(&connection)
|
||||
}
|
||||
|
||||
pub fn get_event_older_than(database_url: String, time: DateTime<Utc>) -> Result<Vec<Event>, Error> {
|
||||
use schema::events::dsl::{events, event_time};
|
||||
|
||||
let connection = establish_connection(database_url);
|
||||
|
||||
events.filter(event_time.lt(time.naive_utc())).load(&connection)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use chrono::{DateTime, NaiveDateTime, Utc, Duration};
|
||||
use serenity::async_trait;
|
||||
use serenity::framework::standard::macros::help;
|
||||
use serenity::framework::standard::macros::hook;
|
||||
|
@ -17,9 +17,10 @@ use serenity::utils::Colour;
|
|||
use serenity::Result;
|
||||
use strfmt::strfmt;
|
||||
use tokio::sync::RwLockReadGuard;
|
||||
use percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
|
||||
|
||||
use crate::database::models::{Event, NewEvent};
|
||||
use crate::database::{get_all_events, get_event_by_id, get_event_by_msg_id, remove_event};
|
||||
use crate::database::{get_all_events, get_event_by_id, get_event_by_msg_id, remove_event, get_event_older_than};
|
||||
use crate::hypebot_config::HypeBotConfig;
|
||||
use crate::reminder::Reminders;
|
||||
use crate::{INTERESTED_EMOJI, UNINTERESTED_EMOJI};
|
||||
|
@ -83,9 +84,11 @@ pub async fn send_dm_message(ctx: &Context, user: User, message: &str) {
|
|||
}
|
||||
}
|
||||
|
||||
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
|
||||
|
||||
/// Create a countdown link for the event
|
||||
pub fn get_countdown_link(event_name: &str, utc: &DateTime<Utc>) -> String {
|
||||
let msg = event_name.replace(" ", "+");
|
||||
let msg = utf8_percent_encode(event_name, FRAGMENT);
|
||||
let time = utc.format("%G%m%dT%H%M").to_string();
|
||||
|
||||
format!(
|
||||
|
@ -312,6 +315,22 @@ pub async fn send_reminders_task(ctx: &Context) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Delete old events
|
||||
pub async fn cleanup_task(ctx: &Context) {
|
||||
let duration = tokio::time::Duration::from_secs(60*60);
|
||||
loop {
|
||||
tokio::time::sleep(duration).await;
|
||||
let config = get_config(&ctx.data.read().await)
|
||||
.await
|
||||
.expect("Unable to get config");
|
||||
let one_week_ago = Utc::now() - Duration::from_std(std::time::Duration::from_secs(60*60*24*7)).unwrap();
|
||||
|
||||
for event in get_event_older_than(config.db_url.clone(), one_week_ago).unwrap() {
|
||||
delete_event(ctx, &event).await.ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete event
|
||||
pub async fn delete_event(ctx: &Context, event: &Event) -> CommandResult {
|
||||
let config = get_config(&ctx.data.read().await)
|
||||
|
@ -412,13 +431,17 @@ impl EventHandler for Handler {
|
|||
.await
|
||||
.expect("Unable to get config");
|
||||
for event in get_all_events(config.db_url.clone()).unwrap() {
|
||||
if event.reminder_sent == 0 {
|
||||
schedule_event(&ctx, &event).await;
|
||||
}
|
||||
schedule_event(&ctx, &event).await;
|
||||
}
|
||||
|
||||
let cleanup_ctx = ctx.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
send_reminders_task(&ctx).await;
|
||||
});
|
||||
|
||||
tokio::spawn(async move {
|
||||
cleanup_task(&cleanup_ctx).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue