From aa4b91e0fcc24333a2a7895de20b2600cdf2d570 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 21 Oct 2024 10:20:22 +0200 Subject: [PATCH] Revert "Always save remote image data (#4875)" This reverts commit 9b5a9ee7be290d29b74329cc7fd66d50d8beb33d. --- crates/api_common/src/utils.rs | 11 +++--- crates/db_schema/src/impls/images.rs | 49 ++++++++++++++++++--------- crates/db_schema/src/source/images.rs | 7 ++++ 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 548765d6c..63967d198 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -17,7 +17,7 @@ use lemmy_db_schema::{ community::{Community, CommunityModerator, CommunityUpdateForm}, community_block::CommunityBlock, email_verification::{EmailVerification, EmailVerificationForm}, - images::{ImageDetails, RemoteImage}, + images::RemoteImage, instance::Instance, instance_block::InstanceBlock, local_site::LocalSite, @@ -971,7 +971,6 @@ pub async fn process_markdown( if context.settings().pictrs_config()?.image_mode() == PictrsImageMode::ProxyAllImages { let (text, links) = markdown_rewrite_image_links(text); - RemoteImage::create(&mut context.pool(), links.clone()).await?; // Create images and image detail rows for link in links { @@ -981,7 +980,7 @@ pub async fn process_markdown( let proxied = build_proxied_image_url(&link, &context.settings().get_protocol_and_hostname())?; let details_form = details.build_image_details_form(&proxied); - ImageDetails::create(&mut context.pool(), &details_form).await?; + RemoteImage::create(&mut context.pool(), &details_form).await?; } } Ok(text) @@ -1017,15 +1016,13 @@ async fn proxy_image_link_internal( if link.domain() == Some(&context.settings().hostname) { Ok(link.into()) } else if image_mode == PictrsImageMode::ProxyAllImages { - RemoteImage::create(&mut context.pool(), vec![link.clone()]).await?; - let proxied = build_proxied_image_url(&link, &context.settings().get_protocol_and_hostname())?; // This should fail softly, since pictrs might not even be running let details_res = fetch_pictrs_proxied_image_details(&link, context).await; if let Ok(details) = details_res { let details_form = details.build_image_details_form(&proxied); - ImageDetails::create(&mut context.pool(), &details_form).await?; + RemoteImage::create(&mut context.pool(), &details_form).await?; }; Ok(proxied.into()) @@ -1171,7 +1168,7 @@ mod tests { assert!( RemoteImage::validate(&mut context.pool(), remote_image.into()) .await - .is_ok() + .is_err() ); } } diff --git a/crates/db_schema/src/impls/images.rs b/crates/db_schema/src/impls/images.rs index 8ded98e41..547bfc4e2 100644 --- a/crates/db_schema/src/impls/images.rs +++ b/crates/db_schema/src/impls/images.rs @@ -1,7 +1,14 @@ use crate::{ newtypes::DbUrl, schema::{image_details, local_image, remote_image}, - source::images::{ImageDetails, ImageDetailsForm, LocalImage, LocalImageForm, RemoteImage}, + source::images::{ + ImageDetails, + ImageDetailsForm, + LocalImage, + LocalImageForm, + RemoteImage, + RemoteImageForm, + }, utils::{get_conn, DbPool}, }; use diesel::{ @@ -13,8 +20,7 @@ use diesel::{ NotFound, QueryDsl, }; -use diesel_async::RunQueryDsl; -use url::Url; +use diesel_async::{AsyncPgConnection, RunQueryDsl}; impl LocalImage { pub async fn create( @@ -32,7 +38,7 @@ impl LocalImage { .get_result::(conn) .await; - ImageDetails::create(&mut conn.into(), image_details_form).await?; + ImageDetails::create(conn, image_details_form).await?; local_insert }) as _ @@ -54,16 +60,26 @@ impl LocalImage { } impl RemoteImage { - pub async fn create(pool: &mut DbPool<'_>, links: Vec) -> Result { + pub async fn create(pool: &mut DbPool<'_>, form: &ImageDetailsForm) -> Result { let conn = &mut get_conn(pool).await?; - let forms = links - .into_iter() - .map(|url| remote_image::dsl::link.eq::(url.into())) - .collect::>(); - insert_into(remote_image::table) - .values(forms) - .on_conflict_do_nothing() - .execute(conn) + conn + .build_transaction() + .run(|conn| { + Box::pin(async move { + let remote_image_form = RemoteImageForm { + link: form.link.clone(), + }; + let remote_insert = insert_into(remote_image::table) + .values(remote_image_form) + .on_conflict_do_nothing() + .execute(conn) + .await; + + ImageDetails::create(conn, form).await?; + + remote_insert + }) as _ + }) .await } @@ -84,9 +100,10 @@ impl RemoteImage { } impl ImageDetails { - pub async fn create(pool: &mut DbPool<'_>, form: &ImageDetailsForm) -> Result { - let conn = &mut get_conn(pool).await?; - + pub(crate) async fn create( + conn: &mut AsyncPgConnection, + form: &ImageDetailsForm, + ) -> Result { insert_into(image_details::table) .values(form) .on_conflict_do_nothing() diff --git a/crates/db_schema/src/source/images.rs b/crates/db_schema/src/source/images.rs index 22f5e6eb4..0dea4b84f 100644 --- a/crates/db_schema/src/source/images.rs +++ b/crates/db_schema/src/source/images.rs @@ -51,6 +51,13 @@ pub struct RemoteImage { pub published: DateTime, } +#[derive(Debug, Clone)] +#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] +#[cfg_attr(feature = "full", diesel(table_name = remote_image))] +pub struct RemoteImageForm { + pub link: DbUrl, +} + #[skip_serializing_none] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]