fix: nsfw posts in all/communities timelines

This commit is contained in:
trisua 2025-07-09 22:29:54 -04:00
parent 69067145ce
commit 7960f1ed41
3 changed files with 28 additions and 6 deletions

View file

@ -723,7 +723,7 @@ pub async fn from_communities_request(
};
match data
.get_posts_from_user_communities(user.id, 12, props.page)
.get_posts_from_user_communities(user.id, 12, props.page, &user)
.await
{
Ok(posts) => {

View file

@ -58,7 +58,7 @@ pub async fn index_request(
let list = match data
.0
.get_posts_from_user_communities(user.id, 12, req.page)
.get_posts_from_user_communities(user.id, 12, req.page, &user)
.await
{
Ok(l) => match data
@ -725,7 +725,7 @@ pub async fn swiss_army_timeline_request(
DefaultTimelineChoice::MyCommunities => {
if let Some(ref ua) = user {
data.0
.get_posts_from_user_communities(ua.id, 12, req.page)
.get_posts_from_user_communities(ua.id, 12, req.page, ua)
.await
} else {
return Err(Html(

View file

@ -1452,6 +1452,14 @@ impl DataManager {
false
};
// check if we should hide nsfw posts
let mut hide_nsfw: bool = true;
if let Some(ua) = as_user {
hide_nsfw = !ua.settings.show_nsfw;
}
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
@ -1460,12 +1468,17 @@ impl DataManager {
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE replying_to = 0{} AND NOT context LIKE '%\"is_nsfw\":true%'{} ORDER BY created DESC LIMIT $1 OFFSET $2",
"SELECT * FROM posts WHERE replying_to = 0{}{}{} ORDER BY created DESC LIMIT $1 OFFSET $2",
if before_time > 0 {
format!(" AND created < {before_time}")
} else {
String::new()
},
if hide_nsfw {
" AND NOT context LIKE '%\"is_nsfw\":true%'"
} else {
""
},
if hide_answers {
" AND context::jsonb->>'answering' = '0'"
} else {
@ -1494,6 +1507,7 @@ impl DataManager {
id: usize,
batch: usize,
page: usize,
user: &User,
) -> Result<Vec<Post>> {
let memberships = self.get_memberships_by_owner(id).await?;
let mut memberships = memberships.iter();
@ -1508,6 +1522,9 @@ impl DataManager {
query_string.push_str(&format!(" OR community = {}", membership.community));
}
// check if we should hide nsfw posts
let hide_nsfw: bool = !user.settings.show_nsfw;
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
@ -1517,8 +1534,13 @@ impl DataManager {
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE (community = {} {query_string}) AND NOT context LIKE '%\"is_nsfw\":true%' AND replying_to = 0 AND is_deleted = 0 ORDER BY created DESC LIMIT $1 OFFSET $2",
first.community
"SELECT * FROM posts WHERE (community = {} {query_string}){} AND replying_to = 0 AND is_deleted = 0 ORDER BY created DESC LIMIT $1 OFFSET $2",
first.community,
if hide_nsfw {
" AND NOT context LIKE '%\"is_nsfw\":true%'"
} else {
""
},
),
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }