Skip to content

Commit

Permalink
Merge pull request #74 from catchroom/develop
Browse files Browse the repository at this point in the history
Develop ๋ฐฐํฌ
  • Loading branch information
sungjiwoon authored Jan 26, 2024
2 parents 0fe686d + 2acca51 commit 33570d3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/main/java/com/catchroom/chat/message/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,38 @@ public String getRoomId(String destination) {
*/
public void sendChatMessage(ChatMessageDto chatMessage, String accessToken) {

chatRoomRedisRepository.setLastChatMessage(chatMessage.getRoomId(), chatMessage);
Long before = System.currentTimeMillis();

// 1. redis ์— ์ฑ„ํŒ…๋ฐฉ์ด ์—†์œผ๋ฉด, ๋ ˆ๋””์Šค์— ๋ฆฌ์ŠคํŠธ๋ฅผ ๋จผ์ € ๋„ฃ์–ด์ค€๋‹ค.
initIfChatRoomAbsent(chatMessage, accessToken);

// 2. ์ƒˆ๋กœ์šด ์ฑ„ํŒ…๋ฐฉ ์ •๋ณด๊ฐ€ ์—†๋‹ค๋ฉด, ๋„ฃ์–ด์ค€๋‹ค.
setNewChatRoomInfo(chatMessage, accessToken);

// 3. ์ฑ„ํŒ…๋ฐฉ์— ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
setRedisChatMessage(chatMessage);

// 4. ์ฑ„ํŒ…๋ฐฉ์ด ์‚ญ์ œ๋˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด delete๋ฅผ ํ•ด์ค€๋‹ค.
if (chatMessage.getType().equals(MessageType.DELETE)) {
chatRoomRedisRepository.deleteChatRoom(chatMessage.getUserId(), chatMessage.getRoomId());
deleteChatRoom(accessToken, chatMessage.getRoomId());
}

// 5. ์ฑ„ํŒ…๋ฐฉ ์ •๋ณด ์—…๋ฐ์ดํŠธ
List<ChatRoomListGetResponse> chatRoomListGetResponseList =
chatRoomService.getChatRoomList(chatMessage.getUserId(), accessToken, chatMessage.getType());
chatRoomRedisRepository.getChatRoomList(chatMessage.getUserId());

// 6. ๋งˆ์ง€๋ง‰ ๋ฉ”์„ธ์ง€ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
sortChatRoomListLatest(chatRoomListGetResponseList);

MessageSubDto messageSubDto = MessageSubDto.builder()
.chatMessageDto(chatMessage)
.list(chatRoomListGetResponseList)
.build();

Long after = System.currentTimeMillis() - before;
log.info("message Time : {}", after);

redisPublisher.publish(messageSubDto);
}

Expand Down Expand Up @@ -137,10 +155,12 @@ private void setLastMessage(List<ChatRoomListGetResponse> chatRoomListGetRespons
String chatRoomNumber = chatRoomListGetResponse.getChatRoomNumber();

if (chatRoomListGetResponse.getLastChatmessageDto() == null) {
ChatMessage lastChatMessageMongo = chatMessageRepository.findAllByRoomId(
chatRoomNumber).get(0);
chatRoomListGetResponse.updateChatMessageDto(
ChatMessageDto.fromEntity(lastChatMessageMongo));
if (chatMessageRepository.findAllByRoomId(chatRoomNumber) != null) {
ChatMessage lastChatMessageMongo = chatMessageRepository.findAllByRoomId(
chatRoomNumber).get(0);
chatRoomListGetResponse.updateChatMessageDto(
ChatMessageDto.fromEntity(lastChatMessageMongo));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.catchroom.chat.message.service;

import static org.junit.jupiter.api.Assertions.*;

import com.catchroom.chat.message.dto.ChatMessageDto;
import com.catchroom.chat.message.type.MessageType;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ChatServiceTest {

@Autowired
ChatService chatService;

@Test
void ์‹คํ–‰_์„ฑ๊ณต_ํ† ํฐ_์žฌ์ž‘์„ฑ_ํ•„์ˆ˜() {
String accessToken = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJuYW1lIjoi6rmA7LKg7IiYIiwicGhvbmVOdW1iZXIiOiIwMTA4ODI1NDQ3MSIsIm5pY2tOYW1lIjoi7LKg7IiY7JmA7JiB7Z2sIiwiZW1haWwiOiJ0ZXN0OTI3QG5hdmVyLmNvbSIsImlzcyI6ImNhdGNocm9vbSIsImlhdCI6MTcwNjI2MDc3NSwiZXhwIjoxNzA2MjYyNTc0fQ.hd4zTCpvmTjVROOGOwnKumWMUjW-lKlDJhSML79Fc2MUEDzeXSZBYAznFV1tolhNdl2qrA1N8HgT1tchhKsx2A";
ChatMessageDto messageDto = ChatMessageDto.builder()
.type(MessageType.TALK)
.message("์‹คํ–‰๋˜๋‹ˆ? 3ํƒ„")
.roomId("f5556003-1151-4d26-a3a8-346db5e622a3")
.userId(28L)
.time(LocalDateTime.now().toString())
.build();

chatService.sendChatMessage(messageDto, accessToken);

}
}

0 comments on commit 33570d3

Please sign in to comment.