Populate "quote" field in reposted posts

This commit is contained in:
silverpill 2022-09-11 21:17:07 +00:00
parent 9f3b95c41d
commit de80bd0b40
2 changed files with 56 additions and 19 deletions

View file

@ -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<Post, DatabaseError> {
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));
};
};

View file

@ -454,6 +454,47 @@ pub async fn get_posts(
Ok(posts)
}
pub async fn get_related_posts(
db_client: &impl GenericClient,
posts_ids: Vec<Uuid>,
) -> Result<Vec<Post>, 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<Post> = rows.iter()
.map(Post::try_from)
.collect::<Result<_, _>>()?;
Ok(posts)
}
pub async fn get_posts_by_author(
db_client: &impl GenericClient,
profile_id: &Uuid,