} * * @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); } }