diff --git a/app/public/messages.js b/app/public/messages.js index b8fa549..a4a5b2a 100644 --- a/app/public/messages.js +++ b/app/public/messages.js @@ -116,7 +116,7 @@ function sock_con() { setTimeout(() => { clear_notifications(); - }, 150); + }, 1000); } else if (msg.method === "MessageDelete") { if (document.getElementById(`message_${msg.body}`)) { document.getElementById(`message_${msg.body}`).remove(); diff --git a/src/database/mod.rs b/src/database/mod.rs index 3f9ad34..b991201 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,5 +1,6 @@ mod chats; mod messages; +mod notifications; mod sql; mod users; diff --git a/src/database/notifications.rs b/src/database/notifications.rs new file mode 100644 index 0000000..838fcb0 --- /dev/null +++ b/src/database/notifications.rs @@ -0,0 +1,26 @@ +use super::DataManager; +use oiseau::{params, query_row}; +use tetratto_core::model::{Error, Result}; + +impl DataManager { + /// Get the number of notifications a user has for the given tag. + pub async fn get_notifications_count_by_tag(&self, user: usize, tag: &str) -> Result { + let conn = match self.0.connect().await { + Ok(c) => c, + Err(e) => return Err(Error::DatabaseConnection(e.to_string())), + }; + + let res = query_row!( + &conn, + "SELECT COUNT(*)::int FROM notifications WHERE owner = $1 AND tag = $2", + params![&(user as i64), &tag], + |x| { Ok(x.get::(0)) } + ); + + if let Err(e) = res { + return Err(Error::DatabaseError(e.to_string())); + } + + Ok(res.unwrap() as usize) + } +} diff --git a/src/routes/api/chats.rs b/src/routes/api/chats.rs index c3e8489..d0c9a8e 100644 --- a/src/routes/api/chats.rs +++ b/src/routes/api/chats.rs @@ -556,9 +556,19 @@ pub async fn clear_chat_notifications( None => return Json(Error::NotAllowed.into()), }; + let tag = &id.to_string(); + let count = match data.get_notifications_count_by_tag(user.id, tag).await { + Ok(x) => x, + Err(e) => return Json(e.into()), + }; + + if let Err(e) = data.2.delete_all_notifications_by_tag(&user, tag).await { + return Json(e.into()); + } + if let Err(e) = data .2 - .delete_all_notifications_by_tag(&user, &id.to_string()) + .update_user_notification_count(user.id, (user.notification_count - count) as i32) .await { return Json(e.into());