feat: allow users to delete their own comments#932
Merged
Conversation
Add a delete button to TkAction that is only visible when the current user owns the comment (determined by the server-side `isOwner` field in toCommentDto). The server validates ownership via uid comparison before allowing deletion. All 5 server backends (CloudBase, self-hosted LokiJS, self-hosted MongoDB, Vercel, EdgeOne Pages) are supported. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Contributor
审阅者指南(Reviewer's Guide)通过新增 用户本人评论删除流程的时序图(Sequence diagram)sequenceDiagram
actor User
participant TkComment
participant TkAction
participant ClientAPI
participant Server
participant DB
User->>TkAction: click_delete_button
TkAction->>TkComment: emit_delete
TkComment->>User: show_confirm_dialog
User-->>TkComment: confirm_deletion
TkComment->>ClientAPI: call COMMENT_DELETE_FOR_USER(id)
ClientAPI->>Server: event COMMENT_DELETE_FOR_USER with id and accessToken
Server->>DB: find comment by id
DB-->>Server: comment(uid)
alt uid_matches
Server->>DB: delete comment by id
DB-->>Server: deleted_count
Server-->>ClientAPI: code SUCCESS, deleted
ClientAPI-->>TkComment: success_response
TkComment->>TkComment: emit_load_to_refresh_comments
else uid_does_not_match_or_not_found
Server-->>ClientAPI: code FAIL, message
ClientAPI-->>TkComment: error_response
TkComment->>User: show_error_message
end
TkAction、TkComment 和 CommentDto 的更新后类图(Class diagram)classDiagram
class TkAction {
+Number likeCount
+Number dislikeCount
+Number repliesCount
+Boolean showDislike
+Boolean showDelete
+onLike()
+onDislike()
+onReply()
+onDelete()
}
class TkComment {
+Object comment
+Object config
+String pid
+onLike()
+onDislike()
+onReply(id)
+onDelete()
+onReplyReply(id)
}
class CommentDto {
+String id
+String pid
+String uid
+String ruser
+Boolean top
+Boolean isSpam
+Boolean isOwner
+Number created
+Number updated
}
TkComment --> TkAction : uses
CommentDto <.. TkComment : renders_comment_with_isOwner
CommentDto : isOwner = uid_equals_current_user_uid
文件级变更(File-Level Changes)
可能关联的 Issue(Possibly linked issues)
Tips and commands与 Sourcery 交互(Interacting with Sourcery)
自定义你的体验(Customizing Your Experience)进入你的 dashboard 可以:
获取帮助(Getting Help)Original review guide in EnglishReviewer's GuideImplements user-owned comment deletion across all backends and the client UI by adding a Sequence diagram for user-owned comment deletion flowsequenceDiagram
actor User
participant TkComment
participant TkAction
participant ClientAPI
participant Server
participant DB
User->>TkAction: click_delete_button
TkAction->>TkComment: emit_delete
TkComment->>User: show_confirm_dialog
User-->>TkComment: confirm_deletion
TkComment->>ClientAPI: call COMMENT_DELETE_FOR_USER(id)
ClientAPI->>Server: event COMMENT_DELETE_FOR_USER with id and accessToken
Server->>DB: find comment by id
DB-->>Server: comment(uid)
alt uid_matches
Server->>DB: delete comment by id
DB-->>Server: deleted_count
Server-->>ClientAPI: code SUCCESS, deleted
ClientAPI-->>TkComment: success_response
TkComment->>TkComment: emit_load_to_refresh_comments
else uid_does_not_match_or_not_found
Server-->>ClientAPI: code FAIL, message
ClientAPI-->>TkComment: error_response
TkComment->>User: show_error_message
end
Updated class diagram for TkAction, TkComment, and CommentDtoclassDiagram
class TkAction {
+Number likeCount
+Number dislikeCount
+Number repliesCount
+Boolean showDislike
+Boolean showDelete
+onLike()
+onDislike()
+onReply()
+onDelete()
}
class TkComment {
+Object comment
+Object config
+String pid
+onLike()
+onDislike()
+onReply(id)
+onDelete()
+onReplyReply(id)
}
class CommentDto {
+String id
+String pid
+String uid
+String ruser
+Boolean top
+Boolean isSpam
+Boolean isOwner
+Number created
+Number updated
}
TkComment --> TkAction : uses
CommentDto <.. TkComment : renders_comment_with_isOwner
CommentDto : isOwner = uid_equals_current_user_uid
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Closed
Contributor
There was a problem hiding this comment.
Hey - 我发现了 1 个问题,并且留了一些整体性的反馈:
- 在
TkComment.vue中,删除确认对话使用的是ADMIN_COMMENT_DELETE_CONFIRM这个 i18n key,但这是用户自行发起的删除操作;建议新增一个专门面向普通用户的删除确认文案(例如COMMENT_DELETE_CONFIRM),避免带有“管理员”语气的文案泄露到普通用户界面中。 - 对于
COMMENT_DELETE_FOR_USER的归属检查和删除逻辑,目前在所有后端中都有小差异的重复实现;建议抽取一个小的公共 helper(或者至少统一错误信息/常量),以保持行为一致并降低维护成本。
给 AI Agent 的提示
Please address the comments from this code review:
## Overall Comments
- In `TkComment.vue`, the delete confirmation uses the `ADMIN_COMMENT_DELETE_CONFIRM` i18n key for a user-initiated delete; consider introducing a dedicated user-facing confirmation string (e.g. `COMMENT_DELETE_CONFIRM`) to avoid admin-specific wording leaking into the regular UI.
- The ownership-check and delete logic for `COMMENT_DELETE_FOR_USER` is duplicated with only small variations across all backends; consider extracting a small shared helper (or at least centralizing the error messages/constants) to keep the behavior consistent and reduce maintenance overhead.
## Individual Comments
### Comment 1
<location path="src/client/view/components/TkComment.vue" line_range="264-267" />
<code_context>
},
+ async onDelete () {
+ if (!confirm(t('ADMIN_COMMENT_DELETE_CONFIRM'))) return
+ const result = await call(this.$tcb, 'COMMENT_DELETE_FOR_USER', {
+ id: this.comment.id
+ })
+ if (!result.code) {
+ this.$emit('load')
+ }
</code_context>
<issue_to_address>
**suggestion:** Handle and surface backend errors for comment deletion instead of silently ignoring them.
Currently, a non-zero `COMMENT_DELETE_FOR_USER` `code` is ignored. Please handle this case by surfacing an error (e.g., toast or inline message) so users understand why the comment wasn’t deleted.
Suggested implementation:
```
async onDelete () {
if (!confirm(t('ADMIN_COMMENT_DELETE_CONFIRM'))) return
try {
const result = await call(this.$tcb, 'COMMENT_DELETE_FOR_USER', {
id: this.comment.id
})
if (!result || result.code) {
// Surface backend error to the user
const message =
(result && (result.message || result.errorMsg)) ||
t('ADMIN_COMMENT_DELETE_FAILED')
// TODO: replace alert with project-standard toast/notification
alert(message)
return
}
this.$emit('load')
} catch (e) {
console.error('COMMENT_DELETE_FOR_USER failed', e)
// TODO: replace alert with project-standard toast/notification
alert(t('ADMIN_COMMENT_DELETE_FAILED'))
}
},
```
1. Add a translation key `ADMIN_COMMENT_DELETE_FAILED` to your i18n resources (e.g., `"ADMIN_COMMENT_DELETE_FAILED": "Failed to delete comment. Please try again later."` or similar).
2. Replace the `alert(...)` calls with your app’s standard mechanism for surfacing errors (e.g., `this.$toast.error(message)`, `this.$message.error(message)`, or a dedicated inline error component), following existing patterns elsewhere in the codebase.
3. If your backend uses a different property than `message`/`errorMsg` for error text, adjust the error extraction logic accordingly.
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的评审质量。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- In
TkComment.vue, the delete confirmation uses theADMIN_COMMENT_DELETE_CONFIRMi18n key for a user-initiated delete; consider introducing a dedicated user-facing confirmation string (e.g.COMMENT_DELETE_CONFIRM) to avoid admin-specific wording leaking into the regular UI. - The ownership-check and delete logic for
COMMENT_DELETE_FOR_USERis duplicated with only small variations across all backends; consider extracting a small shared helper (or at least centralizing the error messages/constants) to keep the behavior consistent and reduce maintenance overhead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `TkComment.vue`, the delete confirmation uses the `ADMIN_COMMENT_DELETE_CONFIRM` i18n key for a user-initiated delete; consider introducing a dedicated user-facing confirmation string (e.g. `COMMENT_DELETE_CONFIRM`) to avoid admin-specific wording leaking into the regular UI.
- The ownership-check and delete logic for `COMMENT_DELETE_FOR_USER` is duplicated with only small variations across all backends; consider extracting a small shared helper (or at least centralizing the error messages/constants) to keep the behavior consistent and reduce maintenance overhead.
## Individual Comments
### Comment 1
<location path="src/client/view/components/TkComment.vue" line_range="264-267" />
<code_context>
},
+ async onDelete () {
+ if (!confirm(t('ADMIN_COMMENT_DELETE_CONFIRM'))) return
+ const result = await call(this.$tcb, 'COMMENT_DELETE_FOR_USER', {
+ id: this.comment.id
+ })
+ if (!result.code) {
+ this.$emit('load')
+ }
</code_context>
<issue_to_address>
**suggestion:** Handle and surface backend errors for comment deletion instead of silently ignoring them.
Currently, a non-zero `COMMENT_DELETE_FOR_USER` `code` is ignored. Please handle this case by surfacing an error (e.g., toast or inline message) so users understand why the comment wasn’t deleted.
Suggested implementation:
```
async onDelete () {
if (!confirm(t('ADMIN_COMMENT_DELETE_CONFIRM'))) return
try {
const result = await call(this.$tcb, 'COMMENT_DELETE_FOR_USER', {
id: this.comment.id
})
if (!result || result.code) {
// Surface backend error to the user
const message =
(result && (result.message || result.errorMsg)) ||
t('ADMIN_COMMENT_DELETE_FAILED')
// TODO: replace alert with project-standard toast/notification
alert(message)
return
}
this.$emit('load')
} catch (e) {
console.error('COMMENT_DELETE_FOR_USER failed', e)
// TODO: replace alert with project-standard toast/notification
alert(t('ADMIN_COMMENT_DELETE_FAILED'))
}
},
```
1. Add a translation key `ADMIN_COMMENT_DELETE_FAILED` to your i18n resources (e.g., `"ADMIN_COMMENT_DELETE_FAILED": "Failed to delete comment. Please try again later."` or similar).
2. Replace the `alert(...)` calls with your app’s standard mechanism for surfacing errors (e.g., `this.$toast.error(message)`, `this.$message.error(message)`, or a dedicated inline error component), following existing patterns elsewhere in the codebase.
3. If your backend uses a different property than `message`/`errorMsg` for error text, adjust the error extraction logic accordingly.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
- Move delete button to first position in TkAction - Add dedicated COMMENT_DELETE_CONFIRM i18n key for user-facing text - Extract checkCommentOwnership helper in twikoo-func/utils to unify ownership validation across all 5 server backends - Show backend error messages to user via alert() instead of silently ignoring non-zero response codes Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a delete button to TkAction that is only visible when the current user owns the comment (determined by the server-side
isOwnerfield in toCommentDto). The server validates ownership via uid comparison before allowing deletion. All 5 server backends (CloudBase, self-hosted LokiJS, self-hosted MongoDB, Vercel, EdgeOne Pages) are supported.由 Sourcery 提供的摘要
允许终端用户删除自己的评论,在所有后端通过服务器端的所有权验证,并提供相应的 UI 控件。
新功能:
COMMENT_DELETE_FOR_USERAPI/事件,用于处理自助评论删除。增强:
isOwner标志,以指示当前用户是否拥有各条评论。Original summary in English
Summary by Sourcery
Allow end users to delete their own comments with server-side ownership validation across all backends and corresponding UI controls.
New Features:
Enhancements: