Refactor Status API views

This commit is contained in:
silverpill 2022-05-06 15:03:43 +00:00
parent 54c32c5f00
commit e6e7e6f42a
9 changed files with 134 additions and 104 deletions

View file

@ -21,10 +21,7 @@ use crate::ethereum::identity::{
}; };
use crate::ethereum::subscriptions::create_subscription_signature; use crate::ethereum::subscriptions::create_subscription_signature;
use crate::mastodon_api::oauth::auth::get_current_user; use crate::mastodon_api::oauth::auth::get_current_user;
use crate::models::posts::helpers::{ use crate::mastodon_api::statuses::helpers::build_status_list;
get_actions_for_posts,
get_reposted_posts,
};
use crate::mastodon_api::statuses::types::Status; use crate::mastodon_api::statuses::types::Status;
use crate::models::posts::queries::get_posts_by_author; use crate::models::posts::queries::get_posts_by_author;
use crate::models::profiles::queries::{ use crate::models::profiles::queries::{
@ -442,7 +439,7 @@ async fn get_account_statuses(
}; };
let profile = get_profile_by_id(db_client, &account_id).await?; let profile = get_profile_by_id(db_client, &account_id).await?;
// Include reposts but not replies // Include reposts but not replies
let mut posts = get_posts_by_author( let posts = get_posts_by_author(
db_client, db_client,
&profile.id, &profile.id,
maybe_current_user.as_ref().map(|user| &user.id), maybe_current_user.as_ref().map(|user| &user.id),
@ -451,17 +448,12 @@ async fn get_account_statuses(
query_params.max_id, query_params.max_id,
query_params.limit, query_params.limit,
).await?; ).await?;
get_reposted_posts(db_client, posts.iter_mut().collect()).await?; let statuses = build_status_list(
if let Some(user) = maybe_current_user {
get_actions_for_posts(
db_client, db_client,
&user.id, &config.instance_url(),
posts.iter_mut().collect(), maybe_current_user.as_ref(),
posts,
).await?; ).await?;
}
let statuses: Vec<Status> = posts.into_iter()
.map(|post| Status::from_post(post, &config.instance_url()))
.collect();
Ok(HttpResponse::Ok().json(statuses)) Ok(HttpResponse::Ok().json(statuses))
} }

View file

@ -10,11 +10,8 @@ use crate::activitypub::fetcher::helpers::{
use crate::config::Config; use crate::config::Config;
use crate::errors::{ValidationError, HttpError}; use crate::errors::{ValidationError, HttpError};
use crate::mastodon_api::accounts::types::Account; use crate::mastodon_api::accounts::types::Account;
use crate::mastodon_api::statuses::types::Status; use crate::mastodon_api::statuses::helpers::build_status_list;
use crate::models::posts::helpers::{ use crate::models::posts::helpers::can_view_post;
can_view_post,
get_actions_for_posts,
};
use crate::models::posts::types::Post; use crate::models::posts::types::Post;
use crate::models::profiles::queries::{ use crate::models::profiles::queries::{
search_profile, search_profile,
@ -158,9 +155,11 @@ pub async fn search(
let accounts: Vec<Account> = profiles.into_iter() let accounts: Vec<Account> = profiles.into_iter()
.map(|profile| Account::from_profile(profile, &config.instance_url())) .map(|profile| Account::from_profile(profile, &config.instance_url()))
.collect(); .collect();
get_actions_for_posts(db_client, &current_user.id, posts.iter_mut().collect()).await?; let statuses = build_status_list(
let statuses: Vec<Status> = posts.into_iter() db_client,
.map(|post| Status::from_post(post, &config.instance_url())) &config.instance_url(),
.collect(); Some(current_user),
posts,
).await?;
Ok(SearchResults { accounts, statuses }) Ok(SearchResults { accounts, statuses })
} }

View file

@ -2,10 +2,15 @@ use tokio_postgres::GenericClient;
use crate::activitypub::actor::Actor; use crate::activitypub::actor::Actor;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::posts::helpers::{
add_user_actions,
add_reposted_posts,
};
use crate::models::posts::queries::get_post_author; use crate::models::posts::queries::get_post_author;
use crate::models::posts::types::{Post, Visibility}; use crate::models::posts::types::{Post, Visibility};
use crate::models::relationships::queries::{get_followers, get_subscribers}; use crate::models::relationships::queries::{get_followers, get_subscribers};
use crate::models::users::types::User; use crate::models::users::types::User;
use super::types::Status;
pub async fn get_note_recipients( pub async fn get_note_recipients(
db_client: &impl GenericClient, db_client: &impl GenericClient,
@ -77,3 +82,35 @@ pub async fn get_announce_recipients(
}; };
Ok(Audience { recipients, primary_recipient }) Ok(Audience { recipients, primary_recipient })
} }
/// Load related objects and build status for API response
pub async fn build_status(
db_client: &impl GenericClient,
instance_url: &str,
user: Option<&User>,
mut post: Post,
) -> Result<Status, DatabaseError> {
add_reposted_posts(db_client, vec![&mut post]).await?;
if let Some(user) = user {
add_user_actions(db_client, &user.id, vec![&mut post]).await?;
};
let status = Status::from_post(post, instance_url);
Ok(status)
}
pub async fn build_status_list(
db_client: &impl GenericClient,
instance_url: &str,
user: Option<&User>,
mut posts: Vec<Post>,
) -> Result<Vec<Status>, DatabaseError> {
add_reposted_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?;
};
let statuses: Vec<Status> = posts
.into_iter()
.map(|post| Status::from_post(post, instance_url))
.collect();
Ok(statuses)
}

