Update federated posts to not cache sensitive images if not allow by local site (#3253)

* Update federated posts to not cache sensitive images if not allow by local site

* Refactor thumbnail match to simplify logic
This commit is contained in:
Lemmus.org 2023-06-30 03:42:42 -07:00 committed by GitHub
parent fcc010b5dc
commit 810762762f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 7 deletions

View file

@ -193,6 +193,7 @@ pub async fn fetch_site_data(
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
settings: &Settings, settings: &Settings,
url: Option<&Url>, url: Option<&Url>,
include_image: bool,
) -> (Option<SiteMetadata>, Option<DbUrl>) { ) -> (Option<SiteMetadata>, Option<DbUrl>) {
match &url { match &url {
Some(url) => { Some(url) => {
@ -200,6 +201,9 @@ pub async fn fetch_site_data(
// Ignore errors, since it may be an image, or not have the data. // Ignore errors, since it may be an image, or not have the data.
// Warning, this may ignore SSL errors // Warning, this may ignore SSL errors
let metadata_option = fetch_site_metadata(client, url).await.ok(); let metadata_option = fetch_site_metadata(client, url).await.ok();
if !include_image {
return (metadata_option, None);
}
let missing_pictrs_file = let missing_pictrs_file =
|r: PictrsResponse| r.files.first().expect("missing pictrs file").file.clone(); |r: PictrsResponse| r.files.first().expect("missing pictrs file").file.clone();

View file

@ -428,6 +428,13 @@ pub fn local_site_opt_to_slur_regex(local_site: &Option<LocalSite>) -> Option<Re
.unwrap_or(None) .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( pub fn send_application_approved_email(
user: &LocalUserView, user: &LocalUserView,
settings: &Settings, settings: &Settings,

View file

@ -79,7 +79,7 @@ impl PerformCrud for CreatePost {
// Fetch post links and pictrs cached image // Fetch post links and pictrs cached image
let (metadata_res, thumbnail_url) = 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 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))
.unwrap_or_default(); .unwrap_or_default();

View file

@ -69,7 +69,7 @@ impl PerformCrud for EditPost {
// Fetch post links and Pictrs cached image // Fetch post links and Pictrs cached image
let data_url = data.url.as_ref(); let data_url = data.url.as_ref();
let (metadata_res, thumbnail_url) = 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 let (embed_title, embed_description, embed_video_url) = metadata_res
.map(|u| (Some(u.title), Some(u.description), Some(u.embed_video_url))) .map(|u| (Some(u.title), Some(u.description), Some(u.embed_video_url)))
.unwrap_or_default(); .unwrap_or_default();

View file

@ -25,7 +25,7 @@ use html2md::parse_html;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
request::fetch_site_data, 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::{ use lemmy_db_schema::{
self, self,
@ -197,18 +197,33 @@ impl Object for ApubPost {
} else { } else {
None 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 // 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. // 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() => { 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 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))
.unwrap_or_default(); .unwrap_or_default();
let local_site = LocalSite::read(context.pool()).await.ok();
let slur_regex = &local_site_opt_to_slur_regex(&local_site); let slur_regex = &local_site_opt_to_slur_regex(&local_site);
let body_slurs_removed = let body_slurs_removed =