From ba75522bcca13fc2a839e5ad40d92746c9e2f20b Mon Sep 17 00:00:00 2001 From: Pheuw1 Date: Sat, 18 Mar 2023 19:13:21 +0100 Subject: [PATCH] had to resolve some old code finding it's way back into the repo --- back/volume/src/chat/chat.gateway.ts | 78 +++++++++---------- back/volume/src/chat/entity/channel.entity.ts | 2 +- back/volume/src/chat/entity/message.entity.ts | 2 +- back/volume/src/users/entity/user.entity.ts | 4 +- .../volume/src/components/Leaderboard.svelte | 1 - 5 files changed, 41 insertions(+), 46 deletions(-) diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index dda71b5..f645561 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -19,8 +19,7 @@ import { kickUserDto } from './dto/kickUser.dto' import ConnectedUser from './entity/connection.entity' import { InjectRepository } from '@nestjs/typeorm' import { Repository } from 'typeorm' -import { connect } from 'http2' -import User from 'src/users/entity/user.entity' +import type User from 'src/users/entity/user.entity' @WebSocketGateway({ cors: { @@ -39,23 +38,17 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { private readonly chatService: ChatService, @InjectRepository(ConnectedUser) private readonly connectedUserRepository: Repository - ) {} + ) {} async handleConnection (socket: Socket): Promise {} async handleDisconnect (socket: Socket): Promise { await this.onLeaveChannel(socket) - socket.disconnect() } @SubscribeMessage('joinChannel') async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise { - console.log( - 'User %s is trying to join channel %s', - connect.UserId, - connect.ChannelId, - connect.pwd - ) + console.log('here') const channel = await this.chatService.getFullChannel(connect.ChannelId) if (channel.banned.findIndex((ban) => ban[0] === connect.UserId) !== -1) { throw new WsException('You are banned from entering this channel') @@ -73,14 +66,15 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { channel, user ) - const conUser = { - user : user.id, - channel : channel.id, - socket: socket.id - } - this.connectedUserRepository.create(conUser) + const conUser = { + user: user.id, + channel: channel.id, + socket: socket.id + } + this.connectedUserRepository.create(conUser) this.server.to(socket.id).emit('messages', messages) await socket.join(channel.id.toString()) + console.log(this.server.sockets.adapter.rooms.get(channel.id.toString())) } @SubscribeMessage('getMessages') @@ -96,19 +90,20 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { @SubscribeMessage('leaveChannel') async onLeaveChannel (socket: Socket): Promise { - const connect = await this.connectedUserRepository.findOneBy({socket : socket.id}) - if (connect == null) - return - const channel = await this.chatService.getFullChannel(connect.channel) - socket.disconnect() - if (connect.user == channel.owner.id) { - this.server.in(channel.id.toString()).disconnectSockets() - this.chatService.removeChannel(channel.id) - } else { - channel.users = channel.users.filter((e) => e.id !== connect.user) - } - await this.connectedUserRepository.delete({ socket : socket.id }) - } + const connect = await this.connectedUserRepository.findOneBy({ + socket: socket.id + }) + if (connect == null) return + const channel = await this.chatService.getFullChannel(connect.channel) + socket.disconnect() + if (connect.user === channel.owner.id) { + this.server.in(channel.id.toString()).disconnectSockets() + await this.chatService.removeChannel(channel.id) + } else { + channel.users = channel.users.filter((e) => e.id !== connect.user) + } + await this.connectedUserRepository.delete({ socket: socket.id }) + } @SubscribeMessage('addMessage') async onAddMessage (socket: Socket, message: CreateMessageDto): Promise { @@ -126,18 +121,19 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { @SubscribeMessage('kickUser') async onKickUser (socket: Socket, kick: kickUserDto): Promise { - const channel = await this.chatService.getFullChannel(kick.chan); - if (channel.owner.id == kick.to) - throw new WsException('You cannot kick the owner of a channel') - if ( - channel.owner.id !== kick.from && - channel.admins.findIndex((usr) => usr.id === kick.from) === -1 - ) { - throw new WsException('You do not have the required privileges') - } - const user = await this.userService.findUser(kick.to) as User - const connect = await this.connectedUserRepository.findOneBy({user : user.id}) as ConnectedUser + const channel = await this.chatService.getFullChannel(kick.chan) + if (channel.owner.id === kick.to) { throw new WsException('You cannot kick the owner of a channel') } + if ( + channel.owner.id !== kick.from && + channel.admins.findIndex((usr) => usr.id === kick.from) === -1 + ) { + throw new WsException('You do not have the required privileges') + } + const user = (await this.userService.findUser(kick.to)) as User + const connect = (await this.connectedUserRepository.findOneBy({ + user: user.id + })) as ConnectedUser await this.onLeaveChannel(socket) - this.server.sockets.sockets.get(connect.socket)?.disconnect() + this.server.sockets.sockets.get(connect.socket)?.disconnect() } } diff --git a/back/volume/src/chat/entity/channel.entity.ts b/back/volume/src/chat/entity/channel.entity.ts index da82f44..13cf6ff 100644 --- a/back/volume/src/chat/entity/channel.entity.ts +++ b/back/volume/src/chat/entity/channel.entity.ts @@ -40,7 +40,7 @@ export default class Channel { @JoinTable() users: User[] - @OneToMany(() => Message, (message: Message) => message.channel, {onDelete:'CASCADE'}) + @OneToMany(() => Message, (message: Message) => message.channel, { cascade: true }) messages: Message[] @ManyToOne(() => User) diff --git a/back/volume/src/chat/entity/message.entity.ts b/back/volume/src/chat/entity/message.entity.ts index f338f12..24d3012 100644 --- a/back/volume/src/chat/entity/message.entity.ts +++ b/back/volume/src/chat/entity/message.entity.ts @@ -22,7 +22,7 @@ export default class Message { @JoinColumn() author: User - @ManyToOne(() => Channel, (channel) => channel.messages, {onDelete:'CASCADE'} ) + @ManyToOne(() => Channel, (channel) => channel.messages) @JoinTable() channel: Channel diff --git a/back/volume/src/users/entity/user.entity.ts b/back/volume/src/users/entity/user.entity.ts index d374cf7..077184e 100644 --- a/back/volume/src/users/entity/user.entity.ts +++ b/back/volume/src/users/entity/user.entity.ts @@ -24,13 +24,13 @@ export class User { @Column({ select: false, nullable: true }) authToken: string - @Column({ default: false }) + @Column({ default: false }) twoFA: boolean @Column({ default: false, nullable: true }) isVerified: boolean - @Column('uuid', { unique: true }) + @Column('uuid', { nullable: true, unique: true }) socketKey: string @Column({ unique: true }) diff --git a/front/volume/src/components/Leaderboard.svelte b/front/volume/src/components/Leaderboard.svelte index eb4c0ea..56bbca4 100644 --- a/front/volume/src/components/Leaderboard.svelte +++ b/front/volume/src/components/Leaderboard.svelte @@ -65,7 +65,6 @@ justify-content: center; align-items: center; } - .history { background-color: #343A40; border: 1px solid #198754;