Skip to content

Commit

Permalink
Merge pull request #604 from boostcampwm2023/BE-fixChat-#603
Browse files Browse the repository at this point in the history
[BE/#603] 채팅방 나갔다가 들어오면 이전 채팅 안보이게 구현
  • Loading branch information
namewhat99 authored Jan 14, 2024
2 parents 05e5514 + c10de5a commit 00adc4e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
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();
}*/

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

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
) {
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

0 comments on commit 00adc4e

Please sign in to comment.