From 59b18fd37635862094b61f5246f0cb154b560285 Mon Sep 17 00:00:00 2001 From: Paul <paulmakles@gmail.com> Date: Sat, 1 May 2021 17:12:51 +0100 Subject: [PATCH] Delete old avatar; fix database migration. --- src/database/entities/autumn.rs | 21 +++++++++++++++++++++ src/database/entities/message.rs | 18 +----------------- src/database/migrations/scripts.rs | 2 +- src/routes/users/edit_user.rs | 4 ++++ 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/database/entities/autumn.rs b/src/database/entities/autumn.rs index 8c57250..0838f99 100644 --- a/src/database/entities/autumn.rs +++ b/src/database/entities/autumn.rs @@ -86,4 +86,25 @@ impl File { Err(Error::UnknownAttachment) } } + + pub async fn delete(&self) -> Result<()> { + get_collection("attachments") + .update_one( + doc! { + "_id": &self.id + }, + doc! { + "$set": { + "deleted": true + } + }, + None, + ) + .await + .map(|_| ()) + .map_err(|_| Error::DatabaseError { + operation: "update_one", + with: "attachment", + }) + } } diff --git a/src/database/entities/message.rs b/src/database/entities/message.rs index 0ef9f01..35f484d 100644 --- a/src/database/entities/message.rs +++ b/src/database/entities/message.rs @@ -236,23 +236,7 @@ impl Message { pub async fn delete(&self) -> Result<()> { if let Some(attachment) = &self.attachment { - get_collection("attachments") - .update_one( - doc! { - "_id": &attachment.id - }, - doc! { - "$set": { - "deleted": true - } - }, - None, - ) - .await - .map_err(|_| Error::DatabaseError { - operation: "update_one", - with: "attachment", - })?; + attachment.delete().await?; } get_collection("messages") diff --git a/src/database/migrations/scripts.rs b/src/database/migrations/scripts.rs index 055f2ec..d926ec0 100644 --- a/src/database/migrations/scripts.rs +++ b/src/database/migrations/scripts.rs @@ -10,7 +10,7 @@ struct MigrationInfo { revision: i32, } -pub const LATEST_REVISION: i32 = 1; +pub const LATEST_REVISION: i32 = 2; pub async fn migrate_database() { let migrations = get_collection("migrations"); diff --git a/src/routes/users/edit_user.rs b/src/routes/users/edit_user.rs index 5dfde79..eace4eb 100644 --- a/src/routes/users/edit_user.rs +++ b/src/routes/users/edit_user.rs @@ -67,6 +67,10 @@ pub async fn req(user: User, mut data: Json<Data>, _ignore_id: String) -> Result .publish(user.id.clone()) .await .ok(); + + if let Some(old_avatar) = user.avatar { + old_avatar.delete().await?; + } } Ok(()) -- GitLab