Send notification when user is mentioned
This commit is contained in:
parent
a3ac526bca
commit
2111bae373
4 changed files with 37 additions and 1 deletions
|
@ -33,6 +33,7 @@ impl ApiNotification {
|
|||
EventType::FollowRequest => "follow_request",
|
||||
EventType::Reply => "reply",
|
||||
EventType::Reaction => "favourite",
|
||||
EventType::Mention => "mention",
|
||||
};
|
||||
Self {
|
||||
id: notification.id.to_string(),
|
||||
|
|
|
@ -65,6 +65,18 @@ pub async fn create_reaction_notification(
|
|||
).await
|
||||
}
|
||||
|
||||
pub async fn create_mention_notification(
|
||||
db_client: &impl GenericClient,
|
||||
sender_id: &Uuid,
|
||||
recipient_id: &Uuid,
|
||||
post_id: &Uuid,
|
||||
) -> Result<(), DatabaseError> {
|
||||
create_notification(
|
||||
db_client, sender_id, recipient_id, Some(post_id),
|
||||
EventType::Mention,
|
||||
).await
|
||||
}
|
||||
|
||||
pub async fn get_notifications(
|
||||
db_client: &impl GenericClient,
|
||||
recipient_id: &Uuid,
|
||||
|
|
|
@ -29,6 +29,7 @@ pub enum EventType {
|
|||
FollowRequest,
|
||||
Reply,
|
||||
Reaction,
|
||||
Mention,
|
||||
}
|
||||
|
||||
impl From<&EventType> for i16 {
|
||||
|
@ -38,6 +39,7 @@ impl From<&EventType> for i16 {
|
|||
EventType::FollowRequest => 2,
|
||||
EventType::Reply => 3,
|
||||
EventType::Reaction => 4,
|
||||
EventType::Mention => 5,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +53,7 @@ impl TryFrom<i16> for EventType {
|
|||
2 => Self::FollowRequest,
|
||||
3 => Self::Reply,
|
||||
4 => Self::Reaction,
|
||||
5 => Self::Mention,
|
||||
_ => return Err(ConversionError),
|
||||
};
|
||||
Ok(event_type)
|
||||
|
|
|
@ -13,7 +13,10 @@ use crate::models::cleanup::{
|
|||
find_orphaned_ipfs_objects,
|
||||
DeletionQueue,
|
||||
};
|
||||
use crate::models::notifications::queries::create_reply_notification;
|
||||
use crate::models::notifications::queries::{
|
||||
create_mention_notification,
|
||||
create_reply_notification,
|
||||
};
|
||||
use crate::models::profiles::queries::update_post_count;
|
||||
use crate::models::profiles::types::DbActorProfile;
|
||||
use super::types::{DbPost, Post, PostCreateData, Visibility};
|
||||
|
@ -230,6 +233,7 @@ pub async fn create_post(
|
|||
.collect::<Result<_, _>>()?;
|
||||
// Update counters
|
||||
let author = update_post_count(&transaction, &db_post.author_id, 1).await?;
|
||||
let mut notified_users = vec![];
|
||||
if let Some(in_reply_to_id) = &db_post.in_reply_to_id {
|
||||
update_reply_count(&transaction, in_reply_to_id, 1).await?;
|
||||
let in_reply_to = get_post_by_id(&transaction, in_reply_to_id).await?;
|
||||
|
@ -242,11 +246,27 @@ pub async fn create_post(
|
|||
&in_reply_to.author.id,
|
||||
&db_post.id,
|
||||
).await?;
|
||||
notified_users.push(in_reply_to.author.id);
|
||||
}
|
||||
}
|
||||
if let Some(repost_of_id) = &db_post.repost_of_id {
|
||||
update_repost_count(&transaction, repost_of_id, 1).await?;
|
||||
};
|
||||
// Notify mentioned users
|
||||
for profile in db_mentions.iter() {
|
||||
if profile.is_local() &&
|
||||
profile.id != db_post.author_id &&
|
||||
// Don't send mention notification to the author of parent post
|
||||
!notified_users.contains(&profile.id)
|
||||
{
|
||||
create_mention_notification(
|
||||
&transaction,
|
||||
&db_post.author_id,
|
||||
&profile.id,
|
||||
&db_post.id,
|
||||
).await?;
|
||||
};
|
||||
};
|
||||
|
||||
transaction.commit().await?;
|
||||
let post = Post::new(db_post, author, db_attachments, db_mentions)?;
|
||||
|
|
Loading…
Reference in a new issue