tetratto_core2/database/
polls.rs1use oiseau::cache::Cache;
2use crate::model::posts::Poll;
3use crate::model::moderation::AuditLogEntry;
4use crate::model::{Error, Result, auth::User, permissions::FinePermission};
5use crate::model::id::Id;
6use crate::{auto_method, DataManager};
7
8use oiseau::PostgresRow;
9
10use oiseau::{execute, get, query_rows, params};
11
12impl DataManager {
13 pub(crate) fn get_poll_from_row(x: &PostgresRow) -> Poll {
15 Poll {
16 id: Id::deserialize(&get!(x->0(String))),
17 owner: Id::deserialize(&get!(x->1(String))),
18 created: get!(x->2(i64)) as u128,
19 expires: get!(x->3(i32)) as u128,
20 option_a: get!(x->4(String)),
21 option_b: get!(x->5(String)),
22 option_c: get!(x->6(String)),
23 option_d: get!(x->7(String)),
24 votes_a: get!(x->8(i32)) as usize,
25 votes_b: get!(x->9(i32)) as usize,
26 votes_c: get!(x->10(i32)) as usize,
27 votes_d: get!(x->11(i32)) as usize,
28 }
29 }
30
31 auto_method!(get_poll_by_id()@get_poll_from_row -> "SELECT * FROM polls WHERE id = $1" --name="poll" --returns=Poll --cache-key-tmpl="atto.poll:{}");
32
33 pub async fn get_polls_by_owner_all(&self, owner: crate::model::id::Id) -> Result<Vec<Poll>> {
38 let conn = match self.0.connect().await {
39 Ok(c) => c,
40 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
41 };
42
43 let res = query_rows!(
44 &conn,
45 "SELECT * FROM polls WHERE owner = $1 ORDER BY created DESC",
46 &[&owner.printable()],
47 |x| { Self::get_poll_from_row(x) }
48 );
49
50 if res.is_err() {
51 return Err(Error::GeneralNotFound("poll".to_string()));
52 }
53
54 Ok(res.unwrap())
55 }
56
57 pub async fn create_poll(&self, data: Poll) -> Result<Id> {
62 if data.option_a.len() < 2 {
64 return Err(Error::DataTooShort("option A".to_string()));
65 } else if data.option_a.len() > 128 {
66 return Err(Error::DataTooLong("option A".to_string()));
67 }
68
69 if data.option_b.len() < 2 {
70 return Err(Error::DataTooShort("option B".to_string()));
71 } else if data.option_b.len() > 128 {
72 return Err(Error::DataTooLong("option B".to_string()));
73 }
74
75 if data.option_c.len() > 128 {
76 return Err(Error::DataTooLong("option C".to_string()));
77 }
78
79 if data.option_d.len() > 128 {
80 return Err(Error::DataTooLong("option D".to_string()));
81 }
82
83 let conn = match self.0.connect().await {
85 Ok(c) => c,
86 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
87 };
88
89 let res = execute!(
90 &conn,
91 "INSERT INTO polls VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)",
92 params![
93 &data.id.printable(),
94 &data.owner.printable(),
95 &(data.created as i64),
96 &(data.expires as i32),
97 &data.option_a,
98 &data.option_b,
99 &data.option_c,
100 &data.option_d,
101 &(data.votes_a as i32),
102 &(data.votes_b as i32),
103 &(data.votes_c as i32),
104 &(data.votes_d as i32),
105 ]
106 );
107
108 if let Err(e) = res {
109 return Err(Error::DatabaseError(e.to_string()));
110 }
111
112 Ok(data.id)
113 }
114
115 pub async fn delete_poll(&self, id: &Id, user: &User) -> Result<()> {
116 let y = self.get_poll_by_id(id).await?;
117
118 if user.id != y.owner {
119 if !user.permissions.check(FinePermission::ManagePosts) {
120 return Err(Error::NotAllowed);
121 } else {
122 self.create_audit_log_entry(AuditLogEntry::new(
123 user.id.to_owned(),
124 format!("invoked `delete_poll` with x value `{id}`"),
125 ))
126 .await?
127 }
128 }
129 let conn = match self.0.connect().await {
130 Ok(c) => c,
131 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
132 };
133
134 let res = execute!(&conn, "DELETE FROM polls WHERE id = $1", &[&id.printable()]);
135
136 if let Err(e) = res {
137 return Err(Error::DatabaseError(e.to_string()));
138 }
139
140 self.0.1.remove(format!("atto.poll:{}", id)).await;
141
142 let res = execute!(
144 &conn,
145 "DELETE FROM pollvotes WHERE poll_id = $1",
146 &[&id.printable()]
147 );
148
149 if let Err(e) = res {
150 return Err(Error::DatabaseError(e.to_string()));
151 }
152
153 Ok(())
155 }
156
157 pub async fn cache_clear_poll(&self, poll: &Poll) {
158 self.0.1.remove(format!("atto.poll:{}", poll.id)).await;
159 }
160
161 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);
162 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);
163
164 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);
165 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);
166
167 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);
168 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);
169
170 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);
171 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);
172}