v1.6.0/conference call upgrade
- anyone can start call - colision check for calls - better error handing for calls
This commit is contained in:
+8
-1
@@ -1,13 +1,19 @@
|
||||
const FunctionModules = require('./modules/index');
|
||||
const FunctionModules2 = require('./modules2/index');
|
||||
const Webhooks = require('./webhooks/index');
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
|
||||
// WEBHOOKS
|
||||
const webhooks = express();
|
||||
webhooks.use(cors({ origin: true }));
|
||||
webhooks.all('/conferenceEnded',Webhooks.conferenceEnd);
|
||||
|
||||
// VERSION 1
|
||||
const app = express();
|
||||
app.use(cors({ origin: true }));
|
||||
app.use(FunctionModules.authenticator);
|
||||
app.get('/validator', FunctionModules.tokenValidator);
|
||||
app.get('/validator/:partyName', FunctionModules.tokenValidator);
|
||||
app.get('/refresher/:token', FunctionModules.tokenRefresher);
|
||||
app.post('/create', FunctionModules.createRoom);
|
||||
app.post('/join', FunctionModules.joinRoom);
|
||||
@@ -35,4 +41,5 @@ app2.delete('/delete/:rid', FunctionModules2.deleteRoom);
|
||||
module.exports = {
|
||||
v1: app,
|
||||
v2: app2,
|
||||
webhooks,
|
||||
};
|
||||
|
||||
@@ -19,3 +19,5 @@ exports.widgets2 = functions.https.onRequest(REST.v2);
|
||||
exports.scheduledCleanUp = functions.pubsub
|
||||
.schedule('every 60 minutes')
|
||||
.onRun(FunctionModules.scheduledCleanUp);
|
||||
|
||||
exports.webhooks = functions.https.onRequest(REST.webhooks);
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
const VoxeetSecret = require('../VoxeetSecret.json');
|
||||
const admin = require('firebase-admin');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
const bodyData = JSON.parse(req.body);
|
||||
const { key, secret } = VoxeetSecret;
|
||||
const credentials = Buffer.from(encodeURI(key) + ':' + encodeURI(secret)).toString('base64');
|
||||
const headers = {
|
||||
@@ -14,8 +17,48 @@ module.exports = async (req, res) => {
|
||||
body: JSON.stringify({
|
||||
grant_type: 'client_credentials',
|
||||
}),
|
||||
})
|
||||
.then((oauthResponse) => oauthResponse.json())
|
||||
.catch((err) => res.status(500).send(err));
|
||||
if (oauth) res.status(200).send(oauth);
|
||||
}).then((oauthResponse) => oauthResponse.json())
|
||||
.catch((err) => res.status(500).send({err, message: 'Unable to generate token for call'}));
|
||||
|
||||
const [exists, conferencePending, conference, adminOnly, adminUid] = await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(req.params.partyName)
|
||||
.get()
|
||||
.then(snap => {
|
||||
const data = snap.exists ? snap.data() : {};
|
||||
return [snap.exists, data.conferencePending, data.conference, data.adminOnly, data.adminUid];
|
||||
});
|
||||
|
||||
if (exists === true) {
|
||||
if (conference === undefined || conference === null) {
|
||||
if (conferencePending === undefined || conferencePending === null) {
|
||||
if (!adminOnly || (adminOnly && (adminUid === bodyData.uid)) ) {
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(req.params.partyName)
|
||||
.update({
|
||||
conferencePending: bodyData.uid
|
||||
});
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(req.params.partyName)
|
||||
.collection('messages')
|
||||
.add({
|
||||
uid: '<system>',
|
||||
name: '',
|
||||
text: 'A call session is being created (will be available soon).',
|
||||
time: new Date(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (oauth) res.status(200).send(oauth);
|
||||
} else {
|
||||
res.status(400).send({ message: 'Unable to get party for call.' });
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -1,47 +1,68 @@
|
||||
const admin = require('firebase-admin');
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
const { conference, partyName, partyPin, userName } = JSON.parse(req.body);
|
||||
const { conference, partyName, partyPin, userName, uid } = JSON.parse(req.body);
|
||||
// GET ROOM PIN
|
||||
const roomPin = await admin
|
||||
const [exists, roomPin, conferencePending, partyConference] = await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(partyName)
|
||||
.get()
|
||||
.then((roomSnap) => (roomSnap.exists ? roomSnap.data().pin : null));
|
||||
.then((roomSnap) => {
|
||||
const data = roomSnap.exists ? roomSnap.data() : {};
|
||||
return [roomSnap.exists, data.pin, data.conferencePending, data.conference];
|
||||
});
|
||||
// CHECK IF ROOM EXISTS
|
||||
if (roomPin) {
|
||||
// CHECK FOR CORRECT PIN
|
||||
if (roomPin === partyPin) {
|
||||
// UPDATE CONFERENCE DETAILS
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(partyName)
|
||||
.update({ conference })
|
||||
.catch((err) =>
|
||||
res.status(500).send({ message: 'Unable to update the call details', error: err })
|
||||
);
|
||||
// ADD SYSTEM MESSAGE
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(partyName)
|
||||
.collection('messages')
|
||||
.add({
|
||||
uid: '<system>',
|
||||
name: '',
|
||||
text: `${userName} started the call.`,
|
||||
time: new Date(),
|
||||
})
|
||||
.catch((err) => console.error('Failed to add update conference message', err));
|
||||
res.status(200).send({ message: 'Started the conference' });
|
||||
} else {
|
||||
// WRONG ROOM PIN
|
||||
return res.status(401).send({ message: 'Wrong party pin, try rejoining' });
|
||||
if (exists === true) {
|
||||
if (roomPin) {
|
||||
// CHECK FOR CORRECT PIN
|
||||
if (roomPin === partyPin) {
|
||||
if (conferencePending) {
|
||||
if (conferencePending === uid) {
|
||||
// UPDATE CONFERENCE DETAILS
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(partyName)
|
||||
.update({ conference })
|
||||
.catch((err) =>
|
||||
res.status(500).send({ message: 'Unable to update the call details', error: err })
|
||||
);
|
||||
// REMOVE PENDING STATUS
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(partyName)
|
||||
.update({ conferencePending : admin.firestore.FieldValue.delete() })
|
||||
.catch((err) => console.error('Failed to remove pending status', err));
|
||||
// ADD SYSTEM MESSAGE
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(partyName)
|
||||
.collection('messages')
|
||||
.add({
|
||||
uid: '<system>',
|
||||
name: '',
|
||||
text: `${userName} started the call, everyone can join now.`,
|
||||
time: new Date(),
|
||||
})
|
||||
.catch((err) => console.error('Failed to add update conference message', err));
|
||||
res.status(200).send({ message: 'Started the conference' });
|
||||
} else {
|
||||
res.status(409).send({ message: 'Looks like someone is already starting a call' });
|
||||
}
|
||||
} else if (partyConference) {
|
||||
res.status(409).send({ message: 'Call already exists', conference: partyConference });
|
||||
}
|
||||
} else {
|
||||
// WRONG ROOM PIN
|
||||
res.status(401).send({ message: 'Wrong party pin, try rejoining' });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// ROOM DOESN'T EXIST
|
||||
return res.status(404).send({ message: 'Party Not Found' });
|
||||
res.status(404).send({ message: 'Party Not Found' });
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
const admin = require('firebase-admin');
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
console.info(`[START]\tWEBHOOKS/Conference End\nID:${req.body.conference.confId}\nParty:${req.body.conference.confAlias}`);
|
||||
if (req.body.eventType === 'Conference.Ended') {
|
||||
if (req.body.participants.length > 0) {
|
||||
if (req.body.confAlias.startsWith('AUTODELETE_')) {
|
||||
const partyName = req.body.confAlias.replace(/AUTODELETE_/, "");
|
||||
admin.firestore()
|
||||
.collection('rooms')
|
||||
.doc(partyName)
|
||||
.update({ conference: admin.firestore.FieldValue.delete() })
|
||||
.then(() => {
|
||||
console.info(`[DONE]\tWEBHOOKS/Conference End\nID:${req.body.conference.confId}\nParty:${req.body.conference.confAlias}`);
|
||||
return;
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`[ERROR]\tWEBHOOKS/Conference End/Firebase Error\nID:${req.body.conference.confId}\nParty:${req.body.conference.confAlias}\nError:${JSON.stringify(err)}`)
|
||||
});
|
||||
res.status(200).send({ message: 'Processed conference end'});
|
||||
}
|
||||
} else {
|
||||
console.info(`[DONE]\tWEBHOOKS/Conference End/End Of Duplicate Conference\nID:${req.body.conference.confId}\nParty:${req.body.conference.confAlias}`);
|
||||
}
|
||||
} else {
|
||||
console.error(`[ERROR]\tWEBHOOKS/Conference End/Wrong Event Type\nID:${req.body.conference.confId}\nParty:${req.body.conference.confAlias}`)
|
||||
res.status(400).send({ message: 'Wrong event type' });
|
||||
}
|
||||
return;
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
const conferenceEnd = require('./conferenceEnd');
|
||||
|
||||
module.exports = {
|
||||
conferenceEnd,
|
||||
};
|
||||
Reference in New Issue
Block a user