Refactor posts::helpers, remove get_actions_for_post function
This commit is contained in:
parent
f716a61d0e
commit
6d243d86e6
3 changed files with 15 additions and 24 deletions
|
@ -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::profiles::queries::get_followers;
|
||||
use crate::models::posts::helpers::{
|
||||
get_actions_for_post,
|
||||
get_actions_for_posts,
|
||||
};
|
||||
use crate::models::posts::queries::{
|
||||
|
@ -125,7 +124,7 @@ async fn get_status(
|
|||
return Err(HttpError::NotFoundError("post"));
|
||||
};
|
||||
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());
|
||||
Ok(HttpResponse::Ok().json(status))
|
||||
|
@ -183,7 +182,7 @@ async fn favourite(
|
|||
Err(other_error) => return Err(other_error.into()),
|
||||
};
|
||||
let mut post = get_post_by_id(db_client, &status_id).await?;
|
||||
get_actions_for_post(db_client, ¤t_user.id, &mut post).await?;
|
||||
get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?;
|
||||
|
||||
if reaction_created {
|
||||
let maybe_remote_actor = post.author.remote_actor()
|
||||
|
@ -222,7 +221,7 @@ async fn unfavourite(
|
|||
other_result => other_result?,
|
||||
}
|
||||
let mut post = get_post_by_id(db_client, &status_id).await?;
|
||||
get_actions_for_post(db_client, ¤t_user.id, &mut post).await?;
|
||||
get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?;
|
||||
let status = Status::from_post(post, &config.instance_url());
|
||||
Ok(HttpResponse::Ok().json(status))
|
||||
}
|
||||
|
@ -285,7 +284,7 @@ async fn make_permanent(
|
|||
// Update post
|
||||
post.ipfs_cid = Some(post_metadata_cid);
|
||||
update_post(db_client, &post).await?;
|
||||
get_actions_for_post(db_client, ¤t_user.id, &mut post).await?;
|
||||
get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?;
|
||||
let status = Status::from_post(post, &config.instance_url());
|
||||
Ok(HttpResponse::Ok().json(status))
|
||||
}
|
||||
|
|
|
@ -2,30 +2,21 @@ use tokio_postgres::GenericClient;
|
|||
use uuid::Uuid;
|
||||
|
||||
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 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(
|
||||
db_client: &impl GenericClient,
|
||||
user_id: &Uuid,
|
||||
posts: Vec<&mut Post>,
|
||||
) -> Result<(), DatabaseError> {
|
||||
let posts_ids = posts.iter().map(|post| post.id).collect();
|
||||
let favourited = get_favourited(db_client, user_id, posts_ids).await?;
|
||||
let posts_ids: Vec<Uuid> = posts.iter().map(|post| post.id).collect();
|
||||
let favourites = find_favourited_by_user(db_client, user_id, &posts_ids).await?;
|
||||
for post in posts {
|
||||
let actions = PostActions { favourited: favourited.contains(&post.id) };
|
||||
let actions = PostActions {
|
||||
favourited: favourites.contains(&post.id),
|
||||
};
|
||||
post.actions = Some(actions);
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -55,10 +55,11 @@ pub async fn delete_reaction(
|
|||
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,
|
||||
user_id: &Uuid,
|
||||
posts_ids: Vec<Uuid>,
|
||||
posts_ids: &[Uuid],
|
||||
) -> Result<Vec<Uuid>, DatabaseError> {
|
||||
let rows = db_client.query(
|
||||
"
|
||||
|
@ -68,8 +69,8 @@ pub async fn get_favourited(
|
|||
",
|
||||
&[&user_id, &posts_ids],
|
||||
).await?;
|
||||
let favourited: Vec<Uuid> = rows.iter()
|
||||
let favourites: Vec<Uuid> = rows.iter()
|
||||
.map(|row| row.try_get("post_id"))
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(favourited)
|
||||
Ok(favourites)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue