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) => {
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<User[]> {
let users = (await this.channelService.getFullChannel(id)).users
users.forEach((u) => u.socketKey = '')
return users;
async getUsersOfChannel (
@Param('id', ParseIntPipe) id: number
): Promise<User[]> {
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<void> {
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<void> {
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<Channel[]> {
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<Channel> {
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
}
}

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'
@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<void> {}
@ -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<void> {
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)
}

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> {
channel.users.push(user)
return await this.ChannelRepository.save(channel)
@ -135,7 +146,7 @@ export class ChatService {
async getFullChannel (id: number): Promise<Channel> {
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<boolean> {
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<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 * 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[][]

4
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");

Loading…
Cancel
Save