|
@ -19,6 +19,7 @@ import { InjectRepository } from '@nestjs/typeorm' |
|
|
import { Repository } from 'typeorm' |
|
|
import { Repository } from 'typeorm' |
|
|
import ConnectedUser from './entity/connection.entity' |
|
|
import ConnectedUser from './entity/connection.entity' |
|
|
import { ConnectionDto } from './dto/connection.dto' |
|
|
import { ConnectionDto } from './dto/connection.dto' |
|
|
|
|
|
import { plainToClass } from 'class-transformer' |
|
|
|
|
|
|
|
|
@WebSocketGateway({ |
|
|
@WebSocketGateway({ |
|
|
cors: { origin: /^(http|ws):\/\/localhost(:\d+)?$/ } |
|
|
cors: { origin: /^(http|ws):\/\/localhost(:\d+)?$/ } |
|
@ -43,32 +44,42 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { |
|
|
|
|
|
|
|
|
@SubscribeMessage('joinChannel') |
|
|
@SubscribeMessage('joinChannel') |
|
|
async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise<void> { |
|
|
async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise<void> { |
|
|
console.log(connect.ChannelId, connect.UserId, connect.pwd) |
|
|
console.log( |
|
|
|
|
|
'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.find((ban) => ban.id === connect.UserId) !== null) { |
|
|
console.log('1') |
|
|
|
|
|
if (channel.banned.findIndex((ban) => ban.ftId === connect.UserId) !== -1) { |
|
|
throw new WsException('You are banned from entering this channel') |
|
|
throw new WsException('You are banned from entering this channel') |
|
|
} |
|
|
} |
|
|
const user = (await this.userService.findUser(connect.UserId)) as User |
|
|
console.log('2') |
|
|
if ( channel.password !== '' && |
|
|
const user = await this.userService.getFullUser(connect.UserId) |
|
|
!(await bcrypt.compare(channel.password, connect.pwd)) |
|
|
console.log('3') |
|
|
) { |
|
|
console.log('Channel psw: ', channel.password) |
|
|
throw new WsException('Wrong password') |
|
|
if (channel.password && channel.password !== '') { |
|
|
|
|
|
if ( |
|
|
|
|
|
!connect.pwd || |
|
|
|
|
|
!(await bcrypt.compare(connect.pwd, channel.password)) |
|
|
|
|
|
) { |
|
|
|
|
|
throw new WsException('Wrong password') |
|
|
|
|
|
} |
|
|
} else await this.chatService.addUserToChannel(channel, user) |
|
|
} else await this.chatService.addUserToChannel(channel, user) |
|
|
|
|
|
console.log('5') |
|
|
{ |
|
|
const conUser = { |
|
|
const conUser = new ConnectedUser() |
|
|
user, |
|
|
conUser.user = user |
|
|
channel, |
|
|
conUser.channel = channel |
|
|
socket: socket.id |
|
|
conUser.socket = socket.id |
|
|
|
|
|
await this.connectedUserRepository.save(conUser) |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
this.connectedUserRepository.create(conUser) |
|
|
const messages = await this.messageService.findMessagesInChannelForUser( |
|
|
const messages = await this.messageService.findMessagesInChannelForUser( |
|
|
channel, |
|
|
channel, |
|
|
user |
|
|
user |
|
|
) |
|
|
) |
|
|
this.server.to(socket.id).emit('messages', messages) |
|
|
this.server.to(socket.id).emit('messages', messages) |
|
|
await socket.join(channel.name) |
|
|
await socket.join(channel.id.toString()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@SubscribeMessage('leaveChannel') |
|
|
@SubscribeMessage('leaveChannel') |
|
@ -80,7 +91,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { |
|
|
|
|
|
|
|
|
@SubscribeMessage('addMessage') |
|
|
@SubscribeMessage('addMessage') |
|
|
async onAddMessage (socket: Socket, message: CreateMessageDto): Promise<void> { |
|
|
async onAddMessage (socket: Socket, message: CreateMessageDto): Promise<void> { |
|
|
console.log(JSON.stringify(message)); |
|
|
console.log(JSON.stringify(message)) |
|
|
const channel = await this.chatService.getChannel(message.ChannelId) |
|
|
const channel = await this.chatService.getChannel(message.ChannelId) |
|
|
if ( |
|
|
if ( |
|
|
(await this.chatService.getMuteDuration(channel.id, message.UserId)) > 0 |
|
|
(await this.chatService.getMuteDuration(channel.id, message.UserId)) > 0 |
|
@ -90,7 +101,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { |
|
|
const createdMessage: Message = await this.messageService.createMessage( |
|
|
const createdMessage: Message = await this.messageService.createMessage( |
|
|
message |
|
|
message |
|
|
) |
|
|
) |
|
|
socket.in(channel.name).emit('newMessage', createdMessage) |
|
|
socket.in(channel.toString()).emit('newMessage', createdMessage) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@SubscribeMessage('kickUser') |
|
|
@SubscribeMessage('kickUser') |
|
|