Browse Source

mute ban kick in back fron

master
Pheuw1 2 years ago
parent
commit
7961fc4ba2
  1. 39
      back/volume/src/chat/chat.controller.ts
  2. 19
      back/volume/src/chat/chat.gateway.ts
  3. 13
      back/volume/src/chat/chat.service.ts
  4. 12
      back/volume/src/chat/dto/kickUser.dto.ts
  5. 6
      front/volume/src/components/Chat.svelte

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

@ -87,30 +87,6 @@ export class ChatController {
await this.channelService.save(channel)
}
@Delete(':id/kick')
async removeUser (
@Param('id', ParseIntPipe) id: number,
@Body() target: IdDto,
@Profile42() profile: Profile
): Promise<void> {
const channel = await this.channelService.getFullChannel(id)
if (!(await this.channelService.isAdmin(channel.id, +profile.id))) {
throw new BadRequestException(
'You are not allowed to kick users from this channel'
)
}
if (!(await this.channelService.isUser(channel.id, target.id))) {
throw new BadRequestException('User is not in this channel')
}
if (await this.channelService.isOwner(channel.id, target.id)) {
throw new BadRequestException('You cannot kick the owner of the channel')
}
channel.users = channel.users.filter((usr: User) => {
return usr.ftId !== target.id
})
await this.channelService.save(channel)
}
@Get(':id/users')
async getUsersOfChannel (
@Param('id', ParseIntPipe) id: number
@ -166,27 +142,28 @@ export class ChatController {
@Post(':id/ban')
async addBan (
@Param('id', ParseIntPipe) id: number,
@Body() target: IdDto,
@Body() duration: number,
@Body() target: MuteDto,
@Profile42() profile: Profile
): Promise<void> {
const channel = await this.channelService.getFullChannel(id)
const user: User | null = await this.usersService.findUser(target.id)
const user: User | null = await this.usersService.findUser(target.data[0])
console.log(target)
if (user == null) {
throw new NotFoundException(`User #${target.id} not found`)
throw new NotFoundException(`User #${target.data[0]} not found`)
}
if (!(await this.channelService.isAdmin(channel.id, +profile.id))) {
throw new BadRequestException(
'You are not allowed to ban users from this channel'
)
}
if (await this.channelService.isOwner(channel.id, target.id)) {
if (await this.channelService.isOwner(channel.id, target.data[0])) {
throw new BadRequestException('You cannot ban the owner of the channel')
}
if (await this.channelService.isBanned(channel.id, target.id)) {
if (await this.channelService.isBanned(channel.id, target.data[0])) {
throw new BadRequestException('User is already banned from this channel')
}
channel.banned.push([user.id, duration])
channel.banned.push([target.data[0], Date.now() + target.data[1] * 1000])
console.log(channel.banned)
await this.channelService.save(channel)
}

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

@ -15,6 +15,7 @@ import * as bcrypt from 'bcrypt'
import { MessageService } from './message.service'
import { CreateMessageDto } from './dto/create-message.dto'
import { ConnectionDto } from './dto/connection.dto'
import { kickUserDto } from './dto/kickUser.dto'
@WebSocketGateway({
cors: {
@ -52,7 +53,6 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
if (channel.banned.findIndex((ban) => ban[0] === connect.UserId) !== -1) {
throw new WsException('You are banned from entering this channel')
}
const user = await this.userService.getFullUser(connect.UserId)
if (channel.password && channel.password !== '') {
if (
@ -68,7 +68,6 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
)
this.server.to(socket.id).emit('messages', messages)
await socket.join(channel.id.toString())
console.log("joinchan succ")
}
@SubscribeMessage('getMessages')
@ -84,7 +83,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@SubscribeMessage('leaveChannel')
async onLeaveChannel (socket: Socket): Promise<void> {
const id = socket.id as any
socket.disconnect()
}
@SubscribeMessage('addMessage')
@ -102,16 +101,12 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
}
@SubscribeMessage('kickUser')
async onKickUser (
socket: Socket,
chan: number,
from: number,
to: number
): Promise<void> {
const channel = await this.chatService.getChannel(chan)
async onKickUser (socket: Socket, msg: kickUserDto): Promise<void> {
console.log('kick called')
const channel = await this.chatService.getFullChannel(msg.chan)
if (
channel.owner.id !== from ||
channel.admins.find((e) => e.id === from) == null
channel.owner.id !== msg.from &&
channel.admins.find((e) => e.id == msg.from) == null
) {
throw new WsException('You do not have the required privileges')
}

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

@ -40,8 +40,8 @@ export class ChatService {
const channels = await this.getChannelsForUser(user.id)
const dmAlreadyExists = channels.find((channel: Channel) => {
return (
(channel.name === (user.ftId + '&' + otherUser.ftId) ||
channel.name === (otherUser.ftId + '&' + user.ftId)) &&
(channel.name === user.ftId + '&' + otherUser.ftId ||
channel.name === otherUser.ftId + '&' + user.ftId) &&
channel.isPrivate &&
(channel.password === undefined || channel.password === '')
)
@ -110,7 +110,7 @@ export class ChatService {
const channels = await this.ChannelRepository.find({})
channels.forEach((channel) => {
channel.muted = channel.muted.filter((data) => {
return data[0] - Date.now() > 0
return data[1] - Date.now() > 0
})
void this.ChannelRepository.save(channel)
})
@ -118,13 +118,14 @@ export class ChatService {
@Cron('*/6 * * * * *')
async updateBanlists (): Promise<void> {
console.log('checking bans')
const channels = await this.ChannelRepository.find({})
channels.forEach((channel) => {
for (const channel of channels) {
console.log((channel.banned.length) > 0)
channel.banned = channel.banned.filter((data) => {
return data[1] - Date.now() > 0
})
void this.ChannelRepository.save(channel)
})
}
}
async addUserToChannel (channel: Channel, user: User): Promise<Channel> {

12
back/volume/src/chat/dto/kickUser.dto.ts

@ -0,0 +1,12 @@
import { IsNumber } from 'class-validator'
export class kickUserDto {
@IsNumber()
chan: number
@IsNumber()
from: number
@IsNumber()
to: number
}

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

@ -162,7 +162,7 @@
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: target.ftId, duration: duration}),
body: JSON.stringify({ data: [target.ftId, duration]}),
});
socket.emit("kickUser", channel.id, $store.ftId, target.ftId);
}
@ -203,8 +203,8 @@
});
if (response.ok) {
const target = await response.json();
socket.emit("kickUser", channel.id, $store.ftId, target.ftId);
}
socket.emit("kickUser", {chan : channel.id, from : $store.ftId, to: target.ftId});
} else {alert("merde")}
};
//--------------------------------------------------------------------------------/

Loading…
Cancel
Save