Notify user about new reactions

This commit is contained in:
silverpill 2021-10-29 12:30:28 +00:00
parent 6c050a37e6
commit 29940b5541
4 changed files with 27 additions and 1 deletions

View file

@ -32,6 +32,7 @@ impl ApiNotification {
EventType::Follow => "follow",
EventType::FollowRequest => "follow_request",
EventType::Reply => "reply",
EventType::Reaction => "favourite",
};
Self {
id: notification.id.to_string(),

View file

@ -52,6 +52,18 @@ pub async fn create_reply_notification(
).await
}
pub async fn create_reaction_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::Reaction,
).await
}
pub async fn get_notifications(
db_client: &impl GenericClient,
recipient_id: &Uuid,

View file

@ -26,6 +26,7 @@ pub enum EventType {
Follow,
FollowRequest,
Reply,
Reaction,
}
impl From<EventType> for i16 {
@ -34,6 +35,7 @@ impl From<EventType> for i16 {
EventType::Follow => 1,
EventType::FollowRequest => 2,
EventType::Reply => 3,
EventType::Reaction => 4,
}
}
}
@ -46,6 +48,7 @@ impl TryFrom<i16> for EventType {
1 => Self::Follow,
2 => Self::FollowRequest,
3 => Self::Reply,
4 => Self::Reaction,
_ => return Err(ConversionError),
};
Ok(event_type)

View file

@ -3,7 +3,8 @@ use uuid::Uuid;
use crate::database::catch_unique_violation;
use crate::errors::DatabaseError;
use crate::models::posts::queries::update_reaction_count;
use crate::models::notifications::queries::create_reaction_notification;
use crate::models::posts::queries::{get_post_by_id, update_reaction_count};
pub async fn create_reaction(
db_client: &mut impl GenericClient,
@ -20,6 +21,15 @@ pub async fn create_reaction(
&[&reaction_id, &author_id, &post_id],
).await.map_err(catch_unique_violation("reaction"))?;
update_reaction_count(&transaction, post_id, 1).await?;
let post = get_post_by_id(&transaction, post_id).await?;
if post.author.is_local() {
create_reaction_notification(
&transaction,
author_id,
&post.author.id,
&post.id,
).await?;
}
transaction.commit().await?;
Ok(())
}