add: chat notifications

use sql_chanes/notifications_tag.sql; ignore first statement if you never used a preview commit
This commit is contained in:
trisua 2025-05-03 17:51:36 -04:00
parent a009ef9e34
commit 59cfec4819
22 changed files with 267 additions and 136 deletions

View file

@ -45,7 +45,6 @@ impl DataManager {
post_count: get!(x->15(i32)) as usize,
request_count: get!(x->16(i32)) as usize,
connections: serde_json::from_str(&get!(x->17(String)).to_string()).unwrap(),
subscriptions: serde_json::from_str(&get!(x->18(String)).to_string()).unwrap(),
}
}
@ -140,7 +139,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO users VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)",
"INSERT INTO users VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)",
params![
&(data.id as i64),
&(data.created as i64),
@ -160,7 +159,6 @@ impl DataManager {
&0_i32,
&0_i32,
&serde_json::to_string(&data.connections).unwrap(),
&serde_json::to_string(&data.subscriptions).unwrap(),
]
);

View file

@ -2,6 +2,7 @@ use std::collections::HashMap;
use super::*;
use crate::cache::Cache;
use crate::model::auth::Notification;
use crate::model::moderation::AuditLogEntry;
use crate::model::socket::{SocketMessage, SocketMethod};
use crate::model::{
@ -116,7 +117,7 @@ impl DataManager {
///
/// # Arguments
/// * `data` - a mock [`Message`] object to insert
pub async fn create_message(&self, data: Message) -> Result<()> {
pub async fn create_message(&self, mut data: Message) -> Result<()> {
if data.content.len() < 2 {
return Err(Error::DataTooLong("content".to_string()));
}
@ -125,19 +126,78 @@ impl DataManager {
return Err(Error::DataTooLong("content".to_string()));
}
let user = self.get_user_by_id(data.owner).await?;
let owner = self.get_user_by_id(data.owner).await?;
let channel = self.get_channel_by_id(data.channel).await?;
// check user permission in community
let membership = self
.get_membership_by_owner_community(user.id, channel.community)
.get_membership_by_owner_community(owner.id, channel.community)
.await?;
// check user permission to post in channel
if !channel.check_post(user.id, Some(membership.role)) {
if !channel.check_post(owner.id, Some(membership.role)) {
return Err(Error::NotAllowed);
}
// send mention notifications
let mut already_notified: HashMap<String, User> = HashMap::new();
for username in User::parse_mentions(&data.content) {
let user = {
if let Some(ua) = already_notified.get(&username) {
ua.to_owned()
} else {
let user = self.get_user_by_username(&username).await?;
self.create_notification(Notification::new(
"You've been mentioned in a message!".to_string(),
format!(
"[@{}](/api/v1/auth/user/find/{}) has mentioned you in their [message](/chats/{}/{}?message={}).",
owner.username, owner.id, channel.community, data.channel, data.id
),
user.id,
))
.await?;
already_notified.insert(username.to_owned(), user.clone());
user
}
};
data.content = data.content.replace(
&format!("@{username}"),
&format!(
"<a href=\"/api/v1/auth/user/find/{}\" target=\"_top\">@{username}</a>",
user.id
),
);
}
// send notifs to members (if this message isn't associated with a channel)
if channel.community == 0 {
for member in [channel.members, vec![channel.owner]].concat() {
if member == owner.id {
continue;
}
let mut notif = Notification::new(
"You've received a new message!".to_string(),
format!(
"[@{}](/api/v1/auth/user/find/{}) has sent a [message](/chats/{}/{}?message={}) in [{}](/chats/{}/{}).",
owner.username,
owner.id,
channel.community,
data.channel,
data.id,
channel.title,
channel.community,
data.channel
),
member,
);
notif.tag = format!("chats/{}", channel.id);
self.create_notification(notif).await?;
}
}
// ...
let conn = match self.connect().await {
Ok(c) => c,

View file

@ -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,