Add get_post_author() to posts::queries

This commit is contained in:
silverpill 2021-12-04 22:48:00 +00:00
parent c2d070c4fe
commit e7955d8ebb
2 changed files with 38 additions and 17 deletions

View file

@ -110,33 +110,33 @@ pub async fn create_post(
let mut notified_users = vec![]; let mut notified_users = vec![];
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?;
let in_reply_to = get_post_by_id(&transaction, in_reply_to_id).await?; let in_reply_to_author = get_post_author(&transaction, in_reply_to_id).await?;
if in_reply_to.author.is_local() && if in_reply_to_author.is_local() &&
in_reply_to.author.id != db_post.author_id in_reply_to_author.id != db_post.author_id
{ {
create_reply_notification( create_reply_notification(
&transaction, &transaction,
&db_post.author_id, &db_post.author_id,
&in_reply_to.author.id, &in_reply_to_author.id,
&db_post.id, &db_post.id,
).await?; ).await?;
notified_users.push(in_reply_to.author.id); notified_users.push(in_reply_to_author.id);
} };
} }
if let Some(repost_of_id) = &db_post.repost_of_id { if let Some(repost_of_id) = &db_post.repost_of_id {
update_repost_count(&transaction, repost_of_id, 1).await?; update_repost_count(&transaction, repost_of_id, 1).await?;
let repost_of = get_post_by_id(&transaction, repost_of_id).await?; let repost_of_author = get_post_author(&transaction, repost_of_id).await?;
if repost_of.author.is_local() && if repost_of_author.is_local() &&
repost_of.author.id != db_post.author_id && repost_of_author.id != db_post.author_id &&
!notified_users.contains(&repost_of.author.id) !notified_users.contains(&repost_of_author.id)
{ {
create_repost_notification( create_repost_notification(
&transaction, &transaction,
&db_post.author_id, &db_post.author_id,
&repost_of.author.id, &repost_of_author.id,
&db_post.id, &db_post.id,
).await?; ).await?;
notified_users.push(repost_of.author.id); notified_users.push(repost_of_author.id);
}; };
}; };
// Notify mentioned users // Notify mentioned users
@ -531,6 +531,24 @@ pub async fn update_repost_count(
Ok(()) Ok(())
} }
pub async fn get_post_author(
db_client: &impl GenericClient,
post_id: &Uuid,
) -> Result<DbActorProfile, DatabaseError> {
let maybe_row = db_client.query_opt(
"
SELECT actor_profile
FROM post
JOIN actor_profile ON post.author_id = actor_profile.id
WHERE post.id = $1
",
&[&post_id],
).await?;
let row = maybe_row.ok_or(DatabaseError::NotFound("post"))?;
let author: DbActorProfile = row.try_get("actor_profile")?;
Ok(author)
}
/// Finds reposts of given posts and returns their IDs /// Finds reposts of given posts and returns their IDs
pub async fn find_reposts_by_user( pub async fn find_reposts_by_user(
db_client: &impl GenericClient, db_client: &impl GenericClient,

View file

@ -4,7 +4,10 @@ use uuid::Uuid;
use crate::database::catch_unique_violation; use crate::database::catch_unique_violation;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::notifications::queries::create_reaction_notification; use crate::models::notifications::queries::create_reaction_notification;
use crate::models::posts::queries::{get_post_by_id, update_reaction_count}; use crate::models::posts::queries::{
update_reaction_count,
get_post_author,
};
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
pub async fn create_reaction( pub async fn create_reaction(
@ -22,13 +25,13 @@ pub async fn create_reaction(
&[&reaction_id, &author_id, &post_id], &[&reaction_id, &author_id, &post_id],
).await.map_err(catch_unique_violation("reaction"))?; ).await.map_err(catch_unique_violation("reaction"))?;
update_reaction_count(&transaction, post_id, 1).await?; update_reaction_count(&transaction, post_id, 1).await?;
let post = get_post_by_id(&transaction, post_id).await?; let post_author = get_post_author(&transaction, post_id).await?;
if post.author.is_local() && post.author.id != *author_id { if post_author.is_local() && post_author.id != *author_id {
create_reaction_notification( create_reaction_notification(
&transaction, &transaction,
author_id, author_id,
&post.author.id, &post_author.id,
&post.id, post_id,
).await?; ).await?;
} }
transaction.commit().await?; transaction.commit().await?;