본문 바로가기
💻 뚝딱뚝딱/북북클럽

[개발일지 #027] 신고(Report) 누적 시, 블라인드 처리

by 뚜루리 2025. 5. 1.
728x90
320x100

🎯 오늘의 목표

  • 신고(Report) 누적 시, 블라인드 처리

⚙️ 진행한 작업

  • 신고(Report) 누적 시, 블라인드 처리

 


🛠️ 개발내용

📌  피드(Feed) 엔티티 수정

@Column(nullable = false)
private boolean isBlinded = false;

/** 피드 블라인드 처리 */
public void blind() {
    this.isBlinded = true;
}

/** 블라인드 해제 (선택적으로 추가) */
public void unblind() {
    this.isBlinded = false;
}

 

📌  ReportRepository 수정

long countByFeed(Feed feed);

 

 

📌  신고 횟수 관리

application.yml

report:
  blind-threshold: 5

 

ReportProperties.java

 

package ddururi.bookbookclub.global.config;

import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Getter
@Component
@ConfigurationProperties(prefix = "report")
public class ReportProperties {
    private int blindThreshold;

    public void setBlindThreshold(int blindThreshold) {
        this.blindThreshold = blindThreshold;
    }
}

 

📌  ReportService 수정

private final ReportProperties reportProperties;

public void reportFeed(Long feedId, Long reporterId, ReasonType reason) {
    User reporter = userRepository.findById(reporterId)
            .orElseThrow(UserNotFoundException::new);

    Feed feed = feedRepository.findById(feedId)
            .orElseThrow(FeedNotFoundException::new);

    if (reportRepository.existsByReporterAndFeed(reporter, feed)) {
        throw new AlreadyReportedException();
    }

    Report report = Report.of(reporter, feed, reason);
    reportRepository.save(report);

    // 누적 신고 수 확인
    long reportCount = reportRepository.countByFeed(feed);
   int threshold = reportProperties.getBlindThreshold();

    if (reportCount >= threshold && !feed.isBlinded()) {
        feed.blind();
        feedRepository.save(feed);
    }
}

 

여기까지 구현하면, 신고할 때 신고 횟수가 쌓이는 로직까진 완성이 된다.

 


여기서 부터는 신고 횟수가 일정 횟수 이상일 때는 피드 조회가 불가능하도록 추가 수정에 대한 내용.

 

📌  FeedRepository 메서드 추가

Page<Feed> findByIsBlindedFalse(Pageable pageable);

 

📌  FeedService 메서드 수정

      /**
     * 피드 단건 조회
     *
     * @param feedId 조회할 피드 ID
     * @return 조회된 피드
     * @throws FeedNotFoundException 피드가 존재하지 않을 경우
     */
    public FeedResponse getFeed(Long feedId, Long userId) {
        Feed feed = feedRepository.findById(feedId)
                .orElseThrow(FeedNotFoundException::new);
                
		 // ✅ 블라인드(false) 상태인 피드만 조회하도록 수정
        if (feed.isBlinded()) {
            throw new FeedBlindedException();
        }

        long likeCount = likeService.getLikeCount(feedId);
        boolean liked = likeService.hasUserLiked(userId, feedId);
        return new FeedResponse(feed, likeCount, liked);

    } 
  /**
     * 피드 목록 조회 (페이징, 좋아요 수, 좋아요 여부 포함)
     * @param pageable 페이징 요청 정보
     * @param userId 현재 사용자 ID
     * @return 피드 목록 (페이지 형태)
     */
    @Transactional
    public Page<FeedResponse> getFeeds(Pageable pageable, Long userId) {
        Pageable sortedPageable = PageRequest.of(
                pageable.getPageNumber(),
                pageable.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createdAt")
        );
        // ✅ 블라인드(false) 상태인 피드만 조회하도록 수정
        Page<Feed> feeds = feedRepository.findByIsBlindedFalse(sortedPageable);
	
        return feeds.map(feed -> {
            long likeCount = likeService.getLikeCount(feed.getId());
            boolean liked = likeService.hasUserLiked(userId, feed.getId());
            return new FeedResponse(feed, likeCount, liked);
        });
    }

 

여기까지 완성하면 신고 횟수를 저장하고, 신고 횟수에 따라 조회 되도록 구현 완료!

728x90
320x100