Delete old avatar; fix database migration.

This commit is contained in:
Paul
2021-05-01 17:12:51 +01:00
parent f135a57a9b
commit 59b18fd376
4 changed files with 27 additions and 18 deletions

View File

@@ -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",
})
}
}

View File

@@ -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")

View File

@@ -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");

View File

@@ -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(())