|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use redis::AsyncCommands; |
| 5 | +use redis::aio::ConnectionManager; |
| 6 | +use serenity::all::MessageId; |
| 7 | +use uuid::Uuid; |
| 8 | + |
| 9 | +pub type Error = redis::RedisError; |
| 10 | + |
| 11 | +const KEY_PREFIX: &str = "playmatch:suggestion:"; |
| 12 | + |
| 13 | +fn key_for(uuid: Uuid) -> String { |
| 14 | + format!("{KEY_PREFIX}{uuid}") |
| 15 | +} |
| 16 | + |
| 17 | +/// Persistent map of suggestion UUID → Discord message id. Backed by Redis so the bot |
| 18 | +/// can restart without re-posting cards or losing track of already-posted ones. |
| 19 | +pub struct SuggestionStore { |
| 20 | + conn: ConnectionManager, |
| 21 | +} |
| 22 | + |
| 23 | +impl SuggestionStore { |
| 24 | + pub async fn connect(url: &str) -> Result<Self, Error> { |
| 25 | + let client = redis::Client::open(url)?; |
| 26 | + let conn = ConnectionManager::new(client).await?; |
| 27 | + Ok(Self { conn }) |
| 28 | + } |
| 29 | + |
| 30 | + pub async fn mark_posted(&self, uuid: Uuid, message_id: MessageId) -> Result<(), Error> { |
| 31 | + let mut c = self.conn.clone(); |
| 32 | + c.set::<_, _, ()>(key_for(uuid), message_id.get()).await |
| 33 | + } |
| 34 | + |
| 35 | + pub async fn get(&self, uuid: Uuid) -> Result<Option<MessageId>, Error> { |
| 36 | + let mut c = self.conn.clone(); |
| 37 | + let raw: Option<u64> = c.get(key_for(uuid)).await?; |
| 38 | + Ok(raw.map(MessageId::new)) |
| 39 | + } |
| 40 | + |
| 41 | + pub async fn remove(&self, uuid: Uuid) -> Result<(), Error> { |
| 42 | + let mut c = self.conn.clone(); |
| 43 | + c.del::<_, ()>(key_for(uuid)).await |
| 44 | + } |
| 45 | + |
| 46 | + pub async fn list_all(&self) -> Result<HashMap<Uuid, MessageId>, Error> { |
| 47 | + let mut conn = self.conn.clone(); |
| 48 | + let pattern = format!("{KEY_PREFIX}*"); |
| 49 | + let mut cursor = 0u64; |
| 50 | + let mut keys: Vec<String> = Vec::new(); |
| 51 | + loop { |
| 52 | + let (next_cursor, batch): (u64, Vec<String>) = redis::cmd("SCAN") |
| 53 | + .arg(cursor) |
| 54 | + .arg("MATCH") |
| 55 | + .arg(&pattern) |
| 56 | + .arg("COUNT") |
| 57 | + .arg(100) |
| 58 | + .query_async(&mut conn) |
| 59 | + .await?; |
| 60 | + keys.extend(batch); |
| 61 | + if next_cursor == 0 { |
| 62 | + break; |
| 63 | + } |
| 64 | + cursor = next_cursor; |
| 65 | + } |
| 66 | + |
| 67 | + if keys.is_empty() { |
| 68 | + return Ok(HashMap::new()); |
| 69 | + } |
| 70 | + |
| 71 | + let values: Vec<Option<u64>> = conn.mget(&keys).await?; |
| 72 | + let mut result = HashMap::with_capacity(keys.len()); |
| 73 | + for (key, value) in keys.into_iter().zip(values) { |
| 74 | + let Some(uuid_str) = key.strip_prefix(KEY_PREFIX) else { |
| 75 | + continue; |
| 76 | + }; |
| 77 | + let Ok(uuid) = Uuid::from_str(uuid_str) else { |
| 78 | + continue; |
| 79 | + }; |
| 80 | + let Some(message_id) = value else { |
| 81 | + continue; |
| 82 | + }; |
| 83 | + result.insert(uuid, MessageId::new(message_id)); |
| 84 | + } |
| 85 | + Ok(result) |
| 86 | + } |
| 87 | + |
| 88 | + pub async fn is_empty(&self) -> Result<bool, Error> { |
| 89 | + Ok(self.list_all().await?.is_empty()) |
| 90 | + } |
| 91 | +} |
0 commit comments