mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat(comments): implement delete comment functionality with confirmation modal
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
<script lang="ts">
|
||||
import type { CommentReaction, CommentTreeNode } from '$lib/types/comment';
|
||||
import { dateToRelativeString } from '$lib/utils/misc';
|
||||
import { dateToRelativeString, tooltip } from '$lib/utils/misc';
|
||||
import { parseCommentMarkdown } from '$lib/utils/markdown';
|
||||
import { escapeHtml } from '$lib/utils/html';
|
||||
import { user } from '$lib/stores/user';
|
||||
import PasteCommentItem from '$lib/components/PasteCommentItem.svelte';
|
||||
import { ThumbsUp, ThumbsDown } from '@lucide/svelte';
|
||||
import { ThumbsUp, ThumbsDown, Trash2, Shredder } from '@lucide/svelte';
|
||||
import { resolve } from '$app/paths';
|
||||
import RichTextEditor from './RichTextEditor.svelte';
|
||||
import { hasRole } from '$lib/utils/auth';
|
||||
|
||||
interface Props {
|
||||
node: CommentTreeNode;
|
||||
@@ -16,9 +17,19 @@
|
||||
reactingIds: Record<number, boolean>;
|
||||
onReact: (commentId: number, target: 1 | 2, current: CommentReaction) => Promise<void>;
|
||||
onReply: (parentCommentID: number, content: string) => Promise<boolean>;
|
||||
deleteCommentAction: (commentId: number, hardDelete?: boolean) => Promise<boolean>;
|
||||
}
|
||||
|
||||
let { node, depth = 1, now, reactingIds, onReact, onReply }: Props = $props();
|
||||
let {
|
||||
node,
|
||||
depth = 1,
|
||||
now,
|
||||
reactingIds,
|
||||
onReact,
|
||||
onReply,
|
||||
deleteCommentAction
|
||||
}: Props = $props();
|
||||
let deleting = $state(false);
|
||||
|
||||
const maxCommentLength = 320;
|
||||
let expanded = $state(false);
|
||||
@@ -29,7 +40,7 @@
|
||||
let replyError = $state('');
|
||||
let replyEditor = $state<{ focus: () => void } | undefined>(undefined);
|
||||
|
||||
let commentText = $derived(node.content ?? 'Deleted comment');
|
||||
let commentText = $derived(node.content ?? '*[Deleted comment]*');
|
||||
let isLongComment = $derived(commentText.length > maxCommentLength);
|
||||
let isDeleted = $derived(node.content === null);
|
||||
let parsedHtml = $derived(
|
||||
@@ -73,6 +84,13 @@
|
||||
replyEditor?.focus();
|
||||
});
|
||||
}
|
||||
|
||||
async function handleDelete(hardDelete = false) {
|
||||
if (deleting) return;
|
||||
deleting = true;
|
||||
await deleteCommentAction(node.id, hardDelete);
|
||||
deleting = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="p-3">
|
||||
@@ -94,7 +112,11 @@
|
||||
</a>
|
||||
{/if}
|
||||
{#if node.updatedAt}
|
||||
<span class="text-neutral-500">edited</span>
|
||||
<span
|
||||
class="text-sm text-neutral-500"
|
||||
use:tooltip={`Edited on ${new Date(node.updatedAt * 1000).toLocaleString()} • ${dateToRelativeString(new Date(node.updatedAt * 1000), true, true, now, 3)}`}
|
||||
>edited</span
|
||||
>
|
||||
{/if}
|
||||
</div>
|
||||
<span class="shrink-0"
|
||||
@@ -103,7 +125,7 @@
|
||||
</div>
|
||||
|
||||
<div class="text-sm leading-relaxed text-neutral-200">
|
||||
<div class="wrap-break-word" class:text-neutral-500={isDeleted}>
|
||||
<div class="wrap-break-word" class:italic={isDeleted} class:text-neutral-800={isDeleted}>
|
||||
{#if expanded || !isLongComment}
|
||||
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
|
||||
{@html parsedHtml}
|
||||
@@ -164,7 +186,7 @@
|
||||
{#if !replyOpen}
|
||||
<button
|
||||
type="button"
|
||||
class="h-7 min-w-12 rounded border border-neutral-700 px-2 py-1 text-xs text-neutral-300 hover:text-neutral-100"
|
||||
class="h-7 min-w-12 rounded border border-neutral-700 px-2 py-1 text-xs text-neutral-300 hover:bg-neutral-800 hover:text-neutral-100 active:bg-neutral-700"
|
||||
onclick={() => {
|
||||
openReply(node.content ? `> ${node.content.replace(/\n/g, '\n> ')}\n\n` : '');
|
||||
}}
|
||||
@@ -174,7 +196,7 @@
|
||||
{/if}
|
||||
<button
|
||||
type="button"
|
||||
class="h-7 min-w-12 rounded border border-neutral-700 px-2 py-1 text-xs text-neutral-300 hover:text-neutral-100"
|
||||
class="h-7 min-w-12 rounded border border-neutral-700 px-2 py-1 text-xs text-neutral-300 hover:bg-neutral-800 hover:text-neutral-100 active:bg-neutral-700"
|
||||
onclick={() => {
|
||||
if (replyOpen) {
|
||||
replyOpen = false;
|
||||
@@ -187,6 +209,26 @@
|
||||
>
|
||||
{replyOpen ? 'Cancel' : 'Reply'}
|
||||
</button>
|
||||
<div class="ml-auto">
|
||||
<button
|
||||
type="button"
|
||||
class="h-7 rounded border border-neutral-700 px-2 py-1 text-xs text-neutral-300 hover:bg-neutral-800 hover:text-neutral-100 active:bg-neutral-700"
|
||||
onclick={() => handleDelete(false)}
|
||||
disabled={deleting}
|
||||
>
|
||||
<Trash2 class="inline h-3.5 w-3.5" />
|
||||
</button>
|
||||
{#if hasRole($user.roles, 4)}
|
||||
<button
|
||||
type="button"
|
||||
class="h-7 rounded border border-neutral-700 px-2 py-1 text-xs text-neutral-300 hover:bg-neutral-800 hover:text-neutral-100 active:bg-neutral-700"
|
||||
onclick={() => handleDelete(true)}
|
||||
disabled={deleting}
|
||||
>
|
||||
<Shredder class="inline h-3.5 w-3.5" />
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
@@ -227,6 +269,7 @@
|
||||
{reactingIds}
|
||||
{onReact}
|
||||
{onReply}
|
||||
{deleteCommentAction}
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
@@ -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<boolean> {
|
||||
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<boolean>({
|
||||
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;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<section id="comments-section" class="mt-6 rounded border border-neutral-800 p-4">
|
||||
@@ -239,6 +286,7 @@
|
||||
{reactingIds}
|
||||
onReact={reactToComment}
|
||||
onReply={(parentCommentID, content) => postComment(content, parentCommentID)}
|
||||
deleteCommentAction={deleteComment}
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
@@ -514,6 +514,40 @@ namespace SharpbinV3.Server.Controllers
|
||||
);
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Authorize(Policy = "JwtOnly")]
|
||||
[Route("{id}/comments/{commentId}")]
|
||||
public async Task<IActionResult> 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")]
|
||||
|
||||
Reference in New Issue
Block a user