diff --git a/crates/app/src/routes/api/v1/communities/posts.rs b/crates/app/src/routes/api/v1/communities/posts.rs index 9e35fd9..5737fc5 100644 --- a/crates/app/src/routes/api/v1/communities/posts.rs +++ b/crates/app/src/routes/api/v1/communities/posts.rs @@ -478,7 +478,10 @@ pub async fn community_posts_request( None => return Json(Error::NotAllowed.into()), }; - match data.get_posts_by_community(id, 12, props.page).await { + match data + .get_posts_by_community(id, 12, props.page, &Some(user.clone())) + .await + { Ok(posts) => { let ignore_users = crate::ignore_users_gen!(user!, #data); Json(ApiReturn { diff --git a/crates/app/src/routes/pages/communities.rs b/crates/app/src/routes/pages/communities.rs index 626675a..e11b685 100644 --- a/crates/app/src/routes/pages/communities.rs +++ b/crates/app/src/routes/pages/communities.rs @@ -417,7 +417,7 @@ pub async fn feed_request( let feed = match data .0 - .get_posts_by_community(community.id, 12, props.page) + .get_posts_by_community(community.id, 12, props.page, &user) .await { Ok(p) => match data.0.fill_posts(p, &ignore_users, &user).await { diff --git a/crates/app/src/routes/pages/forge.rs b/crates/app/src/routes/pages/forge.rs index 404455c..be1769c 100644 --- a/crates/app/src/routes/pages/forge.rs +++ b/crates/app/src/routes/pages/forge.rs @@ -161,7 +161,7 @@ pub async fn tickets_request( let feed = match data .0 - .get_posts_by_community(community.id, 12, props.page) + .get_posts_by_community(community.id, 12, props.page, &user) .await { Ok(p) => match data.0.fill_posts(p, &ignore_users, &user).await { diff --git a/crates/core/src/database/posts.rs b/crates/core/src/database/posts.rs index e52490a..656b30e 100644 --- a/crates/core/src/database/posts.rs +++ b/crates/core/src/database/posts.rs @@ -1043,15 +1043,31 @@ impl DataManager { id: usize, batch: usize, page: usize, + user: &Option, ) -> Result> { let conn = match self.0.connect().await { Ok(c) => c, Err(e) => return Err(Error::DatabaseConnection(e.to_string())), }; + // check if we should hide nsfw posts + let mut hide_nsfw: bool = true; + + if let Some(ua) = user { + hide_nsfw = !ua.settings.show_nsfw; + } + + // ... let res = query_rows!( &conn, - "SELECT * FROM posts WHERE community = $1 AND replying_to = 0 AND NOT context LIKE '%\"is_pinned\":true%' AND is_deleted = 0 ORDER BY created DESC LIMIT $2 OFFSET $3", + &format!( + "SELECT * FROM posts WHERE community = $1 AND replying_to = 0 AND NOT context LIKE '%\"is_pinned\":true%' AND is_deleted = 0 {} ORDER BY created DESC LIMIT $2 OFFSET $3", + if hide_nsfw { + "AND NOT (context::json->>'is_nsfw')::boolean" + } else { + "" + } + ), &[&(id as i64), &(batch as i64), &((page * batch) as i64)], |x| { Self::get_post_from_row(x) } );