| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use common::{ |
| 4 | constant::MAXIMUM_ATTACHMENTS_PER_MESSAGE, |
| 5 | error::Error, |
| 6 | model::{Attachment, AttachmentInfo, AttachmentKind, CommunityUsage, Id, Usage}, |
| 7 | time::now_ms, |
| 8 | }; |
| 9 | |
| 10 | use super::{Connection, Reader, community, one, run, user}; |
| 11 | use crate::error::{ServerError, ensure_with}; |
| 12 | |
| 13 | const LIST_LIMIT: u32 = 200; |
| 14 | const USAGE_LIMIT: u32 = 50; |
| 15 | |
| 16 | pub const COLUMNS: &str = "attachment.id, attachment.name, attachment.size, attachment.kind, attachment.width, attachment.height, attachment.created_at"; |
| 17 | |
| 18 | pub struct Stored { |
| 19 | pub attachment: Attachment, |
| 20 | pub file: String, |
| 21 | pub uploader_id: Id, |
| 22 | pub community_id: Id, |
| 23 | pub message_id: Option<Id>, |
| 24 | } |
| 25 | |
| 26 | pub fn row(r: &mut Reader) -> Result<Attachment, ServerError> { |
| 27 | Ok(Attachment { |
| 28 | id: r.get()?, |
| 29 | name: r.get()?, |
| 30 | size: r.get()?, |
| 31 | kind: r.named()?, |
| 32 | width: r.get()?, |
| 33 | height: r.get()?, |
| 34 | created_at: r.get()?, |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | pub async fn of_message( |
| 39 | message_id: Id, |
| 40 | snippet_position: Option<u32>, |
| 41 | db: &Connection, |
| 42 | ) -> Result<Vec<Attachment>, ServerError> { |
| 43 | super::all( |
| 44 | db, |
| 45 | &format!( |
| 46 | "SELECT {COLUMNS} FROM attachment WHERE message_id = ?1 AND snippet_position IS ?2 ORDER BY id" |
| 47 | ), |
| 48 | (message_id, snippet_position), |
| 49 | row, |
| 50 | ) |
| 51 | .await |
| 52 | } |
| 53 | |
| 54 | pub async fn bind( |
| 55 | message_id: Id, |
| 56 | user_id: Id, |
| 57 | community_id: Id, |
| 58 | attachment_ids: &[Id], |
| 59 | db: &Connection, |
| 60 | ) -> Result<(), ServerError> { |
| 61 | ensure_with( |
| 62 | attachment_ids.len() <= MAXIMUM_ATTACHMENTS_PER_MESSAGE, |
| 63 | Error::NotFound, |
| 64 | )?; |
| 65 | for attachment_id in attachment_ids { |
| 66 | let bound = run( |
| 67 | db, |
| 68 | "UPDATE attachment SET message_id = ?1 WHERE id = ?2 AND uploader_id = ?3 AND community_id = ?4 AND message_id IS NULL", |
| 69 | (message_id, *attachment_id, user_id, community_id), |
| 70 | ) |
| 71 | .await?; |
| 72 | ensure_with(bound == 1, Error::NotFound)?; |
| 73 | } |
| 74 | |
| 75 | Ok(()) |
| 76 | } |
| 77 | |
| 78 | pub async fn copy_row( |
| 79 | id: Id, |
| 80 | message_id: Id, |
| 81 | user_id: Id, |
| 82 | community_id: Id, |
| 83 | snippet_position: Option<u32>, |
| 84 | db: &Connection, |
| 85 | ) -> Result<(), ServerError> { |
| 86 | let copied = run( |
| 87 | db, |
| 88 | "INSERT INTO attachment (uploader_id, community_id, message_id, snippet_position, created_at, file, name, size, kind, width, height) SELECT ?2, ?3, ?4, ?5, ?6, file, name, size, kind, width, height FROM attachment WHERE id = ?1", |
| 89 | (id, user_id, community_id, message_id, snippet_position, now_ms()), |
| 90 | ) |
| 91 | .await?; |
| 92 | ensure_with(copied == 1, Error::NotFound) |
| 93 | } |
| 94 | |
| 95 | #[allow(clippy::too_many_arguments)] |
| 96 | pub async fn insert( |
| 97 | uploader_id: Id, |
| 98 | community_id: Id, |
| 99 | file: &str, |
| 100 | name: &str, |
| 101 | size: u64, |
| 102 | kind: AttachmentKind, |
| 103 | width: Option<u32>, |
| 104 | height: Option<u32>, |
| 105 | db: &Connection, |
| 106 | ) -> Result<Attachment, ServerError> { |
| 107 | let id: Id = one( |
| 108 | db, |
| 109 | "INSERT INTO attachment (uploader_id, community_id, created_at, file, name, size, kind, width, height) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9) RETURNING id", |
| 110 | ( |
| 111 | uploader_id, |
| 112 | community_id, |
| 113 | now_ms(), |
| 114 | file, |
| 115 | name, |
| 116 | size, |
| 117 | kind.as_str(), |
| 118 | width, |
| 119 | height, |
| 120 | ), |
| 121 | |r| r.get(), |
| 122 | ) |
| 123 | .await?; |
| 124 | |
| 125 | Ok(read(id, db).await?.attachment) |
| 126 | } |
| 127 | |
| 128 | pub async fn read(id: Id, db: &Connection) -> Result<Stored, ServerError> { |
| 129 | one( |
| 130 | db, |
| 131 | &format!( |
| 132 | "SELECT {COLUMNS}, attachment.file, attachment.uploader_id, attachment.community_id, attachment.message_id FROM attachment WHERE attachment.id = ?1" |
| 133 | ), |
| 134 | (id,), |
| 135 | |r| { |
| 136 | Ok(Stored { |
| 137 | attachment: row(r)?, |
| 138 | file: r.get()?, |
| 139 | uploader_id: r.get()?, |
| 140 | community_id: r.get()?, |
| 141 | message_id: r.get()?, |
| 142 | }) |
| 143 | }, |
| 144 | ) |
| 145 | .await |
| 146 | } |
| 147 | |
| 148 | pub async fn delete(id: Id, db: &Connection) -> Result<(), ServerError> { |
| 149 | run(db, "DELETE FROM attachment WHERE id = ?1", (id,)).await?; |
| 150 | |
| 151 | Ok(()) |
| 152 | } |
| 153 | |
| 154 | pub async fn delete_uploads(user_id: Id, db: &Connection) -> Result<(), ServerError> { |
| 155 | run( |
| 156 | db, |
| 157 | "DELETE FROM attachment WHERE uploader_id = ?1", |
| 158 | (user_id,), |
| 159 | ) |
| 160 | .await?; |
| 161 | |
| 162 | Ok(()) |
| 163 | } |
| 164 | |
| 165 | pub async fn delete_stale_unbound(cutoff: u64, db: &Connection) -> Result<(), ServerError> { |
| 166 | run( |
| 167 | db, |
| 168 | "DELETE FROM attachment WHERE message_id IS NULL AND created_at < ?1", |
| 169 | (cutoff,), |
| 170 | ) |
| 171 | .await?; |
| 172 | |
| 173 | Ok(()) |
| 174 | } |
| 175 | |
| 176 | pub async fn delete_before(cutoff: u64, limit: u32, db: &Connection) -> Result<u64, ServerError> { |
| 177 | run( |
| 178 | db, |
| 179 | "DELETE FROM attachment WHERE id IN (SELECT id FROM attachment WHERE created_at < ?1 ORDER BY id LIMIT ?2)", |
| 180 | (cutoff, limit), |
| 181 | ) |
| 182 | .await |
| 183 | } |
| 184 | |
| 185 | pub async fn files(db: &Connection) -> Result<HashSet<String>, ServerError> { |
| 186 | let files = super::all(db, "SELECT file FROM attachment", (), |r| r.get()).await?; |
| 187 | |
| 188 | Ok(files.into_iter().collect()) |
| 189 | } |
| 190 | |
| 191 | pub async fn quota( |
| 192 | user_id: Id, |
| 193 | community_id: Id, |
| 194 | db: &Connection, |
| 195 | ) -> Result<(u64, u64), ServerError> { |
| 196 | one( |
| 197 | db, |
| 198 | "SELECT (SELECT COALESCE(SUM(size), 0) FROM attachment WHERE uploader_id = ?1), (SELECT COALESCE(SUM(size), 0) FROM attachment WHERE community_id = ?2)", |
| 199 | (user_id, community_id), |
| 200 | |r| Ok((r.get()?, r.get()?)), |
| 201 | ) |
| 202 | .await |
| 203 | } |
| 204 | |
| 205 | pub async fn all( |
| 206 | user_id: Option<Id>, |
| 207 | community_id: Option<Id>, |
| 208 | db: &Connection, |
| 209 | ) -> Result<Vec<AttachmentInfo>, ServerError> { |
| 210 | let rows = super::all( |
| 211 | db, |
| 212 | &format!( |
| 213 | "SELECT {COLUMNS}, attachment.uploader_id, attachment.community_id FROM attachment WHERE (?1 IS NULL OR attachment.uploader_id = ?1) AND (?2 IS NULL OR attachment.community_id = ?2) ORDER BY attachment.size DESC LIMIT ?3" |
| 214 | ), |
| 215 | (user_id, community_id, LIST_LIMIT), |
| 216 | |r| Ok((row(r)?, r.get::<Id>()?, r.get::<Id>()?)), |
| 217 | ) |
| 218 | .await?; |
| 219 | |
| 220 | let mut infos = Vec::new(); |
| 221 | for (attachment, uploader_id, community_id) in rows { |
| 222 | infos.push(AttachmentInfo { |
| 223 | attachment, |
| 224 | uploader: Some(user::read(uploader_id, db).await?), |
| 225 | community: community::label(community_id, db).await?, |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | Ok(infos) |
| 230 | } |
| 231 | |
| 232 | pub async fn usage(db: &Connection) -> Result<(Vec<Usage>, Vec<CommunityUsage>), ServerError> { |
| 233 | let by_user: Vec<(Id, u64, u64)> = super::all( |
| 234 | db, |
| 235 | "SELECT uploader_id, SUM(size), COUNT(*) FROM attachment GROUP BY uploader_id ORDER BY SUM(size) DESC LIMIT ?1", |
| 236 | (USAGE_LIMIT,), |
| 237 | |r| Ok((r.get()?, r.get()?, r.get()?)), |
| 238 | ) |
| 239 | .await?; |
| 240 | let by_community: Vec<(Id, u64, u64)> = super::all( |
| 241 | db, |
| 242 | "SELECT community_id, SUM(size), COUNT(*) FROM attachment GROUP BY community_id ORDER BY SUM(size) DESC LIMIT ?1", |
| 243 | (USAGE_LIMIT,), |
| 244 | |r| Ok((r.get()?, r.get()?, r.get()?)), |
| 245 | ) |
| 246 | .await?; |
| 247 | |
| 248 | let mut users = Vec::new(); |
| 249 | for (user_id, bytes, count) in by_user { |
| 250 | users.push(Usage { |
| 251 | user: user::read(user_id, db).await?, |
| 252 | bytes, |
| 253 | count, |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | let mut communities = Vec::new(); |
| 258 | for (community_id, bytes, count) in by_community { |
| 259 | communities.push(CommunityUsage { |
| 260 | community: community::label(community_id, db).await?, |
| 261 | community_id, |
| 262 | bytes, |
| 263 | count, |
| 264 | }); |
| 265 | } |
| 266 | |
| 267 | Ok((users, communities)) |
| 268 | } |