add: circle stacks

This commit is contained in:
trisua 2025-06-15 16:09:02 -04:00
parent 50704d27a9
commit 56cea83933
27 changed files with 419 additions and 107 deletions

View file

@ -1,10 +1,12 @@
use oiseau::cache::Cache;
use crate::model::{
Error, Result,
auth::User,
permissions::FinePermission,
stacks::{StackPrivacy, UserStack, StackMode, StackSort},
communities::{Community, Poll, Post, Question},
use crate::{
database::posts::FullPost,
model::{
auth::User,
permissions::FinePermission,
stacks::{StackMode, StackPrivacy, StackSort, UserStack},
Error, Result,
},
};
use crate::{auto_method, DataManager};
@ -37,16 +39,7 @@ impl DataManager {
page: usize,
ignore_users: &Vec<usize>,
user: &Option<User>,
) -> Result<
Vec<(
Post,
User,
Community,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool, bool)>,
)>,
> {
) -> Result<Vec<FullPost>> {
let stack = self.get_stack_by_id(id).await?;
Ok(match stack.mode {
@ -89,6 +82,19 @@ impl DataManager {
"You should use `get_stack_users` for this type".to_string(),
));
}
StackMode::Circle => {
if !stack.users.contains(&as_user_id) && as_user_id != stack.owner {
return Err(Error::NotAllowed);
}
self.fill_posts_with_community(
self.get_posts_by_stack(stack.id, batch, page).await?,
as_user_id,
&ignore_users,
user,
)
.await?
}
})
}
@ -119,9 +125,11 @@ impl DataManager {
/// Get all stacks by user.
///
/// Also pulls stacks that are of "Circle" type AND the user is added to the `users` list.
///
/// # Arguments
/// * `id` - the ID of the user to fetch stacks for
pub async fn get_stacks_by_owner(&self, id: usize) -> Result<Vec<UserStack>> {
pub async fn get_stacks_by_user(&self, id: usize) -> Result<Vec<UserStack>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
@ -129,8 +137,8 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM stacks WHERE owner = $1 ORDER BY name ASC",
&[&(id as i64)],
"SELECT * FROM stacks WHERE owner = $1 OR (mode = '\"Circle\"' AND users LIKE $2) ORDER BY name ASC",
&[&(id as i64), &format!("%{id}%")],
|x| { Self::get_stack_from_row(x) }
);
@ -142,6 +150,7 @@ impl DataManager {
}
const MAXIMUM_FREE_STACKS: usize = 5;
pub const MAXIMUM_FREE_STACK_USERS: usize = 50;
/// Create a new stack in the database.
///
@ -159,7 +168,7 @@ impl DataManager {
let owner = self.get_user_by_id(data.owner).await?;
if !owner.permissions.check(FinePermission::SUPPORTER) {
let stacks = self.get_stacks_by_owner(data.owner).await?;
let stacks = self.get_stacks_by_user(data.owner).await?;
if stacks.len() >= Self::MAXIMUM_FREE_STACKS {
return Err(Error::MiscError(
@ -216,6 +225,25 @@ impl DataManager {
return Err(Error::DatabaseError(e.to_string()));
}
// delete stackblocks
let res = execute!(
&conn,
"DELETE FROM stackblocks WHERE stack = $1",
&[&(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// delete posts
let res = execute!(&conn, "DELETE FROM posts WHERE stack = $1", &[&(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// ...
self.0.1.remove(format!("atto.stack:{}", id)).await;
Ok(())
}