add: user stacks

This commit is contained in:
trisua 2025-05-08 22:18:04 -04:00
parent 8c3024cb40
commit 75d72460ae
28 changed files with 1028 additions and 9 deletions

View file

@ -732,6 +732,55 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get posts from all users in the given stack.
///
/// # Arguments
/// * `id` - the ID of the stack
/// * `batch` - the limit of posts in each page
/// * `page` - the page number
pub async fn get_posts_from_stack(
&self,
id: usize,
batch: usize,
page: usize,
) -> Result<Vec<Post>> {
let users = self.get_stack_by_id(id).await?.users;
let mut users = users.iter();
let first = match users.next() {
Some(f) => f,
None => return Ok(Vec::new()),
};
let mut query_string: String = String::new();
for user in users {
query_string.push_str(&format!(" OR owner = {}", user));
}
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE (owner = {} {query_string}) AND replying_to = 0 ORDER BY created DESC LIMIT $1 OFFSET $2",
first
),
&[&(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())
}
/// Check if the given `uid` can post in the given `community`.
pub async fn check_can_post(&self, community: &Community, uid: usize) -> bool {
match community.write_access {