use std::time::Instant;
use super::DiscordState;
use super::interactions::FormSpec;
fn expired(created: &Instant, ttl_hours: f64) -> bool {
created.elapsed().as_secs_f64() > ttl_hours * 3600.0
}
impl DiscordState {
pub(crate) async fn register_select(&self, id: String, options: Vec<String>) {
self.pending_selects
.lock()
.await
.insert(id, (Instant::now(), options));
}
pub(crate) async fn take_select(&self, id: &str, ttl_hours: f64) -> Option<Vec<String>> {
let mut map = self.pending_selects.lock().await;
let (created, _) = map.get(id)?;
if expired(created, ttl_hours) {
map.remove(id);
return None;
}
map.remove(id).map(|(_, opts)| opts)
}
pub(crate) async fn register_form(&self, id: String, spec: FormSpec) {
self.pending_forms
.lock()
.await
.insert(id, (Instant::now(), spec));
}
pub(crate) async fn get_form(&self, id: &str, ttl_hours: f64) -> Option<FormSpec> {
let mut map = self.pending_forms.lock().await;
let (created, _) = map.get(id)?;
if expired(created, ttl_hours) {
map.remove(id);
return None;
}
map.get(id).map(|(_, spec)| spec.clone())
}
pub(crate) async fn take_form(&self, id: &str) -> Option<FormSpec> {
self.pending_forms.lock().await.remove(id).map(|(_, s)| s)
}
}