use std::collections::HashSet; use common::{ error::Error, model::{Attachment, AttachmentInfo, AttachmentKind, CommunityUsage, Id, UserUsage}, time::now_ms, }; use super::{Connection, Reader, all, community, one, run, user}; use crate::error::{ServerError, ensure_with}; const LIST_LIMIT: u32 = 200; const USAGE_LIMIT: u32 = 50; const COLUMNS: &str = "attachment.id, attachment.name, attachment.size, attachment.kind, attachment.width, attachment.height, attachment.created_at"; pub struct Stored { pub attachment: Attachment, pub file: String, pub uploader_id: Id, pub community_id: Id, pub message_id: Option, } fn row(r: &mut Reader) -> Result { Ok(Attachment { id: r.get()?, name: r.get()?, size: r.get()?, kind: r.named()?, width: r.get()?, height: r.get()?, created_at: r.get()?, }) } pub async fn of_message( message_id: Id, snippet_position: Option, db: &Connection, ) -> Result, ServerError> { all( db, &format!( "SELECT {COLUMNS} FROM attachment WHERE message_id = ?1 AND snippet_position IS ?2 ORDER BY id" ), (message_id, snippet_position), row, ) .await } pub async fn bind( message_id: Id, user_id: Id, community_id: Id, attachment_ids: &[Id], db: &Connection, ) -> Result<(), ServerError> { for attachment_id in attachment_ids { let bound = run( db, "UPDATE attachment SET message_id = ?1 WHERE id = ?2 AND uploader_id = ?3 AND community_id = ?4 AND message_id IS NULL", (message_id, *attachment_id, user_id, community_id), ) .await?; ensure_with(bound == 1, Error::NotFound)?; } Ok(()) } pub async fn copy_row( id: Id, message_id: Id, user_id: Id, community_id: Id, snippet_position: Option, db: &Connection, ) -> Result<(), ServerError> { let copied = run( db, "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", (id, user_id, community_id, message_id, snippet_position, now_ms()), ) .await?; ensure_with(copied == 1, Error::NotFound) } #[allow(clippy::too_many_arguments)] pub async fn insert( uploader_id: Id, community_id: Id, file: &str, name: &str, size: u64, kind: AttachmentKind, width: Option, height: Option, db: &Connection, ) -> Result { let id: Id = one( db, "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", ( uploader_id, community_id, now_ms(), file, name, size, kind.as_str(), width, height, ), |r| r.get(), ) .await?; Ok(read(id, db).await?.attachment) } pub async fn read(id: Id, db: &Connection) -> Result { one( db, &format!( "SELECT {COLUMNS}, attachment.file, attachment.uploader_id, attachment.community_id, attachment.message_id FROM attachment WHERE attachment.id = ?1" ), (id,), |r| { Ok(Stored { attachment: row(r)?, file: r.get()?, uploader_id: r.get()?, community_id: r.get()?, message_id: r.get()?, }) }, ) .await } pub async fn delete_file(id: Id, db: &Connection) -> Result, ServerError> { let message_ids: Vec> = all( db, "DELETE FROM attachment WHERE file = (SELECT file FROM attachment WHERE id = ?1) RETURNING message_id", (id,), |r| r.get(), ) .await?; Ok(message_ids.into_iter().flatten().collect()) } pub async fn delete_uploads(user_id: Id, db: &Connection) -> Result<(), ServerError> { run( db, "DELETE FROM attachment WHERE uploader_id = ?1", (user_id,), ) .await?; Ok(()) } pub async fn delete_stale_unbound(cutoff: u64, db: &Connection) -> Result<(), ServerError> { run( db, "DELETE FROM attachment WHERE message_id IS NULL AND created_at < ?1", (cutoff,), ) .await?; Ok(()) } pub async fn delete_before(cutoff: u64, limit: u32, db: &Connection) -> Result { run( db, "DELETE FROM attachment WHERE id IN (SELECT id FROM attachment WHERE created_at < ?1 ORDER BY id LIMIT ?2)", (cutoff, limit), ) .await } pub async fn files(db: &Connection) -> Result, ServerError> { let files = all(db, "SELECT file FROM attachment", (), |r| r.get()).await?; Ok(files.into_iter().collect()) } pub async fn quota( user_id: Id, community_id: Id, db: &Connection, ) -> Result<(u64, u64), ServerError> { one( db, "SELECT (SELECT COALESCE(SUM(size), 0) FROM (SELECT DISTINCT file, size FROM attachment WHERE uploader_id = ?1)), (SELECT COALESCE(SUM(size), 0) FROM (SELECT DISTINCT file, size FROM attachment WHERE community_id = ?2))", (user_id, community_id), |r| Ok((r.get()?, r.get()?)), ) .await } pub async fn list( user_id: Option, community_id: Option, db: &Connection, ) -> Result, ServerError> { let rows = all( db, &format!( "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" ), (user_id, community_id, LIST_LIMIT), |r| Ok((row(r)?, r.get::()?, r.get::()?)), ) .await?; let mut infos = Vec::new(); for (attachment, uploader_id, community_id) in rows { infos.push(AttachmentInfo { attachment, uploader: Some(user::read(uploader_id, db).await?), community: community::label(community_id, db).await?, }); } Ok(infos) } pub async fn usage(db: &Connection) -> Result<(Vec, Vec), ServerError> { let by_user: Vec<(Id, u64, u64)> = all( db, "SELECT uploader_id, SUM(size), COUNT(*) FROM (SELECT DISTINCT uploader_id, file, size FROM attachment) GROUP BY uploader_id ORDER BY SUM(size) DESC LIMIT ?1", (USAGE_LIMIT,), |r| Ok((r.get()?, r.get()?, r.get()?)), ) .await?; let by_community: Vec<(Id, u64, u64)> = all( db, "SELECT community_id, SUM(size), COUNT(*) FROM (SELECT DISTINCT community_id, file, size FROM attachment) GROUP BY community_id ORDER BY SUM(size) DESC LIMIT ?1", (USAGE_LIMIT,), |r| Ok((r.get()?, r.get()?, r.get()?)), ) .await?; let mut users = Vec::new(); for (user_id, bytes, count) in by_user { users.push(UserUsage { user: user::read(user_id, db).await?, bytes, count, }); } let mut communities = Vec::new(); for (community_id, bytes, count) in by_community { communities.push(CommunityUsage { community: community::label(community_id, db).await?, community_id, bytes, count, }); } Ok((users, communities)) }