diff --git a/socket/src/main/java/Backend/socket/api/chat/controller/ChatController.java b/socket/src/main/java/Backend/socket/api/chat/controller/ChatController.java new file mode 100644 index 00000000..5eae3457 --- /dev/null +++ b/socket/src/main/java/Backend/socket/api/chat/controller/ChatController.java @@ -0,0 +1,39 @@ +package Backend.socket.api.chat.controller; + + +import Backend.socket.api.chat.service.dto.request.ChatMessageRoomRequestDto; +import Backend.socket.api.chat.service.ChatService; +import Backend.socket.global.common.MessageSuccessCode; +import Backend.socket.global.common.MessageSuccessResponse; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.messaging.handler.annotation.DestinationVariable; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; + +@RestController +public class ChatController { + private final ChatService chatService; + private final SimpMessagingTemplate template; + private final RedisTemplate redisTemplate; + public ChatController(ChatService chatService, SimpMessagingTemplate template, + @Qualifier("redisTemplate") RedisTemplate redisTemplate) { + this.chatService = chatService; + this.template = template; + this.redisTemplate = redisTemplate; + } + + @MessageMapping("/room/{roomName}") + @SendTo("/sub/room/{roomName}") + public MessageSuccessResponse sendChatMessageInRoom(@DestinationVariable("roomName") final String roomName, + @RequestBody final ChatMessageRoomRequestDto chatMessageRoomRequestDto) throws IOException { + return MessageSuccessResponse.of(MessageSuccessCode.RECEIVED, chatService.createSendMessageContentInRoom(roomName, chatMessageRoomRequestDto).getMessage()); + } + + +} diff --git a/socket/src/main/java/Backend/socket/api/chat/service/ChatService.java b/socket/src/main/java/Backend/socket/api/chat/service/ChatService.java new file mode 100644 index 00000000..77ca4a16 --- /dev/null +++ b/socket/src/main/java/Backend/socket/api/chat/service/ChatService.java @@ -0,0 +1,116 @@ +package Backend.socket.api.chat.service; + +import Backend.socket.api.chat.service.dto.request.ChatMessageRoomRequestDto; +import Backend.socket.api.chat.service.dto.response.ChatMessageElementResponseDto; +import Backend.socket.api.chat.service.dto.response.ChatMessageRoomResponseDto; +import Backend.socket.domain.chat.domain.*; +import Backend.socket.domain.chat.repository.RoomRepository; +import Backend.socket.domain.user.repository.UserRepository; +import Backend.socket.domain.user.entity.User; +import Backend.socket.global.error.socketException.EntityNotFoundException; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static Backend.socket.domain.chat.domain.ChatContent.createChatContent; +import static Backend.socket.global.error.ErrorCode.USER_NOT_FOUND; + + +@RequiredArgsConstructor +@Transactional +@Service +public class ChatService { + private final MongoTemplate mongoTemplate; + private final UserRepository userRepository; + private final RoomRepository roomRepository; + public ChatMessageRoomResponseDto createSendMessageContentInRoom(String roomName, ChatMessageRoomRequestDto chatMessageRoomRequestDto) { + Room room = getChatBySessionsInRoom(roomName, chatMessageRoomRequestDto.getChatSession()); + User user = userRepository.findBySessionId(chatMessageRoomRequestDto.getChatSession()).orElseThrow(); + ChatContent chatContent = createChatContent(chatMessageRoomRequestDto.getFromUserName(), chatMessageRoomRequestDto.getContent(), room, chatMessageRoomRequestDto.getImage()); + ChatMessageElementResponseDto chatMessage = ChatMessageElementResponseDto.of(chatContent, chatMessageRoomRequestDto.getChatSession(), user.getProfile(), chatMessageRoomRequestDto.getImage(), validate(room.getOwnerSession(),chatMessageRoomRequestDto.getChatSession())); + List sessionIdList = getSessionIdListInRoom(roomName, chatMessageRoomRequestDto.getChatSession()); + saveChatRoom(room); + return ChatMessageRoomResponseDto.of(chatMessageRoomRequestDto.getToRoomName(), sessionIdList, chatMessage); + } + private Boolean validate(String ownerSession, String sessionId){ + if(ownerSession.equals(sessionId)) + return true; + return false; + } + + private List getSessionIdListInRoom(String roomName, String sessionId) { + List sessionList = new ArrayList<>(); + + // roomId를 기반으로 Room 문서 찾기 + Room room = findRoomChatByRoomName(roomName); + + if (room != null) { + // Room에 속한 모든 ChatUser의 sessionId를 리스트에 추가 + for (ChatUser chatUser : room.getChatUserList()) { + sessionList.add(chatUser.getSessionId()); + } + } + + return sessionList; + } + + + private Room getChatBySessionsInRoom(String roomName, String sessionId) { + Room room = findRoomChatByRoomName(roomName); + if (Objects.isNull(room)) { + // 채팅방이 없는 경우 새로운 채팅방 생성 + room = createNewRoom(roomName); + } + + // 채팅방에 sessionId를 가진 유저가 있는지 확인 + if (!isUserExistsInRoom(room, sessionId)) { + // 유저가 없다면 새로운 유저 생성하여 채팅방에 추가 + ChatUser chatUser = createChatUser(sessionId); + room.addChatRoom(chatUser); + } + + return room; + } + + private Room createNewRoom(String roomName) { + Room room = Room.builder() + .roomName(roomName) + .build(); + return room; + } + private boolean isUserExistsInRoom(Room room, String sessionId) { + for (ChatUser chatUser : room.getChatUserList()) { + if (chatUser.getSessionId().equals(sessionId)) { + return true; + } + } + return false; + } + private ChatUser createChatUser(String sessionId) { + User user = getUserFromSessionId(sessionId); + return ChatUser.createChatUser(user); + } + + private Room findRoomChatByRoomName(String roomName) { + Query query = new Query(); + query.addCriteria(Criteria.where("roomName").is(roomName)); + return mongoTemplate.findOne(query, Room.class); + } + + + private User getUserFromSessionId(String sessionId) { + return userRepository.findBySessionId(sessionId) + .orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND)); + } + + public void saveChatRoom(Room room) { + roomRepository.save(room); + } +} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageRoomRequestDto.java b/socket/src/main/java/Backend/socket/api/chat/service/dto/request/ChatMessageRoomRequestDto.java similarity index 85% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageRoomRequestDto.java rename to socket/src/main/java/Backend/socket/api/chat/service/dto/request/ChatMessageRoomRequestDto.java index 92159a41..34cb81b1 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageRoomRequestDto.java +++ b/socket/src/main/java/Backend/socket/api/chat/service/dto/request/ChatMessageRoomRequestDto.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.controller.dto.request; +package Backend.socket.api.chat.service.dto.request; import lombok.AccessLevel; import lombok.Getter; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageElementResponseDto.java b/socket/src/main/java/Backend/socket/api/chat/service/dto/response/ChatMessageElementResponseDto.java similarity index 83% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageElementResponseDto.java rename to socket/src/main/java/Backend/socket/api/chat/service/dto/response/ChatMessageElementResponseDto.java index 18c33366..8d16e5b2 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageElementResponseDto.java +++ b/socket/src/main/java/Backend/socket/api/chat/service/dto/response/ChatMessageElementResponseDto.java @@ -1,14 +1,9 @@ -package Backend.socket.domain.chat.application.controller.dto.response; +package Backend.socket.api.chat.service.dto.response; -import Backend.socket.domain.chat.application.service.TriFunction; import Backend.socket.domain.chat.domain.ChatContent; -import Backend.socket.domain.chat.domain.ChatUser; import lombok.Builder; import lombok.Getter; -import java.util.List; -import java.util.stream.Collectors; - @Builder @Getter public class ChatMessageElementResponseDto { diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageRoomResponseDto.java b/socket/src/main/java/Backend/socket/api/chat/service/dto/response/ChatMessageRoomResponseDto.java similarity index 88% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageRoomResponseDto.java rename to socket/src/main/java/Backend/socket/api/chat/service/dto/response/ChatMessageRoomResponseDto.java index 26ead2d4..9caeaa19 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageRoomResponseDto.java +++ b/socket/src/main/java/Backend/socket/api/chat/service/dto/response/ChatMessageRoomResponseDto.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.controller.dto.response; +package Backend.socket.api.chat.service.dto.response; import lombok.Builder; import lombok.Getter; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/RoomController.java b/socket/src/main/java/Backend/socket/api/room/controller/RoomController.java similarity index 76% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/RoomController.java rename to socket/src/main/java/Backend/socket/api/room/controller/RoomController.java index a27d3a5f..a7db8d13 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/RoomController.java +++ b/socket/src/main/java/Backend/socket/api/room/controller/RoomController.java @@ -1,20 +1,15 @@ -package Backend.socket.domain.chat.application.controller; +package Backend.socket.api.room.controller; -import Backend.socket.domain.chat.application.controller.dto.request.ChatListRequestDto; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageListRequestDto; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageRoomRequestDto; -import Backend.socket.domain.chat.application.controller.dto.response.*; -import Backend.socket.domain.chat.application.service.ChatService; -import Backend.socket.domain.chat.application.service.RoomService; -import Backend.socket.domain.chat.domain.Chat; +import Backend.socket.api.chat.service.dto.request.ChatMessageRoomRequestDto; +import Backend.socket.api.room.service.response.RoomListResponseDto; +import Backend.socket.api.room.service.response.RoomMessageListResponseDto; +import Backend.socket.api.chat.service.ChatService; +import Backend.socket.api.room.service.RoomService; import Backend.socket.global.common.MessageSuccessCode; import Backend.socket.global.common.MessageSuccessResponse; -import Backend.socket.infra.config.auth.UserId; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.http.ResponseEntity; import org.springframework.messaging.handler.annotation.DestinationVariable; -import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.web.bind.annotation.PathVariable; @@ -42,7 +37,6 @@ public void sendChatDetailMessage(@DestinationVariable("roomName") final String final RoomMessageListResponseDto responseDto = roomService.sendRoomDetailMessage(roomName); template.convertAndSend("/sub/room/" + roomName, MessageSuccessResponse.of(MessageSuccessCode.MESSAGE, responseDto)); } - // @MessageMapping("/room/all/{sessionId}") public void sendUserChatListMessage(@DestinationVariable("sessionId") final String sessionId) { final RoomListResponseDto responseDto = roomService.sendUserChatListMessage(sessionId); diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/service/RoomService.java b/socket/src/main/java/Backend/socket/api/room/service/RoomService.java similarity index 90% rename from socket/src/main/java/Backend/socket/domain/chat/application/service/RoomService.java rename to socket/src/main/java/Backend/socket/api/room/service/RoomService.java index 02f0ce42..c322637b 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/service/RoomService.java +++ b/socket/src/main/java/Backend/socket/api/room/service/RoomService.java @@ -1,14 +1,11 @@ -package Backend.socket.domain.chat.application.service; +package Backend.socket.api.room.service; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageListRequestDto; -import Backend.socket.domain.chat.application.controller.dto.response.*; -import Backend.socket.domain.chat.domain.Chat; +import Backend.socket.api.room.service.response.*; +import Backend.socket.domain.chat.service.Formatter; import Backend.socket.domain.chat.domain.ChatContent; import Backend.socket.domain.chat.domain.ChatUser; import Backend.socket.domain.chat.domain.Room; -import Backend.socket.domain.chat.repository.ChatRepository; import Backend.socket.domain.chat.repository.RoomRepository; -import Backend.socket.domain.chat.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.data.mongodb.core.MongoTemplate; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatUserResponseDto.java b/socket/src/main/java/Backend/socket/api/room/service/response/ChatUserResponseDto.java similarity index 91% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatUserResponseDto.java rename to socket/src/main/java/Backend/socket/api/room/service/response/ChatUserResponseDto.java index ce4a44e6..8dfb120b 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatUserResponseDto.java +++ b/socket/src/main/java/Backend/socket/api/room/service/response/ChatUserResponseDto.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.controller.dto.response; +package Backend.socket.api.room.service.response; import Backend.socket.domain.chat.domain.ChatUser; import lombok.Builder; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomChatResponseDto.java b/socket/src/main/java/Backend/socket/api/room/service/response/RoomChatResponseDto.java similarity index 93% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomChatResponseDto.java rename to socket/src/main/java/Backend/socket/api/room/service/response/RoomChatResponseDto.java index 13d806a7..7b09b5c0 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomChatResponseDto.java +++ b/socket/src/main/java/Backend/socket/api/room/service/response/RoomChatResponseDto.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.controller.dto.response; +package Backend.socket.api.room.service.response; import Backend.socket.domain.chat.domain.Room; import lombok.Builder; @@ -6,7 +6,6 @@ import java.time.Duration; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; @Builder @Getter diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomListResponseDto.java b/socket/src/main/java/Backend/socket/api/room/service/response/RoomListResponseDto.java similarity index 86% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomListResponseDto.java rename to socket/src/main/java/Backend/socket/api/room/service/response/RoomListResponseDto.java index 7fefb62f..a99aafec 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomListResponseDto.java +++ b/socket/src/main/java/Backend/socket/api/room/service/response/RoomListResponseDto.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.controller.dto.response; +package Backend.socket.api.room.service.response; import lombok.Builder; import lombok.Getter; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomMessageElementResponseDto.java b/socket/src/main/java/Backend/socket/api/room/service/response/RoomMessageElementResponseDto.java similarity index 93% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomMessageElementResponseDto.java rename to socket/src/main/java/Backend/socket/api/room/service/response/RoomMessageElementResponseDto.java index 3c290275..00cd3e81 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomMessageElementResponseDto.java +++ b/socket/src/main/java/Backend/socket/api/room/service/response/RoomMessageElementResponseDto.java @@ -1,6 +1,6 @@ -package Backend.socket.domain.chat.application.controller.dto.response; +package Backend.socket.api.room.service.response; -import Backend.socket.domain.chat.application.service.TriFunction; +import Backend.socket.global.common.TriFunction; import Backend.socket.domain.chat.domain.ChatContent; import Backend.socket.domain.chat.domain.ChatUser; import lombok.Builder; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomMessageListResponseDto.java b/socket/src/main/java/Backend/socket/api/room/service/response/RoomMessageListResponseDto.java similarity index 88% rename from socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomMessageListResponseDto.java rename to socket/src/main/java/Backend/socket/api/room/service/response/RoomMessageListResponseDto.java index e3c2615a..ee540bfd 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/RoomMessageListResponseDto.java +++ b/socket/src/main/java/Backend/socket/api/room/service/response/RoomMessageListResponseDto.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.controller.dto.response; +package Backend.socket.api.room.service.response; import lombok.Builder; import lombok.Getter; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/ChatController.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/ChatController.java deleted file mode 100644 index b19c710e..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/ChatController.java +++ /dev/null @@ -1,87 +0,0 @@ -package Backend.socket.domain.chat.application.controller; - - -import Backend.socket.domain.chat.application.controller.dto.request.ChatListRequestDto; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageListRequestDto; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageRequestDto; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageRoomRequestDto; -import Backend.socket.domain.chat.application.controller.dto.response.ChatListResponseDto; -import Backend.socket.domain.chat.application.controller.dto.response.ChatMessageListResponseDto; -import Backend.socket.domain.chat.application.controller.dto.response.ChatMessageResponseDto; -import Backend.socket.domain.chat.application.controller.dto.response.ChatMessageRoomResponseDto; -import Backend.socket.domain.chat.application.service.ChatService; -import Backend.socket.global.common.MessageSuccessCode; -import Backend.socket.global.common.MessageSuccessResponse; -import Backend.socket.global.common.image; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.messaging.handler.annotation.DestinationVariable; -import org.springframework.messaging.handler.annotation.Header; -import org.springframework.messaging.handler.annotation.MessageMapping; -import org.springframework.messaging.handler.annotation.SendTo; -import org.springframework.messaging.simp.SimpMessagingTemplate; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -import java.io.IOException; - -@RestController -public class ChatController { - private final ChatService chatService; - private final SimpMessagingTemplate template; - private final RedisTemplate redisTemplate; - public ChatController(ChatService chatService, SimpMessagingTemplate template, - @Qualifier("redisTemplate") RedisTemplate redisTemplate) { - this.chatService = chatService; - this.template = template; - this.redisTemplate = redisTemplate; - } -// @MessageMapping("/chat/{sessionId}") -// public void sendChatMessage(@DestinationVariable("sessionId") final String sessionId, -// @RequestBody final ChatMessageRequestDto chatMessageRequestDto) { -// final ChatMessageResponseDto responseDto = chatService.createSendMessageContent(sessionId, chatMessageRequestDto); -// redisTemplate.convertAndSend("meetingRoom", responseDto); -// } - @MessageMapping("/room/{roomName}") - @SendTo("/sub/room/{roomName}") - public MessageSuccessResponse sendChatMessageInRoom(@DestinationVariable("roomName") final String roomName, - @RequestBody final ChatMessageRoomRequestDto chatMessageRoomRequestDto) throws IOException { - return MessageSuccessResponse.of(MessageSuccessCode.RECEIVED, chatService.createSendMessageContentInRoom(roomName, chatMessageRoomRequestDto).getMessage()); - } -// @MessageMapping("/room/image/{roomName}") -// @SendTo("/sub/room/{roomName}") -// public MessageSuccessResponse sendImageMessageInRoom(@DestinationVariable("roomName") final String roomName, -// @RequestBody final ChatMessageRoomRequestDto chatMessageRoomRequestDto) throws IOException { -// return MessageSuccessResponse.of(MessageSuccessCode.RECEIVED, chatService.createSendMessageContentInRoom(roomName, chatMessageRoomRequestDto).getMessage()); -// } -// @MessageMapping("/room/image/{roomName}") -// @SendTo("/sub/room/{roomName}") -// public MessageSuccessResponse sendImageMessageInRoom(@DestinationVariable("roomName") final String roomName, -// @RequestBody final image image) throws IOException { -// return MessageSuccessResponse.of(MessageSuccessCode.RECEIVED, chatService.createSendImageContentInRoom(roomName, image).getMessage()); -// } -// @MessageMapping("/room/{roomName}") -// public void sendChatMessageInRoom(@DestinationVariable("roomName") final String roomName, -// @RequestBody final ChatMessageRoomRequestDto chatMessageRoomRequestDto) { -// final ChatMessageRoomResponseDto responseDto = chatService.createSendMessageContentInRoom(roomName, chatMessageRoomRequestDto); -// redisTemplate.convertAndSend("meetingRoom", responseDto); -// } - -// @MessageMapping("/chat/detail/{sessionId}") -// public void sendChatDetailMessage(@DestinationVariable("sessionId") final String sessionId, -// @RequestBody final ChatMessageListRequestDto chatMessageListRequestDto) { -// final ChatMessageListResponseDto responseDto = chatService.sendChatDetailMessage(sessionId, chatMessageListRequestDto); -// template.convertAndSend("/sub/chat/" + sessionId, MessageSuccessResponse.of(MessageSuccessCode.MESSAGE, responseDto)); -// } -// -// @MessageMapping("/chat/all") -// public void sendUserChatListMessage(@Header("sessionId") final String sessionId, -// @RequestBody final ChatListRequestDto chatListRequestDto) { -// final ChatListResponseDto responseDto = chatService.sendUserChatListMessage(sessionId, chatListRequestDto); -// template.convertAndSend("/sub/chat/" + sessionId, MessageSuccessResponse.of(MessageSuccessCode.CHATLIST, responseDto)); -// } - -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatListRequestDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatListRequestDto.java deleted file mode 100644 index 239df31f..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatListRequestDto.java +++ /dev/null @@ -1,11 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.request; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class ChatListRequestDto { - private String userName; -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageListRequestDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageListRequestDto.java deleted file mode 100644 index d555d5cc..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageListRequestDto.java +++ /dev/null @@ -1,13 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.request; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class ChatMessageListRequestDto { - private String chatSession; - private String fromUserName; - private String toUserName; -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageRequestDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageRequestDto.java deleted file mode 100644 index 98a782e2..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/ChatMessageRequestDto.java +++ /dev/null @@ -1,14 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.request; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class ChatMessageRequestDto { - private String chatSession; - private String fromUserName; - private String toUserName; - private String content; -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/RoomChatMessageListReq.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/RoomChatMessageListReq.java deleted file mode 100644 index d1d5b21b..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/RoomChatMessageListReq.java +++ /dev/null @@ -1,14 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.request; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class RoomChatMessageListReq { - private String roomSession; - private String fromUserName; - private String toRoomName; - -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/RoomMessageListRequestDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/RoomMessageListRequestDto.java deleted file mode 100644 index f88b51b1..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/request/RoomMessageListRequestDto.java +++ /dev/null @@ -1,13 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.request; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class RoomMessageListRequestDto { - private String chatSession; - private String fromUserName; - private String toRoomName; -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatListResponseDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatListResponseDto.java deleted file mode 100644 index 4f690fad..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatListResponseDto.java +++ /dev/null @@ -1,18 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.response; - -import lombok.Builder; -import lombok.Getter; - -import java.util.List; - -@Builder -@Getter -public class ChatListResponseDto { - private List chatList; - - public static ChatListResponseDto of(List chatList) { - return ChatListResponseDto.builder() - .chatList(chatList) - .build(); - } -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageListResponseDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageListResponseDto.java deleted file mode 100644 index 8b5939b2..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageListResponseDto.java +++ /dev/null @@ -1,20 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.response; - -import lombok.Builder; -import lombok.Getter; - -import java.util.List; - -@Builder -@Getter -public class ChatMessageListResponseDto { - private ChatUserResponseDto user; - private List chatMessageList; - - public static ChatMessageListResponseDto of(ChatUserResponseDto chatUserResponseDto, List chatMessageList) { - return ChatMessageListResponseDto.builder() - .user(chatUserResponseDto) - .chatMessageList(chatMessageList) - .build(); - } -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageResponseDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageResponseDto.java deleted file mode 100644 index f3036ea6..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/ChatMessageResponseDto.java +++ /dev/null @@ -1,22 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.response; - -import lombok.Builder; -import lombok.Getter; - -import java.util.List; - -@Builder -@Getter -public class ChatMessageResponseDto { - private String receivedUser; - private List sessionList; - private ChatMessageElementResponseDto message; - - public static ChatMessageResponseDto of(String receivedUser, List sessionList, ChatMessageElementResponseDto message) { - return ChatMessageResponseDto.builder() - .receivedUser(receivedUser) - .sessionList(sessionList) - .message(message) - .build(); - } -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/SendMessageResponseDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/SendMessageResponseDto.java deleted file mode 100644 index cc9c6672..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/SendMessageResponseDto.java +++ /dev/null @@ -1,18 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.response; - -import lombok.Builder; -import lombok.Getter; - -@Builder -@Getter -public class SendMessageResponseDto { - private String receivedUser; - private ChatMessageElementResponseDto message; - - public static SendMessageResponseDto of(String receivedUser, ChatMessageElementResponseDto message) { - return SendMessageResponseDto.builder() - .receivedUser(receivedUser) - .message(message) - .build(); - } -} \ No newline at end of file diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/UserChatResponseDto.java b/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/UserChatResponseDto.java deleted file mode 100644 index ef2dfaa1..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/controller/dto/response/UserChatResponseDto.java +++ /dev/null @@ -1,27 +0,0 @@ -package Backend.socket.domain.chat.application.controller.dto.response; - -import Backend.socket.domain.chat.domain.ChatUser; -import lombok.Builder; -import lombok.Getter; - -import java.time.LocalDateTime; - -@Builder -@Getter -public class UserChatResponseDto { - private String sessionId; - private String profile; - private String userName; - private String content; - private String time; - - public static UserChatResponseDto of(ChatUser user, String content, LocalDateTime time) { - return UserChatResponseDto.builder() - .sessionId(user.getSessionId()) - .profile(user.getProfile()) - .userName(user.getName()) - .content(content) - .time(time.toString()) - .build(); - } -} \ No newline at end of file diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/service/ChatService.java b/socket/src/main/java/Backend/socket/domain/chat/application/service/ChatService.java deleted file mode 100644 index 7f16eb18..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/application/service/ChatService.java +++ /dev/null @@ -1,246 +0,0 @@ -package Backend.socket.domain.chat.application.service; - -import Backend.socket.domain.chat.application.controller.dto.request.ChatListRequestDto; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageListRequestDto; -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageRequestDto; - -import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageRoomRequestDto; -import Backend.socket.domain.chat.application.controller.dto.response.*; -import Backend.socket.domain.chat.domain.*; -import Backend.socket.domain.chat.repository.ChatRepository; -import Backend.socket.domain.chat.repository.RoomRepository; -import Backend.socket.domain.chat.repository.UserRepository; -import Backend.socket.global.common.image; -import Backend.socket.global.error.socketException.EntityNotFoundException; -import Backend.socket.infra.external.AwsService; -//import Backend.socket.infra.external.fcm.service.PushNotificationService; -import jakarta.transaction.Transactional; -import lombok.RequiredArgsConstructor; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.stereotype.Service; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -import static Backend.socket.domain.chat.domain.ChatContent.createChatContent; -import static Backend.socket.global.error.ErrorCode.USER_NOT_FOUND; - - -@RequiredArgsConstructor -@Transactional -@Service -public class ChatService { - private final MongoTemplate mongoTemplate; - private final ChatRepository chatRepository; - private final UserRepository userRepository; - private final RoomRepository roomRepository; - private final AwsService awsService; -// private final PushNotificationService pushNotificationService; - -// public ChatMessageResponseDto createSendMessageContent(String sessionId, ChatMessageRequestDto chatMessageRequestDto) { -// Chat chat = getChatBySessions(sessionId, chatMessageRequestDto.getChatSession()); -// User user = userRepository.findBySessionId(chatMessageRequestDto.getChatSession()).orElseThrow(); -// ChatContent chatContent = createChatContent(chatMessageRequestDto.getFromUserName(), chatMessageRequestDto.getContent(), chat); -// ChatMessageElementResponseDto chatMessage = ChatMessageElementResponseDto.of(chatContent, chatMessageRequestDto.getChatSession(), user.getProfile()); -// List sessionIdList = getSessionIdList(sessionId, chatMessageRequestDto.getChatSession()); -// saveChat(chat); -// return ChatMessageResponseDto.of(chatMessageRequestDto.getToUserName(), sessionIdList, chatMessage); -// } - public ChatMessageRoomResponseDto createSendMessageContentInRoom(String roomName, ChatMessageRoomRequestDto chatMessageRoomRequestDto) throws IOException { -// StringBuilder imageBuilder = new StringBuilder(); -// for (String imagePart : chatMessageRoomRequestDto.getImage()) { -// imageBuilder.append(imagePart); -// } -// String image = imageBuilder.toString(); -// String modifiedImageString = image.replaceAll("[\\[\\]]", "").replaceAll(",", " "); -// System.out.println("Modified byte array: " + modifiedImageString); - Room room = getChatBySessionsInRoom(roomName, chatMessageRoomRequestDto.getChatSession()); - User user = userRepository.findBySessionId(chatMessageRoomRequestDto.getChatSession()).orElseThrow(); -// String images = awsService.uploadImageToS3(modifiedImageString); - ChatContent chatContent = createChatContent(chatMessageRoomRequestDto.getFromUserName(), chatMessageRoomRequestDto.getContent(), room, chatMessageRoomRequestDto.getImage()); - - ChatMessageElementResponseDto chatMessage = ChatMessageElementResponseDto.of(chatContent, chatMessageRoomRequestDto.getChatSession(), user.getProfile(), chatMessageRoomRequestDto.getImage(), validate(room.getOwnerSession(),chatMessageRoomRequestDto.getChatSession())); - List sessionIdList = getSessionIdListInRoom(roomName, chatMessageRoomRequestDto.getChatSession()); - saveChatRoom(room); -// pushNotificationService.sendChatMessageNotification(room, chatMessage, sessionIdList); //채팅 알림 - return ChatMessageRoomResponseDto.of(chatMessageRoomRequestDto.getToRoomName(), sessionIdList, chatMessage); - } - private Boolean validate(String ownerSession, String sessionId){ - if(ownerSession.equals(sessionId)) - return true; - return false; - } -// public ChatMessageRoomResponseDto createSendImageContentInRoom(String roomName, image chatMessageRoomRequestDto) throws IOException { -// // 대괄호 제거 및 공백으로 구분 -// String modifiedImageString = chatMessageRoomRequestDto.getImage().replaceAll("[\\[\\]]", "").replaceAll(",", " "); -// System.out.println("Modified byte array: " + modifiedImageString); -// -// String imageUrl = awsService.uploadImageToS3(modifiedImageString); -// String images = awsService.uploadImageToS3(imageUrl); -// Room room = getChatBySessionsInRoom(roomName, "113828093759900814627_ef4a27"); -// User user = userRepository.findBySessionId("113828093759900814627_ef4a27").orElseThrow(); -// ChatContent chatContent = createChatContent("양규리", images, room); -// ChatMessageElementResponseDto chatMessage = ChatMessageElementResponseDto.of(chatContent, "113828093759900814627_ef4a27", user.getProfile(), images); -// List sessionIdList = getSessionIdListInRoom(roomName, "113828093759900814627_ef4a27"); -// saveChatRoom(room); -// return ChatMessageRoomResponseDto.of("eksxhr", sessionIdList, chatMessage); -// } - -// public ChatMessageListResponseDto sendChatDetailMessage(String sessionId, ChatMessageListRequestDto chatMessageListRequestDto) { -// Chat chat = getChatBySessions(sessionId, chatMessageListRequestDto.getChatSession()); -// ChatUserResponseDto chatUserResponseDto = getChatUserResponseDto(chat, chatMessageListRequestDto.getFromUserName()); -// List chatMessageList = ChatMessageElementResponseDto.listOf(chat.getChatContentList(), chatMessageListRequestDto.getChatSession(), null); -// saveChat(chat); -// return ChatMessageListResponseDto.of(chatUserResponseDto, chatMessageList); -// } -// -// public ChatListResponseDto sendUserChatListMessage(String sessionId, ChatListRequestDto chatListRequestDto) { -// List chatList = findChatListBySession(sessionId); -// List userChatResponseDtoList = createUserChatResponseDto(chatList, chatListRequestDto.getUserName()); -// userChatResponseDtoList.sort(Comparator.comparing(UserChatResponseDto::getTime).reversed()); -// return ChatListResponseDto.of(userChatResponseDtoList); -// } - - private List getSessionIdList(String firstSessionId, String secondSessionId) { - List sessionList = new ArrayList<>(); - sessionList.add(firstSessionId); - sessionList.add(secondSessionId); - return sessionList; - } - private List getSessionIdListInRoom(String roomName, String sessionId) { - List sessionList = new ArrayList<>(); - - // roomId를 기반으로 Room 문서 찾기 - Room room = findRoomChatByRoomName(roomName); - - if (room != null) { - // Room에 속한 모든 ChatUser의 sessionId를 리스트에 추가 - for (ChatUser chatUser : room.getChatUserList()) { - sessionList.add(chatUser.getSessionId()); - } - } - - return sessionList; - } - -// private ChatUserResponseDto getChatUserResponseDto(Chat chat, String name) { -// ChatUser chatUser = getChatUserReceivedUser(chat, name); -// return ChatUserResponseDto.of(chatUser); -// } - - private List createUserChatResponseDto(List chatList, String userName) { - List filterChat = getChatEmptyContentFilter(chatList); - return filterChat.stream() - .map(chat -> - UserChatResponseDto.of( - getChatUserReceivedUser(chat, userName), - getLastChatContent(chat.getChatContentList()).getContent(), - getLastChatContent(chat.getChatContentList()).getTime())) - .collect(Collectors.toList()); - } - - private List getChatEmptyContentFilter(List chatList) { - return chatList.stream() - .filter(chat -> (chat.getChatContentList().size() != 0)) - .collect(Collectors.toList()); - } - - private ChatUser getChatUserReceivedUser(Chat chat, String name) { - if (!Objects.equals(chat.getChatUserList().get(0).getName(), name)) - return chat.getChatUserList().get(0); - else - return chat.getChatUserList().get(1); - } - - - private ChatContent getLastChatContent(List chatContentList) { - return chatContentList.get(chatContentList.size() - 1); - } - - private Chat getChatBySessions(String firstSessionId, String secondSessionId) { - Chat chat = findFirstChatBySessions(firstSessionId, secondSessionId); - if (Objects.isNull(chat)) { - ChatUser firstChatUser = createChatUser(firstSessionId); - ChatUser secondChatUser = createChatUser(secondSessionId); - return Chat.creatChat(firstChatUser, secondChatUser); - } else - return chat; - } - private Room getChatBySessionsInRoom(String roomName, String sessionId) { - Room room = findRoomChatByRoomName(roomName); - if (Objects.isNull(room)) { - // 채팅방이 없는 경우 새로운 채팅방 생성 - room = createNewRoom(roomName); - } - - // 채팅방에 sessionId를 가진 유저가 있는지 확인 - if (!isUserExistsInRoom(room, sessionId)) { - // 유저가 없다면 새로운 유저 생성하여 채팅방에 추가 - ChatUser chatUser = createChatUser(sessionId); - room.addChatRoom(chatUser); - } - - return room; - } - - private Room createNewRoom(String roomName) { - Room room = Room.builder() - .roomName(roomName) - .build(); - return room; - } - private boolean isUserExistsInRoom(Room room, String sessionId) { - for (ChatUser chatUser : room.getChatUserList()) { - if (chatUser.getSessionId().equals(sessionId)) { - return true; - } - } - return false; - } - private ChatUser createChatUser(String sessionId) { - User user = getUserFromSessionId(sessionId); - return ChatUser.createChatUser(user); - } - - private String getReceivedUserName(Chat chat, String user) { - if (!Objects.equals(chat.getChatUserList().get(0).getName(), user)) - return chat.getChatUserList().get(0).getName(); - else - return chat.getChatUserList().get(1).getName(); - } - - private Chat findFirstChatBySessions(String firstSessionId, String secondSessionId) { - Query query = new Query(); - query.addCriteria(Criteria.where("chatUserList.sessionId").all(firstSessionId, secondSessionId)); - return mongoTemplate.findOne(query, Chat.class); - } - private Room findRoomChatByRoomName(String roomName) { - Query query = new Query(); - query.addCriteria(Criteria.where("roomName").is(roomName)); - return mongoTemplate.findOne(query, Room.class); - } - - private List findChatListBySession(String sessionId) { - Query query = new Query(); - query.addCriteria(Criteria.where("chatUserList.sessionId").all(sessionId)); - return mongoTemplate.find(query, Chat.class); - } - - private User getUserFromSessionId(String sessionId) { - return userRepository.findBySessionId(sessionId) - .orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND)); - } - - public void saveChat(Chat chat) { - chatRepository.save(chat); - } - public void saveChatRoom(Room room) { - roomRepository.save(room); - } -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/Chat.java b/socket/src/main/java/Backend/socket/domain/chat/domain/Chat.java deleted file mode 100644 index e8e3b994..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/Chat.java +++ /dev/null @@ -1,37 +0,0 @@ -package Backend.socket.domain.chat.domain; - -import lombok.Builder; -import lombok.Getter; -import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.mapping.Document; - -import java.util.ArrayList; -import java.util.List; - -@Getter -@Builder -@Document(collection = "chat") -public class Chat { - @Id - private String chatId; - @Builder.Default - private List chatUserList = new ArrayList<>(); - @Builder.Default - private List chatContentList = new ArrayList<>(); - - public static Chat creatChat(ChatUser firstUser, ChatUser secondUser) { - Chat chat = Chat.builder().build(); - chat.addChatUser(firstUser); - chat.addChatUser(secondUser); - return chat; - } - - public void addChatContent(ChatContent content) { - this.chatContentList.add(content); - } - - public void addChatUser(ChatUser chatUser) { - this.chatUserList.add(chatUser); - } - -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/ChatContent.java b/socket/src/main/java/Backend/socket/domain/chat/domain/ChatContent.java index bb3e3fb3..74fdf99c 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/ChatContent.java +++ b/socket/src/main/java/Backend/socket/domain/chat/domain/ChatContent.java @@ -13,15 +13,15 @@ public class ChatContent { private String image; private LocalDateTime time; - public static ChatContent createChatContent(String userName, String content,Chat chat) { - ChatContent chatContent = ChatContent.builder() - .userName(userName) - .content(content) - .time(LocalDateTime.now()) - .build(); - chat.addChatContent(chatContent); - return chatContent; - } +// public static ChatContent createChatContent(String userName, String content,Chat chat) { +// ChatContent chatContent = ChatContent.builder() +// .userName(userName) +// .content(content) +// .time(LocalDateTime.now()) +// .build(); +// chat.addChatContent(chatContent); +// return chatContent; +// } public static ChatContent createChatContent(String userName, String content, Room room,String image) { ChatContent chatContent = ChatContent.builder() .userName(userName) diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/ChatUser.java b/socket/src/main/java/Backend/socket/domain/chat/domain/ChatUser.java index 280b1ec9..dba5d3d3 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/ChatUser.java +++ b/socket/src/main/java/Backend/socket/domain/chat/domain/ChatUser.java @@ -1,5 +1,6 @@ package Backend.socket.domain.chat.domain; +import Backend.socket.domain.user.entity.User; import lombok.Builder; import lombok.Getter; diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/repository/NotificationHistoryRepository.java b/socket/src/main/java/Backend/socket/domain/chat/domain/notification/repository/NotificationHistoryRepository.java deleted file mode 100644 index dcae7bb5..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/repository/NotificationHistoryRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package Backend.socket.domain.chat.domain.notification.repository; - -import Backend.socket.domain.chat.domain.notification.entity.NotificationHistory; -import Backend.socket.domain.chat.domain.notification.entity.TopCategory; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface NotificationHistoryRepository extends JpaRepository { -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/repository/ChatRepository.java b/socket/src/main/java/Backend/socket/domain/chat/repository/ChatRepository.java deleted file mode 100644 index 45c4c415..00000000 --- a/socket/src/main/java/Backend/socket/domain/chat/repository/ChatRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package Backend.socket.domain.chat.repository; - - -import Backend.socket.domain.chat.domain.Chat; -import org.springframework.data.mongodb.repository.MongoRepository; - -public interface ChatRepository extends MongoRepository { -} diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/service/Formatter.java b/socket/src/main/java/Backend/socket/domain/chat/service/Formatter.java similarity index 95% rename from socket/src/main/java/Backend/socket/domain/chat/application/service/Formatter.java rename to socket/src/main/java/Backend/socket/domain/chat/service/Formatter.java index 5e60dfbf..002eec9d 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/service/Formatter.java +++ b/socket/src/main/java/Backend/socket/domain/chat/service/Formatter.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.service; +package Backend.socket.domain.chat.service; import Backend.socket.domain.chat.domain.ChatUser; import Backend.socket.domain.chat.domain.Room; diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/service/RedisSubscriber.java b/socket/src/main/java/Backend/socket/domain/chat/service/RedisSubscriber.java similarity index 55% rename from socket/src/main/java/Backend/socket/domain/chat/application/service/RedisSubscriber.java rename to socket/src/main/java/Backend/socket/domain/chat/service/RedisSubscriber.java index 61ef887b..50c684ab 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/service/RedisSubscriber.java +++ b/socket/src/main/java/Backend/socket/domain/chat/service/RedisSubscriber.java @@ -1,13 +1,10 @@ -package Backend.socket.domain.chat.application.service; +package Backend.socket.domain.chat.service; -import Backend.socket.domain.chat.application.controller.dto.response.ChatMessageElementResponseDto; -import Backend.socket.domain.chat.application.controller.dto.response.ChatMessageResponseDto; -import Backend.socket.domain.chat.application.controller.dto.response.ChatMessageRoomResponseDto; -import Backend.socket.domain.chat.application.controller.dto.response.SendMessageResponseDto; +import Backend.socket.api.chat.service.dto.response.ChatMessageElementResponseDto; +import Backend.socket.api.chat.service.dto.response.ChatMessageRoomResponseDto; import Backend.socket.global.common.MessageSuccessCode; import Backend.socket.global.common.MessageSuccessResponse; import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.connection.Message; @@ -29,32 +26,15 @@ public RedisSubscriber(ObjectMapper objectMapper, @Qualifier("redisTemplate") Re this.redisTemplate = redisTemplate; this.messagingTemplate = messagingTemplate; } -// @Override -// public void onMessage(Message message, byte[] pattern) { -// String publishMessage = getPublishMessage(message); -// ChatMessageResponseDto messageResponseDto = getChatMessageFromObjectMapper(publishMessage); -// SendMessageResponseDto sendMessageResponseDto -// = SendMessageResponseDto.of(messageResponseDto.getReceivedUser(), messageResponseDto.getMessage()); -// messageResponseDto.getSessionList().forEach(sessionId -> sendChatMessage(sessionId, sendMessageResponseDto)); -// } + @Override public void onMessage(Message message, byte[] pattern) { String publishMessage = getPublishMessage(message); ChatMessageRoomResponseDto messageResponseDto = getChatMessageFromObjectMapper(publishMessage); -// SendMessageResponseDto sendMessageResponseDto -// = SendMessageResponseDto.of(messageResponseDto.getReceivedUser(), messageResponseDto.getMessage()); messageResponseDto.getSessionList().forEach(sessionId -> sendChatMessage(messageResponseDto.getRoom(), messageResponseDto.getMessage())); } -// private ChatMessageResponseDto getChatMessageFromObjectMapper(String publishMessage) { -// ChatMessageResponseDto messageResponseDto; -// try { -// messageResponseDto = objectMapper.readValue(publishMessage, ChatMessageResponseDto.class); -// } catch (Exception e) { -// throw new MessageDeliveryException("Error"); -// } -// return messageResponseDto; -// } + private ChatMessageRoomResponseDto getChatMessageFromObjectMapper(String publishMessage) { ChatMessageRoomResponseDto messageResponseDto; try { @@ -71,8 +51,5 @@ private void sendChatMessage(String roomName, ChatMessageElementResponseDto mess messagingTemplate.convertAndSend("/sub/room/" + roomName, MessageSuccessResponse.of(MessageSuccessCode.RECEIVED, message)); } -// private void sendChatMessage(String sessionId, SendMessageResponseDto publishMessage) { -// messagingTemplate.convertAndSend("/sub/chat/" + sessionId, -// MessageSuccessResponse.of(MessageSuccessCode.RECEIVED, publishMessage)); -// } + } diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/NotificationHistory.java b/socket/src/main/java/Backend/socket/domain/notification/entity/NotificationHistory.java similarity index 93% rename from socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/NotificationHistory.java rename to socket/src/main/java/Backend/socket/domain/notification/entity/NotificationHistory.java index 29f35265..b226c50e 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/NotificationHistory.java +++ b/socket/src/main/java/Backend/socket/domain/notification/entity/NotificationHistory.java @@ -1,6 +1,6 @@ -package Backend.socket.domain.chat.domain.notification.entity; +package Backend.socket.domain.notification.entity; -import Backend.socket.domain.chat.domain.User; +import Backend.socket.domain.user.entity.User; import jakarta.persistence.*; import lombok.*; diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/NotificationType.java b/socket/src/main/java/Backend/socket/domain/notification/entity/NotificationType.java similarity index 63% rename from socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/NotificationType.java rename to socket/src/main/java/Backend/socket/domain/notification/entity/NotificationType.java index a0951c31..9c8dd19b 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/NotificationType.java +++ b/socket/src/main/java/Backend/socket/domain/notification/entity/NotificationType.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.domain.notification.entity; +package Backend.socket.domain.notification.entity; public enum NotificationType { CHAT, diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/TopCategory.java b/socket/src/main/java/Backend/socket/domain/notification/entity/TopCategory.java similarity index 92% rename from socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/TopCategory.java rename to socket/src/main/java/Backend/socket/domain/notification/entity/TopCategory.java index 30643ea8..fa97ffce 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/notification/entity/TopCategory.java +++ b/socket/src/main/java/Backend/socket/domain/notification/entity/TopCategory.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.domain.notification.entity; +package Backend.socket.domain.notification.entity; import Backend.socket.global.error.httpException.InvalidValueException; import lombok.Getter; diff --git a/socket/src/main/java/Backend/socket/domain/notification/repository/NotificationHistoryRepository.java b/socket/src/main/java/Backend/socket/domain/notification/repository/NotificationHistoryRepository.java new file mode 100644 index 00000000..bbbfc907 --- /dev/null +++ b/socket/src/main/java/Backend/socket/domain/notification/repository/NotificationHistoryRepository.java @@ -0,0 +1,9 @@ +package Backend.socket.domain.notification.repository; + +import Backend.socket.domain.notification.entity.NotificationHistory; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface NotificationHistoryRepository extends JpaRepository { +} diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/Gender.java b/socket/src/main/java/Backend/socket/domain/user/entity/Gender.java similarity index 83% rename from socket/src/main/java/Backend/socket/domain/chat/domain/Gender.java rename to socket/src/main/java/Backend/socket/domain/user/entity/Gender.java index d6717d84..46528d8f 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/Gender.java +++ b/socket/src/main/java/Backend/socket/domain/user/entity/Gender.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.domain; +package Backend.socket.domain.user.entity; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/Language.java b/socket/src/main/java/Backend/socket/domain/user/entity/Language.java similarity index 82% rename from socket/src/main/java/Backend/socket/domain/chat/domain/Language.java rename to socket/src/main/java/Backend/socket/domain/user/entity/Language.java index 715ea220..35ec0c7d 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/Language.java +++ b/socket/src/main/java/Backend/socket/domain/user/entity/Language.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.domain; +package Backend.socket.domain.user.entity; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/Platform.java b/socket/src/main/java/Backend/socket/domain/user/entity/Platform.java similarity index 93% rename from socket/src/main/java/Backend/socket/domain/chat/domain/Platform.java rename to socket/src/main/java/Backend/socket/domain/user/entity/Platform.java index b2a3826b..86dfa445 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/Platform.java +++ b/socket/src/main/java/Backend/socket/domain/user/entity/Platform.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.domain; +package Backend.socket.domain.user.entity; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/SyncType.java b/socket/src/main/java/Backend/socket/domain/user/entity/SyncType.java similarity index 93% rename from socket/src/main/java/Backend/socket/domain/chat/domain/SyncType.java rename to socket/src/main/java/Backend/socket/domain/user/entity/SyncType.java index dc005f22..ff62f15e 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/SyncType.java +++ b/socket/src/main/java/Backend/socket/domain/user/entity/SyncType.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.domain; +package Backend.socket.domain.user.entity; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/socket/src/main/java/Backend/socket/domain/chat/domain/User.java b/socket/src/main/java/Backend/socket/domain/user/entity/User.java similarity index 95% rename from socket/src/main/java/Backend/socket/domain/chat/domain/User.java rename to socket/src/main/java/Backend/socket/domain/user/entity/User.java index e9fab53b..c3d99040 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/domain/User.java +++ b/socket/src/main/java/Backend/socket/domain/user/entity/User.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.domain; +package Backend.socket.domain.user.entity; import Backend.socket.global.common.BaseEntity; diff --git a/socket/src/main/java/Backend/socket/domain/chat/repository/UserRepository.java b/socket/src/main/java/Backend/socket/domain/user/repository/UserRepository.java similarity index 69% rename from socket/src/main/java/Backend/socket/domain/chat/repository/UserRepository.java rename to socket/src/main/java/Backend/socket/domain/user/repository/UserRepository.java index 47ad8d41..54b81779 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/repository/UserRepository.java +++ b/socket/src/main/java/Backend/socket/domain/user/repository/UserRepository.java @@ -1,6 +1,6 @@ -package Backend.socket.domain.chat.repository; +package Backend.socket.domain.user.repository; -import Backend.socket.domain.chat.domain.User; +import Backend.socket.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; diff --git a/socket/src/main/java/Backend/socket/global/common/HealthCheck.java b/socket/src/main/java/Backend/socket/global/common/HealthCheck.java index 1dfe8596..e8d4cf67 100644 --- a/socket/src/main/java/Backend/socket/global/common/HealthCheck.java +++ b/socket/src/main/java/Backend/socket/global/common/HealthCheck.java @@ -1,100 +1,16 @@ package Backend.socket.global.common; -import Backend.socket.infra.external.AwsService; import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Base64; -import java.util.List; -import java.util.Base64; @RestController @RequiredArgsConstructor @RequestMapping public class HealthCheck { - private final AwsService awsService; + @GetMapping("/") public String MeetUpServer() { return "test"; } - @GetMapping("/image") - public String uploadImage(@RequestParam(name = "image") String image) throws IOException { - String[] strings = image.split(" "); // ","을 기준으로 바이트 코드를 나눠준다 - String base64Image = strings[1]; - String extension = ""; // if 문을 통해 확장자명을 정해줌 - if (strings[0].equals("data:image/jpeg;base64")) { - extension = "jpeg"; - } else if (strings[0].equals("data:image/png;base64")){ - extension = "png"; - } else { - extension = "jpg"; - } - - -// ... - - byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image); // 바이트 코드를 // 바이트 코드를 - - File tempFile = File.createTempFile("image", "." + extension); // createTempFile을 통해 임시 파일을 생성해준다. (임시파일은 지워줘야함) - try (OutputStream outputStream = new FileOutputStream(tempFile)) { - outputStream.write(imageBytes); // OutputStream outputStream = new FileOutputStream(tempFile)을 통해 생성한 outputStream 객체에 imageBytes를 작성해준다. - } - // 문자열을 공백을 기준으로 분리하여 문자열 배열로 변환 - String[] byteStrings = image.split(" "); - // byte 배열 생성 - byte[] imageData = new byte[byteStrings.length]; - - for (int i = 0; i < byteStrings.length; i++) { - if (byteStrings[i].matches("-?[0-9]+")) { - imageData[i] = Byte.parseByte(byteStrings[i]); - } else if (byteStrings[i].matches("-?0x[0-9a-fA-F]+")) { - imageData[i] = (byte) Integer.parseInt(byteStrings[i].substring(2), 16); - } else { - // 잘못된 형식의 문자열인 경우 처리할 작업 - imageData[i] = 0; - } - } - - // 변환된 byte 배열을 사용하여 이미지 업로드 -// String imageUrl = awsService.uploadImageToS3(imageBytes); - return null; - } - @PostMapping("/images") - public List uploadImages(@RequestBody List imageDataList) { - List imageUrls = awsService.uploadImages(imageDataList); - return imageUrls; - } - - @PostMapping("/test") - public String uploadImagea(@RequestBody String image) throws IOException { - int size = image.length(); - // 대괄호 제거 및 공백으로 구분 - String modifiedImageString = image.replaceAll("[\\[\\]]", "").replaceAll(",", " "); -// System.out.println("Modified byte array: " + modifiedImageString); - - String imageUrl = awsService.uploadImageToS3(modifiedImageString); - return imageUrl; - } - - @GetMapping("/image/byte") - public ResponseEntity uploadImage(@RequestBody byte[] image) throws IOException { - - // Base64 인코딩 - String base64EncodedString = Base64.getEncoder().encodeToString(image); - int encodedSize = base64EncodedString.length(); - System.out.println("Base64 Encoded String: " + base64EncodedString); - System.out.println("Encoded size: " + encodedSize + " bytes"); - - // AWS S3 업로드 - String imageUrl = awsService.uploadImageToS3(base64EncodedString); - - return ResponseEntity.ok(imageUrl); - } } diff --git a/socket/src/main/java/Backend/socket/domain/chat/application/service/TriFunction.java b/socket/src/main/java/Backend/socket/global/common/TriFunction.java similarity index 60% rename from socket/src/main/java/Backend/socket/domain/chat/application/service/TriFunction.java rename to socket/src/main/java/Backend/socket/global/common/TriFunction.java index db2964e0..7af2ad09 100644 --- a/socket/src/main/java/Backend/socket/domain/chat/application/service/TriFunction.java +++ b/socket/src/main/java/Backend/socket/global/common/TriFunction.java @@ -1,4 +1,4 @@ -package Backend.socket.domain.chat.application.service; +package Backend.socket.global.common; @FunctionalInterface public interface TriFunction { diff --git a/socket/src/main/java/Backend/socket/global/common/image.java b/socket/src/main/java/Backend/socket/global/common/image.java deleted file mode 100644 index e2398043..00000000 --- a/socket/src/main/java/Backend/socket/global/common/image.java +++ /dev/null @@ -1,11 +0,0 @@ -package Backend.socket.global.common; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class image { - private String image; -} diff --git a/socket/src/main/java/Backend/socket/global/common/imageList.java b/socket/src/main/java/Backend/socket/global/common/imageList.java deleted file mode 100644 index cbecacdb..00000000 --- a/socket/src/main/java/Backend/socket/global/common/imageList.java +++ /dev/null @@ -1,4 +0,0 @@ -package Backend.socket.global.common; - -public class imageList { -} diff --git a/socket/src/main/java/Backend/socket/infra/config/FCMConfig.java b/socket/src/main/java/Backend/socket/infra/config/FCMConfig.java deleted file mode 100644 index 2ae8db77..00000000 --- a/socket/src/main/java/Backend/socket/infra/config/FCMConfig.java +++ /dev/null @@ -1,38 +0,0 @@ -//package Backend.socket.infra.config; -// -//import com.google.auth.oauth2.GoogleCredentials; -//import com.google.firebase.FirebaseApp; -//import com.google.firebase.FirebaseOptions; -//import com.google.firebase.messaging.FirebaseMessaging; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -//import org.springframework.core.io.ClassPathResource; -// -//import java.io.IOException; -//import java.io.InputStream; -//import java.util.List; -// -//@Configuration -//public class FCMConfig { -// @Bean -// FirebaseMessaging firebaseMessaging() throws IOException { -// ClassPathResource resource = new ClassPathResource("firebase/AccountKey.json"); -// InputStream refreshToken = resource.getInputStream(); -// -// FirebaseApp firebaseApp = null; -// List firebaseAppList = FirebaseApp.getApps(); -// if(firebaseAppList != null && !firebaseAppList.isEmpty()) { -// for (FirebaseApp app : firebaseAppList){ -// if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { -// firebaseApp = app; -// } -// } -// } else { -// FirebaseOptions options = FirebaseOptions.builder() -// .setCredentials(GoogleCredentials.fromStream(refreshToken)) -// .build(); -// firebaseApp = FirebaseApp.initializeApp(options); -// } -// return FirebaseMessaging.getInstance(firebaseApp); -// } -//} diff --git a/socket/src/main/java/Backend/socket/infra/config/RedisConfig.java b/socket/src/main/java/Backend/socket/infra/config/RedisConfig.java index f62b2634..6a70eb09 100644 --- a/socket/src/main/java/Backend/socket/infra/config/RedisConfig.java +++ b/socket/src/main/java/Backend/socket/infra/config/RedisConfig.java @@ -1,5 +1,5 @@ package Backend.socket.infra.config; -import Backend.socket.domain.chat.application.service.RedisSubscriber; +import Backend.socket.domain.chat.service.RedisSubscriber; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -40,112 +40,4 @@ public RedisTemplate redisTemplate(RedisConnectionFactory connec public ChannelTopic channelTopic() { return new ChannelTopic("meetingRoom"); } -} -// -//import Backend.socket.domain.chat.application.service.RedisSubscriber; -//import org.springframework.beans.factory.annotation.Qualifier; -//import org.springframework.beans.factory.annotation.Value; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -//import org.springframework.data.redis.connection.RedisConnectionFactory; -//import org.springframework.data.redis.connection.RedisStandaloneConfiguration; -//import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; -//import org.springframework.data.redis.core.RedisTemplate; -//import org.springframework.data.redis.core.StringRedisTemplate; -//import org.springframework.data.redis.listener.ChannelTopic; -//import org.springframework.data.redis.listener.RedisMessageListenerContainer; -//import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; -//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; -//import org.springframework.data.redis.serializer.StringRedisSerializer; -// -//@Configuration -//public class RedisConfig { -// @Value("${spring.redis.chat.host}") -// private String chatRedisHost; -// -// @Value("${spring.redis.chat.port}") -// private int chatRedisPort; -// -// @Value("${spring.redis.chat.password}") -// private String chatRedisPassword; -// -// @Value("${spring.redis.fcm.host}") -// private String fcmRedisHost; -// -// @Value("${spring.redis.fcm.port}") -// private int fcmRedisPort; -// -// @Value("${spring.redis.fcm.password}") -// private String fcmRedisPassword; -// -// @Bean -// public RedisTemplate redisTemplate(@Qualifier("chatRedisConnectionFactory") RedisConnectionFactory connectionFactory) { -// RedisTemplate template = new RedisTemplate<>(); -// template.setConnectionFactory(connectionFactory); -// return template; -// } -// @Bean -// public StringRedisTemplate stringRedisTemplate() { -// return new StringRedisTemplate(fcmRedisConnectionFactory()); -// } -// @Bean -// @Qualifier("chatRedisConnectionFactory") -// public RedisConnectionFactory chatRedisConnectionFactory() { -// RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); -// config.setHostName(chatRedisHost); -// config.setPort(chatRedisPort); -// config.setPassword(chatRedisPassword); -// return new LettuceConnectionFactory(config); -// } -// -// @Bean -// @Qualifier("fcmRedisConnectionFactory") -// public RedisConnectionFactory fcmRedisConnectionFactory() { -// RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); -// config.setHostName(fcmRedisHost); -// config.setPort(fcmRedisPort); -// config.setPassword(fcmRedisPassword); -// return new LettuceConnectionFactory(config); -// } -// -// @Bean -// public RedisMessageListenerContainer redisMessageListenerContainer( -// @Qualifier("chatRedisConnectionFactory") RedisConnectionFactory connectionFactory, -// MessageListenerAdapter listenerAdapter, -// ChannelTopic channelTopic) { -// RedisMessageListenerContainer container = new RedisMessageListenerContainer(); -// container.setConnectionFactory(connectionFactory); -// container.addMessageListener(listenerAdapter, channelTopic); -// return container; -// } -// -// @Bean -// public MessageListenerAdapter listenerAdapter(RedisSubscriber subscriber) { -// return new MessageListenerAdapter(subscriber, "onMessage"); -// } -// -// @Bean -// public RedisTemplate chatRedisTemplate( -// @Qualifier("chatRedisConnectionFactory") RedisConnectionFactory connectionFactory) { -// RedisTemplate redisTemplate = new RedisTemplate<>(); -// redisTemplate.setConnectionFactory(connectionFactory); -// redisTemplate.setKeySerializer(new StringRedisSerializer()); -// redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(String.class)); -// return redisTemplate; -// } -// -// @Bean -// public RedisTemplate fcmRedisTemplate( -// @Qualifier("fcmRedisConnectionFactory") RedisConnectionFactory connectionFactory) { -// RedisTemplate redisTemplate = new RedisTemplate<>(); -// redisTemplate.setConnectionFactory(connectionFactory); -// redisTemplate.setKeySerializer(new StringRedisSerializer()); -// redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(String.class)); -// return redisTemplate; -// } -// -// @Bean -// public ChannelTopic channelTopic() { -// return new ChannelTopic("meetingRoom"); -// } -//} \ No newline at end of file +} \ No newline at end of file diff --git a/socket/src/main/java/Backend/socket/infra/config/WebConfig.java b/socket/src/main/java/Backend/socket/infra/config/WebConfig.java deleted file mode 100644 index 02076335..00000000 --- a/socket/src/main/java/Backend/socket/infra/config/WebConfig.java +++ /dev/null @@ -1,25 +0,0 @@ -//package Backend.socket.infra.config; -// -//import org.springframework.context.annotation.Configuration; -//import org.springframework.web.servlet.config.annotation.CorsRegistry; -//import org.springframework.web.servlet.config.annotation.EnableWebMvc; -//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -// -//@EnableWebMvc -//@Configuration -//public class WebConfig implements WebMvcConfigurer { -// @Override -// public void addCorsMappings(CorsRegistry registry) { -// registry.addMapping("/**") -// .allowedOrigins("*") -// .allowedMethods("*") -// .exposedHeaders("Access-Control-Allow-Origin", -// "Access-Control-Allow-Methods", -// "Access-Control-Allow-Headers", -// "Access-Control-Max-Age", -// "Access-Control-Request-Headers", -// "Access-Control-Request-Method") -// .allowCredentials(false) -// .maxAge(30000000); -// } -//} diff --git a/socket/src/main/java/Backend/socket/infra/config/auth/JwtAuthenticationFilter.java b/socket/src/main/java/Backend/socket/infra/config/auth/JwtAuthenticationFilter.java index ea88c214..21fd824f 100644 --- a/socket/src/main/java/Backend/socket/infra/config/auth/JwtAuthenticationFilter.java +++ b/socket/src/main/java/Backend/socket/infra/config/auth/JwtAuthenticationFilter.java @@ -24,52 +24,16 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter { private static final String AUTHORIZATION = "Authorization"; private static final String BEARER = "Bearer "; private final JwtProvider jwtProvider; + @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - - //url이 통과해도되는지 체크 (http 메서드와 함께 자료구조에 저장하여 검증) - String url = request.getRequestURI(); - String method = request.getMethod(); - - if(!IgnorePathConsts.isIgnorablePath(url, HttpMethod.valueOf(method))){ -// //위가 통과되면 토큰을 검출 -// String authorization = request.getHeader("Authorization"); -// -// /*//Authorization 헤더 검증 -// if (authorization == null || !authorization.startsWith("Bearer ")) { -// -// System.out.println("token null"); -// filterChain.doFilter(request, response); //doFilter를 통해 request와 response를 -// -// }*/ -// -// System.out.println("authorization now"); -// //Bearer 부분 제거 후 순수 토큰만 획득 -// String token = authorization.split(" ")[1]; -// -// //토큰이 유효한지 검증, 유효성 검증은 extract 메서드에서 처리 -// Long userId = jwtUtil.extractUserClaim(token).getUserId(); -// -// Authentication authToken = new JwtAuthentication(userId); -// -// //세션에 사용자 등록 -// SecurityContextHolder.getContext().setAuthentication(authToken); - } - - - + final String accessToken = getAccessTokenFromHttpServletRequest(request); + jwtProvider.validateAccessToken(accessToken); + final Long userId = jwtProvider.getSubject(accessToken); + setAuthentication(request, userId); filterChain.doFilter(request, response); } -// @Override -// protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { -// final String accessToken = getAccessTokenFromHttpServletRequest(request); -// jwtProvider.validateAccessToken(accessToken); -// final Long userId = jwtProvider.getSubject(accessToken); -// setAuthentication(request, userId); -// filterChain.doFilter(request, response); -// } - private String getAccessTokenFromHttpServletRequest(HttpServletRequest request) { String accessToken = request.getHeader(AUTHORIZATION); diff --git a/socket/src/main/java/Backend/socket/infra/config/auth/SecurityConfig.java b/socket/src/main/java/Backend/socket/infra/config/auth/SecurityConfig.java index f6d6bbd6..18a37fe8 100644 --- a/socket/src/main/java/Backend/socket/infra/config/auth/SecurityConfig.java +++ b/socket/src/main/java/Backend/socket/infra/config/auth/SecurityConfig.java @@ -28,38 +28,21 @@ public class SecurityConfig { // return web -> web.ignoring().requestMatchers(whiteList); // } -// @Bean -// public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { -// return http -// .formLogin(AbstractHttpConfigurer::disable) -// .httpBasic(AbstractHttpConfigurer::disable) -// .csrf(AbstractHttpConfigurer::disable) -// .sessionManagement(sessionManagementConfigurer -> -// sessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) -// .exceptionHandling(exceptionHandlingConfigurer -> -// exceptionHandlingConfigurer.authenticationEntryPoint(jwtAuthenticationEntryPoint)) -// .authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> -// authorizationManagerRequestMatcherRegistry.anyRequest().authenticated()) -// .addFilterBefore(new JwtAuthenticationFilter(jwtProvider), UsernamePasswordAuthenticationFilter.class) -// .addFilterBefore(new ExceptionHandlerFilter(), JwtAuthenticationFilter.class) -// .build(); -// } @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - http.cors(AbstractHttpConfigurer::disable); - - http.csrf(AbstractHttpConfigurer::disable); - - http.authorizeHttpRequests(request ->{ - request.anyRequest().permitAll(); - }); - - http.formLogin(AbstractHttpConfigurer::disable); - http.httpBasic(AbstractHttpConfigurer::disable); - http.logout(AbstractHttpConfigurer::disable); - http.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)); - - return http.build(); + return http + .formLogin(AbstractHttpConfigurer::disable) + .httpBasic(AbstractHttpConfigurer::disable) + .csrf(AbstractHttpConfigurer::disable) + .sessionManagement(sessionManagementConfigurer -> + sessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) + .exceptionHandling(exceptionHandlingConfigurer -> + exceptionHandlingConfigurer.authenticationEntryPoint(jwtAuthenticationEntryPoint)) + .authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> + authorizationManagerRequestMatcherRegistry.requestMatchers("/ws/**").permitAll().anyRequest().authenticated()) + .addFilterBefore(new JwtAuthenticationFilter(jwtProvider), UsernamePasswordAuthenticationFilter.class) + .addFilterBefore(new ExceptionHandlerFilter(), JwtAuthenticationFilter.class) + .build(); } } diff --git a/socket/src/main/java/Backend/socket/infra/external/AwsService.java b/socket/src/main/java/Backend/socket/infra/external/AwsService.java deleted file mode 100644 index 3c7f0b4e..00000000 --- a/socket/src/main/java/Backend/socket/infra/external/AwsService.java +++ /dev/null @@ -1,143 +0,0 @@ -package Backend.socket.infra.external; - - -import Backend.socket.global.error.httpException.InternalServerException; -import Backend.socket.global.error.httpException.InvalidValueException; -import com.amazonaws.services.s3.AmazonS3; -import com.amazonaws.services.s3.model.CannedAccessControlList; -import com.amazonaws.services.s3.model.DeleteObjectRequest; -import com.amazonaws.services.s3.model.ObjectMetadata; -import com.amazonaws.services.s3.model.PutObjectRequest; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.server.ResponseStatusException; - -import java.io.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.UUID; - -import static Backend.socket.global.error.ErrorCode.INVALID_IMAGE_TYPE; -import static Backend.socket.global.error.ErrorCode.S3_UPLOAD_ERROR; - - -@Slf4j -@RequiredArgsConstructor -@Transactional -@Service -public class AwsService { - @Value("${cloud.aws.s3.bucket}") - private String bucket; - - private final AmazonS3 amazonS3; - - public String uploadImageToS3(String image) throws IOException { - String[] strings = image.split(" "); // ","을 기준으로 바이트 코드를 나눠준다 - String base64Image = strings[1]; - String extension = ""; // if 문을 통해 확장자명을 정해줌 - if (strings[0].equals("data:image/jpeg;base64")) { - extension = "jpeg"; - } else if (strings[0].equals("data:image/png;base64")){ - extension = "png"; - } else { - extension = "jpg"; - } - - -// ... - - byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image); // 바이트 코드를 // 바이트 코드를 - - File tempFile = File.createTempFile("image", "." + extension); // createTempFile을 통해 임시 파일을 생성해준다. (임시파일은 지워줘야함) - try (OutputStream outputStream = new FileOutputStream(tempFile)) { - outputStream.write(imageBytes); // OutputStream outputStream = new FileOutputStream(tempFile)을 통해 생성한 outputStream 객체에 imageBytes를 작성해준다. - } - // 문자열을 공백을 기준으로 분리하여 문자열 배열로 변환 - String[] byteStrings = image.split(" "); - - // byte 배열 생성 - byte[] imageData = new byte[byteStrings.length]; - - for (int i = 0; i < byteStrings.length; i++) { - if (byteStrings[i].matches("-?[0-9]+")) { - imageData[i] = Byte.parseByte(byteStrings[i]); - } else if (byteStrings[i].matches("-?0x[0-9a-fA-F]+")) { - imageData[i] = (byte) Integer.parseInt(byteStrings[i].substring(2), 16); - } else { - // 잘못된 형식의 문자열인 경우 처리할 작업 - imageData[i] = 0; - } - } - - String fileName = UUID.randomUUID().toString(); - String fileUrl = ""; - - try { - ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentType("image/jpeg"); - objectMetadata.setContentLength(imageData.length); - - InputStream inputStream = new ByteArrayInputStream(imageData); - amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) - .withCannedAcl(CannedAccessControlList.PublicRead)); - - fileUrl = amazonS3.getUrl(bucket, fileName).toString(); - } catch (Exception e) { - e.printStackTrace(); - } - - return fileUrl; - } - - public List uploadImages(List imageDataList) { - if (imageDataList.isEmpty()) - return null; - List fileUrlList = new ArrayList<>(); - - imageDataList.forEach(imageData -> { - String fileName = UUID.randomUUID().toString(); - String fileUrl = ""; - - try { - ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentType("image/jpeg"); - objectMetadata.setContentLength(imageData.length); - - InputStream inputStream = new ByteArrayInputStream(imageData); - amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) - .withCannedAcl(CannedAccessControlList.PublicRead)); - - fileUrl = amazonS3.getUrl(bucket, fileName).toString(); - } catch (Exception e) { - log.error(e.getMessage()); - throw new InternalServerException(S3_UPLOAD_ERROR); - } - - fileUrlList.add(fileUrl); - }); - - return fileUrlList; - } - public void deleteImage(String fileName) { - amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileName)); - } - public String createFileName(String fileName) { - return UUID.randomUUID().toString().concat(getFileExtension(fileName)); - } - - private String getFileExtension(String fileName) { - try { - return fileName.substring(fileName.lastIndexOf(".")); - } catch(StringIndexOutOfBoundsException e) { - throw new InvalidValueException(INVALID_IMAGE_TYPE); - } - } - -} - diff --git a/socket/src/main/java/Backend/socket/infra/external/fcm/service/PushNotificationService.java b/socket/src/main/java/Backend/socket/infra/external/fcm/service/PushNotificationService.java index 0baf6481..738d36a8 100644 --- a/socket/src/main/java/Backend/socket/infra/external/fcm/service/PushNotificationService.java +++ b/socket/src/main/java/Backend/socket/infra/external/fcm/service/PushNotificationService.java @@ -1,14 +1,14 @@ //package Backend.socket.infra.external.fcm.service; // -//import Backend.socket.domain.chat.application.controller.dto.response.ChatMessageElementResponseDto; +//import Backend.socket.api.chat.service.dto.response.ChatMessageElementResponseDto; //import Backend.socket.domain.chat.domain.Room; -//import Backend.socket.domain.chat.domain.User; +//import Backend.socket.domain.user.entity.User; //import Backend.socket.domain.chat.domain.notification.entity.NotificationHistory; //import Backend.socket.domain.chat.domain.notification.entity.NotificationType; //import Backend.socket.domain.chat.domain.notification.entity.TopCategory; //import Backend.socket.domain.chat.domain.notification.repository.NotificationHistoryRepository; //import Backend.socket.domain.chat.repository.RoomRepository; -//import Backend.socket.domain.chat.repository.UserRepository; +//import Backend.socket.domain.user.repository.UserRepository; //import Backend.socket.infra.external.fcm.MessageTemplate; //import Backend.socket.infra.external.fcm.repository.FCMTokenRepository; //import Backend.socket.infra.external.fcm.service.dto.NotificationDto;