Browse Source

* Fixed kicks

master
vvandenb 2 years ago
parent
commit
cc72d16289
  1. 1
      back/volume/src/chat/chat.controller.ts
  2. 83
      back/volume/src/chat/chat.gateway.ts
  3. 4
      back/volume/src/chat/chat.service.ts
  4. 2
      front/volume/src/components/Chat.svelte

1
back/volume/src/chat/chat.controller.ts

@ -160,7 +160,6 @@ export class ChatController {
if (await this.channelService.isBanned(channel.id, target.data[0])) { if (await this.channelService.isBanned(channel.id, target.data[0])) {
throw new BadRequestException('User is already banned from this channel') throw new BadRequestException('User is already banned from this channel')
} }
console.log(channel.banned)
channel.banned.push([target.data[0], Date.now() + target.data[1] * 1000]) channel.banned.push([target.data[0], Date.now() + target.data[1] * 1000])
await this.channelService.save(channel) await this.channelService.save(channel)
} }

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

@ -5,26 +5,26 @@ import {
WebSocketGateway, WebSocketGateway,
WebSocketServer, WebSocketServer,
WsException, WsException,
} from "@nestjs/websockets"; } from '@nestjs/websockets';
import { Socket, Server } from "socket.io"; import { Socket, Server } from 'socket.io';
// import { User } from 'users/user.entity'; // import { User } from 'users/user.entity';
import { UsersService } from "src/users/users.service"; import { UsersService } from 'src/users/users.service';
import { ChatService } from "./chat.service"; import { ChatService } from './chat.service';
import type Message from "./entity/message.entity"; import type Message from './entity/message.entity';
import * as bcrypt from "bcrypt"; import * as bcrypt from 'bcrypt';
import { MessageService } from "./message.service"; import { MessageService } from './message.service';
import { CreateMessageDto } from "./dto/create-message.dto"; import { CreateMessageDto } from './dto/create-message.dto';
import { ConnectionDto } from "./dto/connection.dto"; import { ConnectionDto } from './dto/connection.dto';
import { kickUserDto } from "./dto/kickUser.dto"; 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 type User from "src/users/entity/user.entity"; import type User from 'src/users/entity/user.entity';
@WebSocketGateway({ @WebSocketGateway({
cors: { cors: {
origin: new RegExp( origin: new RegExp(
`^(http|ws)://${process.env.HOST ?? "localhost"}(:\\d+)?$` `^(http|ws)://${process.env.HOST ?? 'localhost'}(:\\d+)?$`
), ),
}, },
}) })
@ -44,31 +44,34 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
async handleDisconnect(socket: Socket): Promise<void> { async handleDisconnect(socket: Socket): Promise<void> {
const connect = await this.connectedUserRepository.findOneBy({ const connect = await this.connectedUserRepository.findOneBy({
socket: socket.id, socket: socket.id
}); });
if (connect) { if (connect) {
await this.connectedUserRepository.delete({ socket: socket.id }); await this.connectedUserRepository.delete({ user: connect.user })
} }
socket.disconnect(); socket.disconnect()
console.log("socket %s has disconnected", socket.id); console.log('socket %s has disconnected', socket.id)
} }
@SubscribeMessage("joinChannel") @SubscribeMessage('joinChannel')
async onJoinChannel(socket: Socket, connect: ConnectionDto): Promise<void> { async onJoinChannel(socket: Socket, connect: ConnectionDto): Promise<void> {
console.log("here"); await this.connectedUserRepository.delete({ user: connect.UserId })
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) {
this.server this.server
.to(socket.id) .to(socket.id)
.emit("failedJoin", "You are banned from this channel"); .emit('failedJoin', 'You are banned from this channel');
throw new WsException('You are banned from this channel');
} }
const user = await this.userService.getFullUser(connect.UserId); const user = await this.userService.getFullUser(connect.UserId);
if (channel.password && channel.password !== "") { if (channel.password && channel.password !== '') {
if ( if (
!connect.pwd || !connect.pwd ||
!(await bcrypt.compare(connect.pwd, channel.password)) !(await bcrypt.compare(connect.pwd, channel.password))
) { ) {
this.server.to(socket.id).emit("failedJoin", "Wrong password"); this.server.to(socket.id).emit('failedJoin', 'Wrong password');
throw new WsException('Wrong password');
} }
} }
await this.chatService.addUserToChannel(channel, user); await this.chatService.addUserToChannel(channel, user);
@ -81,18 +84,15 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
conUser.channel = channel.id; conUser.channel = channel.id;
conUser.socket = socket.id; conUser.socket = socket.id;
const test = await this.connectedUserRepository.save(conUser); const test = await this.connectedUserRepository.save(conUser);
console.log(test);
await socket.join(channel.id.toString()); await socket.join(channel.id.toString());
this.server.to(socket.id).emit("messages", messages); this.server.to(socket.id).emit('messages', messages);
console.log(this.server.sockets.adapter.rooms.get(channel.id.toString()));
} }
@SubscribeMessage("leaveChannel") @SubscribeMessage('leaveChannel')
async onLeaveChannel(socket: Socket): Promise<void> { async onLeaveChannel(socket: Socket): Promise<void> {
const connect = await this.connectedUserRepository.findOneBy({ const connect = await this.connectedUserRepository.findOneBy({
socket: socket.id, socket: socket.id,
}); });
console.log("connection removed", connect?.user);
if (connect == null) 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();
@ -105,37 +105,38 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
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> {
const channel = await this.chatService.getChannel(message.ChannelId); const channel = await this.chatService.getChannel(message.ChannelId);
if (await this.chatService.isMuted(channel.id, message.UserId)) { if (await this.chatService.isMuted(channel.id, message.UserId)) {
throw new WsException("You are muted"); throw new WsException('You are muted');
} }
const createdMessage: Message = await this.messageService.createMessage( const createdMessage: Message = await this.messageService.createMessage(
message message
); );
this.server.to(channel.id.toString()).emit("newMessage", createdMessage); this.server.to(channel.id.toString()).emit('newMessage', createdMessage);
} }
@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.ftId === kick.to) { if (channel.owner.ftId === 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.ftId !== kick.from && channel.owner.ftId !== kick.from &&
channel.admins.findIndex((usr) => +usr.ftId === kick.from) === -1 channel.admins.findIndex((usr) => +usr.ftId === 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.ftId, user: user.ftId
})) as ConnectedUser; })) as ConnectedUser
if (connect) { if (connect) {
console.log(`kicking ${user.username} from ${channel.name}`); console.log(`kicking ${user.username} from ${channel.name} with socket ${connect.socket}`)
this.server.sockets.sockets.get(connect.socket)?.emit("kicked"); // this.server.sockets.sockets.get(connect.socket)?.emit('kicked');
this.server.to(connect.socket).emit('kicked')
} }
} }
} }

4
back/volume/src/chat/chat.service.ts

@ -207,7 +207,7 @@ export class ChatService {
if (channel === null) { if (channel === null) {
throw new BadRequestException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }
return channel.banned.findIndex((ban) => ban[0] === userId) !== -1 return channel.banned.findIndex((ban) => +ban[0] === userId) !== -1
} }
async isMuted (id: number, userId: number): Promise<boolean> { async isMuted (id: number, userId: number): Promise<boolean> {
@ -217,6 +217,6 @@ export class ChatService {
if (channel === null) { if (channel === null) {
throw new BadRequestException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }
return channel.muted.findIndex((mute) => mute[0] === userId) !== -1 return channel.muted.findIndex((mute) => +mute[0] === userId) !== -1
} }
} }

2
front/volume/src/components/Chat.svelte

@ -338,7 +338,7 @@
}); });
} }
if (response.ok) { if (response.ok) {
await show_popup("User admined", false); await show_popup("User de-admined", false);
} else { } else {
await show_popup("Failed to admin user", false); await show_popup("Failed to admin user", false);
} }

Loading…
Cancel
Save