Skip to content

Commit 78491f4

Browse files
committed
[REFACTOR] 채팅 메시지 검색 API JWT 인증 적용
1 parent bad3635 commit 78491f4

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/chat/chat.routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ router.post("", createChatroom);
1414
// 채팅방 삭제 API
1515
router.delete("/delete", authenticate, deleteChatrooms);
1616

17-
// 채팅 메시지 검색 API
18-
router.get("/search/messages", getMessageByKeyword);
17+
// 채팅 메시지 검색 API (다중)
18+
router.get("/search/messages", authenticate, getMessageByKeyword);
1919

2020
// 채팅방 조회 API
2121
router.get("", authenticate, getChatroom);

src/chat/controller/chat.controller.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ export const getMessages = async (req, res, next) => {
2626

2727
export const getMessageByKeyword = async (req, res, next) => {
2828
try {
29-
const dto = new FindChatroomByMessageDto(req.query);
29+
const userId = BigInt(req.user.userId);
30+
31+
const dto = new FindChatroomByMessageDto({
32+
keyword: req.query.keyword,
33+
userId: userId,
34+
});
3035

31-
const messages = await ChatService.searchMessagesByKeyword(dto.keyword);
36+
const messages = await ChatService.searchMessagesByKeyword(dto);
3237
const responseData = parseWithBigInt(stringifyWithBigInt(messages));
3338

3439
res.status(StatusCodes.OK).success(responseData);

src/chat/dto/chat.dto.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export class GetMessagesDto {
88
}
99

1010
export class FindChatroomByMessageDto {
11-
constructor(query) {
12-
this.keyword = query.keyword;
11+
constructor({ keyword, userId }) {
12+
this.keyword = keyword;
13+
this.userId = BigInt(userId);
1314
}
1415
}

src/chat/service/chat.service.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ export const ChatService = {
1818
return messages;
1919
},
2020

21-
async searchMessagesByKeyword(keyword) {
22-
const messages = await ChatRepository.searchByKeyword(keyword);
21+
async searchMessagesByKeyword(dto) {
22+
const userChatrooms = await ChatroomRepository.findChatroomsByUser(dto.userId);
23+
const chatroomIds = userChatrooms.map(cr => cr.id);
24+
25+
const messages = await ChatRepository.searchByKeyword(dto.keyword, chatroomIds);
2326
return messages;
2427
},
2528
};

0 commit comments

Comments
 (0)