@frappe.whitelist()
def message_delete(chat_id: str, socket_room: str):
try:
doc = frappe.get_doc(‘Chat Message VM’,chat_id)
if doc.sender != frappe.session.user:
frappe.publish_realtime(
event=“message_delete_confirmation”,
message={“success”: False,“message”: “Not allowed to perform operation”},
room=socket_room
)
return
threads = frappe.get_list(
‘Chat Threads’,
filters={‘chat_id’: chat_id},
pluck=“name”
)
reactions = frappe.get_list(
‘Chat Reactions’,
filters={‘target_id’: chat_id},
pluck=“name”
)
attachments = frappe.get_list(
‘Chat Attachments’,
filters={‘chat_id’: doc.file_id},
pluck=“name”
)
for thread in threads:
frappe.delete_doc(‘Chat Threads’,thread,force=True,ignore_permissions=True,ignore_on_trash=True)
for reaction in reactions:
frappe.delete_doc(‘Chat Reactions’,reaction,force=True)
for attach in attachments:
frappe.delete_doc(‘Chat Attachments’,attach,force=True)
doc.delete(ignore_permissions=True)
frappe.publish_realtime(
event=“message_delete_confirmation”,
message={“success”: True,“message”: “Message deleted successfully”},
room=socket_room
)
frappe.db.commit()
return
except Exception as e:
frappe.log_error(“Failed to delete message”,frappe.get_traceback())
frappe.publish_realtime(
event=“message_delete_confirmation”,
message={“success”: False,“message”: str(e)},
room=socket_room
)
return
frappe.exceptions.LinkExistsError: Cannot delete or cancel because Chat Message VM 1vrllrl7ur is linked with Chat Threads 3vtqu0e30q