Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ public boolean hasAccess(User user, StudentCouncil writer) {
user.getSchool().getSchoolId().equals(writer.getSchool().getSchoolId());
}

@Override public String topic(StudentCouncil writer) {
@Override
public String topic(StudentCouncil writer) {
return "school_" + writer.getSchool().getSchoolId();
}

},
COLLEGE_COUNCIL {
@Override
public boolean hasAccess(User user, StudentCouncil writer) {
return user.getCollege() != null &&
user.getCollege().getCollegeId().equals(writer.getCollege().getCollegeId());
}
@Override public String topic(StudentCouncil writer) {

@Override
public String topic(StudentCouncil writer) {
return "college_" + writer.getCollege().getCollegeId();
}
},
Expand All @@ -30,11 +34,16 @@ public boolean hasAccess(User user, StudentCouncil writer) {
return user.getMajor() != null &&
user.getMajor().getMajorId().equals(writer.getMajor().getMajorId());
}
@Override public String topic(StudentCouncil writer) {

@Override
public String topic(StudentCouncil writer) {
return "major_" + writer.getMajor().getMajorId();
}

};

public abstract boolean hasAccess(User user, StudentCouncil writer);

public abstract String topic(StudentCouncil writer);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.campus.campus.domain.councilpost.application.dto.request.CouncilPostCreatedEvent;
import com.campus.campus.domain.councilpost.domain.entity.PostCategory;
import com.campus.campus.domain.notification.application.service.NotificationService;
import com.campus.campus.global.firebase.application.service.FirebaseCloudMessageService;

import lombok.RequiredArgsConstructor;
Expand All @@ -25,6 +26,7 @@ public class CouncilPostPushListener {
private static final String DATA_KEY_CATEGORY = "category";

private final FirebaseCloudMessageService firebaseCloudMessageService;
private final NotificationService notificationService;

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
Expand All @@ -36,6 +38,8 @@ public void handleCouncilPostCreatedEvent(CouncilPostCreatedEvent event) {
log.info("[PUSH] after_commit event received. topic={}, postId={}, category={}",
event.topic(), event.postId(), event.category());

notificationService.savePostCreatedNotification(event, title, body);

firebaseCloudMessageService.sendToTopic(
event.topic(),
title,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.campus.campus.domain.notification.application.dto;

import java.util.List;

public record CursorResponse<T>(
List<T> items,
NextCursor nextCursor,
boolean hasNext
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.campus.campus.domain.notification.application.dto;

import java.time.LocalDateTime;

public record NextCursor(
LocalDateTime createdAt,
Long id
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.campus.campus.domain.notification.application.dto;

import com.campus.campus.domain.notification.domain.entity.NotificationType;

import io.swagger.v3.oas.annotations.media.Schema;

public record NotificationResponse(
@Schema(description = "알림 ID", example = "1")
Long id,

@Schema(description = "알림 타입", example = "COUNCIL_POST_CREATED")
NotificationType type,

@Schema(description = "알림 제목", example = "총학생회")
String title,

@Schema(description = "알림 내용", example = "새 행사글이 등록되었습니다.")
String body,

@Schema(description = "참조 ID (게시글 ID 등)", example = "123")
Long referenceId,

@Schema(description = "읽음 여부", example = "false")
boolean isRead,

@Schema(description = "생성 시각 (상대 시간)", example = "5분 전")
String createTimeBeforeNow
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.campus.campus.domain.notification.application.exception;

import org.springframework.http.HttpStatus;

import com.campus.campus.global.common.exception.ErrorCodeInterface;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ErrorCode implements ErrorCodeInterface {

NOTIFICATION_NOT_FOUND(2801, HttpStatus.NOT_FOUND, "알림을 찾을 수 없습니다."),
NOTIFICATION_ACCESS_DENIED(2802, HttpStatus.FORBIDDEN, "해당 알림에 접근할 권한이 없습니다."),
INVALID_NOTIFICATION_IDS(2803, HttpStatus.BAD_REQUEST, "유효하지 않은 알림 ID 목록입니다.");

private final int code;
private final HttpStatus status;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.campus.campus.domain.notification.application.exception;

import com.campus.campus.global.common.exception.ApplicationException;

public class NotificationAccessDeniedException extends ApplicationException {
public NotificationAccessDeniedException() {
super(ErrorCode.NOTIFICATION_ACCESS_DENIED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.campus.campus.domain.notification.application.exception;

import com.campus.campus.global.common.exception.ApplicationException;

public class NotificationNotFoundException extends ApplicationException {
public NotificationNotFoundException() {
super(ErrorCode.NOTIFICATION_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.campus.campus.domain.notification.application.mapper;

import org.springframework.stereotype.Component;

import com.campus.campus.domain.notification.application.dto.NotificationResponse;
import com.campus.campus.domain.notification.domain.entity.Notification;
import com.campus.campus.domain.notification.domain.entity.NotificationType;
import com.campus.campus.domain.notification.util.TimeFormatter;
import com.campus.campus.domain.user.domain.entity.User;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class NotificationMapper {

private final TimeFormatter timeFormatter;

public NotificationResponse toResponse(Notification notification) {
return new NotificationResponse(
notification.getId(),
notification.getType(),
notification.getTitle(),
notification.getBody(),
notification.getReferenceId(),
notification.isRead(),
timeFormatter.formatRelativeTime(notification.getCreatedAt())
);
}

public Notification createNotification(User user, NotificationType type,
String title, String body, Long referenceId) {
return Notification.builder()
.user(user)
.type(type)
.title(title)
.body(body)
.referenceId(referenceId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.campus.campus.domain.notification.application.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.campus.campus.domain.councilpost.application.dto.request.CouncilPostCreatedEvent;
import com.campus.campus.domain.notification.application.dto.CursorResponse;
import com.campus.campus.domain.notification.application.dto.NextCursor;
import com.campus.campus.domain.notification.application.dto.NotificationResponse;
import com.campus.campus.domain.notification.application.exception.NotificationAccessDeniedException;
import com.campus.campus.domain.notification.application.exception.NotificationNotFoundException;
import com.campus.campus.domain.notification.application.mapper.NotificationMapper;
import com.campus.campus.domain.notification.domain.entity.Notification;
import com.campus.campus.domain.notification.domain.entity.NotificationType;
import com.campus.campus.domain.notification.domain.repository.NotificationRepository;
import com.campus.campus.domain.user.application.exception.UserNotFoundException;
import com.campus.campus.domain.user.domain.entity.User;
import com.campus.campus.domain.user.domain.repository.UserRepository;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class NotificationService {

private final NotificationRepository notificationRepository;
private final NotificationMapper notificationMapper;
private final UserRepository userRepository;

public CursorResponse<NotificationResponse> getNotificationsByCursor(
Long userId,
LocalDateTime cursorCreatedAt,
Long cursorId,
int limit
) {
User user = userRepository.findByIdAndDeletedAtIsNull(userId)
.orElseThrow(UserNotFoundException::new);

int pageSize = Math.min(Math.max(limit, 1), 50);
Pageable pageable = PageRequest.of(0, pageSize);

boolean isFirst = (cursorCreatedAt == null || cursorId == null);

List<Notification> list = isFirst
? notificationRepository.findByUserOrderByCreatedAtDescIdDesc(user, pageable)
: notificationRepository.findNextByCursor(user, cursorCreatedAt, cursorId, pageable);

List<NotificationResponse> items = list.stream()
.map(notificationMapper::toResponse)
.toList();

boolean hasNext = list.size() == pageSize;

NextCursor nextCursor = null;
if (!list.isEmpty()) {
Notification last = list.get(list.size() - 1);
nextCursor = new NextCursor(last.getCreatedAt(), last.getId());
}

return new CursorResponse<>(items, nextCursor, hasNext);
}

@Transactional
public void markAsRead(Long userId, Long notificationId) {
User user = userRepository.findByIdAndDeletedAtIsNull(userId)
.orElseThrow(UserNotFoundException::new);

Notification notification = notificationRepository.findById(notificationId)
.orElseThrow(NotificationNotFoundException::new);

if (!notification.getUser().getId().equals(user.getId())) {
throw new NotificationAccessDeniedException();
}

notification.markAsRead();
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void savePostCreatedNotification(CouncilPostCreatedEvent event, String title, String body) {

List<User> targetUsers = findUsersByTopic(event.topic());

List<Notification> notifications = targetUsers.stream()
.map(user -> notificationMapper.createNotification(
user,
NotificationType.COUNCIL_POST_CREATED,
title,
body,
event.postId()
))
.toList();

notificationRepository.saveAll(notifications);
}

@Transactional(readOnly = true)
public boolean hasUnread(Long userId) {
return notificationRepository.existsByUser_IdAndIsReadFalse(userId);
}

private List<User> findUsersByTopic(String topic) {

String[] parts = topic.split("_");
String scope = parts[0];
Long scopeId = Long.valueOf(parts[1]);

return switch (scope) {
case "major" -> userRepository.findAllByMajor_MajorIdAndDeletedAtIsNull(scopeId);
case "college" -> userRepository.findAllByCollege_CollegeIdAndDeletedAtIsNull(scopeId);
case "school" -> userRepository.findAllBySchool_SchoolIdAndDeletedAtIsNull(scopeId);
default -> List.of();
};
}
Comment on lines +109 to +121
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

토픽 파싱 시 예외 처리가 필요합니다.

topic.split("_") 결과가 예상과 다를 경우(예: "invalid", "major" 등) ArrayIndexOutOfBoundsException 또는 NumberFormatException이 발생할 수 있습니다. 이 메서드는 @Async 이벤트 리스너에서 호출되므로, 예외 발생 시 알림 저장이 실패하고 로깅 없이 문제를 추적하기 어려워집니다.

🔒️ 방어적 코드 추가 권장
 	private List<User> findUsersByTopic(String topic) {
+		if (topic == null || !topic.contains("_")) {
+			log.warn("Invalid topic format: {}", topic);
+			return List.of();
+		}
 
 		String[] parts = topic.split("_");
+		if (parts.length < 2) {
+			log.warn("Invalid topic format: {}", topic);
+			return List.of();
+		}
+
 		String scope = parts[0];
-		Long scopeId = Long.valueOf(parts[1]);
+		Long scopeId;
+		try {
+			scopeId = Long.valueOf(parts[1]);
+		} catch (NumberFormatException e) {
+			log.warn("Invalid scopeId in topic: {}", topic);
+			return List.of();
+		}
 
 		return switch (scope) {
 			case "major" -> userRepository.findAllByMajor_MajorIdAndDeletedAtIsNull(scopeId);
 			case "college" -> userRepository.findAllByCollege_CollegeIdAndDeletedAtIsNull(scopeId);
 			case "school" -> userRepository.findAllBySchool_SchoolIdAndDeletedAtIsNull(scopeId);
 			default -> List.of();
 		};
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private List<User> findUsersByTopic(String topic) {
String[] parts = topic.split("_");
String scope = parts[0];
Long scopeId = Long.valueOf(parts[1]);
return switch (scope) {
case "major" -> userRepository.findAllByMajor_MajorIdAndDeletedAtIsNull(scopeId);
case "college" -> userRepository.findAllByCollege_CollegeIdAndDeletedAtIsNull(scopeId);
case "school" -> userRepository.findAllBySchool_SchoolIdAndDeletedAtIsNull(scopeId);
default -> List.of();
};
}
private List<User> findUsersByTopic(String topic) {
if (topic == null || !topic.contains("_")) {
log.warn("Invalid topic format: {}", topic);
return List.of();
}
String[] parts = topic.split("_");
if (parts.length < 2) {
log.warn("Invalid topic format: {}", topic);
return List.of();
}
String scope = parts[0];
Long scopeId;
try {
scopeId = Long.valueOf(parts[1]);
} catch (NumberFormatException e) {
log.warn("Invalid scopeId in topic: {}", topic);
return List.of();
}
return switch (scope) {
case "major" -> userRepository.findAllByMajor_MajorIdAndDeletedAtIsNull(scopeId);
case "college" -> userRepository.findAllByCollege_CollegeIdAndDeletedAtIsNull(scopeId);
case "school" -> userRepository.findAllBySchool_SchoolIdAndDeletedAtIsNull(scopeId);
default -> List.of();
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.campus.campus.domain.notification.domain.entity;

import java.time.LocalDateTime;

import com.campus.campus.domain.user.domain.entity.User;
import com.campus.campus.global.entity.BaseEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Notification extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private NotificationType type;

@Column(nullable = false)
private String title;

@Column(nullable = false)
private String body;

@Column(name = "reference_id")
private Long referenceId; // postId, commentId 등

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

@Column(name = "read_at")
private LocalDateTime readAt;

@Builder
public Notification(User user, NotificationType type, String title,
String body, Long referenceId) {
this.user = user;
this.type = type;
this.title = title;
this.body = body;
this.referenceId = referenceId;
}

public void markAsRead() {
this.isRead = true;
this.readAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.campus.campus.domain.notification.domain.entity;

public enum NotificationType {
COUNCIL_POST_CREATED,
SYSTEM_NOTICE
}
Loading