From de80bd0b40f5bdd629740ffe6ab6fb55c276c696 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 11 Sep 2022 21:17:07 +0000 Subject: [PATCH] Populate "quote" field in reposted posts --- src/models/posts/helpers.rs | 34 ++++++++++++++---------------- src/models/posts/queries.rs | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/models/posts/helpers.rs b/src/models/posts/helpers.rs index ed4d0c6..aa52117 100644 --- a/src/models/posts/helpers.rs +++ b/src/models/posts/helpers.rs @@ -6,37 +6,33 @@ use crate::models::reactions::queries::find_favourited_by_user; use crate::models::relationships::queries::has_relationship; use crate::models::relationships::types::RelationshipType; use crate::models::users::types::User; -use super::queries::{get_posts, find_reposted_by_user}; +use super::queries::{get_related_posts, find_reposted_by_user}; use super::types::{Post, PostActions, Visibility}; pub async fn add_related_posts( db_client: &impl GenericClient, posts: Vec<&mut Post>, ) -> Result<(), DatabaseError> { - let mut related_ids = vec![]; - for post in posts.iter() { - if let Some(repost_of_id) = post.repost_of_id { - related_ids.push(repost_of_id); - }; - related_ids.extend(post.links.clone()); + let posts_ids = posts.iter().map(|post| post.id).collect(); + let related = get_related_posts(db_client, posts_ids).await?; + let get_post = |post_id: &Uuid| -> Result { + let post = related.iter() + .find(|post| post.id == *post_id) + .ok_or(DatabaseError::NotFound("post"))? + .clone(); + Ok(post) }; - if related_ids.is_empty() { - return Ok(()); - }; - let related = get_posts(db_client, related_ids).await?; for post in posts { if let Some(ref repost_of_id) = post.repost_of_id { - let repost_of = related.iter() - .find(|post| post.id == *repost_of_id) - .ok_or(DatabaseError::NotFound("post"))? - .clone(); + let mut repost_of = get_post(repost_of_id)?; + if let Some(quote_id) = repost_of.links.get(0) { + let quote = get_post(quote_id)?; + repost_of.quote = Some(Box::new(quote)); + }; post.repost_of = Some(Box::new(repost_of)); }; if let Some(quote_id) = post.links.get(0) { - let quote = related.iter() - .find(|post| post.id == *quote_id) - .ok_or(DatabaseError::NotFound("post"))? - .clone(); + let quote = get_post(quote_id)?; post.quote = Some(Box::new(quote)); }; }; diff --git a/src/models/posts/queries.rs b/src/models/posts/queries.rs index 65b1190..3f21316 100644 --- a/src/models/posts/queries.rs +++ b/src/models/posts/queries.rs @@ -454,6 +454,47 @@ pub async fn get_posts( Ok(posts) } +pub async fn get_related_posts( + db_client: &impl GenericClient, + posts_ids: Vec, +) -> Result, DatabaseError> { + let statement = format!( + " + SELECT + post, actor_profile, + {related_attachments}, + {related_mentions}, + {related_tags}, + {related_links} + FROM post + JOIN actor_profile ON post.author_id = actor_profile.id + WHERE post.id IN ( + SELECT post.repost_of_id + FROM post WHERE post.id = ANY($1) + UNION ALL + SELECT post_link.target_id + FROM post_link WHERE post_link.source_id = ANY($1) + UNION ALL + SELECT post_link.target_id + FROM post_link JOIN post ON (post.repost_of_id = post_link.source_id) + WHERE post.id = ANY($1) + ) + ", + related_attachments=RELATED_ATTACHMENTS, + related_mentions=RELATED_MENTIONS, + related_tags=RELATED_TAGS, + related_links=RELATED_LINKS, + ); + let rows = db_client.query( + &statement, + &[&posts_ids], + ).await?; + let posts: Vec = rows.iter() + .map(Post::try_from) + .collect::>()?; + Ok(posts) +} + pub async fn get_posts_by_author( db_client: &impl GenericClient, profile_id: &Uuid,