add: stacks mode and sort
This commit is contained in:
parent
281e9bea44
commit
d174b44f57
9 changed files with 272 additions and 18 deletions
|
@ -4,5 +4,7 @@ CREATE TABLE IF NOT EXISTS stacks (
|
|||
owner BIGINT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
users TEXT NOT NULL,
|
||||
privacy TEXT NOT NULL
|
||||
privacy TEXT NOT NULL,
|
||||
mode TEXT NOT NULL,
|
||||
sort TEXT NOT NULL
|
||||
)
|
||||
|
|
|
@ -157,6 +157,15 @@ impl DataManager {
|
|||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
||||
// check if the user can read the channel
|
||||
let membership = self
|
||||
.get_membership_by_owner_community(user.id, channel.community)
|
||||
.await?;
|
||||
|
||||
if !channel.check_read(user.id, Some(membership.role)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// create notif
|
||||
self.create_notification(Notification::new(
|
||||
"You've been mentioned in a message!".to_string(),
|
||||
|
|
|
@ -6,6 +6,7 @@ use crate::model::auth::Notification;
|
|||
use crate::model::communities::Question;
|
||||
use crate::model::communities_permissions::CommunityPermission;
|
||||
use crate::model::moderation::AuditLogEntry;
|
||||
use crate::model::stacks::StackSort;
|
||||
use crate::model::{
|
||||
Error, Result,
|
||||
auth::User,
|
||||
|
@ -743,6 +744,7 @@ impl DataManager {
|
|||
id: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
sort: StackSort,
|
||||
) -> Result<Vec<Post>> {
|
||||
let users = self.get_stack_by_id(id).await?.users;
|
||||
let mut users = users.iter();
|
||||
|
@ -767,8 +769,13 @@ impl DataManager {
|
|||
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
|
||||
"SELECT * FROM posts WHERE (owner = {} {query_string}) AND replying_to = 0 ORDER BY {} DESC LIMIT $1 OFFSET $2",
|
||||
first,
|
||||
if sort == StackSort::Created {
|
||||
"created"
|
||||
} else {
|
||||
"likes"
|
||||
}
|
||||
),
|
||||
&[&(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Self::get_post_from_row(x) }
|
||||
|
|
|
@ -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:{}");
|
||||
}
|
||||
|
|
|
@ -15,6 +15,34 @@ impl Default for StackPrivacy {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum StackMode {
|
||||
/// `users` vec contains ID of users to INCLUDE into the timeline;
|
||||
/// every other user is excluded
|
||||
Include,
|
||||
/// `users` vec contains ID of users to EXCLUDE from the timeline;
|
||||
/// every other user is included
|
||||
Exclude,
|
||||
}
|
||||
|
||||
impl Default for StackMode {
|
||||
fn default() -> Self {
|
||||
Self::Include
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum StackSort {
|
||||
Created,
|
||||
Likes,
|
||||
}
|
||||
|
||||
impl Default for StackSort {
|
||||
fn default() -> Self {
|
||||
Self::Created
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UserStack {
|
||||
pub id: usize,
|
||||
|
@ -23,6 +51,8 @@ pub struct UserStack {
|
|||
pub name: String,
|
||||
pub users: Vec<usize>,
|
||||
pub privacy: StackPrivacy,
|
||||
pub mode: StackMode,
|
||||
pub sort: StackSort,
|
||||
}
|
||||
|
||||
impl UserStack {
|
||||
|
@ -35,6 +65,8 @@ impl UserStack {
|
|||
name,
|
||||
users,
|
||||
privacy: StackPrivacy::default(),
|
||||
mode: StackMode::default(),
|
||||
sort: StackSort::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue