Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE/#603] 채팅방 나갔다가 들어오면 이전 채팅 안보이게 구현 #604

Merged
merged 8 commits into from
Jan 14, 2024
70 changes: 59 additions & 11 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ export class ChatService {
'chat_info.sender as sender',
])
.where('chat_room.writer = :userId', { userId: userId })
.andWhere('chat_room.writer_left IS false')
.andWhere('chat_room.writer_hide IS false')
.orWhere('chat_room.user = :userId', { userId: userId })
.andWhere('chat_room.user_left IS false')
.andWhere('chat_room.user_hide IS false')
.orderBy('chat_info.create_date', 'DESC')
.getRawMany();

Expand Down Expand Up @@ -206,7 +206,7 @@ export class ChatService {
return { all_read: true };
}

async findRoomById(roomId: number, userId: string) {
async makeAllRead(roomId: number, userId: string) {
await this.chatRepository
.createQueryBuilder('chat')
.update()
Expand All @@ -215,15 +215,57 @@ export class ChatService {
.andWhere('chat.is_read = :isRead', { isRead: false })
.andWhere('chat.sender != :userId', { userId: userId })
.execute();
}

const room = await this.chatRoomRepository.findOne({
/*async getRoomAndChatInfoPagination(roomId: number, chatId: number) {
return await this.chatRoomRepository
.createQueryBuilder('chat_room')
.innerJoinAndSelect('chat_room.chats', 'chat_info')
.innerJoinAndSelect('chat_room.writerUser', 'writer')
.innerJoinAndSelect('chat_room.userUser', 'user')
.where('chat_room.id = :roomId', { roomId: roomId })
.andWhere('chat_info.id < :chatId', { chatId: chatId })
.orderBy('chat_info.id', 'DESC')
.limit(30)
.getOne();
}*/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분 채팅 페이지네이션 미리 적용해본건데 나중에 이걸로 바꾸겠습니다

async getRoomAndChatInfo(roomId: number, userId: string) {
return await this.chatRoomRepository.findOne({
where: {
id: roomId,
},
relations: ['chats', 'userUser', 'writerUser'],
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일단 room이 찾아 지지 않을 때의 예외처리가 없어서 삭제되거나 존재하지 않는 roomId로 접근하면 에러가 발생 할 것 같네요..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마찬가지로 위에 update 문도 없는 방을 업데이트 하려면 예외처리가 필요할 것 같고 유저의 권한을 확인 하는 로직이 밑에 있어서 본인의 방이 아닌 경우에도 채팅 읽음 처리를 하는 문제가 생길 수도 있을 것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러면 checkAuth 를 맨 처음으로 옮겨서 처음에 해당 방이 존재하는지와 해당 방에 존재하는 사람이 맞는지 확인 후에 아래 로직들을 실행하는 방식으로 바꾸겠습니당~

}

async findRoomById(roomId: number, userId: string) {
await this.makeAllRead(roomId, userId);

const room = await this.getRoomAndChatInfo(roomId, userId);

this.checkAuth(room, userId);

let chats = room.chats;

if (
room.writer === userId &&
room.writer_hide === false &&
room.writer_left_time !== null
) {
Comment on lines +251 to +255
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

room.writer_hide === false 이 부분이 true 인 경우의 처리가 없는 것 같습니다. 사용자에게 안보이도록 x_hide 가 true 라면 예외처리가 필요하지 않을까 하는 생각이 드는데 어떻게 생각하시는지요...?
404나 요런 처리가 필요하지 않을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url 로 접근하는 부분때문인가요? 자신이 숨긴 채팅방을 url 을 통해 400 혹은 409 로 던지면 될 것 같은데 어케 생각하시나요?

chats = chats.filter((chat) => {
return chat.create_date > room.writer_left_time;
});
} else if (
room.user === userId &&
room.user_hide === false &&
room.user_left_time !== null
) {
chats = chats.filter((chat) => {
return chat.create_date > room.user_left_time;
});
}

return {
writer: room.writer,
writer_profile_img:
Expand All @@ -236,7 +278,7 @@ export class ChatService {
? this.configService.get('DEFAULT_PROFILE_IMAGE')
: room.userUser.profile_img,
post_id: room.post_id,
chat_log: room.chats,
chat_log: chats,
};
}

Expand All @@ -245,6 +287,10 @@ export class ChatService {
throw new HttpException('존재하지 않는 채팅방입니다.', 404);
} else if (room.writer !== userId && room.user !== userId) {
throw new HttpException('권한이 없습니다.', 403);
} else if (room.writer === userId && room.writer_hide === true) {
throw new HttpException('숨긴 채팅방입니다.', 403);
} else if (room.user === userId && room.user_hide === true) {
throw new HttpException('숨긴 채팅방입니다.', 403);
}
}

Expand Down Expand Up @@ -275,10 +321,10 @@ export class ChatService {
where: { id: roomId },
});

if (room.writer === userId && room.user_left !== false) {
room.user_left = false;
} else if (room.user === userId && room.writer_left !== false) {
room.writer_left = false;
if (room.writer === userId && room.user_hide !== false) {
room.user_hide = false;
} else if (room.user === userId && room.writer_hide !== false) {
room.writer_hide = false;
}
await this.chatRoomRepository.save(room);
}
Expand All @@ -302,9 +348,11 @@ export class ChatService {
});

if (room.writer === userId) {
room.writer_left = true;
room.writer_hide = true;
room.writer_left_time = new Date();
} else if (room.user === userId) {
room.user_left = true;
room.user_hide = true;
room.user_left_time = new Date();
}

await this.chatRoomRepository.save(room);
Expand Down
10 changes: 8 additions & 2 deletions BE/src/entities/chatRoom.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ export class ChatRoomEntity {
last_chat_id: number;

@Column({ default: false })
writer_left: boolean;
writer_hide: boolean;

@Column({ default: false })
user_left: boolean;
user_hide: boolean;

@Column({ default: null, type: 'timestamp' })
writer_left_time: Date;

@Column({ default: null, type: 'timestamp' })
user_left_time: Date;

@OneToMany(() => ChatEntity, (chat) => chat.chatRoom)
chats: ChatEntity[];
Expand Down
Loading