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,6 +209,7 @@
>
{replyOpen ? 'Cancel' : 'Reply'}
</button>
{#if node.author?.uuid === $user.uuid || hasRole($user.roles, 4) || hasRole($user.roles, 2)}
<div class="ml-auto">
<button
type="button"
@@ -218,7 +219,7 @@
>
<Trash2 class="inline h-3.5 w-3.5" />
</button>
{#if hasRole($user.roles, 4)}
{#if hasRole($user.roles, 4) || hasRole($user.roles, 2)}
<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"
@@ -230,6 +231,7 @@
{/if}
</div>
{/if}
{/if}
</div>
{#if replyOpen}
+20 -1
View File
@@ -78,7 +78,26 @@ namespace SharpbinV3.Server.Services
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();
return result > 0;
}