diff --git a/badges/test.svg b/badges/test.svg
index a7859aa..3a9c483 100644
--- a/badges/test.svg
+++ b/badges/test.svg
@@ -1 +1 @@
-
+
diff --git a/src/votes.rs b/src/votes.rs
index 37c96d6..a2e9fbd 100644
--- a/src/votes.rs
+++ b/src/votes.rs
@@ -17,7 +17,7 @@ use std::{
sync::{LazyLock, Mutex},
};
-use chrono::{NaiveDateTime, TimeDelta};
+use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@@ -209,6 +209,12 @@ pub(crate) enum EventStatus {
/// The event was cancelled
Cancelled,
}
+// impl EventStatus {
+// /// Check if the event isn't closed, remove this abreration when you add another CLOSED_EVENT struct
+// fn not_closed(&self) -> bool {
+// matches!(self, EventStatus::Closed)
+// }
+// }
/// An event, wether ongoing or planned
#[derive(Debug, Serialize, Deserialize, Default)]
@@ -224,6 +230,8 @@ pub struct ParsedVote {
launch_date: NaiveDateTime,
/// Date at which to autoclose the vote process
close_date: NaiveDateTime,
+ /// Date at which to autodelete the vote process
+ auto_delete_date: NaiveDateTime,
/// The inner list of votes cast
votes: Vec,
/// The list of values to vote for, this list all the valid dates to vote for
@@ -247,14 +255,18 @@ impl ParsedVote {
// }
/// All the logic behind a vote becoming live
- fn launch(&mut self, id: Uuid) -> Result<(), IdleError> {
- let _ = id;
+ fn launch(&mut self) -> Result<(), IdleError> {
// TODO: Define this
self.status = EventStatus::Started;
// Maybe send a message ? the bot will need the Uuid of the event to be able to add reaction
// TODO: This is a vkypoint, as this method will need an IO interaction
Ok(())
}
+
+ /// Close the event
+ fn close(&mut self) {
+ self.status = EventStatus::Closed;
+ }
}
// impl From for ParsedVote {
@@ -307,8 +319,6 @@ impl PendingEvents {
///
/// Returns an `IdleError` if there's an issue launching any event or adding it to the ongoing list.
pub fn process_upcoming(&mut self, current_time: NaiveDateTime) -> Result<(), IdleError> {
- self.pending_events
- .insert(Uuid::new_v4(), ParsedVote::default());
self.find_upcoming(current_time)
.iter()
.try_for_each(|uuid| {
@@ -330,13 +340,13 @@ impl PendingEvents {
Ok(())
}
- /// Find all events that are scheduled to start before the given current time.
+ /// Find all events that are scheduled to start after the given date.
///
/// Returns a vector of UUIDs corresponding to the upcoming events.
fn find_upcoming(&self, current_time: NaiveDateTime) -> Vec {
self.pending_events
.iter()
- .filter(|&(_, e)| e.launch_date > current_time)
+ .filter(|&(_, e)| e.launch_date <= current_time)
.map(|(k, _v)| (*k))
.collect()
}
@@ -346,6 +356,8 @@ impl PendingEvents {
.remove(&uuid)
.ok_or(IdleError::DeleteEvent(uuid))
}
+
+ // TODO: maybe add an update_upcoming(date) ?
}
/// The list of ongoing events, not yet closed
@@ -382,21 +394,45 @@ impl OngoingEvents {
true => Ok(()),
false => Err(IdleError::EventPrune(failure)),
}
- // self.find_old(current_time).iter().try_for_each(|uuid| {
- // self.ongoing_events
- // .remove(uuid)
- // .ok_or(IdleError::EventLaunch(*uuid))?;
- // Ok(())
- // })
+ }
+ /// Remove all outdated votes for the current user
+ ///
+ /// This function is expected to be called periodically by the watchdog.
+ pub(crate) fn autoclose(&mut self, current_time: NaiveDateTime) -> Result<(), IdleError> {
+ let failure = Vec::new();
+ for uuid in self.find_autoclose(current_time) {
+ self.ongoing_events
+ .get_mut(&uuid)
+ .ok_or(IdleError::EventLaunch(uuid))?
+ .close()
+ }
+ match failure.is_empty() {
+ true => Ok(()),
+ false => Err(IdleError::EventPrune(failure)),
+ }
}
- /// Find all events that are scheduled to start more than 3 days ago from the given current time.
+ /// Find all events that are scheduled to be autoclosed.
+ ///
+ /// Returns a vector of UUIDs corresponding to the old events.
+ fn find_autoclose(&mut self, current_time: NaiveDateTime) -> Vec {
+ self.ongoing_events
+ .iter()
+ // TODO: Remove this when you do the sensible thing and add a CLOSED_EVENT structure
+ .filter(|(_, e)| {
+ e.close_date <= current_time && !matches!(e.status, EventStatus::Closed)
+ })
+ .map(|(k, _)| *k)
+ .collect()
+ }
+
+ /// Find all events that are too old and supposed to be pruned.
///
/// Returns a vector of UUIDs corresponding to the old events.
fn find_old(&mut self, current_time: NaiveDateTime) -> Vec {
self.ongoing_events
.iter()
- .filter(|(_, e)| e.close_date.signed_duration_since(current_time) > TimeDelta::days(3))
+ .filter(|(_, e)| e.auto_delete_date <= current_time)
.map(|(k, _)| *k)
.collect()
}
@@ -405,7 +441,7 @@ impl OngoingEvents {
///
/// This function also launch the event based and adds a callback with the Uuid of the original caller.
pub(crate) fn add_event(&mut self, id: Uuid, mut event: ParsedVote) -> Result<(), IdleError> {
- event.launch(id)?;
+ event.launch()?;
self.ongoing_events.insert(id, event);
Ok(())
}
@@ -427,6 +463,7 @@ impl OngoingEvents {
// TODO: Do something with this string
Ok(event)
}
+ // TODO: maybe add an update_ongoing(date) ?
}
/// The thread safe list of pending events
@@ -438,9 +475,14 @@ pub static ONGOING_EVENTS: LazyLock> =
#[cfg(test)]
mod tests {
- use chrono::Local;
+ use chrono::{Days, Local};
use super::*;
+ macro_rules! gimme {
+ ($ev: ident) => {
+ $ev.lock().unwrap()
+ };
+ }
// Test all structs can be safely initialized
#[test]
@@ -450,14 +492,10 @@ mod tests {
assert!(PENDING_EVENTS.lock().is_ok());
assert!(ONGOING_EVENTS.lock().is_ok());
- assert!(PENDING_EVENTS
- .lock()
- .unwrap()
+ assert!(gimme!(PENDING_EVENTS)
.process_upcoming(Local::now().naive_local())
.is_ok());
- assert!(ONGOING_EVENTS
- .lock()
- .unwrap()
+ assert!(gimme!(ONGOING_EVENTS)
.prune_old(Local::now().naive_local())
.is_ok());
}
@@ -469,59 +507,94 @@ mod tests {
// Can we add a vote ?
let id = add_pending_event(ev.clone()).unwrap();
- assert!(PENDING_EVENTS
- .lock()
- .unwrap()
- .pending_events
- .contains_key(&id)); // Is the vote present ?
+ assert!(gimme!(PENDING_EVENTS).pending_events.contains_key(&id)); // Is the vote present ?
del_pending_event(id).unwrap();
- assert!(!PENDING_EVENTS
- .lock()
- .unwrap()
- .pending_events
- .contains_key(&id)); // Is the vote now deleted
+ assert!(!gimme!(PENDING_EVENTS).pending_events.contains_key(&id)); // Is the vote now deleted
let id = add_pending_event(ev.clone()).unwrap();
make_into_ongoing_event(id).unwrap(); // Launching this event
- assert!(!PENDING_EVENTS
- .lock()
- .unwrap()
- .pending_events
- .contains_key(&id)); // Is the vote now not pending
- assert!(ONGOING_EVENTS
- .lock()
- .unwrap()
- .ongoing_events
- .contains_key(&id)); // Is the vote now ongoing
+ assert!(!gimme!(PENDING_EVENTS).pending_events.contains_key(&id)); // Is the vote now not pending
+ assert!(gimme!(ONGOING_EVENTS).ongoing_events.contains_key(&id)); // Is the vote now ongoing
close_ongoing_event(id).unwrap();
- assert!(!ONGOING_EVENTS
- .lock()
- .unwrap()
- .ongoing_events
- .contains_key(&id)); // Is the vote now not ongoing
+ assert!(!gimme!(ONGOING_EVENTS).ongoing_events.contains_key(&id)); // Is the vote now not ongoing
let id = add_pending_event(ev.clone()).unwrap();
make_into_ongoing_event(id).unwrap(); // Launching this event
del_ongoing_event(id).unwrap();
- assert!(!ONGOING_EVENTS
- .lock()
- .unwrap()
- .ongoing_events
- .contains_key(&id)); // Is the vote now not ongoing
+ assert!(!gimme!(ONGOING_EVENTS).ongoing_events.contains_key(&id)); // Is the vote now not ongoing
}
#[test]
- #[ignore]
fn test_upcomming_events() {
- let _e = ParsedVote::default();
- // e.launch_date = NaiveDateTime
- // ; PENDING_EVENTS.lock().unwrap().add_pending_event(e)
+ let now = Local::now().naive_local();
+ let later = now.checked_add_days(Days::new(1)).unwrap();
+ let very_later = now.checked_add_days(Days::new(3)).unwrap();
+ let very_very_later = now.checked_add_days(Days::new(7)).unwrap();
+ let event = ParsedVote {
+ launch_date: later,
+ close_date: very_later,
+ auto_delete_date: very_very_later,
+ ..Default::default()
+ };
+ assert!(now < later);
+ assert!(later < very_later);
+
+ let id = add_pending_event(event).unwrap();
+ assert!(gimme!(PENDING_EVENTS).find_upcoming(now).is_empty()); // no upcomming events
+ assert!(gimme!(ONGOING_EVENTS).find_old(now).is_empty()); // no old events
+ assert!(gimme!(ONGOING_EVENTS).find_autoclose(now).is_empty()); // no old events
+ gimme!(PENDING_EVENTS).process_upcoming(now).unwrap(); // nothing should be processed
+ gimme!(ONGOING_EVENTS).prune_old(now).unwrap(); // nothing should be processed
+ gimme!(ONGOING_EVENTS).autoclose(now).unwrap(); // nothing should be processed
+ assert!(gimme!(PENDING_EVENTS).find_upcoming(now).is_empty()); // no upcomming events
+ assert!(gimme!(ONGOING_EVENTS).find_old(now).is_empty()); // no old events
+ assert!(gimme!(ONGOING_EVENTS).find_autoclose(now).is_empty()); // no old events
+ assert!(gimme!(PENDING_EVENTS).pending_events.contains_key(&id)); // it's still here
+ assert!(!gimme!(ONGOING_EVENTS).ongoing_events.contains_key(&id)); // it's wasn't processed
+
+ // Now some time passed
+ assert_eq!(gimme!(PENDING_EVENTS).find_upcoming(later), vec![id]); // one upcomming event
+ assert!(gimme!(ONGOING_EVENTS).find_old(later).is_empty()); // no old events
+ assert!(gimme!(ONGOING_EVENTS).find_autoclose(later).is_empty()); // no old events
+ gimme!(PENDING_EVENTS).process_upcoming(later).unwrap(); // one event should be processed
+ gimme!(ONGOING_EVENTS).prune_old(later).unwrap(); // nothing should be processed
+ gimme!(ONGOING_EVENTS).autoclose(later).unwrap(); // nothing should be processed
+ assert!(!gimme!(PENDING_EVENTS).pending_events.contains_key(&id)); // it's not here anymore
+ assert!(gimme!(ONGOING_EVENTS).ongoing_events.contains_key(&id)); // it was processed
+
+ // More time passed
+ assert!(gimme!(PENDING_EVENTS).find_upcoming(very_later).is_empty()); // one upcomming event
+ assert_eq!(gimme!(ONGOING_EVENTS).find_autoclose(very_later), vec![id]); // one old event
+ assert!(gimme!(ONGOING_EVENTS).find_old(very_later).is_empty()); // no old events
+ gimme!(PENDING_EVENTS).process_upcoming(very_later).unwrap(); // no event should be processed
+ gimme!(ONGOING_EVENTS).prune_old(very_later).unwrap(); // nothing should be processed
+ gimme!(ONGOING_EVENTS).autoclose(very_later).unwrap(); // nothing should be processed
+ assert!(!gimme!(PENDING_EVENTS).pending_events.contains_key(&id)); // it's not here anymore
+ assert!(gimme!(ONGOING_EVENTS).ongoing_events.contains_key(&id)); // it was processed
+
+ // More more time passed
+ assert!(gimme!(PENDING_EVENTS)
+ .find_upcoming(very_very_later)
+ .is_empty()); // one upcomming event
+ assert_eq!(gimme!(ONGOING_EVENTS).find_old(very_very_later), vec![id]); // one old event
+ assert!(gimme!(ONGOING_EVENTS)
+ .find_autoclose(very_very_later)
+ .is_empty()); // no old events
+ gimme!(PENDING_EVENTS)
+ .process_upcoming(very_very_later)
+ .unwrap(); // no event should be processed
+ gimme!(ONGOING_EVENTS).prune_old(very_very_later).unwrap(); // nothing should be processed
+ gimme!(ONGOING_EVENTS).autoclose(very_very_later).unwrap(); // nothing should be processed
+ assert!(!gimme!(PENDING_EVENTS).pending_events.contains_key(&id)); // it's not here anymore
+ assert!(!gimme!(ONGOING_EVENTS).ongoing_events.contains_key(&id)); // it was processed
+
+ // TODO: Check the status at several points
}
- #[test]
- #[ignore]
- fn test_ongoing_events() {
- unimplemented!()
- }
+ // #[test]
+ // #[ignore]
+ // fn test_ongoing_events() {
+ // unimplemented!()
+ // }
}