* Update federated posts to not cache sensitive images if not allow by local site
* Refactor thumbnail match to simplify logic
client: &ClientWithMiddleware,
settings: &Settings,
url: Option<&Url>,
+ include_image: bool,
) -> (Option<SiteMetadata>, Option<DbUrl>) {
match &url {
Some(url) => {
// Ignore errors, since it may be an image, or not have the data.
// Warning, this may ignore SSL errors
let metadata_option = fetch_site_metadata(client, url).await.ok();
+ if !include_image {
+ return (metadata_option, None);
+ }
let missing_pictrs_file =
|r: PictrsResponse| r.files.first().expect("missing pictrs file").file.clone();
.unwrap_or(None)
}
+pub fn local_site_opt_to_sensitive(local_site: &Option<LocalSite>) -> bool {
+ local_site
+ .as_ref()
+ .map(|site| site.enable_nsfw)
+ .unwrap_or(false)
+}
+
pub fn send_application_approved_email(
user: &LocalUserView,
settings: &Settings,
// Fetch post links and pictrs cached image
let (metadata_res, thumbnail_url) =
- fetch_site_data(context.client(), context.settings(), data_url).await;
+ fetch_site_data(context.client(), context.settings(), data_url, true).await;
let (embed_title, embed_description, embed_video_url) = metadata_res
.map(|u| (u.title, u.description, u.embed_video_url))
.unwrap_or_default();
// Fetch post links and Pictrs cached image
let data_url = data.url.as_ref();
let (metadata_res, thumbnail_url) =
- fetch_site_data(context.client(), context.settings(), data_url).await;
+ fetch_site_data(context.client(), context.settings(), data_url, true).await;
let (embed_title, embed_description, embed_video_url) = metadata_res
.map(|u| (Some(u.title), Some(u.description), Some(u.embed_video_url)))
.unwrap_or_default();
use lemmy_api_common::{
context::LemmyContext,
request::fetch_site_data,
- utils::{is_mod_or_admin, local_site_opt_to_slur_regex},
+ utils::{is_mod_or_admin, local_site_opt_to_sensitive, local_site_opt_to_slur_regex},
};
use lemmy_db_schema::{
self,
} else {
None
};
+
+ let local_site = LocalSite::read(context.pool()).await.ok();
+ let allow_sensitive = local_site_opt_to_sensitive(&local_site);
+ let page_is_sensitive = page.sensitive.unwrap_or(false);
+ let include_image = allow_sensitive || !page_is_sensitive;
+
// 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 {
+ // Additionally, only fetch image if content is not sensitive or is allowed on local site.
+ let (metadata_res, thumbnail) = match &url {
Some(url) if old_post.is_err() => {
- fetch_site_data(context.client(), context.settings(), Some(url)).await
+ fetch_site_data(
+ context.client(),
+ context.settings(),
+ Some(url),
+ include_image,
+ )
+ .await
}
- _ => (None, page.image.map(|i| i.url.into())),
+ _ => (None, None),
};
+ // If no image was included with metadata, use post image instead when available.
+ let thumbnail_url = thumbnail.or_else(|| 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))
.unwrap_or_default();
- let local_site = LocalSite::read(context.pool()).await.ok();
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
let body_slurs_removed =