add: chat notifications
use sql_chanes/notifications_tag.sql; ignore first statement if you never used a preview commit
This commit is contained in:
parent
a009ef9e34
commit
59cfec4819
22 changed files with 267 additions and 136 deletions
|
@ -26,6 +26,7 @@ impl DataManager {
|
|||
content: get!(x->3(String)),
|
||||
owner: get!(x->4(i64)) as usize,
|
||||
read: get!(x->5(i32)) as i8 == 1,
|
||||
tag: get!(x->6(String)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +53,27 @@ impl DataManager {
|
|||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all notifications by `tag`.
|
||||
pub async fn get_notifications_by_tag(&self, tag: &str) -> Result<Vec<Notification>> {
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM notifications WHERE tag = $1 ORDER BY created DESC",
|
||||
&[&tag],
|
||||
|x| { Self::get_notification_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("notification".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Create a new notification in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
|
@ -64,14 +86,15 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO notifications VALUES ($1, $2, $3, $4, $5, $6)",
|
||||
"INSERT INTO notifications VALUES ($1, $2, $3, $4, $5, $6, $7)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&data.title,
|
||||
&data.content,
|
||||
&(data.owner as i64),
|
||||
&{ if data.read { 1 } else { 0 } }
|
||||
&{ if data.read { 1 } else { 0 } },
|
||||
&data.tag
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -167,6 +190,22 @@ impl DataManager {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_all_notifications_by_tag(&self, user: &User, tag: &str) -> Result<()> {
|
||||
let notifications = self.get_notifications_by_tag(tag).await?;
|
||||
|
||||
for notification in notifications {
|
||||
if user.id != notification.owner
|
||||
&& !user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS)
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
||||
self.delete_notification(notification.id, user).await?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_notification_read(
|
||||
&self,
|
||||
id: usize,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue