77import org .dfbf .soundlink .domain .alert .service .AlertService ;
88import org .dfbf .soundlink .domain .blocklist .repository .BlockListRepository ;
99import org .dfbf .soundlink .domain .chat .dto .ChatRejectDto ;
10+ import org .dfbf .soundlink .domain .chat .dto .ChatReqDto ;
1011import org .dfbf .soundlink .domain .chat .dto .ChatRoomInfoDto ;
1112import org .dfbf .soundlink .domain .chat .dto .ChatRoomListDto ;
12- import org .dfbf .soundlink .domain .chat .entity .redis .ChatRequest ;
13- import org .dfbf .soundlink .domain .chat .dto .ChatReqDto ;
1413import org .dfbf .soundlink .domain .chat .entity .ChatRoom ;
14+ import org .dfbf .soundlink .domain .chat .entity .redis .ChatRequest ;
1515import org .dfbf .soundlink .domain .chat .exception .ChatRoomNotFoundException ;
1616import org .dfbf .soundlink .domain .chat .exception .UnauthorizedAccessException ;
1717import org .dfbf .soundlink .domain .chat .repository .ChatRoomRepository ;
2727import org .dfbf .soundlink .global .exception .ErrorCode ;
2828import org .dfbf .soundlink .global .exception .ResponseResult ;
2929import org .dfbf .soundlink .global .feign .chat .DevChatClient ;
30+ import org .dfbf .soundlink .global .kafka .KafkaProducer ;
3031import org .springframework .data .redis .core .RedisTemplate ;
3132import org .springframework .security .core .annotation .AuthenticationPrincipal ;
3233import org .springframework .stereotype .Service ;
3334import org .springframework .transaction .annotation .Transactional ;
3435
35- import java .time .Duration ;
3636import java .sql .Timestamp ;
37-
38- import java .util .HashMap ;
39- import java .util .List ;
40- import java .util .Map ;
41- import java .util .Optional ;
37+ import java .time .Duration ;
4238
4339import java .util .*;
44- import java .util .stream .Collectors ;
4540
4641
4742@ Service
@@ -56,9 +51,11 @@ public class ChatRoomService {
5651 private final BlockListRepository blockListRepository ;
5752 private final AlertService alertService ;
5853 private final DevChatClient devChatClient ;
54+ private final KafkaProducer kafkaProducer ;
5955 private final UserStatusService userStatusService ;
6056
6157 private static final String CHAT_REQUEST_KEY = "chatRequest" ;
58+ private static final String TOPIC = "alert-topic" ;
6259
6360 // 요청을 Redis에 저장 (TTL: 60초)
6461 public ResponseResult saveRequestToRedis (Long requestUserId , Long emotionRecordId ) {
@@ -74,7 +71,14 @@ public ResponseResult saveRequestToRedis(Long requestUserId, Long emotionRecordI
7471 return new ResponseResult (400 , "You can't chat with yourself." );
7572 }
7673
77- //이미 요청이 있는지 확인(Redis에 emotionRecordId에 대한 요청이 있는지 확인)
74+ // Redis에 이미 requestUserId가 포함되어 있는 경우
75+ if (!redisTemplate .keys (CHAT_REQUEST_KEY + requestUserId + "to*" ).isEmpty ()) {
76+ String firstKey = redisTemplate .keys (CHAT_REQUEST_KEY + requestUserId + "to*" ).iterator ().next (); // 첫 번째 키 가져오기
77+ Long ttl = redisTemplate .getExpire (firstKey );
78+ return new ResponseResult (400 , ttl + "초 후에 다시 시도해주세요." );
79+ }
80+
81+ // 이미 요청이 있는지 확인(Redis에 emotionRecordId에 대한 요청이 있는지 확인)
7882 Set <String > existIngKeys = redisTemplate .keys (CHAT_REQUEST_KEY + "*to" + emotionRecordId + "*" );
7983 if (!existIngKeys .isEmpty ()){
8084 //이미 요청이 있을 경우, 예외처리
@@ -87,13 +91,6 @@ public ResponseResult saveRequestToRedis(Long requestUserId, Long emotionRecordI
8791 return new ResponseResult (400 , "Blocked user." );
8892 }
8993
90- // Redis에 이미 requestUserId가 포함되어 있는 경우
91- if (!redisTemplate .keys (CHAT_REQUEST_KEY + requestUserId + "to*" ).isEmpty ()) {
92- String firstKey = redisTemplate .keys (CHAT_REQUEST_KEY + requestUserId + "to*" ).iterator ().next (); // 첫 번째 키 가져오기
93- Long ttl = redisTemplate .getExpire (firstKey );
94- return new ResponseResult (400 , ttl + "초 후에 다시 시도해주세요." );
95- }
96-
9794 // Key & Request 객체 생성
9895 String key = CHAT_REQUEST_KEY + requestUserId + "to" + emotionRecordId ;
9996 ChatRequest chatRequest = new ChatRequest (requestUserId , responseUserId , emotionRecordId );
@@ -105,8 +102,8 @@ public ResponseResult saveRequestToRedis(Long requestUserId, Long emotionRecordI
105102 User requestUser = userRepository .findByUserIdWithCache (requestUserId )
106103 .orElseThrow (UserNotFoundException ::new );
107104 AlertChatRequest alertChatRequest = new AlertChatRequest (emotionRecordId , requestUser .getNickname ());
108- Alert alert = new Alert ( "chatRequest " , alertChatRequest );
109- alertService .send (responseUserId , "alarm" , alert );
105+ Alert alert = alertService . createAlert ( responseUserId , "alarm " , alertChatRequest );
106+ kafkaProducer .send (TOPIC , alert );
110107
111108 return new ResponseResult (ErrorCode .SUCCESS );
112109 } catch (EmotionRecordNotFoundException e ) {
@@ -131,7 +128,9 @@ public ResponseResult deleteRequestFromRedis(Long userId, Long emotionRecordId)
131128 // Redis에 Key가 존재하는 경우 삭제 (KEY가 없는 경우 400)
132129 if (Boolean .TRUE .equals (redisTemplate .hasKey (key ))) {
133130 redisTemplate .delete (key );
134- alertService .send (recordIdInUserId , "cancel" , "Chat request has been canceled." );
131+ Alert alert = alertService .createAlert (recordIdInUserId , "cancel" , "Chat request has been canceled." );
132+ kafkaProducer .send (TOPIC , alert );
133+ log .info ("tset" );
135134 return new ResponseResult (ErrorCode .SUCCESS );
136135 } else {
137136 return new ResponseResult (400 , "ChatRequest not found or expired." );
@@ -165,7 +164,8 @@ public ResponseResult requestRejected(Long responseUserId, ChatRejectDto chatRej
165164 // Redis에 Key가 존재하는 경우 삭제 (KEY가 없는 경우 400)
166165 if (Boolean .TRUE .equals (redisTemplate .hasKey (key ))) {
167166 redisTemplate .delete (key );
168- alertService .send (requestUserId , "fail" , "채팅 요청을 거부했습니다" );
167+ Alert alert = alertService .createAlert (requestUserId , "fail" , "채팅 요청을 거부했습니다" );
168+ kafkaProducer .send (TOPIC , alert );
169169 return new ResponseResult (ErrorCode .SUCCESS );
170170 } else {
171171 return new ResponseResult (400 , "ChatRequest not found or expired." );
@@ -214,7 +214,10 @@ public ResponseResult createChatRoom(Long userId, Long recordId, String requestN
214214 if (chatRoomId .isPresent ()) {
215215 Map <String , Object > map = new HashMap <>();
216216 map .put ("chatRoomId" , chatRoomId .get ());
217- alertService .send (requestUserId , "accept" , map );
217+
218+ Alert alert = alertService .createAlert (requestUserId , "accept" , map );
219+ kafkaProducer .send (TOPIC , alert );
220+
218221 return new ResponseResult (map );
219222 }
220223
@@ -241,7 +244,8 @@ public ResponseResult createChatRoom(Long userId, Long recordId, String requestN
241244 map .put ("chatRoomId" , chatRoom .getChatRoomId ());
242245
243246 // 요청자에게 방번호를 보냄
244- alertService .send (requestUserId , "accept" , map );
247+ Alert alert = alertService .createAlert (requestUserId , "accept" , map );
248+ kafkaProducer .send (TOPIC , alert );
245249
246250 userStatusService .setChatting (userId , true );
247251
0 commit comments