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
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue