Dont refetch post url metadata when post is received again

This commit is contained in:
Felix Ableitner 2023-05-18 13:22:05 +02:00 committed by Nutomic
parent df57795595
commit 436a293f38

View File

@ -185,6 +185,9 @@ impl Object for ApubPost {
name = name.chars().take(MAX_TITLE_LENGTH).collect(); 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 form = if !page.is_mod_action(context).await? {
let first_attachment = page.attachment.into_iter().map(Attachment::url).next(); let first_attachment = page.attachment.into_iter().map(Attachment::url).next();
let url = if first_attachment.is_some() { let url = if first_attachment.is_some() {
@ -195,10 +198,13 @@ impl Object for ApubPost {
} else { } else {
None None
}; };
let (metadata_res, thumbnail_url) = if let Some(url) = &url { // Only fetch metadata if the post has a url and was not seen previously. We dont want to
fetch_site_data(context.client(), context.settings(), Some(url)).await // waste resources by fetching metadata for the same post multiple times.
} else { let (metadata_res, thumbnail_url) = match &url {
(None, page.image.map(|i| i.url.into())) 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 let (embed_title, embed_description, embed_video_url) = metadata_res
.map(|u| (u.title, u.description, u.embed_video_url)) .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())) .updated(page.updated.map(|u| u.naive_local()))
.build() .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?; let post = Post::create(context.pool(), &form).await?;