Fix timeout for chat rooms

This commit is contained in:
Logan Hunt 2022-04-01 17:07:47 -06:00
parent 638b3e0750
commit 5b8d000856
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 52B3774857EB24B1
2 changed files with 14 additions and 4 deletions

View File

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

View File

@ -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<ChatRoom>,
@InjectRepository(ChatRoomConnection)
private connectedUsersRepository: Repository<ChatRoomConnection>,
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) {