Skip to content

Commit e0a58fb

Browse files
authored
Merge pull request #131 from CommitField/dev
merge
2 parents 5f4734c + 6f99f4a commit e0a58fb

File tree

11 files changed

+108
-30
lines changed

11 files changed

+108
-30
lines changed

โ€Ž.github/workflows/deploy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
aws-region: ${{ secrets.AWS_REGION }}
7777
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
7878
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
79-
instance-ids: "i-0e27758b937bcc047"
79+
instance-ids: "i-050af13cf2c3bc98a"
8080
working-directory: /
8181
comment: Deploy
8282
command: |

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatMessage/controller/response/ChatMsgResponse.java

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@AllArgsConstructor
1111
@Builder
1212
public class ChatMsgResponse {
13+
//์ฑ„ํŒ…๋ฐฉ ID
1314
private Long roomId;
1415
//์‚ฌ์šฉ์ž(user)
1516
private String from;

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatMessage/service/ChatMessageServiceImpl.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class ChatMessageServiceImpl implements ChatMessageService {
3333
private final ChatMessageCustomRepository chatMessageCustomRepository;
3434
private final UserChatRoomRepository userChatRoomRepository;
3535

36+
// ์ปค๋ฐ‹์šฉ ์ฃผ์„
37+
3638
@Override
3739
@Transactional
3840
public ChatMsgResponse sendMessage(ChatMsgRequest message, Long userId, Long roomId) {
@@ -48,17 +50,23 @@ public ChatMsgResponse sendMessage(ChatMsgRequest message, Long userId, Long roo
4850
.user(findUser)
4951
.chatRoom(chatRoom)
5052
.build();
51-
// Response
53+
54+
// Response message
55+
// ์‘๋‹ต ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜
56+
5257
ChatMsgResponse response = ChatMsgResponse.builder()
58+
5359
.roomId(roomId)
5460
.from(findUser.getNickname())
5561
.message(message.getMessage())
5662
.sendAt(chatMsg.getCreatedAt())
5763
.build();
5864
chatMessageRepository.save(chatMsg);
5965
return response;
66+
6067
}
6168

69+
6270
@Transactional(readOnly = true)
6371
@Override
6472
public List<ChatMsgDto> getRoomChatMsgList(Long roomId, Long userId, Long lastId) {
@@ -86,4 +94,4 @@ public List<ChatMsgDto> getRoomChatMsgList(Long roomId, Long userId, Long lastId
8694
}
8795
return chatMsgDtos;
8896
}
89-
}
97+
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/controller/ChatRoomController.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmf.commitField.domain.chat.chatRoom.controller;
22

33
import cmf.commitField.domain.File.service.FileService;
4+
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomJoinRequest;
45
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomRequest;
56
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomUpdateRequest;
67
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto;
@@ -62,13 +63,13 @@ public GlobalResponse<Object> createRoom(
6263

6364
//์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ
6465
@PostMapping("/room/join/{roomId}")
65-
public GlobalResponse<Object> joinRoom(@PathVariable Long roomId, @RequestBody ChatRoomRequest chatRoomRequest) {
66+
public GlobalResponse<Object> joinRoom(@PathVariable Long roomId, @RequestBody ChatRoomJoinRequest chatRoomJoinRequest) {
6667
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
6768

6869
if (authentication instanceof OAuth2AuthenticationToken) {
6970
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
7071
Long userId = principal.getId(); // getId()๋ฅผ ํ†ตํ•ด userId๋ฅผ ์ถ”์ถœ
71-
chatRoomService.joinRoom(roomId, userId, chatRoomRequest); // userId๋ฅผ ์ „๋‹ฌ
72+
chatRoomService.joinRoom(roomId, userId, chatRoomJoinRequest); // userId๋ฅผ ์ „๋‹ฌ
7273
return GlobalResponse.success("ํ•ด๋‹น ์ฑ„ํŒ…๋ฐฉ์— ์ž…์žฅํ•˜์…จ์Šต๋‹ˆ๋‹ค");
7374
} else {
7475
throw new IllegalArgumentException("๋กœ๊ทธ์ธ ํ›„์— ์ด์šฉํ•ด ์ฃผ์„ธ์š”.");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmf.commitField.domain.chat.chatRoom.controller.request;
2+
3+
import jakarta.validation.constraints.Max;
4+
import jakarta.validation.constraints.NotNull;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import org.hibernate.validator.constraints.Length;
8+
9+
@Getter
10+
@Setter
11+
public class ChatRoomJoinRequest {
12+
@NotNull
13+
@Length(min = 2, max = 20)
14+
private String title;
15+
16+
@NotNull
17+
@Max(100)
18+
private Integer userCountMax;
19+
20+
@Length(min = 4, max = 20)
21+
private String password;
22+
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/controller/request/ChatRoomRequest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class ChatRoomRequest {
2828

2929
private MultipartFile file; // ํŒŒ์ผ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ํ•„๋“œ
3030

31-
public boolean isFileSizeValid() {
32-
return file == null || file.getSize() <= 5 * 1024 * 1024; // 5MB
33-
}
31+
// public boolean isFileSizeValid() {
32+
// return file == null || file.getSize() <= 5 * 1024 * 1024; // 5MB
33+
// }
3434

3535
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmf.commitField.domain.chat.chatRoom.controller.response;
2-
3-
public class ChatRoomResponse {
4-
}
1+
//package cmf.commitField.domain.chat.chatRoom.controller.response;
2+
//
3+
//public class ChatRoomResponse {
4+
//}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cmf.commitField.domain.chat.chatRoom.service;
22

3+
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomJoinRequest;
34
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomRequest;
45
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomUpdateRequest;
56
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto;
@@ -34,7 +35,7 @@ public interface ChatRoomService {
3435

3536
List<ChatRoomDto> myHeartRoomList(Long userId, Pageable pageable);
3637

37-
void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest);
38+
void joinRoom(Long roomId, Long userId, ChatRoomJoinRequest chatRoomJoinRequest);
3839

3940
List<ChatRoomDto> searchRoomByTitle(String roomName, Long userId, Pageable pageable);
4041
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomServiceImpl.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmf.commitField.domain.chat.chatRoom.service;
22

33
import cmf.commitField.domain.chat.chatMessage.repository.ChatMessageRepository;
4+
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomJoinRequest;
45
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomRequest;
56
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomUpdateRequest;
67
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto;
@@ -72,6 +73,7 @@ public void createRoom(ChatRoomRequest chatRoomRequest, Long userId, String imag
7273
.imageUrl(imageUrl) // ์ด๋ฏธ์ง€ URL ์ถ”๊ฐ€
7374
.build();
7475
if (password != null) {
76+
System.out.println("Setting password: " + password);
7577
chatRoom.setPassword(password);
7678
chatRoom.setIsPrivate(true);
7779
}
@@ -127,7 +129,7 @@ public List<ChatRoomDto> getUserByRoomPartList(Long userId, Pageable pageable) {
127129

128130
@Override
129131
@Transactional
130-
public void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest) {
132+
public void joinRoom(Long roomId, Long userId, ChatRoomJoinRequest chatRoomJoinRequest) {
131133
RLock lock = redissonClient.getLock("joinRoomLock:" + roomId);
132134
try {
133135
boolean available = lock.tryLock(1, TimeUnit.SECONDS);
@@ -145,13 +147,13 @@ public void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest)
145147
// user_chatroom ํ˜„์žฌ ์ธ์› ์นด์šดํŠธ (๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง)
146148
Long currentUserCount = userChatRoomRepository.countNonLockByChatRoomId(roomId); // lock (๊ธฐ์กด)
147149

148-
if (chatRoom.getIsPrivate() && chatRoomRequest.getPassword() == null) {
150+
if (chatRoom.getIsPrivate() && chatRoomJoinRequest.getPassword() == null) {
149151
throw new CustomException(ErrorCode.NEED_TO_PASSWORD);
150152

151153

152154

153155
}
154-
if (chatRoom.getIsPrivate() && !chatRoomRequest.getPassword().equals(chatRoom.getPassword())) {
156+
if (chatRoom.getIsPrivate() && !chatRoomJoinRequest.getPassword().equals(chatRoom.getPassword())) {
155157
throw new CustomException(ErrorCode.ROOM_PASSWORD_MISMATCH);
156158
}
157159
List<Long> userChatRoomByChatRoomId = userChatRoomRepository
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmf.commitField.global.aws.s3;
22

33
import lombok.RequiredArgsConstructor;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46
import org.springframework.stereotype.Service;
57
import org.springframework.web.multipart.MultipartFile;
68
import software.amazon.awssdk.core.sync.RequestBody;
@@ -11,29 +13,55 @@
1113
import java.io.IOException;
1214
import java.util.UUID;
1315

16+
1417
@Service
1518
@RequiredArgsConstructor
1619
public class S3Service {
1720
private final S3Client s3Client;
1821
private static final String BUCKET_NAME = "cmf-bucket-dev-seoyeon-1";
1922
private static final String REGION = "ap-northeast-2";
23+
private static final long MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
24+
private final Logger logger = LoggerFactory.getLogger(S3Service.class.getName());
2025

2126
// ํŒŒ์ผ ์—…๋กœ๋“œ ๊ธฐ๋Šฅ
2227
public String uploadFile(MultipartFile file, String dirName) throws IOException {
23-
String fileName = dirName + "/" + UUID.randomUUID() + "_" + file.getOriginalFilename();
24-
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
25-
.bucket(BUCKET_NAME)
26-
.key(fileName)
27-
.contentType(file.getContentType())
28-
// .acl(ObjectCannedACL.PUBLIC_READ) // Public Read ๊ถŒํ•œ ์ถ”๊ฐ€
29-
.build();
28+
try {
29+
30+
// ํŒŒ์ผ ํฌ๊ธฐ ๊ฒ€์ฆ
31+
validateFileSize(file);
3032

31-
s3Client.putObject(putObjectRequest,
32-
RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
33+
// UUID๋กœ ๊ณ ์œ ํ•œ ํŒŒ์ผ๋ช… ์ƒ์„ฑ
34+
String fileName = dirName + "/" + UUID.randomUUID() + "_" + file.getOriginalFilename();
35+
// PutObjectRequest ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
36+
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
37+
.bucket(BUCKET_NAME)
38+
.key(fileName)
39+
.contentType(file.getContentType())
40+
// .acl(ObjectCannedACL.PUBLIC_READ) // Public Read ๊ถŒํ•œ ์ถ”๊ฐ€
41+
.build();
42+
// ํŒŒ์ผ S3์— ์—…๋กœ๋“œ
43+
s3Client.putObject(putObjectRequest,
44+
RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
45+
logger.info("ํŒŒ์ผ ์—…๋กœ๋“œ ์„ฑ๊ณต");
3346

34-
return "https://" + BUCKET_NAME + ".s3." + REGION + ".amazonaws.com/" + fileName;
47+
// S3 ํŒŒ์ผ URL ๋ฐ˜ํ™˜
48+
return "https://" + BUCKET_NAME + ".s3." + REGION + ".amazonaws.com/" + fileName;
49+
50+
} catch (IOException e) {
51+
logger.error("ํŒŒ์ผ ์—…๋กœ๋“œ ์‹คํŒจ: {}", e.getMessage());
52+
throw new IOException("ํŒŒ์ผ ์—…๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ", e);
53+
}
54+
}
55+
56+
// ํŒŒ์ผ ํฌ๊ธฐ ๊ฒ€์ฆ ๋ฉ”์„œ๋“œ
57+
private void validateFileSize(MultipartFile file) throws IOException {
58+
if (file.getSize() > MAX_FILE_SIZE) {
59+
throw new IOException("ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ 5MB๋ฅผ ์ดˆ๊ณผํ•˜์—ฌ ์—…๋กœ๋“œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
60+
}
3561
}
3662

63+
64+
3765
// ํŒŒ์ผ ์‚ญ์ œ ๊ธฐ๋Šฅ
3866
public void deleteFile(String fileName) {
3967
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
@@ -42,5 +70,6 @@ public void deleteFile(String fileName) {
4270
.build();
4371

4472
s3Client.deleteObject(deleteObjectRequest);
73+
logger.info("ํŒŒ์ผ ์‚ญ์ œ ์„ฑ๊ณต: {}", fileName);
4574
}
4675
}

โ€Žsrc/main/java/cmf/commitField/global/websocket/ChatWebSocketHandler.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ private void handleChatMessage(WebSocketSession session, JsonNode jsonNode) {
173173
Long roomId = jsonNode.get("roomId").asLong();
174174
Long userId = jsonNode.get("userId").asLong();
175175
String message = jsonNode.get("message").asText();
176+
String from = jsonNode.has("from") ? jsonNode.get("from").asText() : null;
177+
178+
log.info("์ฑ„ํŒ… ๋ฉ”์‹œ์ง€: roomId={}, userId={}, message={}, from={}", roomId, userId, message, from);
176179

177180
if (message == null || message.trim().isEmpty()) {
178181
sendErrorMessage(session, "๋ฉ”์‹œ์ง€ ๋‚ด์šฉ์ด ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค.");
179182
return;
180183
}
181184

182-
log.info("์ฑ„ํŒ… ๋ฉ”์‹œ์ง€: roomId={}, userId={}, message={}", roomId, userId, message);
183-
184185
// ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฒ€์ฆ
185186
User user = userRepository.findById(userId).orElse(null);
186187
if (user == null) {
@@ -194,8 +195,19 @@ private void handleChatMessage(WebSocketSession session, JsonNode jsonNode) {
194195
ChatMsgRequest chatMsgRequest = new ChatMsgRequest(message);
195196
ChatMsgResponse response = chatMessageService.sendMessage(chatMsgRequest, userId, roomId);
196197

197-
// ๋ฉ”์‹œ์ง€ ํฌ๋งท ๋ณ€ํ™˜ํ•˜์—ฌ ์ „์†ก
198-
String messageJson = objectMapper.writeValueAsString(response);
198+
// ์›น์†Œ์ผ“ ๋ฉ”์‹œ์ง€ ํฌ๋งท ์ƒ์„ฑ (ํด๋ผ์ด์–ธํŠธ์™€ ์ผ์น˜์‹œํ‚ด)
199+
Map<String, Object> wsMessage = new HashMap<>();
200+
wsMessage.put("type", "CHAT");
201+
wsMessage.put("roomId", roomId);
202+
wsMessage.put("userId", userId);
203+
wsMessage.put("from", response.getFrom());
204+
wsMessage.put("nickname", response.getFrom()); // ํด๋ผ์ด์–ธํŠธ ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด ๋‘ ํ•„๋“œ ๋ชจ๋‘ ์„ค์ •
205+
wsMessage.put("message", message);
206+
wsMessage.put("sendAt", response.getSendAt().toString());
207+
208+
// ๋ฉ”์‹œ์ง€ JSON ๋ณ€ํ™˜
209+
String messageJson = objectMapper.writeValueAsString(wsMessage);
210+
log.info("Broadcasting message: {}", messageJson);
199211

200212
// ํ•ด๋‹น ์ฑ„ํŒ…๋ฐฉ์˜ ๋ชจ๋“  ์„ธ์…˜์— ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ
201213
broadcastMessageToRoom(roomId, messageJson);
@@ -223,9 +235,11 @@ private void broadcastMessageToRoom(Long roomId, String message) {
223235
for (WebSocketSession session : roomSessions) {
224236
try {
225237
if (session.isOpen()) {
238+
log.debug("Broadcasting to session {}", session.getId());
226239
session.sendMessage(new TextMessage(message));
227240
} else {
228241
failedSessions.add(session);
242+
log.debug("Session closed, adding to failed sessions: {}", session.getId());
229243
}
230244
} catch (IOException e) {
231245
log.error("๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ์ค‘ ์˜ค๋ฅ˜: {}", e.getMessage());

0 commit comments

Comments
ย (0)