From 816bdf0320608690ab90d0ea575e197129bda594 Mon Sep 17 00:00:00 2001 From: hjlarry Date: Sun, 31 Aug 2025 00:28:01 +0800 Subject: [PATCH] add delete comment and reply --- .../console/app/workflow_comment.py | 20 +----- api/services/workflow_comment_service.py | 63 +++++++++++++------ 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/api/controllers/console/app/workflow_comment.py b/api/controllers/console/app/workflow_comment.py index 6088cfbbf9..6896ee6789 100644 --- a/api/controllers/console/app/workflow_comment.py +++ b/api/controllers/console/app/workflow_comment.py @@ -118,7 +118,7 @@ class WorkflowCommentDetailApi(Resource): user_id=current_user.id, ) - return {"message": "Comment deleted successfully"}, 200 + return {"result": "success"}, 204 class WorkflowCommentResolveApi(Resource): @@ -140,22 +140,6 @@ class WorkflowCommentResolveApi(Resource): return comment - @login_required - @setup_required - @account_initialization_required - @get_app_model - @marshal_with(workflow_comment_resolve_fields) - def delete(self, app_model: App, comment_id: str): - """Reopen a resolved workflow comment.""" - comment = WorkflowCommentService.reopen_comment( - tenant_id=current_user.current_tenant_id, - app_id=app_model.id, - comment_id=comment_id, - user_id=current_user.id, - ) - - return comment - class WorkflowCommentReplyApi(Resource): """API for managing comment replies.""" @@ -229,7 +213,7 @@ class WorkflowCommentReplyDetailApi(Resource): WorkflowCommentService.delete_reply(reply_id=reply_id, user_id=current_user.id) - return {"message": "Reply deleted successfully"}, 200 + return {"result": "success"}, 204 # Register API routes diff --git a/api/services/workflow_comment_service.py b/api/services/workflow_comment_service.py index 010dd3b1ed..e205c515f5 100644 --- a/api/services/workflow_comment_service.py +++ b/api/services/workflow_comment_service.py @@ -177,14 +177,29 @@ class WorkflowCommentService: @staticmethod def delete_comment(tenant_id: str, app_id: str, comment_id: str, user_id: str) -> None: """Delete a workflow comment.""" - comment = WorkflowCommentService.get_comment(tenant_id, app_id, comment_id) + with Session(db.engine, expire_on_commit=False) as session: + comment = WorkflowCommentService.get_comment(tenant_id, app_id, comment_id, session) - # Only the creator can delete the comment - if comment.created_by != user_id: - raise Forbidden("Only the comment creator can delete it") + # Only the creator can delete the comment + if comment.created_by != user_id: + raise Forbidden("Only the comment creator can delete it") - db.session.delete(comment) - db.session.commit() + # Delete associated mentions (both comment and reply mentions) + mentions = session.scalars( + select(WorkflowCommentMention).where(WorkflowCommentMention.comment_id == comment_id) + ).all() + for mention in mentions: + session.delete(mention) + + # Delete associated replies + replies = session.scalars( + select(WorkflowCommentReply).where(WorkflowCommentReply.comment_id == comment_id) + ).all() + for reply in replies: + session.delete(reply) + + session.delete(comment) + session.commit() @staticmethod def resolve_comment(tenant_id: str, app_id: str, comment_id: str, user_id: str) -> WorkflowComment: @@ -233,7 +248,7 @@ class WorkflowCommentService: # Create mention linking to specific reply mention = WorkflowCommentMention( comment_id=comment_id, - reply_id=reply.id, # This is a reply mention + reply_id=reply.id, mentioned_user_id=user_id ) session.add(mention) @@ -289,25 +304,33 @@ class WorkflowCommentService: session.add(mention) session.commit() - - return { - "id": reply.id, - "updated_at": reply.updated_at - } + + return { + "id": reply.id, + "updated_at": reply.updated_at + } @staticmethod def delete_reply(reply_id: str, user_id: str) -> None: """Delete a comment reply.""" - reply = db.session.get(WorkflowCommentReply, reply_id) - if not reply: - raise NotFound("Reply not found") + with Session(db.engine, expire_on_commit=False) as session: + reply = session.get(WorkflowCommentReply, reply_id) + if not reply: + raise NotFound("Reply not found") - # Only the creator can delete the reply - if reply.created_by != user_id: - raise Forbidden("Only the reply creator can delete it") + # Only the creator can delete the reply + if reply.created_by != user_id: + raise Forbidden("Only the reply creator can delete it") - db.session.delete(reply) - db.session.commit() + # Delete associated mentions first + mentions = session.scalars( + select(WorkflowCommentMention).where(WorkflowCommentMention.reply_id == reply_id) + ).all() + for mention in mentions: + session.delete(mention) + + session.delete(reply) + session.commit() @staticmethod def validate_comment_access(comment_id: str, tenant_id: str, app_id: str) -> WorkflowComment: