-
Notifications
You must be signed in to change notification settings - Fork 0
✨Feat: 웹소켓 기반 채팅 기능 구현 #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b552b5
:recycle:Refactor: 게시글 카테고리 추가
uni-j-uni 35dc1b9
:heavy_plus_sign:Dependency: WebSocket 의존성 추가
uni-j-uni 920a8db
:recycle:Refactor: 교환 게시글 식별자 추가 및 로직 수정
uni-j-uni 255265e
:sparkles:Feat: 웹소켓 기반 채팅 기능 구현
uni-j-uni 67b5775
Merge branch 'main' into feature/chat
uni-j-uni 59f12f0
:recycle:Refactor: 게시글 쿼리 메소드 오류 수정
uni-j-uni e1b1a72
:recycle:Refactor: 코드 리뷰 기반 수정사항 반영
uni-j-uni 93e6daf
:recycle:Refactor: 코드 리뷰 기반 수정사항 반영
uni-j-uni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/sku/refit/domain/chat/controller/ChatController.java
This file contains hidden or 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,57 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.controller; | ||
|
|
||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
|
|
||
| import com.sku.refit.domain.chat.dto.response.ChatMessageResponse; | ||
| import com.sku.refit.domain.chat.dto.response.ChatRoomResponse; | ||
| import com.sku.refit.global.page.response.InfiniteResponse; | ||
| import com.sku.refit.global.response.BaseResponse; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.Parameter; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
|
||
| @Tag(name = "채팅", description = "채팅 관련 API") | ||
| @RequestMapping("/api/chats") | ||
| public interface ChatController { | ||
|
|
||
| @PostMapping("/exchange/{postId}") | ||
| @Operation(summary = "새 채팅방 생성", description = "특정 교환 게시글의 채팅방을 생성합니다.") | ||
| ResponseEntity<BaseResponse<ChatRoomResponse>> createChatRoom( | ||
| @Parameter(description = "채팅방을 생성할 교환글 식별자", example = "1") @PathVariable Long postId); | ||
|
|
||
| @GetMapping("/rooms") | ||
| @Operation(summary = "채팅방 조회", description = "사용자의 채팅방 내역을 조회합니다.") | ||
| ResponseEntity<BaseResponse<InfiniteResponse<ChatRoomResponse>>> getMyChatRooms( | ||
| @Parameter(description = "마지막으로 조회한 채팅방 식별자(첫 조회 시 생략)", example = "5") | ||
| @RequestParam(required = false) | ||
| Long lastChatRoomId, | ||
| @Parameter(description = "한 번에 조회할 채팅방 개수", example = "5") @RequestParam(defaultValue = "5") | ||
| Integer size); | ||
|
|
||
| @GetMapping("/rooms/{roomId}/messages") | ||
| ResponseEntity<BaseResponse<InfiniteResponse<ChatMessageResponse>>> getMessages( | ||
| @Parameter(description = "채팅방 식별자", example = "1") @PathVariable Long roomId, | ||
| @Parameter(description = "마지막으로 조회한 채팅 식별자(첫 조회 시 생략)", example = "10") | ||
| @RequestParam(required = false) | ||
| Long lastChatId, | ||
| @Parameter(description = "한 번에 조회할 채팅 개수", example = "10") @RequestParam(defaultValue = "10") | ||
| Integer size); | ||
|
|
||
| @PutMapping("/rooms/{roomId}/read") | ||
| ResponseEntity<BaseResponse<Void>> readMessages( | ||
| @Parameter(description = "채팅방 식별자", example = "1") @PathVariable Long roomId); | ||
| } |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/sku/refit/domain/chat/controller/ChatControllerImpl.java
This file contains hidden or 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,52 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.controller; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.sku.refit.domain.chat.dto.response.ChatMessageResponse; | ||
| import com.sku.refit.domain.chat.dto.response.ChatRoomResponse; | ||
| import com.sku.refit.domain.chat.service.ChatService; | ||
| import com.sku.refit.global.page.response.InfiniteResponse; | ||
| import com.sku.refit.global.response.BaseResponse; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class ChatControllerImpl implements ChatController { | ||
|
|
||
| private final ChatService chatService; | ||
|
|
||
| @Override | ||
| public ResponseEntity<BaseResponse<ChatRoomResponse>> createChatRoom(Long postId) { | ||
|
|
||
| ChatRoomResponse response = chatService.createChatRoom(postId); | ||
| return ResponseEntity.ok(BaseResponse.success(response)); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<BaseResponse<InfiniteResponse<ChatRoomResponse>>> getMyChatRooms( | ||
| Long lastChatRoomId, Integer size) { | ||
|
|
||
| return ResponseEntity.ok( | ||
| BaseResponse.success(chatService.getMyChatRooms(lastChatRoomId, size))); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<BaseResponse<InfiniteResponse<ChatMessageResponse>>> getMessages( | ||
| Long roomId, Long lastChatId, Integer size) { | ||
|
|
||
| return ResponseEntity.ok( | ||
| BaseResponse.success(chatService.getMessages(roomId, lastChatId, size))); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<BaseResponse<Void>> readMessages(Long roomId) { | ||
|
|
||
| chatService.readMessages(roomId); | ||
| return ResponseEntity.ok().build(); | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
src/main/java/com/sku/refit/domain/chat/controller/ChatMessageController.java
This file contains hidden or 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,33 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.controller; | ||
|
|
||
| import java.security.Principal; | ||
|
|
||
| import org.springframework.messaging.handler.annotation.MessageMapping; | ||
| import org.springframework.stereotype.Controller; | ||
|
|
||
| import com.sku.refit.domain.chat.dto.request.ChatMessageRequest; | ||
| import com.sku.refit.domain.chat.service.ChatService; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Controller | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class ChatMessageController { | ||
|
|
||
| private final ChatService chatService; | ||
|
|
||
| @MessageMapping("/chat/send") | ||
| public void sendMessage(ChatMessageRequest request, Principal principal) { | ||
|
|
||
| log.info( | ||
| "[WS CONTROLLER] sendMessage 호출됨 roomId={}, principal={}", | ||
| request.getRoomId(), | ||
| principal != null ? principal.getName() : "null"); | ||
| chatService.sendMessage(request, principal); | ||
| } | ||
uni-j-uni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/main/java/com/sku/refit/domain/chat/dto/request/ChatMessageRequest.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.dto.request; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ChatMessageRequest { | ||
|
|
||
| private Long roomId; | ||
| private String content; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/sku/refit/domain/chat/dto/response/ChatMessageResponse.java
This file contains hidden or 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,31 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.dto.response; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @Schema(title = "ChatMessageResponse DTO", description = "채팅 메세지 응답 반환") | ||
| public class ChatMessageResponse { | ||
|
|
||
| @Schema(description = "채팅 메세지 식별자", example = "1") | ||
| private Long messageId; | ||
|
|
||
| @Schema(description = "채팅방 식별자", example = "1") | ||
| private Long roomId; | ||
|
|
||
| @Schema(description = "채팅 발신자", example = "김다입") | ||
| private String senderNickname; | ||
|
|
||
| @Schema(description = "채팅 내용", example = "안녕하세요. 교환 원하시나요?") | ||
| private String content; | ||
|
|
||
| @Schema(description = "메세지 작성 시간", example = "20250101T120000") | ||
| private LocalDateTime createdAt; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/sku/refit/domain/chat/dto/response/ChatRoomResponse.java
This file contains hidden or 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,31 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.dto.response; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @Schema(title = "ChatRoomResponse DTO", description = "채팅방 응답 반환") | ||
| public class ChatRoomResponse { | ||
|
|
||
| @Schema(description = "채팅방 식별자", example = "1") | ||
| private Long roomId; | ||
|
|
||
| @Schema(description = "교환글 식별자", example = "1") | ||
| private Long exchangePostId; | ||
|
|
||
| @Schema(description = "수신자 닉네임", example = "김재생") | ||
| private String receiverNickname; | ||
|
|
||
| @Schema(description = "마지막 메세지", example = "내일 오후 1시에 가능합니다!") | ||
| private String lastMessage; | ||
|
|
||
| @Schema(description = "마지막 메세지 작성 시간", example = "20250101T120000") | ||
| private LocalDateTime lastMessageAt; | ||
| } |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/sku/refit/domain/chat/entity/ChatMessage.java
This file contains hidden or 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,55 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| 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 jakarta.persistence.Table; | ||
|
|
||
| import com.sku.refit.domain.user.entity.User; | ||
| import com.sku.refit.global.common.BaseTimeEntity; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor | ||
| @Table(name = "chat_message") | ||
| public class ChatMessage extends BaseTimeEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "chat_room_id", nullable = false) | ||
| private ChatRoom chatRoom; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "sender_id", nullable = false) | ||
| private User sender; | ||
|
|
||
| @Column(nullable = false) | ||
| private String content; | ||
|
|
||
| @Column(nullable = false) | ||
| @Builder.Default | ||
| private Boolean isRead = false; | ||
|
|
||
| @Column(nullable = false) | ||
| @Builder.Default | ||
| private Boolean isDeleted = false; | ||
| } |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/sku/refit/domain/chat/entity/ChatRoom.java
This file contains hidden or 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,74 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.entity; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| 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 jakarta.persistence.Table; | ||
| import jakarta.persistence.UniqueConstraint; | ||
|
|
||
| import com.sku.refit.domain.exchange.entity.ExchangePost; | ||
| import com.sku.refit.domain.user.entity.User; | ||
| import com.sku.refit.global.common.BaseTimeEntity; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor | ||
| @Table( | ||
| name = "chat_room", | ||
| uniqueConstraints = { | ||
| @UniqueConstraint(columnNames = {"exchange_post_id", "sender_id", "receiver_id"}) | ||
| }) | ||
| public class ChatRoom extends BaseTimeEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "exchange_post_id", nullable = false) | ||
| private ExchangePost exchangePost; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "sender_id", nullable = false) | ||
| private User sender; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "receiver_id", nullable = false) | ||
| private User receiver; | ||
|
|
||
| @Column(columnDefinition = "TEXT") | ||
| @Builder.Default | ||
| private String lastMessage = null; | ||
|
|
||
| @Column @Builder.Default private LocalDateTime lastMessageAt = LocalDateTime.now(); | ||
|
|
||
| @Column @Builder.Default private LocalDateTime senderLastReadAt = LocalDateTime.now(); | ||
|
|
||
| @Column @Builder.Default private LocalDateTime receiverLastReadAt = LocalDateTime.now(); | ||
|
|
||
| public void markAsRead(Long userId, LocalDateTime time) { | ||
| if (sender.getId().equals(userId)) { | ||
| this.senderLastReadAt = time; | ||
| } else { | ||
| this.receiverLastReadAt = time; | ||
| } | ||
| } | ||
uni-j-uni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/main/java/com/sku/refit/domain/chat/exception/ChatErrorCode.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright (c) SKU 다시입을Lab | ||
| */ | ||
| package com.sku.refit.domain.chat.exception; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import com.sku.refit.global.exception.model.BaseErrorCode; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum ChatErrorCode implements BaseErrorCode { | ||
| CHAT_NOT_FOUND("CHAT001", "채팅이 존재하지 않습니다.", HttpStatus.NOT_FOUND), | ||
| ; | ||
|
|
||
| private final String code; | ||
| private final String message; | ||
| private final HttpStatus status; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.