@@ -26,6 +26,17 @@ public class NotificationService {
2626 private final NotificationReadRepository notificationReadRepository ;
2727 private final NotificationWebSocketService webSocketService ;
2828
29+ // ==================== 검증 메서드 ====================
30+
31+ // 발신자와 수신자 검증
32+ private void validateActorAndReceiver (User receiver , User actor ) {
33+ if (receiver .getId ().equals (actor .getId ())) {
34+ log .debug ("자기 자신에게 알림 전송 시도 - receiver: {}, actor: {}" ,
35+ receiver .getId (), actor .getId ());
36+ throw new CustomException (ErrorCode .NOTIFICATION_FORBIDDEN );
37+ }
38+ }
39+
2940 // ==================== 알림 생성 및 전송 ====================
3041
3142 // 개인 알림 생성 및 전송
@@ -37,6 +48,9 @@ public Notification createPersonalNotification(
3748 String content ,
3849 String targetUrl ) {
3950
51+ // 자기 자신에게 알림 방지
52+ validateActorAndReceiver (receiver , actor );
53+
4054 // DB에 알림 저장
4155 Notification notification = Notification .createPersonalNotification (
4256 receiver , actor , title , content , targetUrl );
@@ -72,7 +86,7 @@ public Notification createRoomNotification(
7286 return notification ;
7387 }
7488
75- // 시스템 전체 알림 생성 및 브로드캐스트 (발신자 없음)
89+ // 시스템 전체 알림 생성 및 브로드캐스트
7690 @ Transactional
7791 public Notification createSystemNotification (String title , String content , String targetUrl ) {
7892
@@ -95,6 +109,9 @@ public Notification createCommunityNotification(
95109 String content ,
96110 String targetUrl ) {
97111
112+ // 검증: 자기 자신에게 알림 방지
113+ validateActorAndReceiver (receiver , actor );
114+
98115 Notification notification = Notification .createCommunityNotification (
99116 receiver , actor , title , content , targetUrl );
100117 notificationRepository .save (notification );
@@ -109,28 +126,23 @@ public Notification createCommunityNotification(
109126
110127 // ==================== 알림 조회 ====================
111128
112- // 특정 유저의 알림 목록 조회 (개인 알림 + 시스템 알림)
113129 public Page <Notification > getUserNotifications (Long userId , Pageable pageable ) {
114130 return notificationRepository .findByUserIdOrSystemType (userId , pageable );
115131 }
116132
117- // 특정 유저의 읽지 않은 알림 목록 조회
118133 public Page <Notification > getUnreadNotifications (Long userId , Pageable pageable ) {
119134 return notificationRepository .findUnreadByUserId (userId , pageable );
120135 }
121136
122- // 특정 유저의 읽지 않은 알림 개수 조회
123137 public long getUnreadCount (Long userId ) {
124138 return notificationRepository .countUnreadByUserId (userId );
125139 }
126140
127- // 알림 단건 조회
128141 public Notification getNotification (Long notificationId ) {
129142 return notificationRepository .findById (notificationId )
130143 .orElseThrow (() -> new CustomException (ErrorCode .NOTIFICATION_NOT_FOUND ));
131144 }
132145
133- // 특정 유저가 특정 알림을 읽었는지 확인
134146 public boolean isNotificationRead (Long notificationId , Long userId ) {
135147 return notificationReadRepository .existsByNotificationIdAndUserId (notificationId , userId );
136148 }
@@ -140,16 +152,21 @@ public boolean isNotificationRead(Long notificationId, Long userId) {
140152 // 알림 읽음 처리
141153 @ Transactional
142154 public void markAsRead (Long notificationId , User user ) {
143- // 1. 알림 존재 확인
155+ // 알림 존재 확인
144156 Notification notification = getNotification (notificationId );
145157
146- // 2. 이미 읽은 알림인지 확인
158+ // 알림 접근 권한 확인
159+ if (!notification .isVisibleToUser (user .getId ())) {
160+ throw new CustomException (ErrorCode .NOTIFICATION_FORBIDDEN );
161+ }
162+
163+ // 이미 읽은 알림인지 확인
147164 if (notificationReadRepository .existsByNotificationIdAndUserId (notificationId , user .getId ())) {
148165 log .debug ("이미 읽은 알림 - 알림 ID: {}, 유저 ID: {}" , notificationId , user .getId ());
149- return ;
166+ throw new CustomException ( ErrorCode . NOTIFICATION_ALREADY_READ ) ;
150167 }
151168
152- // 3. 읽음 기록 생성
169+ // 읽음 기록 생성
153170 NotificationRead notificationRead = NotificationRead .create (notification , user );
154171 notificationReadRepository .save (notificationRead );
155172
@@ -161,13 +178,15 @@ public void markAsRead(Long notificationId, User user) {
161178 public void markMultipleAsRead (Long userId , User user ) {
162179 Page <Notification > unreadNotifications = getUnreadNotifications (userId , Pageable .unpaged ());
163180
181+ int count = 0 ;
164182 for (Notification notification : unreadNotifications ) {
165183 if (!notificationReadRepository .existsByNotificationIdAndUserId (notification .getId (), user .getId ())) {
166184 NotificationRead notificationRead = NotificationRead .create (notification , user );
167185 notificationReadRepository .save (notificationRead );
186+ count ++;
168187 }
169188 }
170189
171- log .info ("일괄 읽음 처리 - 유저 ID: {}, 처리 개수: {}" , userId , unreadNotifications . getTotalElements () );
190+ log .info ("일괄 읽음 처리 - 유저 ID: {}, 처리 개수: {}" , userId , count );
172191 }
173192}
0 commit comments