View file

@ -1,3 +1,3 @@
mod helpers; pub mod helpers;
pub mod types; pub mod types;
pub mod views; pub mod views;

View file

@ -26,10 +26,6 @@ use crate::models::attachments::queries::set_attachment_ipfs_cid;
use crate::models::posts::helpers::can_view_post; use crate::models::posts::helpers::can_view_post;
use crate::models::posts::mentions::{find_mentioned_profiles, replace_mentions}; use crate::models::posts::mentions::{find_mentioned_profiles, replace_mentions};
use crate::models::posts::tags::{find_tags, replace_tags}; use crate::models::posts::tags::{find_tags, replace_tags};
use crate::models::posts::helpers::{
get_actions_for_posts,
get_reposted_posts,
};
use crate::models::posts::queries::{ use crate::models::posts::queries::{
create_post, create_post,
get_post_by_id, get_post_by_id,
@ -44,6 +40,8 @@ use crate::models::reactions::queries::{
delete_reaction, delete_reaction,
}; };
use super::helpers::{ use super::helpers::{
build_status,
build_status_list,
get_announce_recipients, get_announce_recipients,
get_like_recipients, get_like_recipients,
get_note_recipients, get_note_recipients,
@ -128,6 +126,7 @@ async fn create_status(
); );
let recipients = get_note_recipients(db_client, &current_user, &post).await?; let recipients = get_note_recipients(db_client, &current_user, &post).await?;
deliver_activity(&config, &current_user, activity, recipients); deliver_activity(&config, &current_user, activity, recipients);
let status = Status::from_post(post, &instance.url()); let status = Status::from_post(post, &instance.url());
Ok(HttpResponse::Created().json(status)) Ok(HttpResponse::Created().json(status))
} }
@ -144,15 +143,16 @@ async fn get_status(
Some(auth) => Some(get_current_user(db_client, auth.token()).await?), Some(auth) => Some(get_current_user(db_client, auth.token()).await?),
None => None, None => None,
}; };
let mut post = get_post_by_id(db_client, &status_id).await?; let post = get_post_by_id(db_client, &status_id).await?;
if !can_view_post(db_client, maybe_current_user.as_ref(), &post).await? { if !can_view_post(db_client, maybe_current_user.as_ref(), &post).await? {
return Err(HttpError::NotFoundError("post")); return Err(HttpError::NotFoundError("post"));
}; };
get_reposted_posts(db_client, vec![&mut post]).await?; let status = build_status(
if let Some(user) = maybe_current_user { db_client,
get_actions_for_posts(db_client, &user.id, vec![&mut post]).await?; &config.instance_url(),
} maybe_current_user.as_ref(),
let status = Status::from_post(post, &config.instance_url()); post,
).await?;
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }
@ -197,23 +197,17 @@ async fn get_context(
Some(auth) => Some(get_current_user(db_client, auth.token()).await?), Some(auth) => Some(get_current_user(db_client, auth.token()).await?),
None => None, None => None,
}; };
let mut posts = get_thread( let posts = get_thread(
db_client, db_client,
&status_id, &status_id,
maybe_current_user.as_ref().map(|user| &user.id), maybe_current_user.as_ref().map(|user| &user.id),
).await?; ).await?;
get_reposted_posts(db_client, posts.iter_mut().collect()).await?; let statuses = build_status_list(
if let Some(user) = maybe_current_user {
get_actions_for_posts(
db_client, db_client,
&user.id, &config.instance_url(),
posts.iter_mut().collect(), maybe_current_user.as_ref(),
posts,
).await?; ).await?;
}
let statuses: Vec<Status> = posts
.into_iter()
.map(|post| Status::from_post(post, &config.instance_url()))
.collect();
Ok(HttpResponse::Ok().json(statuses)) Ok(HttpResponse::Ok().json(statuses))
} }
@ -240,8 +234,6 @@ async fn favourite(
Err(DatabaseError::AlreadyExists(_)) => None, // post already favourited Err(DatabaseError::AlreadyExists(_)) => None, // post already favourited
Err(other_error) => return Err(other_error.into()), Err(other_error) => return Err(other_error.into()),
}; };
get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?;
if let Some(reaction) = maybe_reaction_created { if let Some(reaction) = maybe_reaction_created {
// Federate // Federate
@ -256,9 +248,14 @@ async fn favourite(
&primary_recipient, &primary_recipient,
); );
deliver_activity(&config, &current_user, activity, recipients); deliver_activity(&config, &current_user, activity, recipients);
} };
let status = Status::from_post(post, &config.instance_url()); let status = build_status(
db_client,
&config.instance_url(),
Some(&current_user),
post,
).await?;
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }
@ -282,8 +279,6 @@ async fn unfavourite(
Err(DatabaseError::NotFound(_)) => None, // post not favourited Err(DatabaseError::NotFound(_)) => None, // post not favourited
Err(other_error) => return Err(other_error.into()), Err(other_error) => return Err(other_error.into()),
}; };
get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?;
if let Some(reaction_id) = maybe_reaction_deleted { if let Some(reaction_id) = maybe_reaction_deleted {
// Federate // Federate
@ -298,7 +293,12 @@ async fn unfavourite(
deliver_activity(&config, &current_user, activity, recipients); deliver_activity(&config, &current_user, activity, recipients);
}; };
let status = Status::from_post(post, &config.instance_url()); let status = build_status(
db_client,
&config.instance_url(),
Some(&current_user),
post,
).await?;
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }
@ -319,10 +319,8 @@ async fn reblog(
repost_of_id: Some(status_id.into_inner()), repost_of_id: Some(status_id.into_inner()),
..Default::default() ..Default::default()
}; };
let mut repost = create_post(db_client, &current_user.id, repost_data).await?; let repost = create_post(db_client, &current_user.id, repost_data).await?;
post.repost_count += 1; post.repost_count += 1;
get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?;
// Federate // Federate
let Audience { recipients, .. } = let Audience { recipients, .. } =
@ -335,8 +333,12 @@ async fn reblog(
); );
deliver_activity(&config, &current_user, activity, recipients); deliver_activity(&config, &current_user, activity, recipients);
repost.repost_of = Some(Box::new(post)); let status = build_status(
let status = Status::from_post(repost, &config.instance_url()); db_client,
&config.instance_url(),
Some(&current_user),
repost,
).await?;
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }
@ -357,9 +359,7 @@ async fn unreblog(
let repost_id = reposts.first().ok_or(HttpError::NotFoundError("post"))?; let repost_id = reposts.first().ok_or(HttpError::NotFoundError("post"))?;
// Ignore returned data because reposts don't have attached files // Ignore returned data because reposts don't have attached files
delete_post(db_client, repost_id).await?; delete_post(db_client, repost_id).await?;
let mut post = get_post_by_id(db_client, &status_id).await?; let post = get_post_by_id(db_client, &status_id).await?;
get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?;
// Federate // Federate
let Audience { recipients, primary_recipient } = let Audience { recipients, primary_recipient } =
@ -372,7 +372,12 @@ async fn unreblog(
); );
deliver_activity(&config, &current_user, activity, recipients); deliver_activity(&config, &current_user, activity, recipients);
let status = Status::from_post(post, &config.instance_url()); let status = build_status(
db_client,
&config.instance_url(),
Some(&current_user),
post,
).await?;
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }
@ -426,9 +431,13 @@ async fn make_permanent(
// Update post // Update post
post.ipfs_cid = Some(post_metadata_cid); post.ipfs_cid = Some(post_metadata_cid);
update_post(db_client, &post).await?; update_post(db_client, &post).await?;
get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?; let status = build_status(
let status = Status::from_post(post, &config.instance_url()); db_client,
&config.instance_url(),
Some(&current_user),
post,
).await?;
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }
@ -481,9 +490,13 @@ async fn token_minted(
}; };
post.token_tx_id = Some(transaction_data.into_inner().transaction_id); post.token_tx_id = Some(transaction_data.into_inner().transaction_id);
update_post(db_client, &post).await?; update_post(db_client, &post).await?;
get_reposted_posts(db_client, vec![&mut post]).await?;
get_actions_for_posts(db_client, &current_user.id, vec![&mut post]).await?; let status = build_status(
let status = Status::from_post(post, &config.instance_url()); db_client,
&config.instance_url(),
Some(&current_user),
post,
).await?;
Ok(HttpResponse::Ok().json(status)) Ok(HttpResponse::Ok().json(status))
} }

View file

@ -6,11 +6,7 @@ use crate::config::Config;
use crate::database::{Pool, get_database_client}; use crate::database::{Pool, get_database_client};
use crate::errors::HttpError; use crate::errors::HttpError;
use crate::mastodon_api::oauth::auth::get_current_user; use crate::mastodon_api::oauth::auth::get_current_user;
use crate::mastodon_api::statuses::types::Status; use crate::mastodon_api::statuses::helpers::build_status_list;
use crate::models::posts::helpers::{
get_actions_for_posts,
get_reposted_posts,
};
use crate::models::posts::queries::{get_home_timeline, get_posts_by_tag}; use crate::models::posts::queries::{get_home_timeline, get_posts_by_tag};
use super::types::TimelineQueryParams; use super::types::TimelineQueryParams;
@ -23,22 +19,18 @@ async fn home_timeline(
) -> Result<HttpResponse, HttpError> { ) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?; let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?; let current_user = get_current_user(db_client, auth.token()).await?;
let mut posts = get_home_timeline( let posts = get_home_timeline(
db_client, db_client,
&current_user.id, &current_user.id,
query_params.max_id, query_params.max_id,
query_params.limit, query_params.limit,
).await?; ).await?;
get_reposted_posts(db_client, posts.iter_mut().collect()).await?; let statuses = build_status_list(
get_actions_for_posts(
db_client, db_client,
&current_user.id, &config.instance_url(),
posts.iter_mut().collect(), Some(&current_user),
posts,
).await?; ).await?;
let statuses: Vec<Status> = posts
.into_iter()
.map(|post| Status::from_post(post, &config.instance_url()))
.collect();
Ok(HttpResponse::Ok().json(statuses)) Ok(HttpResponse::Ok().json(statuses))
} }
@ -55,25 +47,19 @@ async fn hashtag_timeline(
Some(auth) => Some(get_current_user(db_client, auth.token()).await?), Some(auth) => Some(get_current_user(db_client, auth.token()).await?),
None => None, None => None,
}; };
let mut posts = get_posts_by_tag( let posts = get_posts_by_tag(
db_client, db_client,
&hashtag, &hashtag,
maybe_current_user.as_ref().map(|user| &user.id), maybe_current_user.as_ref().map(|user| &user.id),
query_params.max_id, query_params.max_id,
query_params.limit, query_params.limit,
).await?; ).await?;
get_reposted_posts(db_client, posts.iter_mut().collect()).await?; let statuses = build_status_list(
if let Some(user) = maybe_current_user {
get_actions_for_posts(
db_client, db_client,
&user.id, &config.instance_url(),
posts.iter_mut().collect(), maybe_current_user.as_ref(),
posts,
).await?; ).await?;
};
let statuses: Vec<Status> = posts
.into_iter()
.map(|post| Status::from_post(post, &config.instance_url()))
.collect();
Ok(HttpResponse::Ok().json(statuses)) Ok(HttpResponse::Ok().json(statuses))
} }

View file

@ -4,7 +4,7 @@ use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::posts::helpers::get_actions_for_posts; use crate::models::posts::helpers::add_user_actions;
use crate::models::posts::queries::{ use crate::models::posts::queries::{
RELATED_ATTACHMENTS, RELATED_ATTACHMENTS,
RELATED_MENTIONS, RELATED_MENTIONS,
@ -133,6 +133,6 @@ pub async fn get_notifications(
let posts = notifications.iter_mut() let posts = notifications.iter_mut()
.filter_map(|item| item.post.as_mut()) .filter_map(|item| item.post.as_mut())
.collect(); .collect();
get_actions_for_posts(db_client, recipient_id, posts).await?; add_user_actions(db_client, recipient_id, posts).await?;
Ok(notifications) Ok(notifications)
} }

View file

@ -9,13 +9,16 @@ use crate::models::users::types::User;
use super::queries::{get_posts, find_reposted_by_user}; use super::queries::{get_posts, find_reposted_by_user};
use super::types::{Post, PostActions, Visibility}; use super::types::{Post, PostActions, Visibility};
pub async fn get_reposted_posts( pub async fn add_reposted_posts(
db_client: &impl GenericClient, db_client: &impl GenericClient,
posts: Vec<&mut Post>, posts: Vec<&mut Post>,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
let reposted_ids: Vec<Uuid> = posts.iter() let reposted_ids: Vec<Uuid> = posts.iter()
.filter_map(|post| post.repost_of_id) .filter_map(|post| post.repost_of_id)
.collect(); .collect();
if reposted_ids.is_empty() {
return Ok(());
};
let reposted = get_posts(db_client, reposted_ids).await?; let reposted = get_posts(db_client, reposted_ids).await?;
for post in posts { for post in posts {
if let Some(ref repost_of_id) = post.repost_of_id { if let Some(ref repost_of_id) = post.repost_of_id {
@ -29,7 +32,7 @@ pub async fn get_reposted_posts(
Ok(()) Ok(())
} }
pub async fn get_actions_for_posts( pub async fn add_user_actions(
db_client: &impl GenericClient, db_client: &impl GenericClient,
user_id: &Uuid, user_id: &Uuid,
posts: Vec<&mut Post>, posts: Vec<&mut Post>,

View file

@ -134,7 +134,7 @@ pub async fn get_profile_by_actor_id(
pub async fn get_profile_by_acct( pub async fn get_profile_by_acct(
db_client: &impl GenericClient, db_client: &impl GenericClient,
account_uri: &str, acct: &str,
) -> Result<DbActorProfile, DatabaseError> { ) -> Result<DbActorProfile, DatabaseError> {
let result = db_client.query_opt( let result = db_client.query_opt(
" "
@ -142,7 +142,7 @@ pub async fn get_profile_by_acct(
FROM actor_profile FROM actor_profile
WHERE actor_profile.acct = $1 WHERE actor_profile.acct = $1
", ",
&[&account_uri], &[&acct],
).await?; ).await?;
let profile = match result { let profile = match result {
Some(row) => row.try_get("actor_profile")?, Some(row) => row.try_get("actor_profile")?,