-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#62: 채팅 �API 개발
- Loading branch information
Showing
19 changed files
with
429 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/space/space_spring/controller/ChattingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package space.space_spring.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.messaging.handler.annotation.*; | ||
import org.springframework.messaging.simp.annotation.SubscribeMapping; | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.socket.messaging.SessionDisconnectEvent; | ||
import space.space_spring.dto.chat.request.ChatMessageRequest; | ||
import space.space_spring.dto.chat.response.ChatMessageLogResponse; | ||
import space.space_spring.dto.chat.response.ChatMessageResponse; | ||
import space.space_spring.service.ChattingService; | ||
import space.space_spring.service.UserChatRoomService; | ||
|
||
import java.util.Map; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class ChattingController { | ||
|
||
private final ChattingService chattingService; | ||
|
||
private final UserChatRoomService userChatRoomService; | ||
|
||
@MessageMapping("/chat/{chatRoomId}") // {chatRoomId} 채팅방으로 보낸 메세지 매핑 | ||
@SendTo("/topic/chat/{chatRoomId}") // {chatRoomId} 채팅방을 구독한 곳들로 메세지 전송 | ||
public ChatMessageResponse sendChatMessage (@Payload ChatMessageRequest chatMessageRequest, @DestinationVariable Long chatRoomId, | ||
@Header("simpSessionAttributes") Map<String, Object> sessionAttributes) { | ||
Long senderId = (Long) sessionAttributes.get("userId"); | ||
// log.info(senderId + " 님이 " + chatRoomId + " 채팅방으로 " + chatMessageRequest.getContent() + " 전송"); | ||
|
||
return chattingService.sendChatMessage(senderId, chatMessageRequest, chatRoomId); | ||
} | ||
|
||
@SubscribeMapping("/chat/{chatRoomId}") // {chatRoomId} 채팅방을 구독 | ||
public ChatMessageLogResponse subscribeChatRoom (@DestinationVariable Long chatRoomId, @Header("simpSessionAttributes") Map<String, Object> sessionAttributes) { | ||
// log.info(chatRoomId + " 채팅방 구독"); | ||
sessionAttributes.put("chatRoomId", chatRoomId); | ||
return chattingService.readChatMessageLog(chatRoomId); | ||
} | ||
|
||
// socket disconnect 시 호출 | ||
@EventListener | ||
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) { | ||
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage()); | ||
Map<String, Object> sessionAttributes = headerAccessor.getSessionAttributes(); | ||
|
||
Long userId = (Long) sessionAttributes.get("userId"); | ||
Long chatRoomId = (Long) sessionAttributes.get("chatRoomId"); | ||
|
||
userChatRoomService.saveLastReadTime(userId, chatRoomId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/space/space_spring/dao/chat/ChattingDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package space.space_spring.dao.chat; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
import space.space_spring.entity.document.ChatMessage; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Repository | ||
public interface ChattingDao extends MongoRepository<ChatMessage, String> { | ||
List<ChatMessage> findByChatRoomId(Long chatRoomId); | ||
|
||
ChatMessage findTopByChatRoomIdOrderByCreatedAtDesc(Long chatRoomId); | ||
|
||
int countByChatRoomIdAndCreatedAtBetween(Long chatRoomId, LocalDateTime lastReadTime, LocalDateTime lastUpdateTime); | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/java/space/space_spring/dao/chat/UserChatRoomDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package space.space_spring.dao.chat; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import space.space_spring.entity.ChatRoom; | ||
import space.space_spring.entity.User; | ||
import space.space_spring.entity.UserChatRoom; | ||
|
||
|
||
public interface UserChatRoomDao extends JpaRepository<UserChatRoom, Long> { | ||
UserChatRoom findByUserAndChatRoom(User userByUserId, ChatRoom chatRoomByChatRoomId); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/space/space_spring/dto/chat/request/ChatMessageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package space.space_spring.dto.chat.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import space.space_spring.entity.enumStatus.ChatMessageType; | ||
|
||
import java.util.HashMap; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ChatMessageRequest { | ||
|
||
@NotBlank(message = "메시지 내용은 공백일 수 없습니다.") | ||
private HashMap<String, String> content; | ||
|
||
@NotBlank(message = "스페이스 아이디는 공백일 수 없습니다.") | ||
private Long spaceId; | ||
|
||
@NotBlank(message = "메시지 타입은 공백일 수 없습니다.") | ||
private ChatMessageType messageType; | ||
} |
10 changes: 0 additions & 10 deletions
10
src/main/java/space/space_spring/dto/chat/request/ChatTestRequest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/space/space_spring/dto/chat/response/ChatMessageLogResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package space.space_spring.dto.chat.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Builder | ||
public class ChatMessageLogResponse { | ||
private List<ChatMessageResponse> chatMessageLog; | ||
|
||
public static ChatMessageLogResponse of(List<ChatMessageResponse> chatMessageList) { | ||
return ChatMessageLogResponse.builder() | ||
.chatMessageLog(chatMessageList) | ||
.build(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/space/space_spring/dto/chat/response/ChatMessageResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package space.space_spring.dto.chat.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import space.space_spring.entity.document.ChatMessage; | ||
import space.space_spring.entity.enumStatus.ChatMessageType; | ||
|
||
import java.util.HashMap; | ||
|
||
|
||
@Builder | ||
@Getter | ||
@AllArgsConstructor | ||
public class ChatMessageResponse { | ||
|
||
private HashMap<String, String> content; | ||
|
||
private String createdAt; | ||
|
||
private ChatMessageType messageType; | ||
|
||
private Long senderId; | ||
|
||
private String senderName; | ||
|
||
private String senderImg; | ||
|
||
public static ChatMessageResponse of(ChatMessage chatMessage) { | ||
return ChatMessageResponse.builder() | ||
.content(chatMessage.getContent()) | ||
.createdAt(String.valueOf(chatMessage.getCreatedAt())) | ||
.messageType(chatMessage.getMessageType()) | ||
.senderId(chatMessage.getSenderId()) | ||
.senderName(chatMessage.getSenderName()) | ||
.senderImg(chatMessage.getSenderImg()) | ||
.build(); | ||
} | ||
|
||
} |
18 changes: 0 additions & 18 deletions
18
src/main/java/space/space_spring/dto/chat/response/ChatTestResponse.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.