|
@ -19,8 +19,7 @@ import { kickUserDto } from './dto/kickUser.dto' |
|
|
import ConnectedUser from './entity/connection.entity' |
|
|
import ConnectedUser from './entity/connection.entity' |
|
|
import { InjectRepository } from '@nestjs/typeorm' |
|
|
import { InjectRepository } from '@nestjs/typeorm' |
|
|
import { Repository } from 'typeorm' |
|
|
import { Repository } from 'typeorm' |
|
|
import { connect } from 'http2' |
|
|
import type User from 'src/users/entity/user.entity' |
|
|
import User from 'src/users/entity/user.entity' |
|
|
|
|
|
|
|
|
|
|
|
@WebSocketGateway({ |
|
|
@WebSocketGateway({ |
|
|
cors: { |
|
|
cors: { |
|
@ -45,17 +44,11 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { |
|
|
|
|
|
|
|
|
async handleDisconnect (socket: Socket): Promise<void> { |
|
|
async handleDisconnect (socket: Socket): Promise<void> { |
|
|
await this.onLeaveChannel(socket) |
|
|
await this.onLeaveChannel(socket) |
|
|
socket.disconnect() |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@SubscribeMessage('joinChannel') |
|
|
@SubscribeMessage('joinChannel') |
|
|
async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise<void> { |
|
|
async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise<void> { |
|
|
console.log( |
|
|
console.log('here') |
|
|
'User %s is trying to join channel %s', |
|
|
|
|
|
connect.UserId, |
|
|
|
|
|
connect.ChannelId, |
|
|
|
|
|
connect.pwd |
|
|
|
|
|
) |
|
|
|
|
|
const channel = await this.chatService.getFullChannel(connect.ChannelId) |
|
|
const channel = await this.chatService.getFullChannel(connect.ChannelId) |
|
|
if (channel.banned.findIndex((ban) => ban[0] === connect.UserId) !== -1) { |
|
|
if (channel.banned.findIndex((ban) => ban[0] === connect.UserId) !== -1) { |
|
|
throw new WsException('You are banned from entering this channel') |
|
|
throw new WsException('You are banned from entering this channel') |
|
@ -81,6 +74,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { |
|
|
this.connectedUserRepository.create(conUser) |
|
|
this.connectedUserRepository.create(conUser) |
|
|
this.server.to(socket.id).emit('messages', messages) |
|
|
this.server.to(socket.id).emit('messages', messages) |
|
|
await socket.join(channel.id.toString()) |
|
|
await socket.join(channel.id.toString()) |
|
|
|
|
|
console.log(this.server.sockets.adapter.rooms.get(channel.id.toString())) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@SubscribeMessage('getMessages') |
|
|
@SubscribeMessage('getMessages') |
|
@ -96,14 +90,15 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { |
|
|
|
|
|
|
|
|
@SubscribeMessage('leaveChannel') |
|
|
@SubscribeMessage('leaveChannel') |
|
|
async onLeaveChannel (socket: Socket): Promise<void> { |
|
|
async onLeaveChannel (socket: Socket): Promise<void> { |
|
|
const connect = await this.connectedUserRepository.findOneBy({socket : socket.id}) |
|
|
const connect = await this.connectedUserRepository.findOneBy({ |
|
|
if (connect == null) |
|
|
socket: socket.id |
|
|
return |
|
|
}) |
|
|
|
|
|
if (connect == null) return |
|
|
const channel = await this.chatService.getFullChannel(connect.channel) |
|
|
const channel = await this.chatService.getFullChannel(connect.channel) |
|
|
socket.disconnect() |
|
|
socket.disconnect() |
|
|
if (connect.user == channel.owner.id) { |
|
|
if (connect.user === channel.owner.id) { |
|
|
this.server.in(channel.id.toString()).disconnectSockets() |
|
|
this.server.in(channel.id.toString()).disconnectSockets() |
|
|
this.chatService.removeChannel(channel.id) |
|
|
await this.chatService.removeChannel(channel.id) |
|
|
} else { |
|
|
} else { |
|
|
channel.users = channel.users.filter((e) => e.id !== connect.user) |
|
|
channel.users = channel.users.filter((e) => e.id !== connect.user) |
|
|
} |
|
|
} |
|
@ -126,17 +121,18 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { |
|
|
|
|
|
|
|
|
@SubscribeMessage('kickUser') |
|
|
@SubscribeMessage('kickUser') |
|
|
async onKickUser (socket: Socket, kick: kickUserDto): Promise<void> { |
|
|
async onKickUser (socket: Socket, kick: kickUserDto): Promise<void> { |
|
|
const channel = await this.chatService.getFullChannel(kick.chan); |
|
|
const channel = await this.chatService.getFullChannel(kick.chan) |
|
|
if (channel.owner.id == kick.to) |
|
|
if (channel.owner.id === kick.to) { throw new WsException('You cannot kick the owner of a channel') } |
|
|
throw new WsException('You cannot kick the owner of a channel') |
|
|
|
|
|
if ( |
|
|
if ( |
|
|
channel.owner.id !== kick.from && |
|
|
channel.owner.id !== kick.from && |
|
|
channel.admins.findIndex((usr) => usr.id === kick.from) === -1 |
|
|
channel.admins.findIndex((usr) => usr.id === kick.from) === -1 |
|
|
) { |
|
|
) { |
|
|
throw new WsException('You do not have the required privileges') |
|
|
throw new WsException('You do not have the required privileges') |
|
|
} |
|
|
} |
|
|
const user = await this.userService.findUser(kick.to) as User |
|
|
const user = (await this.userService.findUser(kick.to)) as User |
|
|
const connect = await this.connectedUserRepository.findOneBy({user : user.id}) as ConnectedUser |
|
|
const connect = (await this.connectedUserRepository.findOneBy({ |
|
|
|
|
|
user: user.id |
|
|
|
|
|
})) as ConnectedUser |
|
|
await this.onLeaveChannel(socket) |
|
|
await this.onLeaveChannel(socket) |
|
|
this.server.sockets.sockets.get(connect.socket)?.disconnect() |
|
|
this.server.sockets.sockets.get(connect.socket)?.disconnect() |
|
|
} |
|
|
} |
|
|