LocChat/server/providers/gateways/chat_room.gateway.ts

103 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2022-03-08 20:18:38 -05:00
import { UseGuards } from '@nestjs/common';
2022-03-31 00:15:20 -04:00
import {
ConnectedSocket,
MessageBody,
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
WsException,
} from '@nestjs/websockets';
2022-03-08 20:18:38 -05:00
import { GatewayJwtBody } from 'server/decorators/gateway_jwt_body.decorator';
import { JwtBodyDto } from 'server/dto/jwt_body.dto';
import { Server, Socket } from 'socket.io';
import { GatewayAuthGuard } from '../guards/gatewayauth.guard';
2022-04-01 18:04:00 -04:00
import { ChatRoomService } from '../services/chat_room.service';
2022-03-08 20:18:38 -05:00
import { JwtService } from '../services/jwt.service';
2022-03-31 00:15:20 -04:00
import { UsersService } from '../services/users.service';
2022-03-08 14:10:53 -05:00
@WebSocketGateway()
2022-03-08 20:18:38 -05:00
@UseGuards(GatewayAuthGuard)
2022-03-31 00:15:20 -04:00
export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
2022-03-08 20:18:38 -05:00
@WebSocketServer()
server: Server;
2022-04-01 18:04:00 -04:00
constructor(
private jwtService: JwtService,
private userService: UsersService,
private chatRoomService: ChatRoomService,
2022-04-01 18:31:24 -04:00
) {
setInterval(async () => {
const inactiveRooms = await chatRoomService.inactiveRooms();
2022-04-01 19:07:47 -04:00
console.log(inactiveRooms);
2022-04-01 18:31:24 -04:00
inactiveRooms.forEach((room) => {
this.server.to(room.id).emit('inactive', room.id);
chatRoomService.remove(room);
});
2022-04-01 19:07:47 -04:00
}, 1000 * 60 * 5);
2022-04-01 18:31:24 -04:00
}
2022-03-08 20:18:38 -05:00
afterInit(server: Server) {
console.log('Sockets initialized');
}
2022-04-01 18:04:00 -04:00
async handleConnection(client: Socket) {
2022-03-08 20:18:38 -05:00
try {
2022-04-01 18:04:00 -04:00
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
const user = await this.userService.find(jwtBody.userId);
2022-04-01 18:31:24 -04:00
2022-04-01 18:04:00 -04:00
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
2022-04-01 18:31:24 -04:00
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),
});
}
2022-03-08 20:18:38 -05:00
} catch (e) {
2022-04-01 18:04:00 -04:00
throw new WsException(e.message);
2022-03-08 20:18:38 -05:00
}
}
2022-04-01 18:04:00 -04:00
async handleDisconnect(client: Socket) {
2022-03-08 20:18:38 -05:00
console.log('Client Disconnected');
2022-04-01 18:04:00 -04:00
const jwtBody = this.jwtService.parseToken(client.handshake.auth.token);
const user = await this.userService.find(jwtBody.userId);
2022-04-01 18:31:24 -04:00
2022-04-01 18:04:00 -04:00
const chatRoom = await this.chatRoomService.findById(client.handshake.query.chatRoomId as unknown as string);
2022-04-01 18:31:24 -04:00
if (chatRoom) {
await this.chatRoomService.disconnectUser(chatRoom, user);
this.server.to(chatRoom.id).emit('userlist', {
users: await this.chatRoomService.connectedUsers(chatRoom),
});
}
2022-03-08 20:18:38 -05:00
}
2022-03-31 00:15:20 -04:00
@SubscribeMessage('message')
public async handleMessage(
2022-03-08 20:18:38 -05:00
@ConnectedSocket() client: Socket,
2022-03-31 00:15:20 -04:00
@MessageBody() data: string,
2022-03-08 20:18:38 -05:00
@GatewayJwtBody() jwtBody: JwtBodyDto,
) {
2022-03-31 00:15:20 -04:00
const user = await this.userService.find(jwtBody.userId);
2022-04-01 18:31:24 -04:00
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,
});
}
2022-03-08 14:10:53 -05:00
}
}