From 562f7fe6a978fbbb9c6e852519cf3b0894b3d0f3 Mon Sep 17 00:00:00 2001 From: vvandenb Date: Thu, 16 Mar 2023 09:47:45 +0100 Subject: [PATCH] * DMs WIP --- back/volume/src/chat/chat.controller.ts | 13 +++++++ back/volume/src/chat/chat.service.ts | 38 +++++++++++++++---- .../volume/src/chat/dto/create-channel.dto.ts | 7 +++- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index f5a99a1..92df932 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -40,6 +40,8 @@ export class ChatController { const user: User | null = await this.usersService.findUser(target.id) if (user == null) throw new NotFoundException(`User #${target.id} not found`) + if (channel.isPrivate && channel.password === '') + throw new BadRequestException('You cannot add more users to a DM') if (!(await this.channelService.isUser(channel.id, +profile.id))) { throw new BadRequestException( 'You are not allowed to invite users to this channel' @@ -185,6 +187,17 @@ export class ChatController { return await this.channelService.getChannelsForUser(+profile.id) } + @Get('dms/:otherId') + async getDMsForUser (@Profile42() profile: Profile, @Param('otherName') otherName: string): Promise { + const other = await this.usersService.findUserByName(otherName) + const otherId = other.ftId + const channels = await this.channelService.getChannelsForUser(+profile.id) + + return channels.filter((channel: Channel) => { + return channel.users?.some((ch) => ch.id === otherId) && channel.isPrivate && channel.password === '' + }) + } + @Post() async createChannel (@Body() channel: CreateChannelDto): Promise { return await this.channelService.createChannel(channel) diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index daa8f31..a13719d 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -22,16 +22,40 @@ export class ChatService { if (user == null) { throw new NotFoundException(`User #${channel.owner} not found`) } - const newChannel = new Channel() - newChannel.owner = user - newChannel.users = [user] - newChannel.admins = [user] - newChannel.name = channel.name - newChannel.isPrivate = channel.isPrivate - newChannel.password = channel.password + + let newChannel: Channel + if (channel.isDM) { + const otherUser: User | null = await this.usersService.findUserByName(channel.otherDMedUsername) + if (otherUser == null) { + throw new NotFoundException(`User #${channel.otherDMedUsername} not found`) + } + newChannel = this.createDM(user, otherUser) + } else { + newChannel = new Channel() + newChannel.owner = user + newChannel.users = [user] + newChannel.admins = [user] + newChannel.name = channel.name + newChannel.isPrivate = channel.isPrivate + newChannel.password = channel.password + } return await this.ChannelRepository.save(newChannel) } + createDM (user: User, otherUser: User): Channel { + const newDM = new Channel() + newDM.isPrivate = true + newDM.password = '' + + newDM.owner = user + newDM.users = [user, otherUser] + newDM.admins = [] + newDM.name = user.username + ' & ' + otherUser.username + newDM.isPrivate = true + newDM.password = '' + return newDM + } + async updatePassword (id: number, password: string) { const channel: Channel | null = await this.ChannelRepository.findOneBy({ id diff --git a/back/volume/src/chat/dto/create-channel.dto.ts b/back/volume/src/chat/dto/create-channel.dto.ts index af1f03a..eccf397 100644 --- a/back/volume/src/chat/dto/create-channel.dto.ts +++ b/back/volume/src/chat/dto/create-channel.dto.ts @@ -1,7 +1,6 @@ import { Transform } from 'class-transformer' import { IsPositive, - IsAlpha, IsString, IsOptional, IsNumber, @@ -25,4 +24,10 @@ export class CreateChannelDto { @IsBoolean() @Transform(({ value }) => value === 'true') isPrivate: boolean + + @IsBoolean() + isDM: boolean + + @IsString() + otherDMedUsername: string }