add: forum posts timeline

This commit is contained in:
trisua 2025-08-04 14:24:25 -04:00
parent 8c779b2f2e
commit d4ff681310
10 changed files with 159 additions and 17 deletions

View file

@ -1606,6 +1606,57 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get forum posts from all communities, sorted by creation.
///
/// # Arguments
/// * `batch` - the limit of posts in each page
/// * `page` - the page number
pub async fn get_latest_forum_posts(
&self,
batch: usize,
page: usize,
as_user: &Option<User>,
before_time: usize,
) -> Result<Vec<Post>> {
// 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())),
};
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE replying_to = 0{}{} AND NOT context LIKE '%\"full_unlist\":true%' AND NOT topic = 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 {
""
}
),
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("post".to_string()));
}
Ok(res.unwrap())
}
/// Get posts from all communities the given user is in.
///
/// # Arguments