Create notification only if recipient is local user

This commit is contained in:
silverpill 2021-10-28 23:26:31 +00:00
parent d98e86a93e
commit b50da3e3a4
3 changed files with 41 additions and 40 deletions

View file

@ -5,34 +5,15 @@ use uuid::Uuid;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::posts::helpers::get_actions_for_posts; use crate::models::posts::helpers::get_actions_for_posts;
use crate::models::posts::types::DbPost;
use super::types::{EventType, Notification}; use super::types::{EventType, Notification};
pub async fn create_notification( async fn create_notification(
db_client: &impl GenericClient, db_client: &impl GenericClient,
sender_id: &Uuid, sender_id: &Uuid,
recipient_id: &Uuid, recipient_id: &Uuid,
post_id: Option<&Uuid>,
event_type: EventType, event_type: EventType,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
db_client.execute(
"
INSERT INTO notification (
sender_id,
recipient_id,
event_type
)
VALUES ($1, $2, $3)
",
&[&sender_id, &recipient_id, &i16::from(event_type)],
).await?;
Ok(())
}
pub async fn create_reply_notification(
db_client: &impl GenericClient,
reply: &DbPost,
) -> Result<(), DatabaseError> {
let event_type: i16 = EventType::Reply.into();
db_client.execute( db_client.execute(
" "
INSERT INTO notification ( INSERT INTO notification (
@ -41,19 +22,36 @@ pub async fn create_reply_notification(
post_id, post_id,
event_type event_type
) )
SELECT $1, post.author_id, $2, $3 VALUES ($1, $2, $3, $4)
FROM post WHERE id = $4
", ",
&[ &[&sender_id, &recipient_id, &post_id, &i16::from(event_type)],
&reply.author_id,
&reply.id,
&event_type,
&reply.in_reply_to_id,
],
).await?; ).await?;
Ok(()) Ok(())
} }
pub async fn create_follow_notification(
db_client: &impl GenericClient,
sender_id: &Uuid,
recipient_id: &Uuid,
) -> Result<(), DatabaseError> {
create_notification(
db_client, sender_id, recipient_id, None,
EventType::Follow,
).await
}
pub async fn create_reply_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::Reply,
).await
}
pub async fn get_notifications( pub async fn get_notifications(
db_client: &impl GenericClient, db_client: &impl GenericClient,
recipient_id: &Uuid, recipient_id: &Uuid,

View file

@ -126,7 +126,15 @@ pub async fn create_post(
let author = update_post_count(&transaction, &db_post.author_id, 1).await?; let author = update_post_count(&transaction, &db_post.author_id, 1).await?;
if let Some(in_reply_to_id) = &db_post.in_reply_to_id { if let Some(in_reply_to_id) = &db_post.in_reply_to_id {
update_reply_count(&transaction, in_reply_to_id, 1).await?; update_reply_count(&transaction, in_reply_to_id, 1).await?;
create_reply_notification(&transaction, &db_post).await?; let in_reply_to = get_post_by_id(&transaction, in_reply_to_id).await?;
if in_reply_to.author.is_local() {
create_reply_notification(
&transaction,
&db_post.author_id,
&in_reply_to.author.id,
&db_post.id,
).await?;
}
} }
transaction.commit().await?; transaction.commit().await?;
@ -292,7 +300,6 @@ pub async fn update_reply_count(
UPDATE post UPDATE post
SET reply_count = reply_count + $1 SET reply_count = reply_count + $1
WHERE id = $2 WHERE id = $2
RETURNING post
", ",
&[&change, &post_id], &[&change, &post_id],
).await?; ).await?;

View file

@ -4,8 +4,7 @@ use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::notifications::queries::create_notification; use crate::models::notifications::queries::create_follow_notification;
use crate::models::notifications::types::EventType;
use crate::models::profiles::queries::{ use crate::models::profiles::queries::{
update_follower_count, update_follower_count,
update_following_count, update_following_count,
@ -99,14 +98,11 @@ pub async fn follow(
log::info!("{}", err); log::info!("{}", err);
return Err(DatabaseError::AlreadyExists("relationship")); return Err(DatabaseError::AlreadyExists("relationship"));
}; };
update_follower_count(&transaction, target_id, 1).await?; let target_profile = update_follower_count(&transaction, target_id, 1).await?;
update_following_count(&transaction, source_id, 1).await?; update_following_count(&transaction, source_id, 1).await?;
create_notification( if target_profile.is_local() {
&transaction, create_follow_notification(&transaction, source_id, target_id).await?;
source_id, }
target_id,
EventType::Follow,
).await?;
let relationship = get_relationship(&transaction, source_id, target_id).await?; let relationship = get_relationship(&transaction, source_id, target_id).await?;
transaction.commit().await?; transaction.commit().await?;
Ok(relationship) Ok(relationship)