Skip to content

Commit bd29e6c

Browse files
author
Tim Mendoza
committed
Add support for changing room type
1 parent 124165f commit bd29e6c

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

src/commands/rtc/apps/video/deploy.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ DeployCommand.flags = Object.assign(
4242
default: false,
4343
description: 'Override an existing App deployment',
4444
}),
45+
'room-type': flags.enum({
46+
options: ['group', 'group-small', 'peer-to-peer'],
47+
description: 'Type of room to use',
48+
required: false,
49+
default: 'group',
50+
}),
4551
},
4652
TwilioClientCommand.flags
4753
);

src/helpers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ async function getAppInfo() {
8181

8282
const passcodeVar = variables.find(v => v.key === 'API_PASSCODE');
8383
const expiryVar = variables.find(v => v.key === 'API_PASSCODE_EXPIRY');
84+
const roomTypeVar = variables.find(v => v.key === 'ROOM_TYPE');
8485

8586
const passcode = passcodeVar ? passcodeVar.value : '';
8687
const expiry = expiryVar ? expiryVar.value : '';
88+
const roomType = roomTypeVar ? roomTypeVar.value : '';
8789

8890
const fullPasscode = getPasscode(environment.domainName, passcode);
8991

@@ -93,6 +95,7 @@ async function getAppInfo() {
9395
sid: app.sid,
9496
passcode: fullPasscode,
9597
hasAssets: Boolean(assets.length),
98+
roomType,
9699
};
97100
}
98101

@@ -107,8 +110,13 @@ async function displayAppInfo() {
107110
if (appInfo.hasAssets) {
108111
console.log(`Web App URL: ${appInfo.url}`);
109112
}
113+
110114
console.log(`Passcode: ${appInfo.passcode.replace(/(\d{3})(\d{3})(\d{4})(\d{4})/, '$1 $2 $3 $4')}`);
111115
console.log(`Expires: ${appInfo.expiry}`);
116+
117+
if (appInfo.roomType) {
118+
console.log(`Room Type: ${appInfo.roomType}`);
119+
}
112120
}
113121

114122
async function deploy() {
@@ -151,6 +159,7 @@ TWILIO_API_SECRET = the secret for the API Key`);
151159
TWILIO_API_KEY_SECRET: this.twilioClient.password,
152160
API_PASSCODE: pin,
153161
API_PASSCODE_EXPIRY: expiryTime,
162+
ROOM_TYPE: this.flags['room-type'],
154163
},
155164
pkgJson: {},
156165
functionsEnv: 'dev',

src/video-token-server.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ const AccessToken = Twilio.jwt.AccessToken;
55
const VideoGrant = AccessToken.VideoGrant;
66
const MAX_ALLOWED_SESSION_DURATION = 14400;
77

8-
module.exports.handler = (context, event, callback) => {
8+
module.exports.handler = async (context, event, callback) => {
99
const {
1010
ACCOUNT_SID,
1111
TWILIO_API_KEY_SID,
1212
TWILIO_API_KEY_SECRET,
1313
API_PASSCODE,
1414
API_PASSCODE_EXPIRY,
1515
DOMAIN_NAME,
16+
ROOM_TYPE,
1617
} = context;
1718

1819
const { user_identity, room_name, passcode } = event;
@@ -30,20 +31,18 @@ module.exports.handler = (context, event, callback) => {
3031
'The passcode used to validate application users has expired. Re-deploy the application to refresh the passcode.',
3132
},
3233
});
33-
callback(null, response);
34-
return;
34+
return callback(null, response);
3535
}
3636

37-
if (API_PASSCODE + appID + serverlessID !== passcode) {
37+
if (API_PASSCODE + appID + serverlessID !== passcode.replace(/\s+/g, '')) {
3838
response.setStatusCode(401);
3939
response.setBody({
4040
error: {
4141
message: 'passcode incorrect',
4242
explanation: 'The passcode used to validate application users is incorrect.',
4343
},
4444
});
45-
callback(null, response);
46-
return;
45+
return callback(null, response);
4746
}
4847

4948
if (!user_identity) {
@@ -54,8 +53,24 @@ module.exports.handler = (context, event, callback) => {
5453
explanation: 'The user_identity parameter is missing.',
5554
},
5655
});
57-
callback(null, response);
58-
return;
56+
return callback(null, response);
57+
}
58+
59+
const client = context.getTwilioClient();
60+
61+
try {
62+
await client.video.rooms.create({ uniqueName: room_name, type: ROOM_TYPE });
63+
} catch (e) {
64+
if (e.code !== 53113) {
65+
response.setStatusCode(401);
66+
response.setBody({
67+
error: {
68+
message: 'error creating room',
69+
explanation: 'Something went wrong when creating a room.',
70+
},
71+
});
72+
return callback(null, response);
73+
}
5974
}
6075

6176
const token = new AccessToken(ACCOUNT_SID, TWILIO_API_KEY_SID, TWILIO_API_KEY_SECRET, {
@@ -65,6 +80,6 @@ module.exports.handler = (context, event, callback) => {
6580
const videoGrant = new VideoGrant({ room: room_name });
6681
token.addGrant(videoGrant);
6782
response.setStatusCode(200);
68-
response.setBody({ token: token.toJwt() });
69-
callback(null, response);
83+
response.setBody({ token: token.toJwt(), room_type: ROOM_TYPE });
84+
return callback(null, response);
7085
};

0 commit comments

Comments
 (0)