Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/modules/call/call.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ export class CallController {
@Body() joinCallDto?: JoinCallDto,
): Promise<JoinCallResponseDto> {
const { uuid, email } = user || {};
const isUserAnonymous =
(!joinCallDto?.anonymousId || joinCallDto?.anonymous) ?? !user;

return await this.callUseCase.joinCall(roomId, {
userId: uuid,
name: joinCallDto?.name,
lastName: joinCallDto?.lastName,
anonymous: joinCallDto?.anonymous || !user,
anonymous: isUserAnonymous,
anonymousId: joinCallDto?.anonymousId,
email: email,
});
}
Expand Down
55 changes: 16 additions & 39 deletions src/modules/call/call.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Logger,
NotFoundException,
} from '@nestjs/common';
import { v4 as uuidv4 } from 'uuid';
import { v4 } from 'uuid';
import { UserTokenData } from '../auth/dto/user.dto';
import { RoomService } from './services/room.service';
import { Room } from './domain/room.domain';
Expand Down Expand Up @@ -73,6 +73,7 @@ export class CallUseCase {
name?: string;
lastName?: string;
anonymous?: boolean;
anonymousId?: string;
email?: string;
},
): Promise<JoinCallResponseDto> {
Expand All @@ -81,16 +82,25 @@ export class CallUseCase {
throw new NotFoundException(`Specified room not found`);
}

const processedUserData = this.processUserData(userData);
const isOwner = processedUserData.userId === room.hostId;
const joiningUserData = {
userId: userData?.anonymous
? (userData?.anonymousId ?? v4())
: userData?.userId,
name: userData?.name,
lastName: userData?.lastName,
anonymous: userData?.anonymous || !userData.userId,
email: userData?.email,
};

const isOwner = joiningUserData.userId === room.hostId;

if (!isOwner && room.isClosed) {
throw new ForbiddenException('Room is closed');
}

const roomUser = await this.roomService.addUserToRoom(
roomId,
processedUserData,
joiningUserData,
);

// Generate token for the user
Expand All @@ -99,10 +109,10 @@ export class CallUseCase {
roomId,
!!roomUser.anonymous,
isOwner,
processedUserData,
joiningUserData,
);

if (processedUserData.userId === room.hostId && room.isClosed) {
if (joiningUserData.userId === room.hostId && room.isClosed) {
await this.roomService.openRoom(roomId);
}

Expand All @@ -114,39 +124,6 @@ export class CallUseCase {
};
}

private processUserData(userData: {
userId?: string;
name?: string;
lastName?: string;
anonymous?: boolean;
email?: string;
}): {
userId: string;
name?: string;
lastName?: string;
anonymous: boolean;
email?: string;
} {
const { userId, name, lastName, anonymous = false, email } = userData;

if (anonymous || !userId) {
return {
userId: uuidv4(),
name,
lastName,
anonymous: true,
};
}

return {
userId,
name,
lastName,
anonymous: false,
email,
};
}

async leaveCall(roomId: string, userId: string): Promise<void> {
const room = await this.roomService.getRoomByRoomId(roomId);

Expand Down
13 changes: 11 additions & 2 deletions src/modules/call/dto/join-call.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsBoolean, IsOptional, IsString } from 'class-validator';
import { IsBoolean, IsOptional, IsString, IsUUID } from 'class-validator';

export class JoinCallDto {
@ApiPropertyOptional({
Expand All @@ -21,12 +21,21 @@ export class JoinCallDto {
@ApiPropertyOptional({
description: 'Whether the user is joining anonymously',
type: Boolean,
default: false,
deprecated: true,
})
@IsBoolean()
@IsOptional()
anonymous?: boolean;

@ApiPropertyOptional({
description: 'Id to use for anonymous user',
type: String,
required: false,
})
@IsUUID()
@IsOptional()
anonymousId?: string;

@IsString()
@IsOptional()
email?: string;
Expand Down
9 changes: 2 additions & 7 deletions src/modules/call/services/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,9 @@ export class RoomService {
}

const { userId, name, lastName, anonymous = false } = userData;
let userIdToUse = userId;

if (anonymous || !userIdToUse) {
userIdToUse = uuidv4();
}

const existingUser = await this.roomUserRepository.findByUserIdAndRoomId(
userIdToUse,
userId,
roomId,
);

Expand All @@ -96,7 +91,7 @@ export class RoomService {

return this.roomUserRepository.create({
roomId,
userId: userIdToUse,
userId,
name,
lastName,
anonymous: Boolean(anonymous),
Expand Down
Loading