|
@ -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,25 +145,22 @@ 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)) |
|
|
|
|
|
throw new BadRequestException('You are not the owner of this channel') |
|
|
await this.channelService.removeChannel(id) |
|
|
await this.channelService.removeChannel(id) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
throw new BadRequestException('You are not the owner of this channel') |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Post(':id/password') |
|
|
@Post(':id/password') |
|
|
@UseGuards(AuthenticatedGuard) |
|
|
@UseGuards(AuthenticatedGuard) |
|
|
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) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
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) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|