Browse Source

* DMs WIP

master
vvandenb 2 years ago
parent
commit
562f7fe6a9
  1. 13
      back/volume/src/chat/chat.controller.ts
  2. 38
      back/volume/src/chat/chat.service.ts
  3. 7
      back/volume/src/chat/dto/create-channel.dto.ts

13
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<Channel[]> {
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<Channel> {
return await this.channelService.createChannel(channel)

38
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

7
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
}

Loading…
Cancel
Save