summaryrefslogtreecommitdiff
path: root/server/providers
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2022-04-01 16:31:24 -0600
committerLogan Hunt <loganhunt@simponic.xyz>2022-04-01 16:31:24 -0600
commit638b3e0750c61aded2d08dfad7680bfcff8abf9f (patch)
tree7076b5e03ed744fa9fdb023a7aa4a2a55c36bf47 /server/providers
parentdbb9eea25f80e7984a112409863be5191af5bf5e (diff)
downloadlocchat-638b3e0750c61aded2d08dfad7680bfcff8abf9f.tar.gz
locchat-638b3e0750c61aded2d08dfad7680bfcff8abf9f.zip
Fix some bugs
Diffstat (limited to 'server/providers')
-rw-r--r--server/providers/gateways/chat_room.gateway.ts56
-rw-r--r--server/providers/services/chat_room.service.ts13
2 files changed, 52 insertions, 17 deletions
diff --git a/server/providers/gateways/chat_room.gateway.ts b/server/providers/gateways/chat_room.gateway.ts
index bee83c5..9c305bf 100644
--- a/server/providers/gateways/chat_room.gateway.ts
+++ b/server/providers/gateways/chat_room.gateway.ts
@@ -28,7 +28,15 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
private jwtService: JwtService,
private userService: UsersService,
private chatRoomService: ChatRoomService,
- ) {}
+ ) {
+ setInterval(async () => {
+ const inactiveRooms = await chatRoomService.inactiveRooms();
+ inactiveRooms.forEach((room) => {
+ this.server.to(room.id).emit('inactive', room.id);
+ chatRoomService.remove(room);
+ });
+ }, 5 * (1000 * 60));
+ }
afterInit(server: Server) {
console.log('Sockets initialized');
@@ -38,12 +46,18 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
try {
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
const user = await this.userService.find(jwtBody.userId);
+
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
- await this.chatRoomService.connectUser(chatRoom, user);
- client.join(chatRoom.id);
- this.server.to(chatRoom.id).emit('userlist', {
- users: await this.chatRoomService.connectedUsers(chatRoom),
- });
+ if (chatRoom) {
+ chatRoom.lastModified = new Date();
+ this.chatRoomService.save(chatRoom);
+
+ await this.chatRoomService.connectUser(chatRoom, user);
+ client.join(chatRoom.id);
+ this.server.to(chatRoom.id).emit('userlist', {
+ users: await this.chatRoomService.connectedUsers(chatRoom),
+ });
+ }
} catch (e) {
throw new WsException(e.message);
}
@@ -53,11 +67,14 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
console.log('Client Disconnected');
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
const user = await this.userService.find(jwtBody.userId);
+
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
- await this.chatRoomService.disconnectUser(chatRoom, user);
- this.server.to(chatRoom.id).emit('userlist', {
- users: await this.chatRoomService.connectedUsers(chatRoom),
- });
+ if (chatRoom) {
+ await this.chatRoomService.disconnectUser(chatRoom, user);
+ this.server.to(chatRoom.id).emit('userlist', {
+ users: await this.chatRoomService.connectedUsers(chatRoom),
+ });
+ }
}
@SubscribeMessage('message')
@@ -67,11 +84,18 @@ export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGa
@GatewayJwtBody() jwtBody: JwtBodyDto,
) {
const user = await this.userService.find(jwtBody.userId);
- this.server.to(client.handshake.query.chatRoomId).emit('new-message', {
- id: user.id * Math.random() * Math.pow(2, 16) * Date.now(),
- content: data,
- userName: `${user.firstName} ${user.lastName}`,
- userId: user.id,
- });
+
+ const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
+ if (chatRoom) {
+ chatRoom.lastModified = new Date();
+ this.chatRoomService.save(chatRoom);
+
+ this.server.to(chatRoom.id).emit('new-message', {
+ id: user.id * Math.random() * Math.pow(2, 16) * Date.now(),
+ content: data,
+ userName: `${user.firstName} ${user.lastName}`,
+ userId: user.id,
+ });
+ }
}
}
diff --git a/server/providers/services/chat_room.service.ts b/server/providers/services/chat_room.service.ts
index 148ff19..de08c02 100644
--- a/server/providers/services/chat_room.service.ts
+++ b/server/providers/services/chat_room.service.ts
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
-import { Repository } from 'typeorm';
+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';
@@ -73,6 +73,17 @@ export class ChatRoomService {
return false;
};
+ async inactiveRooms() {
+ const inactiveRooms = await this.chatRoomRepository.find({
+ where: {
+ lastModified: LessThan(new Date(Date.now() - 2 * 60 * (1000 * 60))),
+ },
+ });
+ return inactiveRooms.filter(async (room) => {
+ return !(await this.connectedUsers(room)).length;
+ });
+ }
+
save(chatRoom: ChatRoom) {
return this.chatRoomRepository.save(chatRoom);
}