From 6d243d86e6340f691984ec0034216bee4c4ddaf7 Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 24 Nov 2021 00:33:22 +0000 Subject: [PATCH] Refactor posts::helpers, remove get_actions_for_post function --- src/mastodon_api/statuses/views.rs | 9 ++++----- src/models/posts/helpers.rs | 21 ++++++--------------- src/models/reactions/queries.rs | 9 +++++---- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 90125c0..364f9e4 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -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)) } diff --git a/src/models/posts/helpers.rs b/src/models/posts/helpers.rs index 2a91535..f608832 100644 --- a/src/models/posts/helpers.rs +++ b/src/models/posts/helpers.rs @@ -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 = 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(()) diff --git a/src/models/reactions/queries.rs b/src/models/reactions/queries.rs index 04c3590..d3283ce 100644 --- a/src/models/reactions/queries.rs +++ b/src/models/reactions/queries.rs @@ -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, + posts_ids: &[Uuid], ) -> Result, DatabaseError> { let rows = db_client.query( " @@ -68,8 +69,8 @@ pub async fn get_favourited( ", &[&user_id, &posts_ids], ).await?; - let favourited: Vec = rows.iter() + let favourites: Vec = rows.iter() .map(|row| row.try_get("post_id")) .collect::>()?; - Ok(favourited) + Ok(favourites) }