add: polls

This commit is contained in:
trisua 2025-06-04 17:21:46 -04:00
parent 4dfa09207e
commit 6555324650
29 changed files with 339 additions and 56 deletions

View file

@ -21,7 +21,7 @@ impl DataManager {
id: get!(x->0(i64)) as usize,
owner: get!(x->1(i64)) as usize,
created: get!(x->2(i64)) as usize,
expires: get!(x->3(i64)) as usize,
expires: get!(x->3(i32)) as usize,
option_a: get!(x->4(String)),
option_b: get!(x->5(String)),
option_c: get!(x->6(String)),
@ -74,15 +74,15 @@ impl DataManager {
&(data.id as i64),
&(data.owner as i64),
&(data.created as i64),
&(data.expires as i64),
&(data.expires as i32),
&data.option_a,
&data.option_b,
&data.option_c,
&data.option_d,
&(data.votes_a as i64),
&(data.votes_b as i64),
&(data.votes_c as i64),
&(data.votes_d as i64),
&(data.votes_a as i32),
&(data.votes_b as i32),
&(data.votes_c as i32),
&(data.votes_d as i32),
]
);
@ -120,6 +120,18 @@ impl DataManager {
self.2.remove(format!("atto.poll:{}", id)).await;
// remove votes
let res = execute!(
&conn,
"DELETE FROM pollvotes WHERE poll_id = $1",
&[&(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// ...
Ok(())
}
@ -127,15 +139,15 @@ impl DataManager {
self.2.remove(format!("atto.poll:{}", poll.id)).await;
}
auto_method!(incr_votes_a_count()@get_poll_by_id -> "UPDATE users SET votes_a = votes_a + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_a_count()@get_poll_by_id -> "UPDATE users SET votes_a = votes_a - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_a);
auto_method!(incr_votes_a_count()@get_poll_by_id -> "UPDATE polls SET votes_a = votes_a + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_a_count()@get_poll_by_id -> "UPDATE polls SET votes_a = votes_a - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_a);
auto_method!(incr_votes_b_count()@get_poll_by_id -> "UPDATE users SET votes_b = votes_b + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_b_count()@get_poll_by_id -> "UPDATE users SET votes_b = votes_b - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_b);
auto_method!(incr_votes_b_count()@get_poll_by_id -> "UPDATE polls SET votes_b = votes_b + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_b_count()@get_poll_by_id -> "UPDATE polls SET votes_b = votes_b - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_b);
auto_method!(incr_votes_c_count()@get_poll_by_id -> "UPDATE users SET votes_a = votes_d + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_c_count()@get_poll_by_id -> "UPDATE users SET votes_a = votes_d - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_c);
auto_method!(incr_votes_c_count()@get_poll_by_id -> "UPDATE polls SET votes_c = votes_d + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_c_count()@get_poll_by_id -> "UPDATE polls SET votes_c = votes_d - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_c);
auto_method!(incr_votes_d_count()@get_poll_by_id -> "UPDATE users SET votes_a = votes_d + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_d_count()@get_poll_by_id -> "UPDATE users SET votes_a = votes_d - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_d);
auto_method!(incr_votes_d_count()@get_poll_by_id -> "UPDATE polls SET votes_d = votes_d + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --incr);
auto_method!(decr_votes_d_count()@get_poll_by_id -> "UPDATE polls SET votes_d = votes_d - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_poll --decr=votes_d);
}

View file

@ -1,6 +1,6 @@
use super::*;
use crate::cache::Cache;
use crate::model::communities::PollVote;
use crate::model::communities::{PollOption, PollVote};
use crate::model::moderation::AuditLogEntry;
use crate::model::{Error, Result, auth::User, permissions::FinePermission};
use crate::{auto_method, execute, get, query_row, params};
@ -45,7 +45,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM pollvotes WHERE id = $1 AND poll_id = $2",
"SELECT * FROM pollvotes WHERE owner = $1 AND poll_id = $2",
&[&(id as i64), &(poll_id as i64)],
|x| { Ok(Self::get_pollvote_from_row(x)) }
);
@ -95,7 +95,7 @@ impl DataManager {
&(data.owner as i64),
&(data.created as i64),
&(data.poll_id as i64),
&(vote_u8 as i64),
&(vote_u8 as i32),
]
);
@ -104,10 +104,20 @@ impl DataManager {
}
// update poll
self.incr_votes_a_count(poll.id).await?;
self.incr_votes_b_count(poll.id).await?;
self.incr_votes_c_count(poll.id).await?;
self.incr_votes_d_count(poll.id).await?;
match data.vote {
PollOption::A => {
self.incr_votes_a_count(poll.id).await?;
}
PollOption::B => {
self.incr_votes_b_count(poll.id).await?;
}
PollOption::C => {
self.incr_votes_c_count(poll.id).await?;
}
PollOption::D => {
self.incr_votes_d_count(poll.id).await?;
}
};
// ...
Ok(data.id)

View file

@ -245,7 +245,7 @@ impl DataManager {
let user = if let Some(ua) = user {
ua
} else {
return Err(Error::MiscError("Could not get user for pull".to_string()));
return Ok(None);
};
if post.poll_id != 0 {
@ -259,7 +259,7 @@ impl DataManager {
Err(_) => return Err(Error::MiscError("Invalid poll ID attached".to_string())),
}))
} else {
return Err(Error::MiscError("Invalid poll ID attached".to_string()));
Ok(None)
}
}
@ -374,15 +374,10 @@ impl DataManager {
Community,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool)>,
)>,
> {
let mut out: Vec<(
Post,
User,
Community,
Option<(User, Post)>,
Option<(Question, User)>,
)> = Vec::new();
let mut out = Vec::new();
let mut seen_before: HashMap<(usize, usize), (User, Community)> = HashMap::new();
let mut seen_user_follow_statuses: HashMap<(usize, usize), bool> = HashMap::new();
@ -403,6 +398,7 @@ impl DataManager {
community.to_owned(),
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?;
@ -450,6 +446,7 @@ impl DataManager {
community,
self.get_post_reposting(&post, ignore_users, user).await,
self.get_post_question(&post, ignore_users).await?,
self.get_post_poll(&post, user).await?,
));
}
}
@ -1423,7 +1420,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, null, $13)",
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, DEFAULT, $13)",
params![
&(data.id as i64),
&(data.created as i64),
@ -1541,6 +1538,11 @@ impl DataManager {
self.delete_upload(upload).await?;
}
// remove poll
if y.poll_id != 0 {
self.delete_poll(y.poll_id, user).await?;
}
// return
Ok(())
}

View file

@ -1,6 +1,6 @@
use super::*;
use crate::cache::Cache;
use crate::model::communities::{Community, Post, Question};
use crate::model::communities::{Community, Poll, Post, Question};
use crate::model::stacks::{StackMode, StackSort};
use crate::model::{
Error, Result,
@ -51,6 +51,7 @@ impl DataManager {
Community,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool)>,
)>,
> {
let stack = self.get_stack_by_id(id).await?;

View file

@ -425,7 +425,7 @@ impl Poll {
/// Poll option (selectors) are stored in the database as numbers 0 to 3.
///
/// This enum allows us to convert from these numbers into letters.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum PollOption {
A,
B,