add: polls backend

TODO: polls frontend
This commit is contained in:
trisua 2025-06-03 23:35:34 -04:00
parent b5e060e8ae
commit 4dfa09207e
18 changed files with 574 additions and 17 deletions

View file

@ -3,7 +3,7 @@ use super::*;
use crate::cache::Cache;
use crate::config::StringBan;
use crate::model::auth::Notification;
use crate::model::communities::Question;
use crate::model::communities::{Poll, Question};
use crate::model::communities_permissions::CommunityPermission;
use crate::model::moderation::AuditLogEntry;
use crate::model::stacks::StackSort;
@ -108,6 +108,8 @@ impl DataManager {
// ...
uploads: serde_json::from_str(&get!(x->10(String))).unwrap(),
is_deleted: get!(x->11(i32)) as i8 == 1,
// SKIP tsvector (12)
poll_id: get!(x->13(i64)) as usize,
}
}
@ -234,14 +236,49 @@ impl DataManager {
}
}
/// Get the poll of the given post (if some).
pub async fn get_post_poll(
&self,
post: &Post,
user: &Option<User>,
) -> Result<Option<(Poll, bool)>> {
let user = if let Some(ua) = user {
ua
} else {
return Err(Error::MiscError("Could not get user for pull".to_string()));
};
if post.poll_id != 0 {
Ok(Some(match self.get_poll_by_id(post.poll_id).await {
Ok(p) => (
p,
self.get_pollvote_by_owner_poll(user.id, post.poll_id)
.await
.is_ok(),
),
Err(_) => return Err(Error::MiscError("Invalid poll ID attached".to_string())),
}))
} else {
return Err(Error::MiscError("Invalid poll ID attached".to_string()));
}
}
/// Complete a vector of just posts with their owner as well.
pub async fn fill_posts(
&self,
posts: Vec<Post>,
ignore_users: &[usize],
user: &Option<User>,
) -> Result<Vec<(Post, User, Option<(User, Post)>, Option<(Question, User)>)>> {
let mut out: Vec<(Post, User, Option<(User, Post)>, Option<(Question, User)>)> = Vec::new();
) -> Result<
Vec<(
Post,
User,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool)>,
)>,
> {
let mut out = Vec::new();
let mut users: HashMap<usize, User> = HashMap::new();
let mut seen_user_follow_statuses: HashMap<(usize, usize), bool> = HashMap::new();
@ -260,6 +297,7 @@ impl DataManager {
ua.clone(),
self.get_post_reposting(&post, ignore_users, user).await,
self.get_post_question(&post, ignore_users).await?,
self.get_post_poll(&post, user).await?,
));
} else {
let ua = self.get_user_by_id(owner).await?;
@ -314,6 +352,7 @@ impl DataManager {
ua,
self.get_post_reposting(&post, ignore_users, user).await,
self.get_post_question(&post, ignore_users).await?,
self.get_post_poll(&post, user).await?,
));
}
}
@ -1384,7 +1423,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)",
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, null, $13)",
params![
&(data.id as i64),
&(data.created as i64),
@ -1401,7 +1440,8 @@ impl DataManager {
&0_i32,
&0_i32,
&serde_json::to_string(&data.uploads).unwrap(),
&{ if data.is_deleted { 1 } else { 0 } }
&{ if data.is_deleted { 1 } else { 0 } },
&(data.poll_id as i64),
]
);