Skip to content

Commit 650c560

Browse files
committed
feat : 알림 웹 소켓 구현 (#18)
1 parent ab45240 commit 650c560

File tree

7 files changed

+77
-6
lines changed

7 files changed

+77
-6
lines changed

src/main/java/cmf/commitField/domain/noti/noti/controller/ApiV1NotiController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import cmf.commitField.domain.noti.noti.service.NotiService;
55
import cmf.commitField.domain.user.entity.User;
66
import cmf.commitField.domain.user.repository.UserRepository;
7-
import cmf.commitField.domain.user.service.CustomOAuth2UserService;
87
import cmf.commitField.global.error.ErrorCode;
98
import cmf.commitField.global.exception.CustomException;
109
import cmf.commitField.global.globalDto.GlobalResponse;
@@ -15,6 +14,7 @@
1514
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
1615
import org.springframework.security.oauth2.core.user.OAuth2User;
1716
import org.springframework.web.bind.annotation.GetMapping;
17+
import org.springframework.web.bind.annotation.PostMapping;
1818
import org.springframework.web.bind.annotation.RequestMapping;
1919
import org.springframework.web.bind.annotation.RestController;
2020

@@ -27,7 +27,6 @@
2727
@Slf4j
2828
public class ApiV1NotiController {
2929
private final NotiService notiService;
30-
private final CustomOAuth2UserService customOAuth2UserService;
3130
private final UserRepository userRepository;
3231

3332
@GetMapping("")
@@ -45,4 +44,9 @@ public GlobalResponse<List<NotiDto>> getNoti() {
4544

4645
return GlobalResponse.error(ErrorCode.LOGIN_REQUIRED);
4746
}
47+
48+
@PostMapping("")
49+
public void createNoti() {
50+
51+
}
4852
}

src/main/java/cmf/commitField/domain/noti/noti/dto/NotiDto.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
@Getter
1010
public class NotiDto {
11+
private Long id;
1112
private String message;
1213
private String formattedCreatedAt; // 변환된 날짜를 저장할 필드
1314

1415
// JPQL에서 사용할 수 있도록 필드 값 직접 받는 생성자 추가
15-
public NotiDto(String message, LocalDateTime createdAt) {
16+
public NotiDto(Long id, String message, LocalDateTime createdAt) {
17+
this.id = id;
1618
this.message = message;
1719
this.formattedCreatedAt = formatCreatedAt(createdAt); // 변환된 날짜 저장
1820
}

src/main/java/cmf/commitField/domain/noti/noti/repository/NotiRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@Repository
1515
public interface NotiRepository extends JpaRepository<Noti, Long> {
1616
Optional<List<Noti>> findNotiByReceiverAndRelId(User receiver, long season);
17-
@Query("SELECT new cmf.commitField.domain.noti.noti.dto.NotiDto(n.message, n.createdAt) " +
17+
@Query("SELECT new cmf.commitField.domain.noti.noti.dto.NotiDto(n.id, n.message, n.createdAt) " +
1818
"FROM Noti n JOIN n.receiver u WHERE u.id = :receiverId AND n.isRead = :isRead")
1919
Optional<List<NotiDto>> findNotiDtoByReceiverId(@Param("receiverId") Long receiverId, @Param("isRead") boolean isRead);
2020

src/main/java/cmf/commitField/domain/noti/noti/service/NotiService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public static String generateMessage(NotiDetailType type, Object... params) {
3434
return message;
3535
}
3636

37+
@Transactional
38+
public void createNoti(User receiver) {
39+
System.out.println("알림 생성");
40+
String message = NotiService.generateMessage(NotiDetailType.STREAK_BROKEN, receiver.getNickname());
41+
42+
Noti noti = Noti.builder()
43+
.typeCode(NotiType.STREAK)
44+
.type2Code(NotiDetailType.STREAK_BROKEN)
45+
.receiver(receiver)
46+
.isRead(false)
47+
.message(message)
48+
.build();
49+
notiRepository.save(noti);
50+
}
51+
3752

3853
public List<NotiDto> getNotReadNoti(User receiver) {
3954
System.out.println("알림 조회");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cmf.commitField.global.scheduler;
2+
3+
import cmf.commitField.domain.noti.noti.service.NotiService;
4+
import cmf.commitField.domain.user.repository.UserRepository;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@RequiredArgsConstructor
10+
public class NotiTestScheduler {
11+
private final NotiService notiService;
12+
private final UserRepository userRepository;
13+
14+
// @Scheduled(cron = "0 44 * * * *")
15+
// public void test() {
16+
// System.out.println("test 실행");
17+
// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
18+
//
19+
// if (authentication instanceof OAuth2AuthenticationToken) {
20+
// OAuth2User principal = (OAuth2User) authentication.getPrincipal();
21+
// Map<String, Object> attributes = principal.getAttributes();
22+
// String username = (String) attributes.get("login"); // GitHub ID
23+
// User user = userRepository.findByUsername(username).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
24+
// notiService.createNoti(user);
25+
// }
26+
//
27+
// }
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmf.commitField.global.websocket;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
5+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
6+
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
7+
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
8+
9+
@Configuration
10+
@EnableWebSocketMessageBroker
11+
public class NotiWebsoketConfig implements WebSocketMessageBrokerConfigurer {
12+
@Override
13+
public void registerStompEndpoints(StompEndpointRegistry registry) {
14+
registry.addEndpoint("/ws") // WebSocket 엔드포인트 설정
15+
.setAllowedOriginPatterns("*") // CORS 허용
16+
.withSockJS(); // SockJS 지원
17+
}
18+
19+
@Override
20+
public void configureMessageBroker(MessageBrokerRegistry registry) {
21+
registry.enableSimpleBroker("/topic"); // 메시지 브로커 활성화
22+
registry.setApplicationDestinationPrefixes("/app"); // 클라이언트에서 보낼 경로
23+
}
24+
}

src/main/java/cmf/commitField/global/websocket/WebSocketConfig.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
public class WebSocketConfig implements WebSocketConfigurer {
1313

1414
private final ChatWebSocketHandler chatWebSocketHandler;
15-
private final NotiWebSocketHandler notiWebSocketHandler;
1615

1716
@Override
1817
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
1918
registry.addHandler(chatWebSocketHandler, "/chat").setAllowedOrigins("*");
20-
registry.addHandler(notiWebSocketHandler, "/notifications").setAllowedOrigins("*"); // 알림 엔드포인트 추가
2119
}
2220
}

0 commit comments

Comments
 (0)