Skip to content

Commit 9646c8c

Browse files
committed
feat(call): allow anonymousId to be created by the frontend
1 parent 590f8de commit 9646c8c

4 files changed

Lines changed: 30 additions & 48 deletions

File tree

‎src/modules/call/call.controller.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export class CallController {
125125
name: joinCallDto?.name,
126126
lastName: joinCallDto?.lastName,
127127
anonymous: joinCallDto?.anonymous || !user,
128+
anonymousId: joinCallDto?.anonymousId,
128129
email: email,
129130
});
130131
}

‎src/modules/call/call.usecase.ts‎

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
Logger,
66
NotFoundException,
77
} from '@nestjs/common';
8-
import { v4 as uuidv4 } from 'uuid';
8+
import { v4 } from 'uuid';
99
import { UserTokenData } from '../auth/dto/user.dto';
1010
import { RoomService } from './services/room.service';
1111
import { Room } from './domain/room.domain';
@@ -73,6 +73,7 @@ export class CallUseCase {
7373
name?: string;
7474
lastName?: string;
7575
anonymous?: boolean;
76+
anonymousId?: string;
7677
email?: string;
7778
},
7879
): Promise<JoinCallResponseDto> {
@@ -81,16 +82,25 @@ export class CallUseCase {
8182
throw new NotFoundException(`Specified room not found`);
8283
}
8384

84-
const processedUserData = this.processUserData(userData);
85-
const isOwner = processedUserData.userId === room.hostId;
85+
const joiningUserData = {
86+
userId: userData?.anonymous
87+
? (userData?.anonymousId ?? v4())
88+
: userData?.userId,
89+
name: userData?.name,
90+
lastName: userData?.lastName,
91+
anonymous: userData?.anonymous || !userData.userId,
92+
email: userData?.email,
93+
};
94+
95+
const isOwner = joiningUserData.userId === room.hostId;
8696

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

91101
const roomUser = await this.roomService.addUserToRoom(
92102
roomId,
93-
processedUserData,
103+
joiningUserData,
94104
);
95105

96106
// Generate token for the user
@@ -99,10 +109,10 @@ export class CallUseCase {
99109
roomId,
100110
!!roomUser.anonymous,
101111
isOwner,
102-
processedUserData,
112+
joiningUserData,
103113
);
104114

105-
if (processedUserData.userId === room.hostId && room.isClosed) {
115+
if (joiningUserData.userId === room.hostId && room.isClosed) {
106116
await this.roomService.openRoom(roomId);
107117
}
108118

@@ -114,39 +124,6 @@ export class CallUseCase {
114124
};
115125
}
116126

117-
private processUserData(userData: {
118-
userId?: string;
119-
name?: string;
120-
lastName?: string;
121-
anonymous?: boolean;
122-
email?: string;
123-
}): {
124-
userId: string;
125-
name?: string;
126-
lastName?: string;
127-
anonymous: boolean;
128-
email?: string;
129-
} {
130-
const { userId, name, lastName, anonymous = false, email } = userData;
131-
132-
if (anonymous || !userId) {
133-
return {
134-
userId: uuidv4(),
135-
name,
136-
lastName,
137-
anonymous: true,
138-
};
139-
}
140-
141-
return {
142-
userId,
143-
name,
144-
lastName,
145-
anonymous: false,
146-
email,
147-
};
148-
}
149-
150127
async leaveCall(roomId: string, userId: string): Promise<void> {
151128
const room = await this.roomService.getRoomByRoomId(roomId);
152129

‎src/modules/call/dto/join-call.dto.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
2-
import { IsBoolean, IsOptional, IsString } from 'class-validator';
2+
import { IsBoolean, IsOptional, IsString, IsUUID } from 'class-validator';
33

44
export class JoinCallDto {
55
@ApiPropertyOptional({
@@ -21,12 +21,21 @@ export class JoinCallDto {
2121
@ApiPropertyOptional({
2222
description: 'Whether the user is joining anonymously',
2323
type: Boolean,
24-
default: false,
24+
deprecated: true,
2525
})
2626
@IsBoolean()
2727
@IsOptional()
2828
anonymous?: boolean;
2929

30+
@ApiPropertyOptional({
31+
description: 'Id to use for anonymous user',
32+
type: String,
33+
required: false,
34+
})
35+
@IsUUID()
36+
@IsOptional()
37+
anonymousId?: string;
38+
3039
@IsString()
3140
@IsOptional()
3241
email?: string;

‎src/modules/call/services/room.service.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,9 @@ export class RoomService {
7979
}
8080

8181
const { userId, name, lastName, anonymous = false } = userData;
82-
let userIdToUse = userId;
83-
84-
if (anonymous || !userIdToUse) {
85-
userIdToUse = uuidv4();
86-
}
8782

8883
const existingUser = await this.roomUserRepository.findByUserIdAndRoomId(
89-
userIdToUse,
84+
userId,
9085
roomId,
9186
);
9287

@@ -96,7 +91,7 @@ export class RoomService {
9691

9792
return this.roomUserRepository.create({
9893
roomId,
99-
userId: userIdToUse,
94+
userId,
10095
name,
10196
lastName,
10297
anonymous: Boolean(anonymous),

0 commit comments

Comments
 (0)