Refactor favourite/unfavourite API methods to make fewer DB queries

This commit is contained in:
silverpill 2021-12-01 14:46:09 +00:00
parent 683996aaf3
commit 0ec69adbe2
2 changed files with 9 additions and 6 deletions

View file

@ -176,18 +176,20 @@ async fn favourite(
) -> Result<HttpResponse, HttpError> { ) -> Result<HttpResponse, HttpError> {
let db_client = &mut **get_database_client(&db_pool).await?; let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?; let current_user = get_current_user(db_client, auth.token()).await?;
let post = get_post_by_id(db_client, &status_id).await?; let mut post = get_post_by_id(db_client, &status_id).await?;
if !can_view_post(Some(&current_user), &post) { if !can_view_post(Some(&current_user), &post) {
return Err(HttpError::NotFoundError("post")); return Err(HttpError::NotFoundError("post"));
}; };
let reaction_created = match create_reaction( let reaction_created = match create_reaction(
db_client, &current_user.id, &status_id, db_client, &current_user.id, &status_id,
).await { ).await {
Ok(_) => true, Ok(_) => {
post.reaction_count += 1;
true
},
Err(DatabaseError::AlreadyExists(_)) => false, // post already favourited Err(DatabaseError::AlreadyExists(_)) => false, // post already favourited
Err(other_error) => return Err(other_error.into()), Err(other_error) => return Err(other_error.into()),
}; };
let mut post = get_post_by_id(db_client, &status_id).await?;
get_reposted_posts(db_client, vec![&mut post]).await?; get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?; get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?;
@ -219,15 +221,15 @@ async fn unfavourite(
) -> Result<HttpResponse, HttpError> { ) -> Result<HttpResponse, HttpError> {
let db_client = &mut **get_database_client(&db_pool).await?; let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?; let current_user = get_current_user(db_client, auth.token()).await?;
let post = get_post_by_id(db_client, &status_id).await?; let mut post = get_post_by_id(db_client, &status_id).await?;
if !can_view_post(Some(&current_user), &post) { if !can_view_post(Some(&current_user), &post) {
return Err(HttpError::NotFoundError("post")); return Err(HttpError::NotFoundError("post"));
}; };
match delete_reaction(db_client, &current_user.id, &status_id).await { match delete_reaction(db_client, &current_user.id, &status_id).await {
Ok(_) => post.reaction_count -= 1,
Err(DatabaseError::NotFound(_)) => (), // post not favourited Err(DatabaseError::NotFound(_)) => (), // post not favourited
other_result => other_result?, Err(other_error) => return Err(other_error.into()),
} }
let mut post = get_post_by_id(db_client, &status_id).await?;
get_reposted_posts(db_client, vec![&mut post]).await?; get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?; get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?;
let status = Status::from_post(post, &config.instance_url()); let status = Status::from_post(post, &config.instance_url());

View file

@ -323,6 +323,7 @@ pub async fn get_thread(
post_id: &Uuid, post_id: &Uuid,
current_user_id: Option<&Uuid>, current_user_id: Option<&Uuid>,
) -> Result<Vec<Post>, DatabaseError> { ) -> Result<Vec<Post>, DatabaseError> {
// Exclude direct messages
let mut condition = format!( let mut condition = format!(
"post.visibility = {visibility_public}", "post.visibility = {visibility_public}",
visibility_public=i16::from(&Visibility::Public), visibility_public=i16::from(&Visibility::Public),