fix: message notification deletion

This commit is contained in:
trisua 2025-09-07 17:07:25 -04:00
parent f3180989af
commit ff1c739367
4 changed files with 39 additions and 2 deletions

View file

@ -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();

View file

@ -1,5 +1,6 @@
mod chats;
mod messages;
mod notifications;
mod sql;
mod users;

View file

@ -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<usize> {
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::<usize, i32>(0)) }
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
Ok(res.unwrap() as usize)
}
}

View file

@ -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());