diff --git a/src/mastodon_api/notifications/types.rs b/src/mastodon_api/notifications/types.rs index 551d94d..9a74dac 100644 --- a/src/mastodon_api/notifications/types.rs +++ b/src/mastodon_api/notifications/types.rs @@ -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(), diff --git a/src/models/notifications/queries.rs b/src/models/notifications/queries.rs index 9a47652..1adaeeb 100644 --- a/src/models/notifications/queries.rs +++ b/src/models/notifications/queries.rs @@ -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, diff --git a/src/models/notifications/types.rs b/src/models/notifications/types.rs index 3978ee4..7eb496a 100644 --- a/src/models/notifications/types.rs +++ b/src/models/notifications/types.rs @@ -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 for EventType { 2 => Self::FollowRequest, 3 => Self::Reply, 4 => Self::Reaction, + 5 => Self::Mention, _ => return Err(ConversionError), }; Ok(event_type) diff --git a/src/models/posts/queries.rs b/src/models/posts/queries.rs index 904b1cc..a86c8e7 100644 --- a/src/models/posts/queries.rs +++ b/src/models/posts/queries.rs @@ -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::>()?; // 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)?;