From 10cad605661f3ee7342041f58690568e20115b25 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 2 Apr 2024 07:55:56 -0700 Subject: [PATCH] feat: chat cascade delete --- backend/apps/web/models/chats.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/backend/apps/web/models/chats.py b/backend/apps/web/models/chats.py index f874958f8..a57434dbe 100644 --- a/backend/apps/web/models/chats.py +++ b/backend/apps/web/models/chats.py @@ -224,7 +224,7 @@ class ChatTable: query = Chat.delete().where((Chat.id == id) & (Chat.user_id == user_id)) query.execute() # Remove the rows, return number of rows removed. - return True + return True and self.delete_shared_chat_by_chat_id(id) except: return False @@ -233,6 +233,20 @@ class ChatTable: query = Chat.delete().where(Chat.user_id == user_id) query.execute() # Remove the rows, return number of rows removed. + return True and self.delete_shared_chats_by_user_id(user_id) + except: + return False + + def delete_shared_chats_by_user_id(self, user_id: str) -> bool: + try: + shared_chat_ids = [ + f"shared-{chat.id}" + for chat in Chat.select().where(Chat.user_id == user_id) + ] + + query = Chat.delete().where(Chat.user_id << shared_chat_ids) + query.execute() # Remove the rows, return number of rows removed. + return True except: return False