Merge branch 'main' into mod_admin_ordering

This commit is contained in:
Dessalines 2024-07-15 13:01:18 -04:00
commit 85fe830d5e
4 changed files with 111 additions and 24 deletions

View file

@ -81,6 +81,8 @@ pub struct GetPosts {
pub show_hidden: Option<bool>, pub show_hidden: Option<bool>,
/// If true, then show the read posts (even if your user setting is to hide them) /// If true, then show the read posts (even if your user setting is to hide them)
pub show_read: Option<bool>, pub show_read: Option<bool>,
/// If true, then show the nsfw posts (even if your user setting is to hide them)
pub show_nsfw: Option<bool>,
pub page_cursor: Option<PaginationCursor>, pub page_cursor: Option<PaginationCursor>,
} }

View file

@ -43,6 +43,7 @@ pub async fn list_posts(
let saved_only = data.saved_only; let saved_only = data.saved_only;
let show_hidden = data.show_hidden; let show_hidden = data.show_hidden;
let show_read = data.show_read; let show_read = data.show_read;
let show_nsfw = data.show_nsfw;
let liked_only = data.liked_only; let liked_only = data.liked_only;
let disliked_only = data.disliked_only; let disliked_only = data.disliked_only;
@ -84,6 +85,7 @@ pub async fn list_posts(
limit, limit,
show_hidden, show_hidden,
show_read, show_read,
show_nsfw,
..Default::default() ..Default::default()
} }
.list(&local_site.site, &mut context.pool()) .list(&local_site.site, &mut context.pool())

View file

