From 93b0a63e956503ba44cd8b57f02c54e2128fa61a Mon Sep 17 00:00:00 2001 From: Pheuw1 Date: Fri, 17 Mar 2023 15:49:12 +0100 Subject: [PATCH] banned --- back/volume/src/chat/chat.controller.ts | 35 ++++++++++--------- back/volume/src/chat/chat.gateway.ts | 17 ++++++--- back/volume/src/chat/chat.service.ts | 18 +++++++--- back/volume/src/chat/entity/channel.entity.ts | 10 ++---- front/volume/src/components/Chat.svelte | 4 +-- 5 files changed, 50 insertions(+), 34 deletions(-) diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index 3c9b907..2f7a53c 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -44,15 +44,15 @@ export class ChatController { } const dms = channels.filter((channel: Channel) => { return ( - (channel.name === (user.ftId + '&' + other.ftId) || - channel.name === (other.ftId + '&' + user.ftId)) && + (channel.name === user.ftId + '&' + other.ftId || + channel.name === other.ftId + '&' + user.ftId) && channel.isPrivate && (channel.password === undefined || channel.password === '') ) }) dms.forEach((c) => { - c.users.forEach((u) => u.socketKey = '') - c.admins.forEach((u) => u.socketKey = '') + c.users.forEach((u) => (u.socketKey = '')) + c.admins.forEach((u) => (u.socketKey = '')) c.owner.socketKey = '' }) return dms @@ -112,10 +112,12 @@ export class ChatController { } @Get(':id/users') - async getUsersOfChannel (@Param('id', ParseIntPipe) id: number): Promise { - let users = (await this.channelService.getFullChannel(id)).users - users.forEach((u) => u.socketKey = '') - return users; + async getUsersOfChannel ( + @Param('id', ParseIntPipe) id: number + ): Promise { + const users = (await this.channelService.getFullChannel(id)).users + users.forEach((u) => (u.socketKey = '')) + return users } @Post(':id/admin') @@ -165,6 +167,7 @@ export class ChatController { async addBan ( @Param('id', ParseIntPipe) id: number, @Body() target: IdDto, + @Body() duration: number, @Profile42() profile: Profile ): Promise { const channel = await this.channelService.getFullChannel(id) @@ -183,7 +186,7 @@ export class ChatController { if (await this.channelService.isBanned(channel.id, target.id)) { throw new BadRequestException('User is already banned from this channel') } - channel.banned.push(user) + channel.banned.push([user.id, duration]) await this.channelService.save(channel) } @@ -245,7 +248,7 @@ export class ChatController { @Param('id', ParseIntPipe) id: number ): Promise { if (await this.channelService.isOwner(id, +profile.id)) { - this.channelService.removeChannel(id) // -> verify that the deletion the break others users behaviors. + await this.channelService.removeChannel(id) // -> verify that the deletion the break others users behaviors. } const channel = await this.channelService.getFullChannel(id) channel.users = channel.users.filter((usr: User) => { @@ -256,16 +259,16 @@ export class ChatController { @Get() async getChannelsForUser (@Profile42() profile: Profile): Promise { - let chan = await this.channelService.getChannelsForUser(+profile.id) - return chan; + const chan = await this.channelService.getChannelsForUser(+profile.id) + return chan } @Post() async createChannel (@Body() channel: CreateChannelDto): Promise { - let chan = await this.channelService.createChannel(channel) - chan.users.forEach((u) => u.socketKey = '') - chan.admins.forEach((u) => u.socketKey = '') + const chan = await this.channelService.createChannel(channel) + chan.users.forEach((u) => (u.socketKey = '')) + chan.admins.forEach((u) => (u.socketKey = '')) chan.owner.socketKey = '' - return chan; + return chan } } diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index 7178b11..8ca26ab 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -17,7 +17,11 @@ import { CreateMessageDto } from './dto/create-message.dto' import { ConnectionDto } from './dto/connection.dto' @WebSocketGateway({ - cors: { origin: new RegExp(`^(http|ws)://${process.env.HOST ?? 'localhost'}(:\\d+)?$`) } + cors: { + origin: new RegExp( + `^(http|ws)://${process.env.HOST ?? 'localhost'}(:\\d+)?$` + ) + } }) export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { @WebSocketServer() @@ -26,7 +30,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { constructor ( private readonly userService: UsersService, private readonly messageService: MessageService, - private readonly chatService: ChatService, + private readonly chatService: ChatService ) {} async handleConnection (socket: Socket): Promise {} @@ -45,9 +49,10 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { connect.pwd ) const channel = await this.chatService.getFullChannel(connect.ChannelId) - if (channel.banned.findIndex((ban) => ban.ftId === connect.UserId) !== -1) { + 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 ( @@ -63,13 +68,17 @@ 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') async onGetMessages (socket: Socket, connect: ConnectionDto): Promise { const user = await this.userService.getFullUser(connect.UserId) const channel = await this.chatService.getFullChannel(connect.ChannelId) - const messages = await this.messageService.findMessagesInChannelForUser(channel, user) + const messages = await this.messageService.findMessagesInChannelForUser( + channel, + user + ) this.server.to(socket.id).emit('messages', messages) } diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index c4068e8..e40f8bc 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -116,6 +116,17 @@ export class ChatService { }) } + @Cron('*/6 * * * * *') + async updateBanlists (): Promise { + const channels = await this.ChannelRepository.find({}) + channels.forEach((channel) => { + 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.users.push(user) return await this.ChannelRepository.save(channel) @@ -135,7 +146,7 @@ export class ChatService { async getFullChannel (id: number): Promise { const channel = await this.ChannelRepository.findOne({ where: { id }, - relations: ['users', 'admins', 'banned', 'owner'] + relations: ['users', 'admins', 'owner'] }) if (channel == null) { throw new BadRequestException(`Channel #${id} not found`) @@ -190,13 +201,12 @@ export class ChatService { async isBanned (id: number, userId: number): Promise { const channel = await this.ChannelRepository.findOne({ - where: { id }, - relations: { banned: true } + where: { id } }) if (channel === null) { throw new BadRequestException(`Channel #${id} not found`) } - return channel.banned.findIndex((user) => user.ftId === userId) !== -1 + return channel.banned.findIndex((ban) => ban[0] === userId) !== -1 } async getMuteDuration (id: number, userId: number): Promise { diff --git a/back/volume/src/chat/entity/channel.entity.ts b/back/volume/src/chat/entity/channel.entity.ts index 7e3c3e9..94e9d30 100644 --- a/back/volume/src/chat/entity/channel.entity.ts +++ b/back/volume/src/chat/entity/channel.entity.ts @@ -13,11 +13,6 @@ import User from 'src/users/entity/user.entity' import Message from './message.entity' import * as bcrypt from 'bcrypt' -export interface Ban { - user: User - duration: number -} - @Entity() export default class Channel { @PrimaryGeneratedColumn() @@ -56,9 +51,8 @@ export default class Channel { @JoinTable() admins: User[] - @ManyToMany(() => User) // refuse connection - @JoinTable() - banned: User[] + @Column('text', { array: true, default: [] }) + banned: number[][] @Column('text', { array: true, default: [] }) muted: number[][] diff --git a/front/volume/src/components/Chat.svelte b/front/volume/src/components/Chat.svelte index 3d8ec20..e839ff4 100644 --- a/front/volume/src/components/Chat.svelte +++ b/front/volume/src/components/Chat.svelte @@ -162,9 +162,9 @@ headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ id: target.ftId }), + body: JSON.stringify({ id: target.ftId, duration: duration}), }); - socket.emit("kickUser", channel.id, $store.ftId, target.ftId, duration); + socket.emit("kickUser", channel.id, $store.ftId, target.ftId); } if (response.ok) { alert("User banned");