Add "quote" property to Status object and treat first link as a quote

This commit is contained in:
silverpill 2022-08-21 20:08:45 +00:00
parent f4d1e756d4
commit 5e97debebb
5 changed files with 45 additions and 11 deletions

View file

@ -996,6 +996,9 @@ components:
id:
type: string
format: uuid
uri:
description: URI of the post used for federation.
type: string
created_at:
description: The date when this post was created.
type: string
@ -1005,6 +1008,9 @@ components:
type: string
format: dateTime
nullable: true
account:
description: The profile that authored this post.
$ref: '#/components/schemas/Account'
content:
description: HTML-encoded post content.
type: string
@ -1013,7 +1019,9 @@ components:
$ref: '#/components/schemas/Visibility'
media_attachments:
description: Media that is attached to this post.
$ref: '#/components/schemas/Attachment'
type: array
items:
$ref: '#/components/schemas/Attachment'
mentions:
description: Mentions of users within the post.
type: array
@ -1032,6 +1040,12 @@ components:
type: string
nullable: true
example: '0x5fe80cdea7f...'
reblog:
description: The post being reposted.
type: object
quote:
description: The post being quoted.
type: object
Subscription:
type: object
properties:

View file

@ -3,7 +3,7 @@ use tokio_postgres::GenericClient;
use crate::errors::DatabaseError;
use crate::models::posts::helpers::{
add_user_actions,
add_reposted_posts,
add_related_posts,
};
use crate::models::posts::types::Post;
use crate::models::users::types::User;
@ -16,7 +16,7 @@ pub async fn build_status(
user: Option<&User>,
mut post: Post,
) -> Result<Status, DatabaseError> {
add_reposted_posts(db_client, vec![&mut post]).await?;
add_related_posts(db_client, vec![&mut post]).await?;
if let Some(user) = user {
add_user_actions(db_client, &user.id, vec![&mut post]).await?;
};
@ -30,7 +30,7 @@ pub async fn build_status_list(
user: Option<&User>,
mut posts: Vec<Post>,
) -> Result<Vec<Status>, DatabaseError> {
add_reposted_posts(db_client, posts.iter_mut().collect()).await?;
add_related_posts(db_client, posts.iter_mut().collect()).await?;
if let Some(user) = user {
add_user_actions(db_client, &user.id, posts.iter_mut().collect()).await?;
};

View file

@ -75,6 +75,7 @@ pub struct Status {
pub ipfs_cid: Option<String>,
pub token_id: Option<i32>,
pub token_tx_id: Option<String>,
quote: Option<Box<Status>>,
}
impl Status {
@ -96,6 +97,10 @@ impl Status {
} else {
None
};
let quote = post.quote.map(|quote| {
let status = Status::from_post(*quote, instance_url);
Box::new(status)
});
let visibility = match post.visibility {
Visibility::Public => "public",
Visibility::Direct => "direct",
@ -123,6 +128,7 @@ impl Status {
ipfs_cid: post.ipfs_cid,
token_id: post.token_id,
token_tx_id: post.token_tx_id,
quote: quote,
}
}
}

View file

@ -9,25 +9,36 @@ use crate::models::users::types::User;
use super::queries::{get_posts, find_reposted_by_user};
use super::types::{Post, PostActions, Visibility};
pub async fn add_reposted_posts(
pub async fn add_related_posts(
db_client: &impl GenericClient,
posts: Vec<&mut Post>,
) -> Result<(), DatabaseError> {
let reposted_ids: Vec<Uuid> = posts.iter()
.filter_map(|post| post.repost_of_id)
.collect();
if reposted_ids.is_empty() {
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());
};
if related_ids.is_empty() {
return Ok(());
};
let reposted = get_posts(db_client, reposted_ids).await?;
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 = reposted.iter()
let repost_of = related.iter()
.find(|post| post.id == *repost_of_id)
.ok_or(DatabaseError::NotFound("post"))?
.clone();
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();
post.quote = Some(Box::new(quote));
};
};
Ok(())
}

View file

@ -107,6 +107,7 @@ pub struct Post {
pub actions: Option<PostActions>,
pub in_reply_to: Option<Box<Post>>,
pub repost_of: Option<Box<Post>>,
pub quote: Option<Box<Post>>,
}
impl Post {
@ -161,6 +162,7 @@ impl Post {
actions: None,
in_reply_to: None,
repost_of: None,
quote: None,
};
Ok(post)
}
@ -203,6 +205,7 @@ impl Default for Post {
actions: None,
in_reply_to: None,
repost_of: None,
quote: None,
}
}
}