Refactor posts::helpers, remove get_actions_for_post function

This commit is contained in:
silverpill 2021-11-24 00:33:22 +00:00
parent f716a61d0e
commit 6d243d86e6
3 changed files with 15 additions and 24 deletions

View file

@ -23,7 +23,6 @@ use crate::models::posts::helpers::can_view_post;
use crate::models::posts::mentions::{find_mentioned_profiles, replace_mentions}; use crate::models::posts::mentions::{find_mentioned_profiles, replace_mentions};
use crate::models::profiles::queries::get_followers; use crate::models::profiles::queries::get_followers;
use crate::models::posts::helpers::{ use crate::models::posts::helpers::{
get_actions_for_post,
get_actions_for_posts, get_actions_for_posts,
}; };
use crate::models::posts::queries::{ use crate::models::posts::queries::{
@ -125,7 +124,7 @@ async fn get_status(
return Err(HttpError::NotFoundError("post")); return Err(HttpError::NotFoundError("post"));
}; };
if let Some(user) = maybe_current_user { if let Some(user) = maybe_current_user {
get_actions_for_post(db_client, &user.id, &mut post).await?; get_actions_for_posts(db_client, &user.id, vec![&mut post]).await?;
} }
let status = Status::from_post(post, &config.instance_url()); let status = Status::from_post(post, &config.instance_url());
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
@ -183,7 +182,7 @@ async fn favourite(
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?; let mut post = get_post_by_id(db_client, &status_id).await?;
get_actions_for_post(db_client, &current_user.id, &mut post).await?; get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?;
if reaction_created { if reaction_created {
let maybe_remote_actor = post.author.remote_actor() let maybe_remote_actor = post.author.remote_actor()
@ -222,7 +221,7 @@ async fn unfavourite(
other_result => other_result?, other_result => other_result?,
} }
let mut post = get_post_by_id(db_client, &status_id).await?; let mut post = get_post_by_id(db_client, &status_id).await?;
get_actions_for_post(db_client, &current_user.id, &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());
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }
@ -285,7 +284,7 @@ async fn make_permanent(
// Update post // Update post
post.ipfs_cid = Some(post_metadata_cid); post.ipfs_cid = Some(post_metadata_cid);
update_post(db_client, &post).await?; update_post(db_client, &post).await?;
get_actions_for_post(db_client, &current_user.id, &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());
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }

View file

@ -2,30 +2,21 @@ use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::reactions::queries::get_favourited; use crate::models::reactions::queries::find_favourited_by_user;
use crate::models::users::types::User; use crate::models::users::types::User;
use super::types::{Post, PostActions, Visibility}; use super::types::{Post, PostActions, Visibility};
pub async fn get_actions_for_post(
db_client: &impl GenericClient,
user_id: &Uuid,
post: &mut Post,
) -> Result<(), DatabaseError> {
let favourited = get_favourited(db_client, user_id, vec![post.id]).await?;
let actions = PostActions { favourited: favourited.contains(&post.id) };
post.actions = Some(actions);
Ok(())
}
pub async fn get_actions_for_posts( pub async fn get_actions_for_posts(
db_client: &impl GenericClient, db_client: &impl GenericClient,
user_id: &Uuid, user_id: &Uuid,
posts: Vec<&mut Post>, posts: Vec<&mut Post>,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
let posts_ids = posts.iter().map(|post| post.id).collect(); let posts_ids: Vec<Uuid> = posts.iter().map(|post| post.id).collect();
let favourited = get_favourited(db_client, user_id, posts_ids).await?; let favourites = find_favourited_by_user(db_client, user_id, &posts_ids).await?;
for post in posts { for post in posts {
let actions = PostActions { favourited: favourited.contains(&post.id) }; let actions = PostActions {
favourited: favourites.contains(&post.id),
};
post.actions = Some(actions); post.actions = Some(actions);
} }
Ok(()) Ok(())

View file

@ -55,10 +55,11 @@ pub async fn delete_reaction(
Ok(()) Ok(())
} }
pub async fn get_favourited( /// Finds favourites among given posts and returns their IDs
pub async fn find_favourited_by_user(
db_client: &impl GenericClient, db_client: &impl GenericClient,
user_id: &Uuid, user_id: &Uuid,
posts_ids: Vec<Uuid>, posts_ids: &[Uuid],
) -> Result<Vec<Uuid>, DatabaseError> { ) -> Result<Vec<Uuid>, DatabaseError> {
let rows = db_client.query( let rows = db_client.query(
" "
@ -68,8 +69,8 @@ pub async fn get_favourited(
", ",
&[&user_id, &posts_ids], &[&user_id, &posts_ids],
).await?; ).await?;
let favourited: Vec<Uuid> = rows.iter() let favourites: Vec<Uuid> = rows.iter()
.map(|row| row.try_get("post_id")) .map(|row| row.try_get("post_id"))
.collect::<Result<_, _>>()?; .collect::<Result<_, _>>()?;
Ok(favourited) Ok(favourites)
} }