v7.0.0 release

This commit is contained in:
HeuJung committed 2026-07-01 10:30:32 +09:00
1 parent 0e7fa04b03
commit c4ea9a6cd1
3582 files changed
+626189 -160151

No files matched your search

@@ -3,10 +3,12 @@
namespace Modules\Sirsoft\Board\Repositories;
use App\Helpers\PermissionHelper;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use Modules\Sirsoft\Board\Enums\TriggerType;
use Modules\Sirsoft\Board\Models\Board;
use Modules\Sirsoft\Board\Models\Comment;
use Modules\Sirsoft\Board\Repositories\Contracts\CommentRepositoryInterface;
@@ -28,6 +30,8 @@ class CommentRepository implements CommentRepositoryInterface
* @param int $postId 게시글 ID
* @param bool $withTrashed 삭제된 댓글 포함 여부 (기본값: false)
* @param string $orderDirection 정렬 방향 (ASC 또는 DESC, 기본값: DESC)
* @param string|null $scopePermission 권한 스코프 식별자 (Service에서 컨텍스트 기반 전달)
* @param int|null $boardId 게시판 ID (전달 시 Board 재조회 생략)
* @return Collection 부모-자식 관계로 정렬된 댓글 컬렉션
*/
public function getByPostId(string $slug, int $postId, bool $withTrashed = false, string $orderDirection = 'DESC', ?string $scopePermission = null, ?int $boardId = null): Collection
@@ -49,11 +53,28 @@ class CommentRepository implements CommentRepositoryInterface
// 삭제된 댓글 포함 여부
if ($withTrashed) {
// 토글 ON(관리자/매니저): 사용자 직접 삭제분 포함 전부 노출
$query->withTrashed();
} else {
// 토글 OFF: 게시글 삭제로 함께 숨겨진(cascade) 댓글은 사용자가 지운 것이 아니므로
// 항상 노출한다 (살아있는 것 + cascade). 사용자 직접 삭제분(user 등)은 제외.
$query->withTrashed()
->where(function ($q) {
$q->whereNull('deleted_at')
->orWhere('trigger_type', TriggerType::Cascade->value);
});
}
$comments = $query->orderBy('created_at', $orderDirection)->get();
// 일반 조회(withTrashed=false)에서 부모가 삭제되어 빠진 경우,
// 살아있는 자식이 트리에서 통째로 누락된다.
// 살아있는 자식이 있는 삭제 부모만 tombstone 으로 복구해 트리를 유지한다.
// withTrashed=true(manager 토글 ON)는 이미 삭제 부모를 모두 포함하므로 건너뛴다.
if (! $withTrashed && $resolvedBoardId) {
$comments = $this->restoreTombstoneParents($comments, $resolvedBoardId, $postId);
}
// 부모-자식 관계로 정렬 (부모 댓글 다음에 답글이 오도록)
$sorted = $this->sortByParentChild($comments, $orderDirection);
@@ -63,6 +84,69 @@ class CommentRepository implements CommentRepositoryInterface
return $sorted;
}
/**
* 살아있는 자식이 있으나 부모가 삭제되어 결과셋에서 빠진 경우,
* 해당 삭제 부모를 tombstone 노드로 복구해 컬렉션에 추가합니다.
*
* 고아 부모 ID 를 모아 1회 IN 조회로 가져오며(N+1 회피), 복구 대상이 없으면
* 추가 쿼리 없이 원본 컬렉션을 그대로 반환합니다.
* 자식 없는 삭제 부모는 고아 자식이 존재하지 않으므로 자연히 제외됩니다.
*
* @param Collection $comments 현재 조회된(삭제 부모 제외) 댓글 컬렉션
* @param int $boardId 게시판 ID
* @param int $postId 게시글 ID
* @return Collection tombstone 부모가 보강된 댓글 컬렉션
*/
private function restoreTombstoneParents(Collection $comments, int $boardId, int $postId): Collection
{
$presentIds = $comments->pluck('id')->all();
// 부모가 결과셋에 없는 자식(고아)의 parent_id 수집
$orphanParentIds = $comments
->filter(fn (Comment $c) => $c->parent_id !== null && ! in_array($c->parent_id, $presentIds, true))
->pluck('parent_id')
->unique()
->values()
->all();
if (empty($orphanParentIds)) {
return $comments;
}
// 고아의 조상 체인을 따라 올라가며 누락된 삭제 부모를 모두 복구
// (다단계: 부모·자식이 연쇄 삭제되고 손자만 살아있는 경우 포함)
$restored = collect();
$knownIds = $presentIds;
$pendingIds = $orphanParentIds;
while (! empty($pendingIds)) {
$tombstones = Comment::query()
->where('board_id', $boardId)
->where('post_id', $postId)
->withTrashed()
->with(['user', 'user.avatarAttachment', 'parent'])
->whereIn('id', $pendingIds)
->get();
if ($tombstones->isEmpty()) {
break;
}
$restored = $restored->merge($tombstones);
$knownIds = array_merge($knownIds, $tombstones->pluck('id')->all());
// 복구된 tombstone 의 부모도 결과셋에 없으면 다음 라운드에서 복구
$pendingIds = $tombstones
->filter(fn (Comment $c) => $c->parent_id !== null && ! in_array($c->parent_id, $knownIds, true))
->pluck('parent_id')
->unique()
->values()
->all();
}
return $comments->merge($restored);
}
/**
* 댓글 목록을 부모-자식 관계로 정렬합니다.
*
@@ -131,14 +215,20 @@ class CommentRepository implements CommentRepositoryInterface
/**
* 재귀적으로 모든 하위 자손 수를 계산합니다.
*
* tombstone(삭제) 댓글은 트리 구조 유지를 위해 노드로는 남기지만,
* "답글 N" 집계 모수에서는 제외한다(PO 확정). 단, 삭제 댓글의
* 살아있는 하위 자손은 계속 카운트해 누락되지 않게 한다.
*
* @param Collection $grouped parent_id로 그룹화된 댓글 컬렉션
* @param int $parentId 부모 댓글 ID
* @return int 전체 하위 자손 수
* @return int 전체 하위 자손 수 (tombstone 제외)
*/
private function countDescendants(Collection $grouped, int $parentId): int
{
$children = $grouped->get($parentId, collect());
$count = $children->count();
// 삭제되지 않은 자식만 카운트 (tombstone 은 모수 제외)
$count = $children->filter(fn (Comment $child) => $child->deleted_at === null)->count();
foreach ($children as $child) {
$count += $this->countDescendants($grouped, $child->id);
@@ -255,6 +345,7 @@ class CommentRepository implements CommentRepositoryInterface
* @param int $id 댓글 ID
* @param string $status 변경할 상태 (published/blinded/deleted)
* @param array $actionLog 작업 이력 데이터
* @param string|null $triggerType 트리거 유형 (admin/user/report 등, 지정 시 함께 갱신)
* @return Comment 수정된 댓글 모델
*
* @throws ModelNotFoundException
@@ -352,6 +443,20 @@ class CommentRepository implements CommentRepositoryInterface
->find($id);
}
/**
* 게시판 ID 기준으로 댓글을 일괄 영구 삭제합니다.
*
* 게시판 영구 삭제(deleteBoard) 시 사용합니다. 소프트 삭제와 달리
* deleted_at 마킹이 아니라 레코드를 물리적으로 제거합니다.
*
* @param int $boardId 게시판 ID
* @return int 삭제된 댓글 수
*/
public function forceDeleteByBoardId(int $boardId): int
{
return Comment::where('board_id', $boardId)->forceDelete();
}
/**
* 게시판 ID 기준으로 댓글을 일괄 소프트 삭제합니다.
*
@@ -364,7 +469,12 @@ class CommentRepository implements CommentRepositoryInterface
}
/**
* 게시글 ID 기준으로 댓글을 일괄 소프트 삭제합니다.
* 게시글 ID 기준으로 살아있는 댓글을 cascade 로 일괄 소프트 삭제합니다.
*
* 게시글 삭제 연쇄로 지워졌음을 trigger_type='cascade' 로 마킹합니다.
* 이미 삭제된(trashed) 댓글은 Eloquent 기본 스코프가 제외하므로,
* 사용자가 먼저 지운 댓글(trigger_type='user')은 영향을 받지 않습니다.
* trigger_type 마킹과 deleted_at 을 단일 쿼리로 처리해 정합성을 보장합니다.
*
* @param string $slug 게시판 슬러그
* @param int $postId 게시글 ID
@@ -376,7 +486,33 @@ class CommentRepository implements CommentRepositoryInterface
return Comment::where('board_id', $board?->id)
->where('post_id', $postId)
->delete();
->update([
'trigger_type' => TriggerType::Cascade->value,
'deleted_at' => now(),
]);
}
/**
* 게시글 ID 기준으로 cascade 로 지워진 댓글만 복원합니다.
*
* 게시글 복원 시, 게시글 삭제 연쇄로 지워진(trigger_type='cascade') 댓글만
* 되살립니다. 사용자가 직접 지운(trigger_type='user') 댓글은 trashed 로 유지됩니다.
*
* @param string $slug 게시판 슬러그
* @param int $postId 게시글 ID
* @return int 복원된 댓글 수
*/
public function restoreCascadedByPostId(string $slug, int $postId): int
{
$board = Board::where('slug', $slug)->first();
return Comment::onlyTrashed()
->where('board_id', $board?->id)
->where('post_id', $postId)
->where('trigger_type', TriggerType::Cascade->value)
->update([
'deleted_at' => null,
]);
}
/**
@@ -469,4 +605,22 @@ class CommentRepository implements CommentRepositoryInterface
return $count;
}
/**
* 특정 날짜에 작성된 전체 게시판의 댓글 수를 조회합니다 (대시보드 집계용).
*
* @param string $date 집계 기준 날짜 (Y-m-d)
* @return int 해당 날짜 작성 댓글 수
*/
public function countCreatedOnDate(string $date): int
{
$start = CarbonImmutable::parse($date)->startOfDay();
$end = $start->addDay();
return Comment::query()
->whereNull('deleted_at')
->where('created_at', '>=', $start)
->where('created_at', '<', $end)
->count();
}
}