2022-03-29 22:17:08 -04:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2022-04-01 18:31:24 -04:00
|
|
|
import { Repository, LessThan } from 'typeorm';
|
2022-03-29 22:17:08 -04:00
|
|
|
import { ChatRoom } from 'server/entities/chat_room.entity';
|
2022-04-01 18:04:00 -04:00
|
|
|
import { User } from 'server/entities/user.entity';
|
|
|
|
import { ChatRoomConnection } from 'server/entities/chat_room_connection.entity';
|
2022-04-01 19:07:47 -04:00
|
|
|
import { UsersService } from './users.service';
|
|
|
|
import { RoleKey } from 'server/entities/role.entity';
|
2022-03-29 22:17:08 -04:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ChatRoomService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(ChatRoom)
|
|
|
|
private chatRoomRepository: Repository<ChatRoom>,
|
2022-04-01 18:04:00 -04:00
|
|
|
@InjectRepository(ChatRoomConnection)
|
|
|
|
private connectedUsersRepository: Repository<ChatRoomConnection>,
|
2022-04-01 19:07:47 -04:00
|
|
|
private usersService: UsersService,
|
2022-03-29 22:17:08 -04:00
|
|
|
) {}
|
|
|
|
|
|
|
|
create(chatRoom: ChatRoom) {
|
|
|
|
return this.chatRoomRepository.save(chatRoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
all() {
|
|
|
|
return this.chatRoomRepository.find();
|
|
|
|
}
|
|
|
|
|
2022-03-31 00:15:20 -04:00
|
|
|
nearOrUserOwns({ lat, lng, userId }: { lat: number; lng: number; userId: number }) {
|
|
|
|
// SQL injection maybe?
|
2022-03-30 17:18:16 -04:00
|
|
|
return this.chatRoomRepository.query(
|
2022-03-31 00:15:20 -04:00
|
|
|
`SELECT * FROM chat_room WHERE calculate_distance(latitude, longitude, ${lat}, ${lng}, 'M') < 5 OR "userId" = ${userId}`,
|
2022-03-30 17:18:16 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:35 -04:00
|
|
|
findById(id: string, relations: string[] = []) {
|
2022-03-30 17:18:16 -04:00
|
|
|
return this.chatRoomRepository.findOne(id, { relations });
|
|
|
|
}
|
|
|
|
|
2022-04-01 18:04:00 -04:00
|
|
|
async connectedUsers(chatRoom: ChatRoom) {
|
|
|
|
return this.connectedUsersRepository
|
|
|
|
.find({
|
|
|
|
where: { chatRoom },
|
|
|
|
relations: ['user'],
|
|
|
|
})
|
|
|
|
.then((x) =>
|
|
|
|
x.map((x) => {
|
|
|
|
return {
|
|
|
|
id: x.user.id,
|
|
|
|
userName: `${x.user.firstName} ${x.user.lastName}`,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
connectUser = async function (chatRoom: ChatRoom, user: User) {
|
|
|
|
const connectedUser = await this.connectedUsersRepository.findOne({
|
|
|
|
where: { chatRoom, user },
|
|
|
|
});
|
|
|
|
if (connectedUser) {
|
|
|
|
return connectedUser;
|
|
|
|
}
|
|
|
|
const chatRoomConnection = new ChatRoomConnection();
|
|
|
|
chatRoomConnection.chatRoom = chatRoom;
|
|
|
|
chatRoomConnection.user = user;
|
|
|
|
await this.connectedUsersRepository.save(chatRoomConnection);
|
|
|
|
return this.connectedUsers(chatRoom);
|
|
|
|
};
|
|
|
|
|
|
|
|
disconnectUser = async function (chatRoom: ChatRoom, user: User) {
|
|
|
|
const connectedUser = await this.connectedUsersRepository.findOne({
|
|
|
|
where: { chatRoom, user },
|
|
|
|
});
|
|
|
|
if (connectedUser) {
|
|
|
|
return this.connectedUsersRepository.remove(connectedUser);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2022-04-01 18:31:24 -04:00
|
|
|
async inactiveRooms() {
|
|
|
|
const inactiveRooms = await this.chatRoomRepository.find({
|
2022-04-01 19:07:47 -04:00
|
|
|
relations: ['user'],
|
2022-04-01 18:31:24 -04:00
|
|
|
where: {
|
2022-04-01 19:07:47 -04:00
|
|
|
lastModified: LessThan(new Date(Date.now() - 1000 * 60 * 60 * 2)),
|
2022-04-01 18:31:24 -04:00
|
|
|
},
|
|
|
|
});
|
2022-04-01 19:07:47 -04:00
|
|
|
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;
|
2022-04-01 18:31:24 -04:00
|
|
|
});
|
2022-04-01 19:07:47 -04:00
|
|
|
const results = await Promise.all(isInactivePromises);
|
|
|
|
|
|
|
|
return inactiveRooms.filter((_, index) => results[index]);
|
2022-04-01 18:31:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-30 17:18:16 -04:00
|
|
|
save(chatRoom: ChatRoom) {
|
|
|
|
return this.chatRoomRepository.save(chatRoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
remove(chatRoom: ChatRoom) {
|
|
|
|
return this.chatRoomRepository.remove(chatRoom);
|
2022-03-29 22:17:08 -04:00
|
|
|
}
|
|
|
|
}
|