From: Felix Ableitner Date: Thu, 18 May 2023 11:22:05 +0000 (+0200) Subject: Dont refetch post url metadata when post is received again X-Git-Url: http://these/git/%22https:/nerdica.net/photo/contact/80/%24%7Bargs.pageFn.jump%20n%7D?a=commitdiff_plain;h=436a293f382e132763de80f78b9475ee1b5e0ea5;p=lemmy.git Dont refetch post url metadata when post is received again --- diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 7bc93339..5b1583a2 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -185,6 +185,9 @@ impl Object for ApubPost { name = name.chars().take(MAX_TITLE_LENGTH).collect(); } + // read existing, local post if any (for generating mod log) + let old_post = page.id.dereference_local(context).await; + let form = if !page.is_mod_action(context).await? { let first_attachment = page.attachment.into_iter().map(Attachment::url).next(); let url = if first_attachment.is_some() { @@ -195,10 +198,13 @@ impl Object for ApubPost { } else { None }; - let (metadata_res, thumbnail_url) = if let Some(url) = &url { - fetch_site_data(context.client(), context.settings(), Some(url)).await - } else { - (None, page.image.map(|i| i.url.into())) + // Only fetch metadata if the post has a url and was not seen previously. We dont want to + // waste resources by fetching metadata for the same post multiple times. + let (metadata_res, thumbnail_url) = match &url { + Some(url) if old_post.is_err() => { + fetch_site_data(context.client(), context.settings(), Some(url)).await + } + _ => (None, page.image.map(|i| i.url.into())), }; let (embed_title, embed_description, embed_video_url) = metadata_res .map(|u| (u.title, u.description, u.embed_video_url)) @@ -245,8 +251,6 @@ impl Object for ApubPost { .updated(page.updated.map(|u| u.naive_local())) .build() }; - // read existing, local post if any (for generating mod log) - let old_post = page.id.dereference_local(context).await; let post = Post::create(context.pool(), &form).await?;