mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-27 06:41:18 +00:00
Don't fetch metadata in background for local API requests.
This commit is contained in:
parent
a38830631d
commit
dde7a44e57
4 changed files with 93 additions and 71 deletions
|
@ -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.
|
/// 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
|
/// TODO: `federated_thumbnail` param can be removed once we federate full metadata and can
|
||||||
/// write it to db directly, without calling this function.
|
/// write it to db directly, without calling this function.
|
||||||
/// https://github.com/LemmyNet/lemmy/issues/4598
|
/// https://github.com/LemmyNet/lemmy/issues/4598
|
||||||
pub fn generate_post_link_metadata(
|
pub async fn generate_post_link_metadata(
|
||||||
post: Post,
|
post: Post,
|
||||||
custom_thumbnail: Option<Url>,
|
custom_thumbnail: Option<Url>,
|
||||||
federated_thumbnail: Option<Url>,
|
federated_thumbnail: Option<Url>,
|
||||||
send_activity: impl FnOnce(Post) -> Option<SendActivityData> + Send + 'static,
|
send_activity: impl FnOnce(Post) -> Option<SendActivityData> + Send + 'static,
|
||||||
local_site: Option<LocalSite>,
|
local_site: Option<LocalSite>,
|
||||||
context: Data<LemmyContext>,
|
context: Data<LemmyContext>,
|
||||||
) {
|
) -> LemmyResult<()> {
|
||||||
spawn_try_task(async move {
|
|
||||||
let metadata = match &post.url {
|
let metadata = match &post.url {
|
||||||
Some(url) => fetch_link_metadata(url, &context).await.unwrap_or_default(),
|
Some(url) => fetch_link_metadata(url, &context).await.unwrap_or_default(),
|
||||||
_ => Default::default(),
|
_ => Default::default(),
|
||||||
|
@ -146,7 +145,28 @@ pub fn generate_post_link_metadata(
|
||||||
ActivityChannel::submit_activity(send_activity, &context).await?;
|
ActivityChannel::submit_activity(send_activity, &context).await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
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.
|
/// Extract site metadata from HTML Opengraph attributes.
|
||||||
|
|
|
@ -161,7 +161,8 @@ pub async fn create_post(
|
||||||
|post| Some(SendActivityData::CreatePost(post)),
|
|post| Some(SendActivityData::CreatePost(post)),
|
||||||
Some(local_site),
|
Some(local_site),
|
||||||
context.reset_request_count(),
|
context.reset_request_count(),
|
||||||
);
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// They like their own post by default
|
// They like their own post by default
|
||||||
let person_id = local_user_view.person.id;
|
let person_id = local_user_view.person.id;
|
||||||
|
|
|
@ -116,7 +116,8 @@ pub async fn update_post(
|
||||||
|post| Some(SendActivityData::UpdatePost(post)),
|
|post| Some(SendActivityData::UpdatePost(post)),
|
||||||
Some(local_site),
|
Some(local_site),
|
||||||
context.reset_request_count(),
|
context.reset_request_count(),
|
||||||
);
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
build_post_response(
|
build_post_response(
|
||||||
context.deref(),
|
context.deref(),
|
||||||
|
|
|
@ -24,7 +24,7 @@ use chrono::{DateTime, Utc};
|
||||||
use html2text::{from_read_with_decorator, render::text_renderer::TrivialDecorator};
|
use html2text::{from_read_with_decorator, render::text_renderer::TrivialDecorator};
|
||||||
use lemmy_api_common::{
|
use lemmy_api_common::{
|
||||||
context::LemmyContext,
|
context::LemmyContext,
|
||||||
request::generate_post_link_metadata,
|
request::generate_post_link_metadata_background,
|
||||||
utils::{
|
utils::{
|
||||||
get_url_blocklist,
|
get_url_blocklist,
|
||||||
local_site_opt_to_slur_regex,
|
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 timestamp = page.updated.or(page.published).unwrap_or_else(naive_now);
|
||||||
let post = Post::insert_apub(&mut context.pool(), timestamp, &form).await?;
|
let post = Post::insert_apub(&mut context.pool(), timestamp, &form).await?;
|
||||||
|
|
||||||
generate_post_link_metadata(
|
generate_post_link_metadata_background(
|
||||||
post.clone(),
|
post.clone(),
|
||||||
None,
|
None,
|
||||||
page.image.map(|i| i.url),
|
page.image.map(|i| i.url),
|
||||||
|
|
Loading…
Reference in a new issue