code cleanup & multi-channel support

This commit is contained in:
2025-10-20 03:45:53 +01:00
parent 07857f1d0a
commit 91ff2e00c4
10 changed files with 304 additions and 82 deletions
+4 -6
View File
@@ -51,8 +51,8 @@ pub async fn initialise(
) -> Result<(), Box<dyn std::error::Error>> {
let key = format!("messages:{}", channel_id);
let length: usize = cache.llen(&key).await?;
if length < 100 {
// less than 100 messages in cache?
if cache.llen::<_, i32>(&key).await? < 100 {
// Fetch from Postgres
let messages = sqlx::query!(
"SELECT u.username, u.display_name, u.id, m.content, m.created_at
@@ -68,14 +68,12 @@ pub async fn initialise(
// Populate cache (in reverse order so oldest is at the end)
for msg in messages.into_iter().rev() {
let chat_msg = ChatMsg {
let msg_json = serde_json::to_string(&ChatMsg {
display_name: Some(msg.display_name.unwrap_or(msg.username)),
user_id: msg.id as usize,
text: msg.content,
timestamp: (msg.created_at.unwrap().unix_timestamp_nanos() / 1_000_000) as usize,
};
let msg_json = serde_json::to_string(&chat_msg)?;
})?;
cache.lpush::<_, _, ()>(&key, &msg_json).await?;
}