Browse Source

ready to try channels management

master
nicolas-arnaud 2 years ago
parent
commit
89ced4319b
  1. 146
      back/volume/src/chat/chat.controller.ts
  2. 24
      back/volume/src/chat/chat.service.ts
  3. 16
      back/volume/src/chat/dto/updateUser.dto.ts
  4. 2
      back/volume/src/chat/entity/channel.entity.ts
  5. 2
      front/volume/src/components/Channels.svelte

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

@ -15,7 +15,7 @@ import { UsersService } from 'src/users/users.service'
import { ChannelService } from './chat.service' import { ChannelService } from './chat.service'
import { CreateChannelDto } from './dto/create-channel.dto' import { CreateChannelDto } from './dto/create-channel.dto'
import { UpdateChannelDto } from './dto/update-channel.dto' import { IdDto, PasswordDto, MuteDto } from './dto/updateUser.dto'
import type User from 'src/users/entity/user.entity' import type User from 'src/users/entity/user.entity'
import type Channel from './entity/channel.entity' import type Channel from './entity/channel.entity'
@ -30,66 +30,113 @@ export class ChatController {
) {} ) {}
@Post(':id/invite') @Post(':id/invite')
async addUser (@Param('id') id: number, @Body() userId: number) { @UseGuards(AuthenticatedGuard)
const channel = await this.channelService.getChannel(id) async addUser (
const user: User | null = await this.usersService.findUser(userId) @Param('id') id: number, @Body() target: IdDto,
if (user == null) throw new NotFoundException(`User #${userId} not found`) @Profile42() profile: Profile) {
const channel = await this.channelService.getFullChannel(id)
const user: User | null = await this.usersService.findUser(target.id)
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))
throw new BadRequestException('User is already in this channel')
if (await this.channelService.isBanned(channel.id, target.id))
throw new BadRequestException('User is banned from this channel')
channel.users.push(user) channel.users.push(user)
this.channelService.update(channel) this.channelService.save(channel)
} }
@Delete(':id/kick') @Delete(':id/kick')
async removeUser (@Param('id') id: number, @Body() userId: number) { @UseGuards(AuthenticatedGuard)
const channel = await this.channelService.getChannel(id) async removeUser (
channel.users = channel.admins.filter((usr: User) => { @Param('id') id: number, @Body() target: IdDto,
return usr.ftId !== userId @Profile42() profile: Profile) {
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))
throw new BadRequestException('User is not in this channel')
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
}) })
this.channelService.update(channel) this.channelService.save(channel)
} }
@Post(':id/admin') @Post(':id/admin')
async addAdmin (@Param('id') id: number, @Body() userId: number) { @UseGuards(AuthenticatedGuard)
const channel = await this.channelService.getChannel(id) async addAdmin (
const user: User | null = await this.usersService.findUser(userId) @Param('id') id: number,
if (user == null) throw new NotFoundException(`User #${userId} not found`) @Body() target: IdDto,
@Profile42() profile: Profile) {
const channel = await this.channelService.getFullChannel(id)
const user: User | null = await this.usersService.findUser(target.id)
if (user == null) throw new NotFoundException(`User #${target.id} not found`)
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))
throw new BadRequestException('User is not in this channel')
if (await this.channelService.isAdmin(channel.id, target.id))
throw new BadRequestException('User is already an admin of this channel')
channel.admins.push(user) channel.admins.push(user)
this.channelService.update(channel) this.channelService.save(channel)
} }
@Delete(':id/admin') @Delete(':id/admin')
async removeAdmin (@Param('id') id: number, @Body() userId: number) { @UseGuards(AuthenticatedGuard)
const channel = await this.channelService.getChannel(id) async removeAdmin (
@Param('id') id: number,
@Body() target: IdDto,
@Profile42() profile: Profile) {
const channel = await this.channelService.getFullChannel(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))
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 !== userId return usr.ftId !== target.id
}) })
this.channelService.update(channel) this.channelService.save(channel)
} }
@Post(':id/ban') @Post(':id/ban')
async addBan (@Param('id') id: number, @Body() userId: number) { @UseGuards(AuthenticatedGuard)
const channel = await this.channelService.getChannel(id) async addBan (
const user: User | null = await this.usersService.findUser(userId) @Param('id') id: number,
if (user == null) throw new NotFoundException(`User #${userId} not found`) @Body() target: IdDto,
@Profile42() profile: Profile) {
const channel = await this.channelService.getFullChannel(id)
const user: User | null = await this.usersService.findUser(target.id)
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))
throw new BadRequestException('You cannot ban the owner of the channel')
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)
this.channelService.update(channel) this.channelService.save(channel)
} }
@Post(':id/mute') @Post(':id/mute')
async addMute (@Param('id') id: number, @Body() userId: number) { @UseGuards(AuthenticatedGuard)
const channel = await this.channelService.getChannel(id) async addMute (
const user: User | null = await this.usersService.findUser(userId) @Param('id') id: number,
if (user == null) throw new NotFoundException(`User #${userId} not found`) @Body() mute: MuteDto, // [userId, duration]
channel.muted.push(user) @Profile42() profile: Profile) {
this.channelService.update(channel) const channel = await this.channelService.getFullChannel(id)
} const user: User | null = await this.usersService.findUser(mute.data[0])
if (user == null) throw new NotFoundException(`User #${mute.data[0]} not found`)
@Delete(':id/mute') if (!await this.channelService.isAdmin(channel.id, +profile.id))
async removeMute (@Param('id') id: number, @Body() userId: number) { throw new BadRequestException('You are not allowed to mute users from this channel')
const channel = await this.channelService.getChannel(id) if (await this.channelService.isOwner(channel.id, mute.data[0]))
channel.muted = channel.muted.filter((usr: User) => { throw new BadRequestException('You cannot mute the owner of the channel')
return usr.ftId !== userId if (await this.channelService.getMuteDuration(channel.id, mute.data[0]) > 0)
}) throw new BadRequestException('User is already muted from this channel')
this.channelService.update(channel) channel.muted.push(mute.data)
this.channelService.save(channel)
} }
@Delete(':id') @Delete(':id')
@ -98,11 +145,10 @@ export class ChatController {
@Profile42() profile: Profile, @Profile42() profile: Profile,
@Param('id') id: number @Param('id') id: number
) { ) {
if (await this.channelService.isOwner(id, +profile.id)) { if (!await this.channelService.isOwner(id, +profile.id))
await this.channelService.removeChannel(id) throw new BadRequestException('You are not the owner of this channel')
return await this.channelService.removeChannel(id)
} return
throw new BadRequestException('You are not the owner of this channel')
} }
@Post(':id/password') @Post(':id/password')
@ -110,13 +156,11 @@ export class ChatController {
async updatePassword ( async updatePassword (
@Profile42() profile: Profile, @Profile42() profile: Profile,
@Param('id') id: number, @Param('id') id: number,
@Body() password: string @Body() data: PasswordDto
) { ) {
if (await this.channelService.isOwner(id, +profile.id)) { if (await this.channelService.isOwner(id, +profile.id))
await this.channelService.updatePassword(id, password) throw new BadRequestException('You are not the owner of this channel')
return await this.channelService.updatePassword(id, data.password)
}
throw new BadRequestException('You are not the owner of this channel')
} }

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

@ -7,7 +7,6 @@ import { UsersService } from 'src/users/users.service'
import type User from 'src/users/entity/user.entity' import type User from 'src/users/entity/user.entity'
import Channel from './entity/channel.entity' import Channel from './entity/channel.entity'
import { classToPlain, plainToClass } from 'class-transformer'
@Injectable() @Injectable()
export class ChannelService { export class ChannelService {
@ -68,8 +67,21 @@ export class ChannelService {
return channel return channel
} }
async getFullChannel (id: number): Promise<Channel> {
const channel = await this.ChannelRepository.findOne({
where: { id },
relations: ['users', 'admins', 'banned', 'muted', 'owner']
})
if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) }
return channel
}
async update (channel: Channel) { async update (channel: Channel) {
this.ChannelRepository.update(channel.id, channel) await this.ChannelRepository.update(channel.id, channel)
}
async save (channel: Channel) {
await this.ChannelRepository.save(channel)
} }
async removeChannel (channelId: number) { async removeChannel (channelId: number) {
@ -112,12 +124,16 @@ export class ChannelService {
return channel.banned.findIndex((user) => user.ftId === userId) != -1 return channel.banned.findIndex((user) => user.ftId === userId) != -1
} }
async isMuted (id: number, userId: number): Promise<boolean> { async getMuteDuration (id: number, userId: number): Promise<number> {
const channel = await this.ChannelRepository.findOne({ const channel = await this.ChannelRepository.findOne({
where: { id }, where: { id },
relations: { muted: true } relations: { muted: true }
}) })
if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) } if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) }
return channel.muted.findIndex((user) => user.ftId === userId) != -1
const mutation: Array<number> | undefined = channel.muted.find((mutation) => mutation[0] === userId)
if (mutation == null) { return 0 }
return mutation[1]
} }
} }

16
back/volume/src/chat/dto/updateUser.dto.ts

@ -0,0 +1,16 @@
import { IsNumber, IsString} from 'class-validator'
export class IdDto {
@IsNumber()
id: number
}
export class PasswordDto {
@IsString()
password: string
}
export class MuteDto {
data: Array<number>
}

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

@ -57,5 +57,5 @@ export default class Channel {
@ManyToMany(() => User) // refuse post @ManyToMany(() => User) // refuse post
@JoinTable() @JoinTable()
muted: User[] muted: Array<Array<number>>
} }

2
front/volume/src/components/Channels.svelte

@ -94,7 +94,7 @@
}); });
if (response.ok) { if (response.ok) {
const user = await response.json(); const user = await response.json();
const response2 = await fetch(API_URL + "/channels/" + id, { const response2 = await fetch(API_URL + "/channels/" + id + "/invite", {
credentials: "include", credentials: "include",
method: "POST", method: "POST",
mode: "cors", mode: "cors",

Loading…
Cancel
Save