yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use common::{ |
| 4 | call::CreatePoll, |
| 5 | error::Error, |
| 6 | model::{Id, Poll, PollOption}, |
| 7 | time::now_ms, |
| 8 | }; |
| 9 | |
| 10 | use super::{Connection, all, maybe, run}; |
| 11 | use crate::error::{ServerError, ensure_with}; |
| 12 | |
| 13 | pub async fn create(message_id: Id, poll: &CreatePoll, db: &Connection) -> Result<(), ServerError> { |
| 14 | let CreatePoll { |
| 15 | question, |
| 16 | options, |
| 17 | multiple, |
| 18 | public, |
| 19 | duration, |
| 20 | .. |
| 21 | } = poll; |
| 22 | let ends_at = duration.map(|seconds| (now_ms() + u64::from(seconds) * 1000) as i64); |
| 23 | run( |
| 24 | db, |
| 25 | "INSERT INTO poll (message_id, question, multiple, public, ends_at) VALUES (?1, ?2, ?3, ?4, ?5)", |
| 26 | (message_id, question.as_str(), *multiple, *public, ends_at), |
| 27 | ) |
| 28 | .await?; |
| 29 | for (position, text) in (0u32..).zip(options) { |
| 30 | run( |
| 31 | db, |
| 32 | "INSERT INTO poll_option (message_id, position, text) VALUES (?1, ?2, ?3)", |
| 33 | (message_id, position, text.as_str()), |
| 34 | ) |
| 35 | .await?; |
| 36 | } |
| 37 | |
| 38 | Ok(()) |
| 39 | } |
| 40 | |
| 41 | pub async fn of(message_id: Id, db: &Connection) -> Result<Option<Poll>, ServerError> { |
| 42 | let Some((question, multiple, closed, public, ends_at)) = maybe( |
| 43 | db, |
| 44 | "SELECT question, multiple, closed, public, ends_at FROM poll WHERE message_id = ?1", |
| 45 | (message_id,), |
| 46 | |r| { |
| 47 | Ok(( |
| 48 | r.get::<String>()?, |
| 49 | r.get::<bool>()?, |
| 50 | r.get::<bool>()?, |
| 51 | r.get::<bool>()?, |
| 52 | r.get::<Option<i64>>()?, |
| 53 | )) |
| 54 | }, |
| 55 | ) |
| 56 | .await? |
| 57 | else { |
| 58 | return Ok(None); |
| 59 | }; |
| 60 | |
| 61 | let mut options: Vec<PollOption> = all( |
| 62 | db, |
| 63 | "SELECT text FROM poll_option WHERE message_id = ?1 ORDER BY position", |
| 64 | (message_id,), |
| 65 | |r| { |
| 66 | Ok(PollOption { |
| 67 | text: r.get()?, |
| 68 | votes: 0, |
| 69 | voter_ids: Vec::new(), |
| 70 | }) |
| 71 | }, |
| 72 | ) |
| 73 | .await?; |
| 74 | let votes: Vec<(u32, Id)> = all( |
| 75 | db, |
| 76 | "SELECT position, user_id FROM poll_vote WHERE message_id = ?1 ORDER BY position, user_id", |
| 77 | (message_id,), |
| 78 | |r| Ok((r.get()?, r.get()?)), |
| 79 | ) |
| 80 | .await?; |
| 81 | let voters = votes |
| 82 | .iter() |
| 83 | .map(|(_, user_id)| user_id) |
| 84 | .collect::<HashSet<_>>() |
| 85 | .len() as u32; |
| 86 | for (position, user_id) in votes { |
| 87 | if let Some(option) = options.get_mut(position as usize) { |
| 88 | option.votes += 1; |
| 89 | if public { |
| 90 | option.voter_ids.push(user_id); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | let ends_at = ends_at.map(|ms| ms as u64); |
| 96 | let closed = closed || ends_at.is_some_and(|end| end <= now_ms()); |
| 97 | |
| 98 | Ok(Some(Poll { |
| 99 | question, |
| 100 | options, |
| 101 | multiple, |
| 102 | closed, |
| 103 | public, |
| 104 | voters, |
| 105 | ends_at, |
| 106 | })) |
| 107 | } |
| 108 | |
| 109 | pub async fn set_votes( |
| 110 | message_id: Id, |
| 111 | user_id: Id, |
| 112 | positions: &[u32], |
| 113 | db: &Connection, |
| 114 | ) -> Result<(), ServerError> { |
| 115 | run( |
| 116 | db, |
| 117 | "DELETE FROM poll_vote WHERE message_id = ?1 AND user_id = ?2", |
| 118 | (message_id, user_id), |
| 119 | ) |
| 120 | .await?; |
| 121 | for position in positions { |
| 122 | run( |
| 123 | db, |
| 124 | "INSERT INTO poll_vote (message_id, position, user_id) VALUES (?1, ?2, ?3)", |
| 125 | (message_id, *position, user_id), |
| 126 | ) |
| 127 | .await?; |
| 128 | } |
| 129 | |
| 130 | Ok(()) |
| 131 | } |
| 132 | |
| 133 | pub async fn my_votes( |
| 134 | message_id: Id, |
| 135 | user_id: Id, |
| 136 | db: &Connection, |
| 137 | ) -> Result<Vec<u32>, ServerError> { |
| 138 | all( |
| 139 | db, |
| 140 | "SELECT position FROM poll_vote WHERE message_id = ?1 AND user_id = ?2 ORDER BY position", |
| 141 | (message_id, user_id), |
| 142 | |r| r.get(), |
| 143 | ) |
| 144 | .await |
| 145 | } |
| 146 | |
| 147 | pub async fn close(message_id: Id, db: &Connection) -> Result<(), ServerError> { |
| 148 | ensure_with( |
| 149 | run( |
| 150 | db, |
| 151 | "UPDATE poll SET closed = 1 WHERE message_id = ?1", |
| 152 | (message_id,), |
| 153 | ) |
| 154 | .await? |
| 155 | == 1, |
| 156 | Error::NotFound, |
| 157 | ) |
| 158 | } |