chore: detach emojis on delete

This commit is contained in:
Paul Makles
2022-07-15 14:01:59 +01:00
parent 4f73e43a03
commit d96c9f62c4
6 changed files with 33 additions and 10 deletions

View File

@@ -28,6 +28,7 @@ pub async fn delete_emoji(db: &Db, user: User, id: Ref) -> Result<EmptyResponse>
.throw_permission(db, Permission::ManageCustomisation) .throw_permission(db, Permission::ManageCustomisation)
.await?; .await?;
} }
EmojiParent::Detached => return Ok(EmptyResponse),
}; };
} }

View File

@@ -33,9 +33,9 @@ impl AbstractEmoji for DummyDb {
Ok(()) Ok(())
} }
/// Delete an emoji by its id /// Detach an emoji by its id
async fn delete_emoji(&self, emoji: &Emoji) -> Result<()> { async fn detach_emoji(&self, emoji: &Emoji) -> Result<()> {
info!("Delete {emoji:?}"); info!("Detach {emoji:?}");
Ok(()) Ok(())
} }
} }

View File

@@ -9,6 +9,7 @@ impl Emoji {
fn parent(&self) -> &str { fn parent(&self) -> &str {
match &self.parent { match &self.parent {
EmojiParent::Server { id } => id, EmojiParent::Server { id } => id,
EmojiParent::Detached => "",
} }
} }
@@ -30,7 +31,6 @@ impl Emoji {
.p(self.parent().to_string()) .p(self.parent().to_string())
.await; .await;
db.mark_attachment_as_deleted(&self.id).await?; db.detach_emoji(&self).await
db.delete_emoji(&self).await
} }
} }

View File

@@ -1,5 +1,7 @@
use bson::Document;
use crate::models::Emoji; use crate::models::Emoji;
use crate::{AbstractEmoji, Result}; use crate::{AbstractEmoji, Error, Result};
use super::super::MongoDb; use super::super::MongoDb;
@@ -42,7 +44,26 @@ impl AbstractEmoji for MongoDb {
} }
/// Delete an emoji by its id /// Delete an emoji by its id
async fn delete_emoji(&self, emoji: &Emoji) -> Result<()> { async fn detach_emoji(&self, emoji: &Emoji) -> Result<()> {
self.delete_one_by_id(COL, &emoji.id).await.map(|_| ()) self.col::<Document>(COL)
.update_one(
doc! {
"_id": &emoji.id
},
doc! {
"$set": {
"parent": {
"type": "Detached"
}
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "emojis",
})
} }
} }

View File

@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum EmojiParent { pub enum EmojiParent {
Server { id: String }, Server { id: String },
Detached,
} }
/// Representation of an Emoji on Revolt /// Representation of an Emoji on Revolt

View File

@@ -15,6 +15,6 @@ pub trait AbstractEmoji: Sync + Send {
/// Insert emoji into database. /// Insert emoji into database.
async fn insert_emoji(&self, emoji: &Emoji) -> Result<()>; async fn insert_emoji(&self, emoji: &Emoji) -> Result<()>;
/// Delete an emoji by its id /// Detach an emoji by its id
async fn delete_emoji(&self, emoji: &Emoji) -> Result<()>; async fn detach_emoji(&self, emoji: &Emoji) -> Result<()>;
} }