Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apsantiso Check that this condition matches what we want. I changed it because I think it was wrong
!joinCallDto?.anonymousId -> !!joinCallDto?.anonymousId


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
78 changes: 37 additions & 41 deletions src/modules/call/call.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
BadRequestException,
ConflictException,
ForbiddenException,
Injectable,
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 +74,7 @@ export class CallUseCase {
name?: string;
lastName?: string;
anonymous?: boolean;
anonymousId?: string;
email?: string;
},
): Promise<JoinCallResponseDto> {
Expand All @@ -81,28 +83,55 @@ 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,
const existentUserInRoom = await this.roomService.getUserInRoom(
joiningUserData.userId,
room.id,
);

const currentUsersCount = await this.roomService.countUsersInRoom(room.id);

const isRoomFull = currentUsersCount >= room.maxUsersAllowed;

if (isRoomFull && !existentUserInRoom) {
throw new BadRequestException('The room is full');
}

const roomUser =
existentUserInRoom ??
(await this.roomService.createUserInRoom({
roomId: room.id,
userId: joiningUserData?.userId,
name: joiningUserData?.name,
lastName: joiningUserData?.lastName,
anonymous: Boolean(joiningUserData?.anonymous),
}));

// Generate token for the user
const tokenData = this.callService.createCallTokenForParticipant(
roomUser.userId,
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 +143,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
39 changes: 22 additions & 17 deletions src/modules/call/services/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { SequelizeRoomRepository } from '../infrastructure/room.repository';
import { SequelizeRoomUserRepository } from '../infrastructure/room-user.repository';
import { Room, RoomAttributes } from '../domain/room.domain';
import { RoomUser } from '../domain/room-user.domain';
import { RoomUser, RoomUserAttributes } from '../domain/room-user.domain';
import { v4 as uuidv4 } from 'uuid';
import { UsersInRoomDto } from '../dto/users-in-room.dto';
import { UserRepository } from '../../../shared/user/user.repository';
Expand All @@ -27,6 +27,10 @@ export class RoomService {
return this.roomRepository.create(data);
}

async createUserInRoom(data: Omit<RoomUserAttributes, 'id'>) {
return this.roomUserRepository.create(data);
}

async getRoomByRoomId(id: RoomAttributes['id']) {
return this.roomRepository.findById(id);
}
Expand Down Expand Up @@ -59,44 +63,36 @@ export class RoomService {
}

async addUserToRoom(
roomId: string,
room: Room,
userData: {
userId?: string;
name?: string;
lastName?: string;
anonymous?: boolean;
},
): Promise<RoomUser> {
const room = await this.getRoomByRoomId(roomId);
if (!room) {
throw new NotFoundException(`Specified room not found`);
}
const currentUsersCount = await this.roomUserRepository.countByRoomId(
room.id,
);

const currentUsersCount =
await this.roomUserRepository.countByRoomId(roomId);
if (currentUsersCount >= room.maxUsersAllowed) {
throw new BadRequestException('The room is full');
}

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

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

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

if (existingUser) {
throw new ConflictException('User is already in this room');
}

return this.roomUserRepository.create({
roomId,
userId: userIdToUse,
roomId: room.id,
userId,
name,
lastName,
anonymous: Boolean(anonymous),
Expand Down Expand Up @@ -159,4 +155,13 @@ export class RoomService {

return userAvatars;
}

async getUserInRoom(userId: string, roomId: string) {
const existingUser = await this.roomUserRepository.findByUserIdAndRoomId(
userId,
roomId,
);

return existingUser;
}
}
Loading