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

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

@ -4,12 +4,11 @@ import {
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
WsException
WsException,
} from '@nestjs/websockets'
import { Socket, Server } from 'socket.io'
// import { User } from 'users/user.entity';
import { UsersService } from 'src/users/users.service'
import { BadRequestException } from '@nestjs/common'
import { ChatService } from './chat.service'
import type Message from './entity/message.entity'
import * as bcrypt from 'bcrypt'
@ -21,6 +20,7 @@ import { Repository } from 'typeorm'
import ConnectedUser from './entity/connection.entity'
import { ConnectionDto } from './dto/connection.dto'
@WebSocketGateway({
cors: { origin: /^(http|ws):\/\/localhost(:\d+)?$/ }
})
@ -36,18 +36,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
private readonly connectedUserRepository: Repository<ConnectedUser>
) {}
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
}
async handleConnection (socket: Socket): Promise<void> {}
handleDisconnect (socket: Socket): void {
socket.disconnect()
@ -56,16 +45,20 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@SubscribeMessage('joinChannel')
async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise<void> {
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')
}
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()
conUser.user = user
@ -73,10 +66,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
conUser.socket = socket.id
await this.connectedUserRepository.save(conUser)
}
const messages = await this.messageService.findMessagesInChannelForUser(
channel,
user
)
const messages =
await this.messageService.findMessagesInChannelForUser(channel, user)
this.server.to(socket.id).emit('messages', messages)
await socket.join(channel.name)
}

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

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

Loading…
Cancel
Save