code cleanup & multi-channel support
This commit is contained in:
@@ -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?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user