diff --git a/SharpbinV3.Client/src/lib/components/PasteCommentItem.svelte b/SharpbinV3.Client/src/lib/components/PasteCommentItem.svelte index 1115bba..aca089b 100644 --- a/SharpbinV3.Client/src/lib/components/PasteCommentItem.svelte +++ b/SharpbinV3.Client/src/lib/components/PasteCommentItem.svelte @@ -1,13 +1,14 @@
@@ -94,7 +112,11 @@ {/if} {#if node.updatedAt} - edited + edited {/if}
-
+
{#if expanded || !isLongComment} {@html parsedHtml} @@ -164,7 +186,7 @@ {#if !replyOpen} +
+ + {#if hasRole($user.roles, 4)} + + {/if} +
{/if}
@@ -227,6 +269,7 @@ {reactingIds} {onReact} {onReply} + {deleteCommentAction} /> {/each}
diff --git a/SharpbinV3.Client/src/lib/components/PasteComments.svelte b/SharpbinV3.Client/src/lib/components/PasteComments.svelte index a2a2676..8af95a1 100644 --- a/SharpbinV3.Client/src/lib/components/PasteComments.svelte +++ b/SharpbinV3.Client/src/lib/components/PasteComments.svelte @@ -11,6 +11,7 @@ import PasteCommentItem from '$lib/components/PasteCommentItem.svelte'; import { SvelteMap } from 'svelte/reactivity'; import RichTextEditor from './RichTextEditor.svelte'; + import { openModal } from '$lib/stores/modal'; interface Props { pasteId: string; @@ -187,6 +188,52 @@ async function submitTopLevelComment(content: string) { await postComment(content, null); } + + async function deleteComment(commentId: number, hardDelete = false): Promise { + errorMessage = ''; + const token = getToken(); + if (!token) { + errorMessage = 'You must be signed in to delete comments.'; + return false; + } + + const title = hardDelete ? 'Hard delete comment?' : 'Delete comment?'; + const message = hardDelete + ? 'This permanently removes the comment from the thread. This cannot be undone.' + : 'This will remove the comment content while preserving the thread structure.'; + const confirmButtonText = hardDelete ? 'Hard delete' : 'Delete'; + const ok = await openModal({ + mode: 'confirm', + title, + message, + confirmButtonText, + cancelValue: false + }); + if (!ok) { + return false; + } + + try { + const query = hardDelete ? '?hardDelete=true' : ''; + const res = await fetch(`/api/paste/${pasteId}/comments/${commentId}${query}`, { + method: 'DELETE', + headers: { + Authorization: `Bearer ${token}` + } + }); + if (!res.ok) { + const err = await res.json().catch(() => ({})); + errorMessage = err?.message || 'Failed to delete comment.'; + return false; + } + + await refreshComments(); + return true; + } catch { + errorMessage = 'Failed to delete comment.'; + return false; + } + }
@@ -239,6 +286,7 @@ {reactingIds} onReact={reactToComment} onReply={(parentCommentID, content) => postComment(content, parentCommentID)} + deleteCommentAction={deleteComment} /> {/each}
diff --git a/SharpbinV3.Server/Controllers/PasteController.cs b/SharpbinV3.Server/Controllers/PasteController.cs index 4452526..9981e0a 100644 --- a/SharpbinV3.Server/Controllers/PasteController.cs +++ b/SharpbinV3.Server/Controllers/PasteController.cs @@ -514,6 +514,40 @@ namespace SharpbinV3.Server.Controllers ); } + [HttpDelete] + [Authorize(Policy = "JwtOnly")] + [Route("{id}/comments/{commentId}")] + public async Task DeleteComment(string id, long commentId, bool hardDelete = false) + { + var paste = await _pasteService.Get(id); + if (paste == null || paste.ExpiresAt != 0 && paste.ExpiresAt < DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) + return NotFound(new { success = false, message = "Paste not found" }); + + var comment = await _commentService.GetCommentByID(commentId, paste.PID); + if (comment == null) + return NotFound(new { success = false, message = "Comment not found for this paste." }); + + var user = await _authService.GetUserFromHttpContext(HttpContext); + if (user == null) + return Unauthorized(new { success = false, message = "User not authenticated." }); + + var hasPrivilegedRole = user.Roles.HasFlag(Role.Admin) || user.Roles.HasFlag(Role.Moderator); + if (comment.UserUUID != user.UUID && !hasPrivilegedRole) + return StatusCode(403, new { success = false, message = "You do not have permission to delete this comment." }); + if (hardDelete && !hasPrivilegedRole) + return StatusCode(403, new { success = false, message = "You do not have permission to hard delete this comment." }); + + bool result; + if (hardDelete) + result = await _commentService.HardDeleteComment(comment); + else + result = await _commentService.DeleteComment(comment); + + if (!result) + return StatusCode(500, new { success = false, message = "An error occurred while deleting the comment." }); + return Ok(new { success = true, message = "Comment deleted successfully." }); + } + [HttpGet] [EnableRateLimiting("NoLimit")] [Route("info")]