Save post CID and attachment CID in a single database transaction

This commit is contained in:
silverpill 2022-05-12 12:00:52 +00:00
parent 6c675c11c4
commit e2d2e2e5de
2 changed files with 14 additions and 12 deletions

View file

@ -22,7 +22,6 @@ use crate::ipfs::store as ipfs_store;
use crate::ipfs::posts::PostMetadata;
use crate::ipfs::utils::get_ipfs_url;
use crate::mastodon_api::oauth::auth::get_current_user;
use crate::models::attachments::queries::set_attachment_ipfs_cid;
use crate::models::posts::helpers::can_view_post;
use crate::models::posts::mentions::{find_mentioned_profiles, replace_mentions};
use crate::models::posts::tags::{find_tags, replace_tags};
@ -388,7 +387,7 @@ async fn make_permanent(
db_pool: web::Data<Pool>,
status_id: web::Path<Uuid>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let mut post = get_post_by_id(db_client, &status_id).await?;
if post.ipfs_cid.is_some() {
@ -401,6 +400,7 @@ async fn make_permanent(
let ipfs_api_url = config.ipfs_api_url.as_ref()
.ok_or(HttpError::NotSupported)?;
let mut attachments = vec![];
for attachment in post.attachments.iter_mut() {
// Add attachment to IPFS
let image_path = config.media_dir().join(&attachment.file_name);
@ -408,7 +408,8 @@ async fn make_permanent(
.map_err(|_| HttpError::InternalError)?;
let image_cid = ipfs_store::add(ipfs_api_url, image_data).await
.map_err(|_| HttpError::InternalError)?;
attachment.ipfs_cid = Some(image_cid);
attachment.ipfs_cid = Some(image_cid.clone());
attachments.push((attachment.id, image_cid));
};
let post_url = post.get_object_id(&config.instance_url());
let maybe_post_image_cid = post.attachments.first()
@ -426,13 +427,7 @@ async fn make_permanent(
let post_metadata_cid = ipfs_store::add(ipfs_api_url, post_metadata_json).await
.map_err(|_| HttpError::InternalError)?;
// Update post
for attachment in &post.attachments {
let image_cid = attachment.ipfs_cid.as_ref()
.ok_or(HttpError::InternalError)?;
set_attachment_ipfs_cid(db_client, &attachment.id, image_cid).await?;
};
set_post_ipfs_cid(db_client, &post.id, &post_metadata_cid).await?;
set_post_ipfs_cid(db_client, &post.id, &post_metadata_cid, attachments).await?;
post.ipfs_cid = Some(post_metadata_cid);
let status = build_status(

View file

@ -7,6 +7,7 @@ use uuid::Uuid;
use crate::database::catch_unique_violation;
use crate::database::query_macro::query;
use crate::errors::DatabaseError;
use crate::models::attachments::queries::set_attachment_ipfs_cid;
use crate::models::attachments::types::DbMediaAttachment;
use crate::models::cleanup::{
find_orphaned_files,
@ -750,11 +751,13 @@ pub async fn update_repost_count(
}
pub async fn set_post_ipfs_cid(
db_client: &impl GenericClient,
db_client: &mut impl GenericClient,
post_id: &Uuid,
ipfs_cid: &str,
attachments: Vec<(Uuid, String)>,
) -> Result<(), DatabaseError> {
let updated_count = db_client.execute(
let transaction = db_client.transaction().await?;
let updated_count = transaction.execute(
"
UPDATE post
SET ipfs_cid = $1
@ -767,6 +770,10 @@ pub async fn set_post_ipfs_cid(
if updated_count == 0 {
return Err(DatabaseError::NotFound("post"));
};
for (attachment_id, cid) in attachments {
set_attachment_ipfs_cid(&transaction, &attachment_id, &cid).await?;
};
transaction.commit().await?;
Ok(())
}