feat(comments): implement recursive comment deletion in HardDeleteComment method

This commit is contained in:
2026-04-24 19:17:26 -04:00
parent 95ca811d42
commit 8d3caf5375
2 changed files with 36 additions and 15 deletions
@@ -209,26 +209,28 @@
> >
{replyOpen ? 'Cancel' : 'Reply'} {replyOpen ? 'Cancel' : 'Reply'}
</button> </button>
<div class="ml-auto"> {#if node.author?.uuid === $user.uuid || hasRole($user.roles, 4) || hasRole($user.roles, 2)}
<button <div class="ml-auto">
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 <button
type="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" 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)} onclick={() => handleDelete(false)}
disabled={deleting} disabled={deleting}
> >
<Shredder class="inline h-3.5 w-3.5" /> <Trash2 class="inline h-3.5 w-3.5" />
</button> </button>
{/if} {#if hasRole($user.roles, 4) || hasRole($user.roles, 2)}
</div> <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}
{/if} {/if}
</div> </div>
+20 -1
View File
@@ -78,7 +78,26 @@ namespace SharpbinV3.Server.Services
public async Task<bool> HardDeleteComment(Comment comment) public async Task<bool> HardDeleteComment(Comment comment)
{ {
_db.Comments.Remove(comment); var commentIdsToDelete = new HashSet<long> { comment.Id };
var currentLevelIds = new List<long> { comment.Id };
while (currentLevelIds.Count != 0)
{
var childIds = await _db
.Comments.Where(c => c.ParentCommentID.HasValue && currentLevelIds.Contains(c.ParentCommentID.Value))
.Select(c => c.Id)
.ToListAsync();
currentLevelIds = [];
foreach (var childId in childIds)
{
if (commentIdsToDelete.Add(childId))
currentLevelIds.Add(childId);
}
}
var commentsToDelete = await _db.Comments.Where(c => commentIdsToDelete.Contains(c.Id)).ToListAsync();
_db.Comments.RemoveRange(commentsToDelete);
var result = await _db.SaveChangesAsync(); var result = await _db.SaveChangesAsync();
return result > 0; return result > 0;
} }