@ -396,7 +396,10 @@ fn queries<'a>() -> Queries<
.filter(not(post::removed.or(post::deleted))); .filter(not(post::removed.or(post::deleted)));
} }
if !options.local_user.show_nsfw(site) { if !options
.show_nsfw
.unwrap_or(options.local_user.show_nsfw(site))
{
query = query query = query
.filter(post::nsfw.eq(false)) .filter(post::nsfw.eq(false))
.filter(community::nsfw.eq(false)); .filter(community::nsfw.eq(false));
@ -621,6 +624,7 @@ pub struct PostQuery<'a> {
pub page_back: Option<bool>, pub page_back: Option<bool>,
pub show_hidden: Option<bool>, pub show_hidden: Option<bool>,
pub show_read: Option<bool>, pub show_read: Option<bool>,
pub show_nsfw: Option<bool>,
} }
impl<'a> PostQuery<'a> { impl<'a> PostQuery<'a> {
@ -1589,6 +1593,48 @@ mod tests {
cleanup(data, pool).await cleanup(data, pool).await
} }
#[tokio::test]
#[serial]
async fn post_listings_hide_nsfw() -> LemmyResult<()> {
let pool = &build_db_pool().await?;
let pool = &mut pool.into();
let data = init_data(pool).await?;
// Mark a post as nsfw
let update_form = PostUpdateForm {
nsfw: Some(true),
..Default::default()
};
Post::update(pool, data.inserted_bot_post.id, &update_form).await?;
// Make sure you don't see the nsfw post in the regular results
let post_listings_hide_nsfw = data.default_post_query().list(&data.site, pool).await?;
assert_eq!(vec![POST], names(&post_listings_hide_nsfw));
// Make sure it does come back with the show_nsfw option
let post_listings_show_nsfw = PostQuery {
sort: Some(SortType::New),
show_nsfw: Some(true),
local_user: Some(&data.local_user_view.local_user),
..Default::default()
}
.list(&data.site, pool)
.await?;
assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_show_nsfw));
// Make sure that nsfw field is true.
assert!(
&post_listings_show_nsfw
.first()
.ok_or(LemmyErrorType::CouldntFindPost)?
.post
.nsfw
);
cleanup(data, pool).await
}
async fn cleanup(data: Data, pool: &mut DbPool<'_>) -> LemmyResult<()> { async fn cleanup(data: Data, pool: &mut DbPool<'_>) -> LemmyResult<()> {
let num_deleted = Post::delete(pool, data.inserted_post.id).await?; let num_deleted = Post::delete(pool, data.inserted_post.id).await?;
Community::delete(pool, data.inserted_community.id).await?; Community::delete(pool, data.inserted_community.id).await?;

View file

@ -41,12 +41,66 @@ pub fn config(
.service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete))); .service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
} }
#[derive(Deserialize)] trait ProcessUrl {
/// If thumbnail or format is given, this uses the pictrs process endpoint.
/// Otherwise, it uses the normal pictrs url (IE image/original).
fn process_url(&self, image_url: &str, pictrs_url: &Url) -> String;
}
#[derive(Deserialize, Clone)]
struct PictrsGetParams { struct PictrsGetParams {
format: Option<String>, format: Option<String>,
thumbnail: Option<i32>, thumbnail: Option<i32>,
} }
impl ProcessUrl for PictrsGetParams {
fn process_url(&self, src: &str, pictrs_url: &Url) -> String {
if self.format.is_none() && self.thumbnail.is_none() {
format!("{}image/original/{}", pictrs_url, src)
} else {
// Take file type from name, or jpg if nothing is given
let format = self
.clone()
.format
.unwrap_or_else(|| src.split('.').last().unwrap_or("jpg").to_string());
let mut url = format!("{}image/process.{}?src={}", pictrs_url, format, src);
if let Some(size) = self.thumbnail {
url = format!("{url}&thumbnail={size}",);
}
url
}
}
}
#[derive(Deserialize, Clone)]
pub struct ImageProxyParams {
url: String,
format: Option<String>,
thumbnail: Option<i32>,
}
impl ProcessUrl for ImageProxyParams {
fn process_url(&self, proxy_url: &str, pictrs_url: &Url) -> String {
if self.format.is_none() && self.thumbnail.is_none() {
format!("{}image/original?proxy={}", pictrs_url, proxy_url)
} else {
// Take file type from name, or jpg if nothing is given
let format = self
.clone()
.format
.unwrap_or_else(|| proxy_url.split('.').last().unwrap_or("jpg").to_string());
let mut url = format!("{}image/process.{}?proxy={}", pictrs_url, format, proxy_url);
if let Some(size) = self.thumbnail {
url = format!("{url}&thumbnail={size}",);
}
url
}
}
}
fn adapt_request( fn adapt_request(
request: &HttpRequest, request: &HttpRequest,
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
@ -133,23 +187,10 @@ async fn full_res(
// If there are no query params, the URL is original // If there are no query params, the URL is original
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
let url = if params.format.is_none() && params.thumbnail.is_none() {
format!("{}image/original/{}", pictrs_config.url, name,)
} else {
// Take file type from name, or jpg if nothing is given
let format = params
.format
.unwrap_or_else(|| name.split('.').last().unwrap_or("jpg").to_string());
let mut url = format!("{}image/process.{}?src={}", pictrs_config.url, format, name,); let processed_url = params.process_url(name, &pictrs_config.url);
if let Some(size) = params.thumbnail { image(processed_url, req, &client).await
url = format!("{url}&thumbnail={size}",);
}
url
};
image(url, req, &client).await
} }
async fn image( async fn image(
@ -208,11 +249,6 @@ async fn delete(
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream()))) Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream())))
} }
#[derive(Deserialize)]
pub struct ImageProxyParams {
url: String,
}
pub async fn image_proxy( pub async fn image_proxy(
Query(params): Query<ImageProxyParams>, Query(params): Query<ImageProxyParams>,
req: HttpRequest, req: HttpRequest,
@ -226,9 +262,10 @@ pub async fn image_proxy(
RemoteImage::validate(&mut context.pool(), url.clone().into()).await?; RemoteImage::validate(&mut context.pool(), url.clone().into()).await?;
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
let url = format!("{}image/original?proxy={}", pictrs_config.url, &params.url);
image(url, req, &client).await let processed_url = params.process_url(&params.url, &pictrs_config.url);
image(processed_url, req, &client).await
} }
fn make_send<S>(mut stream: S) -> impl Stream<Item = S::Item> + Send + Unpin + 'static fn make_send<S>(mut stream: S) -> impl Stream<Item = S::Item> + Send + Unpin + 'static