add: stacks mode and sort

This commit is contained in:
trisua 2025-05-09 15:56:19 -04:00
parent 281e9bea44
commit d174b44f57
9 changed files with 272 additions and 18 deletions

View file

@ -1,5 +1,7 @@
use super::*;
use crate::cache::Cache;
use crate::model::communities::{Community, Post, Question};
use crate::model::stacks::{StackMode, StackSort};
use crate::model::{
Error, Result,
auth::User,
@ -27,11 +29,66 @@ impl DataManager {
name: get!(x->3(String)),
users: serde_json::from_str(&get!(x->4(String))).unwrap(),
privacy: serde_json::from_str(&get!(x->5(String))).unwrap(),
mode: serde_json::from_str(&get!(x->6(String))).unwrap(),
sort: serde_json::from_str(&get!(x->7(String))).unwrap(),
}
}
auto_method!(get_stack_by_id(usize as i64)@get_stack_from_row -> "SELECT * FROM stacks WHERE id = $1" --name="stack" --returns=UserStack --cache-key-tmpl="atto.stack:{}");
pub async fn get_stack_posts(
&self,
as_user_id: usize,
id: usize,
batch: usize,
page: usize,
ignore_users: &Vec<usize>,
) -> Result<
Vec<(
Post,
User,
Community,
Option<(User, Post)>,
Option<(Question, User)>,
)>,
> {
let stack = self.get_stack_by_id(id).await?;
Ok(match stack.mode {
StackMode::Include => {
self.fill_posts_with_community(
self.get_posts_from_stack(id, batch, page, stack.sort)
.await?,
as_user_id,
ignore_users,
)
.await?
}
StackMode::Exclude => {
let ignore_users = [ignore_users.to_owned(), stack.users].concat();
match stack.sort {
StackSort::Created => {
self.fill_posts_with_community(
self.get_latest_posts(batch, page).await?,
as_user_id,
&ignore_users,
)
.await?
}
StackSort::Likes => {
self.fill_posts_with_community(
self.get_popular_posts(batch, page, 604_800_000).await?,
as_user_id,
&ignore_users,
)
.await?
}
}
}
})
}
/// Get all stacks by user.
///
/// # Arguments
@ -90,7 +147,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO stacks VALUES ($1, $2, $3, $4, $5, $6)",
"INSERT INTO stacks VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
params![
&(data.id as i64),
&(data.created as i64),
@ -98,6 +155,8 @@ impl DataManager {
&data.name,
&serde_json::to_string(&data.users).unwrap(),
&serde_json::to_string(&data.privacy).unwrap(),
&serde_json::to_string(&data.mode).unwrap(),
&serde_json::to_string(&data.sort).unwrap(),
]
);
@ -135,6 +194,9 @@ impl DataManager {
}
auto_method!(update_stack_name(&str)@get_stack_by_id:MANAGE_STACKS -> "UPDATE stacks SET name = $1 WHERE id = $2" --cache-key-tmpl="atto.stack:{}");
auto_method!(update_stack_privacy(StackPrivacy)@get_stack_by_id:MANAGE_STACKS -> "UPDATE stacks SET privacy = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.stack:{}");
auto_method!(update_stack_users(Vec<usize>)@get_stack_by_id:MANAGE_STACKS -> "UPDATE stacks SET users = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.stack:{}");
auto_method!(update_stack_privacy(StackPrivacy)@get_stack_by_id:MANAGE_STACKS -> "UPDATE stacks SET privacy = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.stack:{}");
auto_method!(update_stack_mode(StackMode)@get_stack_by_id:MANAGE_STACKS -> "UPDATE stacks SET mode = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.stack:{}");
auto_method!(update_stack_sort(StackSort)@get_stack_by_id:MANAGE_STACKS -> "UPDATE stacks SET sort = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.stack:{}");
}