Skip to content

Commit ab7a4bd

Browse files
authored
Merge pull request #73 from oodd-team/OD-175
채팅, 유저 예외 처리 OD - 175
2 parents 361be68 + 3790abb commit ab7a4bd

7 files changed

Lines changed: 39 additions & 16 deletions

File tree

src/chat-message/chat-message.service.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export class ChatMessageService {
3232
where: { id: newMessage.id },
3333
relations: ['fromUser', 'toUser'],
3434
});
35-
3635
return {
3736
id: newMessageWithUser.id,
3837
chatRoomId: chatRoomId,
@@ -90,4 +89,21 @@ export class ChatMessageService {
9089
content: body.message,
9190
});
9291
}
92+
93+
async deleteMessages(chatRoomId: number): Promise<void> {
94+
const messages = await this.chatMessageRepository.find({
95+
where: { chatRoom: { id: chatRoomId }, status: StatusEnum.ACTIVATED },
96+
});
97+
98+
if (messages.length === 0) {
99+
return;
100+
}
101+
102+
for (const message of messages) {
103+
message.status = StatusEnum.DEACTIVATED;
104+
message.softDelete();
105+
}
106+
107+
await this.chatMessageRepository.save(messages);
108+
}
93109
}

src/chat-room/chat-room.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
3+
import { ChatMessageService } from 'src/chat-message/chat-message.service';
34
import { ChatRoom } from 'src/common/entities/chat-room.entity';
45
import { Matching } from 'src/common/entities/matching.entity';
56
import { StatusEnum } from 'src/common/enum/entityStatus';
@@ -13,6 +14,7 @@ export class ChatRoomService {
1314
constructor(
1415
@InjectRepository(ChatRoom)
1516
private readonly chatRoomRepository: Repository<ChatRoom>,
17+
private readonly chatMessageService: ChatMessageService,
1618
) {}
1719

1820
async getChatRoomsWithLatestMessage(userId: number) {
@@ -84,8 +86,8 @@ export class ChatRoomService {
8486
async deleteChatRoom(chatRoomId: number, userId: number): Promise<void> {
8587
const chatRoom = await this.chatRoomRepository.findOne({
8688
where: { id: chatRoomId },
89+
relations: ['fromUser', 'toUser'],
8790
});
88-
8991
if (!chatRoom) {
9092
throw DataNotFoundException('채팅방을 찾을 수 없습니다.');
9193
}
@@ -102,6 +104,7 @@ export class ChatRoomService {
102104
if (chatRoom.fromUserLeavedAt && chatRoom.toUserLeavedAt) {
103105
chatRoom.status = StatusEnum.DEACTIVATED;
104106
chatRoom.softDelete();
107+
await this.chatMessageService.deleteMessages(chatRoomId);
105108
}
106109

107110
await this.chatRoomRepository.save(chatRoom);

src/eventGateway.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
import { Server, Socket } from 'socket.io';
99
import { ChatRoomService } from './chat-room/chat-room.service';
1010
import { ChatMessageService } from './chat-message/chat-message.service';
11+
import { UserService } from './user/user.service';
12+
import { UserBlockService } from './user-block/user-block.service';
1113

1214
//클라이언트의 패킷들이 게이트웨이를 통해서 들어오게 됩니다.
1315
@WebSocketGateway({ namespace: '/socket/chatting' })
@@ -19,6 +21,8 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
1921
constructor(
2022
private readonly chatRoomService: ChatRoomService,
2123
private readonly chatMessageService: ChatMessageService,
24+
private readonly userService: UserService,
25+
private readonly userBlockService: UserBlockService,
2226
) {}
2327
/*
2428
유저정보는 같지만 소켓이 여러개가 연결되어 있을 경우
@@ -74,6 +78,16 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
7478
},
7579
) {
7680
const { chatRoomId, toUserId, content, fromUserId, createdAt } = payload;
81+
const toUser = await this.userService.getUserById(toUserId);
82+
const blockedUserIds =
83+
await this.userBlockService.getBlockedUserIds(toUserId);
84+
if (!toUser || blockedUserIds.includes(fromUserId)) {
85+
const errorMessage = !toUser
86+
? '존재하지 않는 사용자입니다.'
87+
: '차단된 사용자에게 메시지를 보낼 수 없습니다.';
88+
client.emit('error', errorMessage);
89+
return;
90+
}
7791

7892
// 메시지 저장 로직
7993
const newMessage = await this.chatMessageService.saveMessage(

src/matching/matching.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class MatchingService {
106106

107107
async getMatchings(currentUserId: number): Promise<GetMatchingsResponse> {
108108
const blockedUserIds =
109-
await this.userBlockService.getBlockedUserIdsByRequesterId(currentUserId);
109+
await this.userBlockService.getBlockedUserIds(currentUserId);
110110
const matchings = await this.matchingRepository
111111
.createQueryBuilder('matching')
112112
.leftJoinAndSelect('matching.requester', 'requester')

src/post/post.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class PostService {
4848
currentUserId: number,
4949
): Promise<{ posts: GetAllPostsResponse; total: number }> {
5050
const blockedUserIds =
51-
await this.userBlockService.getBlockedUserIdsByRequesterId(currentUserId);
51+
await this.userBlockService.getBlockedUserIds(currentUserId);
5252

5353
const queryBuilder = this.dataSource
5454
.getRepository(Post)
@@ -388,7 +388,7 @@ export class PostService {
388388
// 게시글 상세 조회
389389
async getPost(postId: number, currentUserId: number): Promise<Post | null> {
390390
const blockedUserIds =
391-
await this.userBlockService.getBlockedUserIdsByRequesterId(currentUserId);
391+
await this.userBlockService.getBlockedUserIds(currentUserId);
392392
return await this.postRepository
393393
.createQueryBuilder('post')
394394
.leftJoinAndSelect(

src/user-block/user-block.service.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ export class UserBlockService {
1818
private readonly userService: UserService,
1919
) {}
2020

21-
async getBlockedUserIdsByRequesterId(
22-
currentUserId: number,
23-
): Promise<number[]> {
24-
const blockedUsers = await this.userBlockRepository.find({
25-
where: { requester: { id: currentUserId }, status: StatusEnum.ACTIVATED },
26-
relations: ['target'],
27-
});
28-
29-
return blockedUsers.map((block) => block.target.id);
30-
}
31-
3221
async createBlock(createUserBlockDto: UserBlockRequest): Promise<string> {
3322
const { requesterId, targetId, action } = createUserBlockDto;
3423

src/user/user.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class UserController {
4747
@Param('userId') userId: number,
4848
): Promise<BaseResponse<GetOtherUserInfo>> {
4949
const user = await this.userService.getUserById(userId);
50+
if (!user) throw DataNotFoundException('존재하지 않는 사용자입니다.');
5051
// MatchingService를 통해 해당 사용자가 친구인지 확인
5152
const isMatching = await this.matchingService.isMatching(
5253
req.user.id,

0 commit comments

Comments
 (0)