From 9a2ccecb89df9e25da0de0e1495cba38e1c0d51e Mon Sep 17 00:00:00 2001
From: Paul <paulmakles@gmail.com>
Date: Wed, 16 Jun 2021 15:42:46 +0100
Subject: [PATCH] Channels: Make sure to delete last, to keep integrity.

---
 src/database/entities/channel.rs | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/database/entities/channel.rs b/src/database/entities/channel.rs
index d110fa8..0ab74fc 100644
--- a/src/database/entities/channel.rs
+++ b/src/database/entities/channel.rs
@@ -189,19 +189,6 @@ impl Channel {
                 with: "messages",
             })?;
 
-        get_collection("channels")
-            .delete_one(
-                doc! {
-                    "_id": id
-                },
-                None,
-            )
-            .await
-            .map_err(|_| Error::DatabaseError {
-                operation: "delete_one",
-                with: "channel",
-            })?;
-
         // Remove from server object.
         if let Channel::TextChannel { server, .. } = &self {
             let server = Ref::from_unchecked(server.clone()).fetch_server().await?;
@@ -258,6 +245,20 @@ impl Channel {
                 })?;
         }
 
+        // Finally, delete the channel object.
+        get_collection("channels")
+            .delete_one(
+                doc! {
+                    "_id": id
+                },
+                None,
+            )
+            .await
+            .map_err(|_| Error::DatabaseError {
+                operation: "delete_one",
+                with: "channel",
+            })?;
+
         ClientboundNotification::ChannelDelete { id: id.to_string() }.publish(id.to_string());
 
         if let Channel::Group { icon, .. } = self {
-- 
GitLab