Browse Source

had to resolve some old code finding it's way back into the repo

master
Pheuw1 2 years ago
parent
commit
ba75522bcc
  1. 78
      back/volume/src/chat/chat.gateway.ts
  2. 2
      back/volume/src/chat/entity/channel.entity.ts
  3. 2
      back/volume/src/chat/entity/message.entity.ts
  4. 2
      back/volume/src/users/entity/user.entity.ts
  5. 1
      front/volume/src/components/Leaderboard.svelte

78
back/volume/src/chat/chat.gateway.ts

@ -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: {
@ -39,23 +38,17 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
private readonly chatService: ChatService, private readonly chatService: ChatService,
@InjectRepository(ConnectedUser) @InjectRepository(ConnectedUser)
private readonly connectedUserRepository: Repository<ConnectedUser> private readonly connectedUserRepository: Repository<ConnectedUser>
) {} ) {}
async handleConnection (socket: Socket): Promise<void> {} async handleConnection (socket: Socket): Promise<void> {}
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')
@ -73,14 +66,15 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
channel, channel,
user user
) )
const conUser = { const conUser = {
user : user.id, user: user.id,
channel : channel.id, channel: channel.id,
socket: socket.id socket: socket.id
} }
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,19 +90,20 @@ 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 })
const channel = await this.chatService.getFullChannel(connect.channel) if (connect == null) return
socket.disconnect() const channel = await this.chatService.getFullChannel(connect.channel)
if (connect.user == channel.owner.id) { socket.disconnect()
this.server.in(channel.id.toString()).disconnectSockets() if (connect.user === channel.owner.id) {
this.chatService.removeChannel(channel.id) this.server.in(channel.id.toString()).disconnectSockets()
} else { await this.chatService.removeChannel(channel.id)
channel.users = channel.users.filter((e) => e.id !== connect.user) } else {
} channel.users = channel.users.filter((e) => e.id !== connect.user)
await this.connectedUserRepository.delete({ socket : socket.id }) }
} await this.connectedUserRepository.delete({ socket: socket.id })
}
@SubscribeMessage('addMessage') @SubscribeMessage('addMessage')
async onAddMessage (socket: Socket, message: CreateMessageDto): Promise<void> { async onAddMessage (socket: Socket, message: CreateMessageDto): Promise<void> {
@ -126,18 +121,19 @@ 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({
const connect = await this.connectedUserRepository.findOneBy({user : user.id}) as ConnectedUser 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()
} }
} }

2
back/volume/src/chat/entity/channel.entity.ts

@ -40,7 +40,7 @@ export default class Channel {
@JoinTable() @JoinTable()
users: User[] users: User[]
@OneToMany(() => Message, (message: Message) => message.channel, {onDelete:'CASCADE'}) @OneToMany(() => Message, (message: Message) => message.channel, { cascade: true })
messages: Message[] messages: Message[]
@ManyToOne(() => User) @ManyToOne(() => User)

2
back/volume/src/chat/entity/message.entity.ts

@ -22,7 +22,7 @@ export default class Message {
@JoinColumn() @JoinColumn()
author: User author: User
@ManyToOne(() => Channel, (channel) => channel.messages, {onDelete:'CASCADE'} ) @ManyToOne(() => Channel, (channel) => channel.messages)
@JoinTable() @JoinTable()
channel: Channel channel: Channel

2
back/volume/src/users/entity/user.entity.ts

@ -30,7 +30,7 @@ export class User {
@Column({ default: false, nullable: true }) @Column({ default: false, nullable: true })
isVerified: boolean isVerified: boolean
@Column('uuid', { unique: true }) @Column('uuid', { nullable: true, unique: true })
socketKey: string socketKey: string
@Column({ unique: true }) @Column({ unique: true })

1
front/volume/src/components/Leaderboard.svelte

@ -65,7 +65,6 @@
justify-content: center; justify-content: center;
align-items: center; align-items: center;
} }
.history { .history {
background-color: #343A40; background-color: #343A40;
border: 1px solid #198754; border: 1px solid #198754;

Loading…
Cancel
Save