From 5b8d0008567eba55a3b425cb475b42990634246b Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 1 Apr 2022 17:07:47 -0600 Subject: [PATCH] Fix timeout for chat rooms --- server/providers/gateways/chat_room.gateway.ts | 3 ++- server/providers/services/chat_room.service.ts | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/server/providers/gateways/chat_room.gateway.ts b/server/providers/gateways/chat_room.gateway.ts index 9c305bf..76f7bd8 100644 --- a/server/providers/gateways/chat_room.gateway.ts +++ b/server/providers/gateways/chat_room.gateway.ts @@ -31,11 +31,12 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa ) { setInterval(async () => { const inactiveRooms = await chatRoomService.inactiveRooms(); + console.log(inactiveRooms); inactiveRooms.forEach((room) => { this.server.to(room.id).emit('inactive', room.id); chatRoomService.remove(room); }); - }, 5 * (1000 * 60)); + }, 1000 * 60 * 5); } afterInit(server: Server) { diff --git a/server/providers/services/chat_room.service.ts b/server/providers/services/chat_room.service.ts index de08c02..0e8ed71 100644 --- a/server/providers/services/chat_room.service.ts +++ b/server/providers/services/chat_room.service.ts @@ -4,6 +4,8 @@ import { Repository, LessThan } from 'typeorm'; import { ChatRoom } from 'server/entities/chat_room.entity'; import { User } from 'server/entities/user.entity'; import { ChatRoomConnection } from 'server/entities/chat_room_connection.entity'; +import { UsersService } from './users.service'; +import { RoleKey } from 'server/entities/role.entity'; @Injectable() export class ChatRoomService { @@ -12,6 +14,7 @@ export class ChatRoomService { private chatRoomRepository: Repository, @InjectRepository(ChatRoomConnection) private connectedUsersRepository: Repository, + private usersService: UsersService, ) {} create(chatRoom: ChatRoom) { @@ -75,13 +78,19 @@ export class ChatRoomService { async inactiveRooms() { const inactiveRooms = await this.chatRoomRepository.find({ + relations: ['user'], where: { - lastModified: LessThan(new Date(Date.now() - 2 * 60 * (1000 * 60))), + lastModified: LessThan(new Date(Date.now() - 1000 * 60 * 60 * 2)), }, }); - return inactiveRooms.filter(async (room) => { - return !(await this.connectedUsers(room)).length; + const isInactivePromises = inactiveRooms.map(async (room) => { + const isAdmin = await this.usersService.hasRootRole(room.user.id, RoleKey.ADMIN); + const hasMoreThanOneConnections = (await this.connectedUsers(room)).length > 1; + return !isAdmin && !hasMoreThanOneConnections; }); + const results = await Promise.all(isInactivePromises); + + return inactiveRooms.filter((_, index) => results[index]); } save(chatRoom: ChatRoom) {