Browse Source

linting

linting
master
nicolas-arnaud 2 years ago
parent
commit
1fdbbd376b
  1. 105
      back/volume/src/chat/chat.controller.ts
  2. 42
      back/volume/src/chat/chat.gateway.ts
  3. 3
      back/volume/src/chat/entity/channel.entity.ts

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

@ -6,6 +6,7 @@ import {
Get, Get,
NotFoundException, NotFoundException,
Param, Param,
ParseIntPipe,
Post, Post,
UseGuards UseGuards
} from '@nestjs/common' } from '@nestjs/common'
@ -22,6 +23,7 @@ import { Profile42 } from 'src/auth/42.decorator'
import { Profile } from 'passport-42' import { Profile } from 'passport-42'
@Controller('channels') @Controller('channels')
@UseGuards(AuthenticatedGuard)
export class ChatController { export class ChatController {
constructor ( constructor (
private readonly channelService: ChatService, private readonly channelService: ChatService,
@ -29,51 +31,44 @@ export class ChatController {
) {} ) {}
@Post(':id/invite') @Post(':id/invite')
@UseGuards(AuthenticatedGuard)
async addUser ( async addUser (
@Param('id') id: number, @Param('id', ParseIntPipe) id: number,
@Body() target: IdDto, @Body() target: IdDto,
@Profile42() profile: Profile @Profile42() profile: Profile
) { ): Promise<void> {
const channel = await this.channelService.getFullChannel(id) 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.id)
if (user == null) { if (user == null)
throw new NotFoundException(`User #${target.id} not found`) throw new NotFoundException(`User #${target.id} not found`)
}
if (!(await this.channelService.isUser(channel.id, +profile.id))) { if (!(await this.channelService.isUser(channel.id, +profile.id))) {
throw new BadRequestException( throw new BadRequestException(
'You are not allowed to invite users to this channel' 'You are not allowed to invite users to this channel'
) )
} }
if (await this.channelService.isUser(channel.id, target.id)) { if (await this.channelService.isUser(channel.id, target.id))
throw new BadRequestException('User is already in this channel') throw new BadRequestException('User is already in this channel')
} if (await this.channelService.isBanned(channel.id, target.id))
if (await this.channelService.isBanned(channel.id, target.id)) {
throw new BadRequestException('User is banned from this channel') throw new BadRequestException('User is banned from this channel')
}
channel.users.push(user) channel.users.push(user)
this.channelService.save(channel) this.channelService.save(channel)
} }
@Delete(':id/kick') @Delete(':id/kick')
@UseGuards(AuthenticatedGuard)
async removeUser ( async removeUser (
@Param('id') id: number, @Param('id', ParseIntPipe) id: number,
@Body() target: IdDto, @Body() target: IdDto,
@Profile42() profile: Profile @Profile42() profile: Profile
) { ): Promise<void> {
const channel = await this.channelService.getFullChannel(id) const channel = await this.channelService.getFullChannel(id)
if (!(await this.channelService.isAdmin(channel.id, +profile.id))) { if (!(await this.channelService.isAdmin(channel.id, +profile.id))) {
throw new BadRequestException( throw new BadRequestException(
'You are not allowed to kick users from this channel' 'You are not allowed to kick users from this channel'
) )
} }
if (!(await this.channelService.isUser(channel.id, target.id))) { if (!(await this.channelService.isUser(channel.id, target.id)))
throw new BadRequestException('User is not in this channel') throw new BadRequestException('User is not in this channel')
} if (await this.channelService.isOwner(channel.id, target.id))
if (await this.channelService.isOwner(channel.id, target.id)) {
throw new BadRequestException('You cannot kick the owner of the channel') throw new BadRequestException('You cannot kick the owner of the channel')
}
channel.users = channel.users.filter((usr: User) => { channel.users = channel.users.filter((usr: User) => {
return usr.ftId !== target.id return usr.ftId !== target.id
}) })
@ -81,44 +76,36 @@ export class ChatController {
} }
@Post(':id/admin') @Post(':id/admin')
@UseGuards(AuthenticatedGuard)
async addAdmin ( async addAdmin (
@Param('id') id: number, @Param('id', ParseIntPipe) id: number,
@Body() target: IdDto, @Body() target: IdDto,
@Profile42() profile: Profile @Profile42() profile: Profile
) { ): Promise<void> {
const channel = await this.channelService.getFullChannel(id) 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.id)
if (user == null) { if (user == null)
throw new NotFoundException(`User #${target.id} not found`) throw new NotFoundException(`User #${target.id} not found`)
} if (!(await this.channelService.isOwner(channel.id, +profile.id)))
if (!(await this.channelService.isOwner(channel.id, +profile.id))) {
throw new BadRequestException('You are not the owner of this channel') throw new BadRequestException('You are not the owner of this channel')
} if (!(await this.channelService.isUser(channel.id, target.id)))
if (!(await this.channelService.isUser(channel.id, target.id))) {
throw new BadRequestException('User is not in this channel') throw new BadRequestException('User is not in this channel')
} if (await this.channelService.isAdmin(channel.id, target.id))
if (await this.channelService.isAdmin(channel.id, target.id)) {
throw new BadRequestException('User is already an admin of this channel') throw new BadRequestException('User is already an admin of this channel')
}
channel.admins.push(user) channel.admins.push(user)
this.channelService.save(channel) this.channelService.save(channel)
} }
@Delete(':id/admin') @Delete(':id/admin')
@UseGuards(AuthenticatedGuard)
async removeAdmin ( async removeAdmin (
@Param('id') id: number, @Param('id', ParseIntPipe) id: number,
@Body() target: IdDto, @Body() target: IdDto,
@Profile42() profile: Profile @Profile42() profile: Profile
) { ): Promise<void> {
const channel = await this.channelService.getFullChannel(id) const channel = await this.channelService.getFullChannel(id)
if (!(await this.channelService.isOwner(channel.id, +profile.id))) { if (!(await this.channelService.isOwner(channel.id, +profile.id)))
throw new BadRequestException('You are not the owner of this channel') throw new BadRequestException('You are not the owner of this channel')
} if (!(await this.channelService.isAdmin(channel.id, target.id)))
if (!(await this.channelService.isAdmin(channel.id, target.id))) {
throw new BadRequestException('User is not an admin of this channel') throw new BadRequestException('User is not an admin of this channel')
}
channel.admins = channel.admins.filter((usr: User) => { channel.admins = channel.admins.filter((usr: User) => {
return usr.ftId !== target.id return usr.ftId !== target.id
}) })
@ -126,92 +113,80 @@ export class ChatController {
} }
@Post(':id/ban') @Post(':id/ban')
@UseGuards(AuthenticatedGuard)
async addBan ( async addBan (
@Param('id') id: number, @Param('id', ParseIntPipe) id: number,
@Body() target: IdDto, @Body() target: IdDto,
@Profile42() profile: Profile @Profile42() profile: Profile
) { ):Promise<void> {
const channel = await this.channelService.getFullChannel(id) 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.id)
if (user == null) { if (user == null)
throw new NotFoundException(`User #${target.id} not found`) throw new NotFoundException(`User #${target.id} not found`)
}
if (!(await this.channelService.isAdmin(channel.id, +profile.id))) { if (!(await this.channelService.isAdmin(channel.id, +profile.id))) {
throw new BadRequestException( throw new BadRequestException(
'You are not allowed to ban users from this channel' '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.id))
throw new BadRequestException('You cannot ban the owner of the channel') 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.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)
this.channelService.save(channel) this.channelService.save(channel)
} }
@Post(':id/mute') @Post(':id/mute')
@UseGuards(AuthenticatedGuard)
async addMute ( async addMute (
@Param('id') id: number, @Param('id', ParseIntPipe) id: number,
@Body() mute: MuteDto, // [userId, duration] @Body() mute: MuteDto, // [userId, duration]
@Profile42() profile: Profile @Profile42() profile: Profile
) { ): Promise<void> {
const channel = await this.channelService.getFullChannel(id) const channel = await this.channelService.getFullChannel(id)
const user: User | null = await this.usersService.findUser(mute.data[0]) const user: User | null = await this.usersService.findUser(mute.data[0])
if (user == null) { if (user == null)
throw new NotFoundException(`User #${mute.data[0]} not found`) throw new NotFoundException(`User #${mute.data[0]} not found`)
}
if (!(await this.channelService.isAdmin(channel.id, +profile.id))) { if (!(await this.channelService.isAdmin(channel.id, +profile.id))) {
throw new BadRequestException( throw new BadRequestException(
'You are not allowed to mute users from this channel' 'You are not allowed to mute users from this channel'
) )
} }
if (await this.channelService.isOwner(channel.id, mute.data[0])) { if (await this.channelService.isOwner(channel.id, mute.data[0]))
throw new BadRequestException('You cannot mute the owner of the channel') throw new BadRequestException('You cannot mute the owner of the channel')
} if (await this.channelService.getMuteDuration(channel.id, mute.data[0]) > 0)
if (
(await this.channelService.getMuteDuration(channel.id, mute.data[0])) > 0
) {
throw new BadRequestException('User is already muted from this channel') throw new BadRequestException('User is already muted from this channel')
}
const newMute: number[] = [mute.data[0], Date.now() + mute.data[1] * 1000] const newMute: number[] = [mute.data[0], Date.now() + mute.data[1] * 1000]
channel.muted.push(newMute) channel.muted.push(newMute)
this.channelService.save(channel) this.channelService.save(channel)
} }
@Delete(':id') @Delete(':id')
@UseGuards(AuthenticatedGuard) async deleteChannel (
async deleteChannel (@Profile42() profile: Profile, @Param('id') id: number) { @Profile42() profile: Profile,
if (!(await this.channelService.isOwner(id, +profile.id))) { @Param('id', ParseIntPipe) id: number
): Promise<void> {
if (!(await this.channelService.isOwner(id, +profile.id)))
throw new BadRequestException('You are not the owner of this channel') throw new BadRequestException('You are not the owner of this channel')
}
await this.channelService.removeChannel(id) await this.channelService.removeChannel(id)
} }
@Post(':id/password') @Post(':id/password')
@UseGuards(AuthenticatedGuard)
async updatePassword ( async updatePassword (
@Profile42() profile: Profile, @Profile42() profile: Profile,
@Param('id') id: number, @Param('id', ParseIntPipe) id: number,
@Body() data: PasswordDto @Body() data: PasswordDto
) { ): Promise<void> {
if (await this.channelService.isOwner(id, +profile.id)) { if (await this.channelService.isOwner(id, +profile.id))
throw new BadRequestException('You are not the owner of this channel') throw new BadRequestException('You are not the owner of this channel')
}
await this.channelService.updatePassword(id, data.password) await this.channelService.updatePassword(id, data.password)
} }
@Get() @Get()
@UseGuards(AuthenticatedGuard)
async getChannelsForUser (@Profile42() profile: Profile): Promise<Channel[]> { async getChannelsForUser (@Profile42() profile: Profile): Promise<Channel[]> {
return await this.channelService.getChannelsForUser(+profile.id) return await this.channelService.getChannelsForUser(+profile.id)
} }
@Post() @Post()
async createChannel (@Body() channel: CreateChannelDto) { async createChannel (@Body() channel: CreateChannelDto): Promise<Channel> {
return await this.channelService.createChannel(channel) return await this.channelService.createChannel(channel)
} }
} }

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

@ -4,12 +4,11 @@ import {
SubscribeMessage, SubscribeMessage,
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 { BadRequestException } from '@nestjs/common'
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'
@ -21,6 +20,7 @@ import { Repository } from 'typeorm'
import ConnectedUser from './entity/connection.entity' import ConnectedUser from './entity/connection.entity'
import { ConnectionDto } from './dto/connection.dto' import { ConnectionDto } from './dto/connection.dto'
@WebSocketGateway({ @WebSocketGateway({
cors: { origin: /^(http|ws):\/\/localhost(:\d+)?$/ } cors: { origin: /^(http|ws):\/\/localhost(:\d+)?$/ }
}) })
@ -36,18 +36,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
private readonly connectedUserRepository: Repository<ConnectedUser> private readonly connectedUserRepository: Repository<ConnectedUser>
) {} ) {}
async handleConnection (socket: Socket): Promise<void> { async handleConnection (socket: Socket): Promise<void> {}
// console.log(socket.handshake.headers)
// const cookie = socket.handshake.headers.cookie as string
// const { authentication: authenticationToken } = parse(cookie)
// console.log(authenticationToken)
// const user = await this.userService.find(authenticationToken)
// if (!user) {
// this.handleDisconnect(socket)
// throw new WsException('Invalid credentials.')
// }
// return user
}
handleDisconnect (socket: Socket): void { handleDisconnect (socket: Socket): void {
socket.disconnect() socket.disconnect()
@ -56,16 +45,20 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@SubscribeMessage('joinChannel') @SubscribeMessage('joinChannel')
async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise<void> { async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise<void> {
const channel = await this.chatService.getChannel(connect.ChannelId) const channel = await this.chatService.getChannel(connect.ChannelId)
if (channel.banned.find((e) => e.id == connect.UserId) != null) { if (channel.banned.find((ban) => ban.id === connect.UserId) !== null)
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.findUser(connect.UserId)) as User const user = (await this.userService.findUser(connect.UserId)) as User
if (channel.password !== '') {
if (!(await bcrypt.compare(channel.password, connect.pwd))) {
throw new BadRequestException()
}
} else await this.chatService.addUserToChannel(channel, user)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// We don't need to verify if the user is already in imo
//
//if (
// channel.users.find((usr) => usr.id === user.id) == null &&
// channel.password !== ''
//) {
if (channel.password !== '' && !(await bcrypt.compare(channel.password, connect.pwd)))
throw new WsException('Wrong password')
else await this.chatService.addUserToChannel(channel, user)
{ {
const conUser = new ConnectedUser() const conUser = new ConnectedUser()
conUser.user = user conUser.user = user
@ -73,10 +66,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
conUser.socket = socket.id conUser.socket = socket.id
await this.connectedUserRepository.save(conUser) await this.connectedUserRepository.save(conUser)
} }
const messages = await this.messageService.findMessagesInChannelForUser(
channel, const messages =
user await this.messageService.findMessagesInChannelForUser(channel, user)
)
this.server.to(socket.id).emit('messages', messages) this.server.to(socket.id).emit('messages', messages)
await socket.join(channel.name) await socket.join(channel.name)
} }

3
back/volume/src/chat/entity/channel.entity.ts

@ -7,7 +7,6 @@ import {
ManyToMany, ManyToMany,
ManyToOne, ManyToOne,
OneToMany, OneToMany,
OneToOne,
PrimaryGeneratedColumn PrimaryGeneratedColumn
} from 'typeorm' } from 'typeorm'
import User from 'src/users/entity/user.entity' import User from 'src/users/entity/user.entity'
@ -56,6 +55,6 @@ export default class Channel {
@JoinTable() @JoinTable()
banned: User[] banned: User[]
@Column('text', { array: true }) @Column('text', { array: true, default: [] })
muted: number[][] muted: number[][]
} }

Loading…
Cancel
Save