From d905202afbd12d57ba368df1f7c79ccc2ff496fe Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 31 Dec 2024 13:04:27 -0800 Subject: [PATCH] refac --- backend/open_webui/models/messages.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/open_webui/models/messages.py b/backend/open_webui/models/messages.py index 4f095a8b5..12f90053c 100644 --- a/backend/open_webui/models/messages.py +++ b/backend/open_webui/models/messages.py @@ -251,6 +251,18 @@ class MessageTable: db.commit() return True + def delete_reactions_by_id(self, id: str) -> bool: + with get_db() as db: + db.query(MessageReaction).filter_by(message_id=id).delete() + db.commit() + return True + + def delete_replies_by_id(self, id: str) -> bool: + with get_db() as db: + db.query(Message).filter_by(parent_id=id).delete() + db.commit() + return True + def delete_message_by_id(self, id: str) -> bool: with get_db() as db: db.query(Message).filter_by(id=id).delete() @@ -258,6 +270,16 @@ class MessageTable: # Delete all reactions to this message db.query(MessageReaction).filter_by(message_id=id).delete() + # List all replies to this message + replies = db.query(Message).filter_by(parent_id=id).all() + + # Delete all reactions to each reply + for reply in replies: + db.query(MessageReaction).filter_by(message_id=reply.id).delete() + + # Delete all replies to this message + db.query(Message).filter_by(parent_id=id).delete() + db.commit() return True