Browse Source

banned

master
Pheuw1 2 years ago
parent
commit
93b0a63e95
  1. 35
      back/volume/src/chat/chat.controller.ts
  2. 17
      back/volume/src/chat/chat.gateway.ts
  3. 18
      back/volume/src/chat/chat.service.ts
  4. 10
      back/volume/src/chat/entity/channel.entity.ts
  5. 4
      front/volume/src/components/Chat.svelte

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

@ -44,15 +44,15 @@ export class ChatController {
} }
const dms = channels.filter((channel: Channel) => { const dms = channels.filter((channel: Channel) => {
return ( return (
(channel.name === (user.ftId + '&' + other.ftId) || (channel.name === user.ftId + '&' + other.ftId ||
channel.name === (other.ftId + '&' + user.ftId)) && channel.name === other.ftId + '&' + user.ftId) &&
channel.isPrivate && channel.isPrivate &&
(channel.password === undefined || channel.password === '') (channel.password === undefined || channel.password === '')
) )
}) })
dms.forEach((c) => { dms.forEach((c) => {
c.users.forEach((u) => u.socketKey = '') c.users.forEach((u) => (u.socketKey = ''))
c.admins.forEach((u) => u.socketKey = '') c.admins.forEach((u) => (u.socketKey = ''))
c.owner.socketKey = '' c.owner.socketKey = ''
}) })
return dms return dms
@ -112,10 +112,12 @@ export class ChatController {
} }
@Get(':id/users') @Get(':id/users')
async getUsersOfChannel (@Param('id', ParseIntPipe) id: number): Promise<User[]> { async getUsersOfChannel (
let users = (await this.channelService.getFullChannel(id)).users @Param('id', ParseIntPipe) id: number
users.forEach((u) => u.socketKey = '') ): Promise<User[]> {
return users; const users = (await this.channelService.getFullChannel(id)).users
users.forEach((u) => (u.socketKey = ''))
return users
} }
@Post(':id/admin') @Post(':id/admin')
@ -165,6 +167,7 @@ export class ChatController {
async addBan ( async addBan (
@Param('id', ParseIntPipe) id: number, @Param('id', ParseIntPipe) id: number,
@Body() target: IdDto, @Body() target: IdDto,
@Body() duration: number,
@Profile42() profile: Profile @Profile42() profile: Profile
): Promise<void> { ): Promise<void> {
const channel = await this.channelService.getFullChannel(id) const channel = await this.channelService.getFullChannel(id)
@ -183,7 +186,7 @@ export class ChatController {
if (await this.channelService.isBanned(channel.id, target.id)) { if (await this.channelService.isBanned(channel.id, target.id)) {
throw new BadRequestException('User is already banned from this channel') 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) await this.channelService.save(channel)
} }
@ -245,7 +248,7 @@ export class ChatController {
@Param('id', ParseIntPipe) id: number @Param('id', ParseIntPipe) id: number
): Promise<void> { ): Promise<void> {
if (await this.channelService.isOwner(id, +profile.id)) { 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) const channel = await this.channelService.getFullChannel(id)
channel.users = channel.users.filter((usr: User) => { channel.users = channel.users.filter((usr: User) => {
@ -256,16 +259,16 @@ export class ChatController {
@Get() @Get()
async getChannelsForUser (@Profile42() profile: Profile): Promise<Channel[]> { async getChannelsForUser (@Profile42() profile: Profile): Promise<Channel[]> {
let chan = await this.channelService.getChannelsForUser(+profile.id) const chan = await this.channelService.getChannelsForUser(+profile.id)
return chan; return chan
} }
@Post() @Post()
async createChannel (@Body() channel: CreateChannelDto): Promise<Channel> { async createChannel (@Body() channel: CreateChannelDto): Promise<Channel> {
let chan = await this.channelService.createChannel(channel) const chan = await this.channelService.createChannel(channel)
chan.users.forEach((u) => u.socketKey = '') chan.users.forEach((u) => (u.socketKey = ''))
chan.admins.forEach((u) => u.socketKey = '') chan.admins.forEach((u) => (u.socketKey = ''))
chan.owner.socketKey = '' chan.owner.socketKey = ''
return chan; return chan
} }
} }

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

@ -17,7 +17,11 @@ import { CreateMessageDto } from './dto/create-message.dto'
import { ConnectionDto } from './dto/connection.dto' import { ConnectionDto } from './dto/connection.dto'
@WebSocketGateway({ @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 { export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer() @WebSocketServer()
@ -26,7 +30,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
constructor ( constructor (
private readonly userService: UsersService, private readonly userService: UsersService,
private readonly messageService: MessageService, private readonly messageService: MessageService,
private readonly chatService: ChatService, private readonly chatService: ChatService
) {} ) {}
async handleConnection (socket: Socket): Promise<void> {} async handleConnection (socket: Socket): Promise<void> {}
@ -45,9 +49,10 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
connect.pwd connect.pwd
) )
const channel = await this.chatService.getFullChannel(connect.ChannelId) 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') throw new WsException('You are banned from entering 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 (
@ -63,13 +68,17 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
) )
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("joinchan succ")
} }
@SubscribeMessage('getMessages') @SubscribeMessage('getMessages')
async onGetMessages (socket: Socket, connect: ConnectionDto): Promise<void> { async onGetMessages (socket: Socket, connect: ConnectionDto): Promise<void> {
const user = await this.userService.getFullUser(connect.UserId) const user = await this.userService.getFullUser(connect.UserId)
const channel = await this.chatService.getFullChannel(connect.ChannelId) 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) this.server.to(socket.id).emit('messages', messages)
} }

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

@ -116,6 +116,17 @@ export class ChatService {
}) })
} }
@Cron('*/6 * * * * *')
async updateBanlists (): Promise<void> {
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> { async addUserToChannel (channel: Channel, user: User): Promise<Channel> {
channel.users.push(user) channel.users.push(user)
return await this.ChannelRepository.save(channel) return await this.ChannelRepository.save(channel)
@ -135,7 +146,7 @@ export class ChatService {
async getFullChannel (id: number): Promise<Channel> { async getFullChannel (id: number): Promise<Channel> {
const channel = await this.ChannelRepository.findOne({ const channel = await this.ChannelRepository.findOne({
where: { id }, where: { id },
relations: ['users', 'admins', 'banned', 'owner'] relations: ['users', 'admins', 'owner']
}) })
if (channel == null) { if (channel == null) {
throw new BadRequestException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
@ -190,13 +201,12 @@ export class ChatService {
async isBanned (id: number, userId: number): Promise<boolean> { async isBanned (id: number, userId: number): Promise<boolean> {
const channel = await this.ChannelRepository.findOne({ const channel = await this.ChannelRepository.findOne({
where: { id }, where: { id }
relations: { banned: true }
}) })
if (channel === null) { if (channel === null) {
throw new BadRequestException(`Channel #${id} not found`) 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<number> { async getMuteDuration (id: number, userId: number): Promise<number> {

10
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 Message from './message.entity'
import * as bcrypt from 'bcrypt' import * as bcrypt from 'bcrypt'
export interface Ban {
user: User
duration: number
}
@Entity() @Entity()
export default class Channel { export default class Channel {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
@ -56,9 +51,8 @@ export default class Channel {
@JoinTable() @JoinTable()
admins: User[] admins: User[]
@ManyToMany(() => User) // refuse connection @Column('text', { array: true, default: [] })
@JoinTable() banned: number[][]
banned: User[]
@Column('text', { array: true, default: [] }) @Column('text', { array: true, default: [] })
muted: number[][] muted: number[][]

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

@ -162,9 +162,9 @@
headers: { headers: {
"Content-Type": "application/json", "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) { if (response.ok) {
alert("User banned"); alert("User banned");

Loading…
Cancel
Save