Don't fetch metadata in background for local API requests.

This commit is contained in:
Dessalines 2024-05-06 08:34:40 -04:00
parent a38830631d
commit dde7a44e57
4 changed files with 93 additions and 71 deletions

View file

@ -66,22 +66,21 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
})
}
/// Generate post thumbnail in background task, because some sites can be very slow to respond.
/// Generates and saves a post thumbnail and metadata.
///
/// Takes a callback to generate a send activity task, so that post can be federated with metadata.
///
/// TODO: `federated_thumbnail` param can be removed once we federate full metadata and can
/// write it to db directly, without calling this function.
/// https://github.com/LemmyNet/lemmy/issues/4598
pub fn generate_post_link_metadata(
pub async fn generate_post_link_metadata(
post: Post,
custom_thumbnail: Option<Url>,
federated_thumbnail: Option<Url>,
send_activity: impl FnOnce(Post) -> Option<SendActivityData> + Send + 'static,
local_site: Option<LocalSite>,
context: Data<LemmyContext>,
) {
spawn_try_task(async move {
) -> LemmyResult<()> {
let metadata = match &post.url {
Some(url) => fetch_link_metadata(url, &context).await.unwrap_or_default(),
_ => Default::default(),
@ -146,7 +145,28 @@ pub fn generate_post_link_metadata(
ActivityChannel::submit_activity(send_activity, &context).await?;
}
Ok(())
});
}
/// Generates a post thumbnail in background task, because some sites can be very slow to respond.
pub fn generate_post_link_metadata_background(
post: Post,
custom_thumbnail: Option<Url>,
federated_thumbnail: Option<Url>,
send_activity: impl FnOnce(Post) -> Option<SendActivityData> + Send + 'static,
local_site: Option<LocalSite>,
context: Data<LemmyContext>,
) {
spawn_try_task(async move {
generate_post_link_metadata(
post,
custom_thumbnail,
federated_thumbnail,
send_activity,
local_site,
context,
)
.await
})
}
/// Extract site metadata from HTML Opengraph attributes.

View file

@ -161,7 +161,8 @@ pub async fn create_post(
|post| Some(SendActivityData::CreatePost(post)),
Some(local_site),
context.reset_request_count(),
);
)
.await?;
// They like their own post by default
let person_id = local_user_view.person.id;

View file

@ -116,7 +116,8 @@ pub async fn update_post(
|post| Some(SendActivityData::UpdatePost(post)),
Some(local_site),
context.reset_request_count(),
);
)
.await?;
build_post_response(
context.deref(),

View file

@ -24,7 +24,7 @@ use chrono::{DateTime, Utc};
use html2text::{from_read_with_decorator, render::text_renderer::TrivialDecorator};
use lemmy_api_common::{
context::LemmyContext,
request::generate_post_link_metadata,
request::generate_post_link_metadata_background,
utils::{
get_url_blocklist,
local_site_opt_to_slur_regex,
@ -278,7 +278,7 @@ impl Object for ApubPost {
let timestamp = page.updated.or(page.published).unwrap_or_else(naive_now);
let post = Post::insert_apub(&mut context.pool(), timestamp, &form).await?;
generate_post_link_metadata(
generate_post_link_metadata_background(
post.clone(),
None,
page.image.map(|i| i.url),