- 열람 권한 없는 비밀글에 추천/비추천을 남길 수 있던 결함을 서버(ReactionService)와 화면(반응 영역 미노출) 양쪽에서 차단. 신고 기능과 동일한 권한 판정 기준 재사용. - 반응 버튼 UX를 다듬어 활성 상태는 색상으로만 표시하고, 클릭 순간에만 살짝 눌리는 촉각 피드백(active:scale-95)을 추가. - 이슈 명세 재검토 중 describe.skip 상태였던 반응 E2E spec 에서 결함 3건 발견·수정: 대상 게시판/게시글이 실제로 반응 불가능한 대상이었던 점, 3단계(해제) 검증이 값 우연 일치로 거짓 통과 가능했던 점을 고치고 실제 실행으로 통과 확인.
182 lines
7.6 KiB
PHP
182 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Sirsoft\Board\Services;
|
|
|
|
use App\Enums\PermissionType;
|
|
use App\Extension\HookManager;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Sirsoft\Board\Exceptions\PostNotFoundException;
|
|
use Modules\Sirsoft\Board\Exceptions\ReactionNotAllowedException;
|
|
use Modules\Sirsoft\Board\Models\Board;
|
|
use Modules\Sirsoft\Board\Repositories\Contracts\PostRepositoryInterface;
|
|
use Modules\Sirsoft\Board\Repositories\Contracts\ReactionRepositoryInterface;
|
|
use Modules\Sirsoft\Board\Repositories\Contracts\ReactionTypeRepositoryInterface;
|
|
use Modules\Sirsoft\Board\Traits\ChecksBoardPermission;
|
|
|
|
/**
|
|
* 반응 서비스
|
|
*
|
|
* 게시글 반응의 등록/전환/해제를 단일 진입점(`react()`)으로 처리합니다.
|
|
* 확정 03(글당 1개, 전환 시 대체), 07(로그인 필수 — 컨트롤러 미들웨어),
|
|
* 08(본인 글 차단), 11(비활성 유형 차단)을 강제합니다.
|
|
*/
|
|
class ReactionService
|
|
{
|
|
use ChecksBoardPermission;
|
|
|
|
/**
|
|
* 현재 지원하는 반응 대상 타입 (확정 10 — 게시글만, 향후 comment 확장 가능).
|
|
*/
|
|
private const TARGET_POST = 'post';
|
|
|
|
/**
|
|
* @param ReactionRepositoryInterface $reactionRepository 반응 이력 Repository
|
|
* @param ReactionTypeRepositoryInterface $reactionTypeRepository 반응 유형 Repository
|
|
* @param PostRepositoryInterface $postRepository 게시글 스코프 조회 Repository
|
|
*/
|
|
public function __construct(
|
|
private readonly ReactionRepositoryInterface $reactionRepository,
|
|
private readonly ReactionTypeRepositoryInterface $reactionTypeRepository,
|
|
private readonly PostRepositoryInterface $postRepository,
|
|
) {}
|
|
|
|
/**
|
|
* 게시글에 반응을 남깁니다 (등록/전환/해제 통합).
|
|
*
|
|
* 게시글이 대상 게시판 소속인지 스코프 검증을 먼저 수행합니다 (교차 접근 차단).
|
|
*
|
|
* - 기존 반응 없음 → 신규(INSERT), 해당 유형 +1
|
|
* - 기존 반응이 같은 유형 → 해제(DELETE), 해당 유형 -1
|
|
* - 기존 반응이 다른 유형 → 전환(UPDATE), 이전 유형 -1 · 신규 유형 +1
|
|
*
|
|
* 카운트 증감과 이력 쓰기는 단일 트랜잭션으로 원자 처리합니다 (확정 04·동시성).
|
|
*
|
|
* @param int $userId 반응한 사용자 ID
|
|
* @param Board $board 대상 게시판 (컨트롤러가 slug 로 해석 후 전달)
|
|
* @param int $postId 대상 게시글 ID
|
|
* @param int $reactionTypeId 반응 유형 ID
|
|
* @return array{action: string, reaction_type_id: int|null, reaction_counts: array<string, int>}
|
|
*
|
|
* @throws PostNotFoundException 게시글이 이 게시판 소속이 아니거나 미존재
|
|
* @throws ReactionNotAllowedException 반응 비활성 게시판 / 비활성 유형 / 본인 글
|
|
*/
|
|
public function react(int $userId, Board $board, int $postId, int $reactionTypeId): array
|
|
{
|
|
// 게시글이 이 게시판 소속인지 스코프 검증 (교차 접근 차단)
|
|
$post = $this->postRepository->findByBoardId($board->id, $postId);
|
|
|
|
if ($post === null) {
|
|
throw new PostNotFoundException($postId);
|
|
}
|
|
|
|
// 반응 기능 off (확정 07 전제)
|
|
if (! $board->use_reaction) {
|
|
throw ReactionNotAllowedException::disabled();
|
|
}
|
|
|
|
// 본인 글 반응 차단 (확정 08)
|
|
if ((int) $post->user_id === $userId) {
|
|
throw ReactionNotAllowedException::selfPost();
|
|
}
|
|
|
|
// 비밀글 반응 차단 — 본문 열람 권한이 없는 사용자는 반응 불가.
|
|
// 신고(PostResource::canViewSecretContent)와 동일한 판정을 재사용해,
|
|
// 본문을 못 보는 사용자가 반응만 남기는 우회를 막는다.
|
|
// 작성자 본인은 위에서 이미 차단되므로 여기서는 게시판별 비밀글 열람 권한만 본다.
|
|
if ($post->is_secret && ! $this->canReactToSecretPost($board->slug)) {
|
|
throw ReactionNotAllowedException::secretDenied();
|
|
}
|
|
|
|
// 요청 유형이 존재하는 활성 유형이면서 게시판이 켠 유형인지 확인 (확정 11)
|
|
$requestedType = $this->reactionTypeRepository->findById($reactionTypeId);
|
|
$activeCodes = $board->active_reaction_types ?? [];
|
|
|
|
if ($requestedType === null
|
|
|| ! $requestedType->is_active
|
|
|| ! in_array($requestedType->code, $activeCodes, true)) {
|
|
throw ReactionNotAllowedException::inactiveType();
|
|
}
|
|
|
|
HookManager::doAction('sirsoft-board.reaction.before_react', $userId, $post, $reactionTypeId);
|
|
|
|
$result = DB::transaction(function () use ($userId, $board, $post, $reactionTypeId) {
|
|
$existing = $this->reactionRepository->findByUserAndTarget(
|
|
$userId,
|
|
self::TARGET_POST,
|
|
$post->id,
|
|
);
|
|
|
|
// 같은 유형 재요청 → 해제
|
|
if ($existing !== null && (int) $existing->reaction_type_id === $reactionTypeId) {
|
|
$this->reactionRepository->delete($existing);
|
|
$counts = $this->reactionRepository->adjustPostReactionCounts(
|
|
$post->id,
|
|
[$reactionTypeId => -1],
|
|
);
|
|
|
|
return ['action' => 'remove', 'reaction_type_id' => null, 'reaction_counts' => $counts];
|
|
}
|
|
|
|
// 다른 유형 → 전환 (이전 -1, 신규 +1)
|
|
if ($existing !== null) {
|
|
$previousTypeId = (int) $existing->reaction_type_id;
|
|
$this->reactionRepository->upsert(
|
|
$userId,
|
|
self::TARGET_POST,
|
|
$post->id,
|
|
$reactionTypeId,
|
|
$board->id,
|
|
);
|
|
$counts = $this->reactionRepository->adjustPostReactionCounts(
|
|
$post->id,
|
|
[$previousTypeId => -1, $reactionTypeId => 1],
|
|
);
|
|
|
|
return ['action' => 'change', 'reaction_type_id' => $reactionTypeId, 'reaction_counts' => $counts];
|
|
}
|
|
|
|
// 신규 등록
|
|
$this->reactionRepository->upsert(
|
|
$userId,
|
|
self::TARGET_POST,
|
|
$post->id,
|
|
$reactionTypeId,
|
|
$board->id,
|
|
);
|
|
$counts = $this->reactionRepository->adjustPostReactionCounts(
|
|
$post->id,
|
|
[$reactionTypeId => 1],
|
|
);
|
|
|
|
return ['action' => 'add', 'reaction_type_id' => $reactionTypeId, 'reaction_counts' => $counts];
|
|
});
|
|
|
|
HookManager::doAction(
|
|
'sirsoft-board.reaction.after_react',
|
|
$userId,
|
|
$post,
|
|
$reactionTypeId,
|
|
$result['action'],
|
|
);
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 비밀글에 반응할 수 있는 열람 권한이 있는지 확인합니다.
|
|
*
|
|
* 반응은 사용자(User) 페이지 전용 기능이므로 게시판별 비밀글 읽기 권한
|
|
* (posts.read-secret) 또는 게시판 매니저 권한만 인정합니다. 작성자 본인은
|
|
* 호출 이전에 이미 selfPost 로 차단되므로 여기서는 고려하지 않습니다.
|
|
* `PostResource::canViewSecretContent` 의 게시판 권한 판정과 동일한 기준입니다.
|
|
*
|
|
* @param string $slug 대상 게시판 슬러그
|
|
* @return bool 비밀글 반응 가능 여부
|
|
*/
|
|
private function canReactToSecretPost(string $slug): bool
|
|
{
|
|
return $this->checkBoardPermission($slug, 'posts.read-secret', PermissionType::User)
|
|
|| $this->checkBoardPermission($slug, 'manager', PermissionType::User);
|
|
}
|
|
}
|