v1.3.0-beta/release

This commit is contained in:
2020-05-31 18:57:17 -04:00
parent 5428c1e299
commit a25753bcfc
24 changed files with 682 additions and 156 deletions
+27 -33
View File
@@ -6,37 +6,31 @@ module.exports = (req, res, next) => {
res.status(200).send();
return next();
}
if (req.headers.authorization) {
try {
const token = req.headers.authorization.split(' ')[1];
const reqToken = token.split(':')[0];
const reqUid = token.split(':')[1];
admin
.auth()
.verifyIdToken(reqToken)
.then((user) => {
if (user.uid === reqUid) {
console.info('Verified Token - UID - ', reqUid);
if (req.method === 'POST') {
const bodydata = JSON.parse(req.body);
bodydata.uid = user.uid;
req.body = JSON.stringify(bodydata);
}
if (req.method === 'GET' || req.method === 'DELETE') {
req.body = JSON.stringify({ uid: user.uid });
}
return next();
} else {
return res.status(401).send({ message: 'Unauthorized' });
}
})
.catch((err) => {
console.error('Failed Auth - ', req.headers.authorization, '\n', err);
return res.status(500).send({ message: 'Unable to verify user token' });
});
} catch (err) {
console.log(err);
return res.status(401).send({ message: 'Something went wrong when authorizing' });
}
} else return res.status(401).send({ message: 'Got no user token' });
const token = req.headers.authorization.split(' ')[1];
const reqToken = token.split(':')[0];
const reqUid = token.split(':')[1];
admin
.auth()
.verifyIdToken(reqToken)
.then((user) => {
if (user.uid === reqUid) {
console.info('Verified Token - UID - ', reqUid);
if (req.method === 'POST') {
const bodydata = JSON.parse(req.body);
bodydata.uid = user.uid;
req.body = JSON.stringify(bodydata);
}
if (req.method === 'GET' || req.method === 'DELETE') {
req.body = JSON.stringify({ uid: user.uid });
}
return next();
} else {
return res.status(401).send({ message: 'Unauthorized' });
}
})
.catch((err) => {
console.error('Failed Auth - ', req.headers.authorization, '\n', err);
return res.status(500).send({ message: 'Unable to verify user token', error: err });
});
return null;
};
+9 -22
View File
@@ -4,26 +4,16 @@ module.exports = async (req, res) => {
const bodydata = JSON.parse(req.body);
console.log('Requested Create - UID - ', bodydata.uid);
if (!bodydata.userName) res.status(400).send({ message: 'Missing user name' });
if (!bodydata.partyName) res.status(400).send({ message: 'Missing party name' });
if (!bodydata.partyPin) res.status(400).send({ message: 'Missing party pin' });
const roomNameTaken = await admin
const roomId = await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.get()
.then((docSnap) => docSnap.exists);
if (roomNameTaken) return res.status(409).send({ message: 'That party name is already taken' });
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.set({
name: bodydata.partyName,
pin: bodydata.partyPin,
.add({
name: bodydata.roomName,
player: bodydata.player,
createdAt: new Date(),
updatedAt: new Date(),
})
.then((ref) => ref.id)
.catch((err) => {
res.status(500).send({ message: 'Unable to start party.' });
console.error(err);
@@ -31,7 +21,7 @@ module.exports = async (req, res) => {
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(roomId)
.collection('messages')
.add({
uid: '<system>',
@@ -40,15 +30,12 @@ module.exports = async (req, res) => {
time: new Date(),
})
.catch((err) => {
console.error(
`Failed to add system create party message : \n\t Room: ${bodydata.partyName}`,
err
);
console.error(`Failed to add system create party message : \n\t Room: ${roomId}`, err);
});
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(roomId)
.collection('users')
.doc(bodydata.uid)
.set({
@@ -57,9 +44,9 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(
`Failed to set admin when creating party : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
`Failed to set admin when creating party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
err
);
});
res.status(201).send({ roomId: bodydata.partyName, message: 'Started a party!' });
res.status(201).send({ roomId, message: 'Started a party!' });
};
-6
View File
@@ -8,9 +8,6 @@ const leaveRoom = require('./leaveRoom');
const deleteRoom = require('./deleteRoom');
const timeDiff = require('./timeDiff');
const updatePlayer = require('./updatePlayer');
const tokenValidator = require('./tokenValidator');
const tokenRefresher = require('./tokenRefresher');
const updateVoxeet = require('./updateVoxeet');
module.exports = {
authenticator,
@@ -23,7 +20,4 @@ module.exports = {
scheduledCleanUp,
cleanUpRoom,
timeDiff,
tokenValidator,
tokenRefresher,
updateVoxeet,
};
+25 -52
View File
@@ -2,72 +2,46 @@ const admin = require('firebase-admin');
module.exports = async (req, res) => {
const bodydata = JSON.parse(req.body);
console.log('Requested Join - UID - ', bodydata.uid, '\n\tRoom - ', bodydata.partyName);
// GET ROOM EXISTS, ROOM SOURCE, & ROOM PIN
const [exists, roomSource, roomPin] = await admin
console.log('Requested Join - UID - ', bodydata.uid, '\n\tRoom - ', bodydata.roomId);
const [exists, roomSource] = await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(bodydata.roomId)
.get()
.then((roomSnap) => [
roomSnap.exists,
roomSnap.exists ? roomSnap.data().player.source : '',
roomSnap.exists ? roomSnap.data().pin : '',
])
.then((roomSnap) => [roomSnap.exists, roomSnap.exists ? roomSnap.data().player.source : ''])
.catch((err) => {
console.error(
`Failed to get room when joining room : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
`Failed to get room when joining room : \n\t Room: ${bodydata.roomId} \n\t User: ${bodydata.uid}`,
err
);
});
if (exists && roomPin !== bodydata.partyPin)
return res.status(401).send({ message: 'Incorrect party pin' });
// GET IF USER EXISTS AND IS ADMIN
const [userExists, userAdmin, userName] = await admin
const [userExists, userAdmin] = await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(bodydata.roomId)
.collection('users')
.doc(bodydata.uid)
.get()
.then((userSnap) => [
userSnap.exists,
userSnap.exists ? userSnap.data().admin : false,
userSnap.exists ? userSnap.data().name : '',
])
.then((userSnap) => [userSnap.exists, userSnap.exists ? userSnap.data().admin : false])
.catch((err) => {
console.error(
`Failed to get user when joining room : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
`Failed to get user when joining room : \n\t Room: ${bodydata.roomId} \n\t User: ${bodydata.uid}`,
err
);
});
// IF USER PRE-EXISTS AND SAME SOURCE - JOIN
if (userExists && exists && roomSource === bodydata.source) {
// UPDATE NAME IF NEEDED
if (userName !== bodydata.userName) {
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.collection('users')
.doc(body.uid)
.update({ name: bodydata.userName });
}
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
}
// USER DOES NOT PRE-EXIST BUT SAME SOURCE AND RIGHT PIN
if (userExists && exists && roomSource === bodydata.source)
res.status(200).send({ roomId: bodydata.roomId, message: 'Joined a party' });
else if (
!userExists &&
exists &&
bodydata.partyName &&
bodydata.roomId &&
bodydata.userName &&
roomSource === bodydata.source &&
roomPin === bodydata.partyPin
roomSource === bodydata.source
) {
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(bodydata.roomId)
.collection('messages')
.add({
uid: '<system>',
@@ -77,14 +51,14 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(
`Failed to add system join party message : \n\t Room: ${partyName} \n\t ${bodydata.uid}`,
`Failed to add system join party message : \n\t Room: ${roomId} \n\t ${bodydata.uid}`,
err
);
});
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(bodydata.roomId)
.collection('users')
.doc(bodydata.uid)
.set({
@@ -93,37 +67,36 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(
`Failed to set admin when joining party : \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`,
`Failed to set admin when joining party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
err
);
});
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(bodydata.roomId)
.update({
updatedAt: new Date(),
})
.catch((err) => {
console.error(
`Failed to set updated time when joining party : \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`,
`Failed to set updated time when joining party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
err
);
});
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
}
// ONLY WRONG SOURCE
res.status(200).send({ roomId: bodydata.roomId, message: 'Joined a party' });
} else if (!userExists && !bodydata.userName)
res.status(400).send({ message: 'Missing user name' });
else if (exists && roomSource !== bodydata.source) {
// MOVE SOURCE IF ADMIN
if (userAdmin === true) {
await admin
.firestore()
.collection('rooms')
.doc(bodydata.partyName)
.doc(bodydata.roomId)
.update({
player: {
showName: null,
source: null,
source: roomSource,
status: 'NOT_PLAYING',
time: 0,
updatedBy: bodydata.uid,
@@ -131,7 +104,7 @@ module.exports = async (req, res) => {
})
.catch(() => {
console.error(
`Failed to update player source when admin joining party \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`
`Failed to update player source when admin joining party \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`
);
res.status(500).send({ message: 'Unable to move party, please try again' });
});
+2 -2
View File
@@ -1,7 +1,7 @@
const admin = require('firebase-admin');
module.exports = async (req, res) => {
const { name, text, time, uid, roomId, type } = JSON.parse(req.body);
const { name, text, uid, roomId, type } = JSON.parse(req.body);
const roomExists = await admin
.firestore()
@@ -24,7 +24,7 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(`Failed to send message to party : \n\t Room: ${roomId} \n\t ${uid}`, err);
res.status(500).send({ message: "Couldn't send your message", ...messageErr });
res.status(500).send({ message: "Couldn't send your message", error: messageErr });
});
await admin
.firestore()
-20
View File
@@ -1,20 +0,0 @@
const VoxeetSecret = require('../VoxeetSecret.json');
module.exports = async (req, res) => {
const { key, secret } = VoxeetSecret;
const credentials = btoa(encodeURI(key) + ':' + encodeURI(secret));
const headers = {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
Authorization: 'Basic ' + credentials,
};
const oauth = await fetch(VoxeetSecret.refreshURL, {
method: 'POST',
headers,
body: JSON.stringify({
refresh_token: req.params.token,
}),
})
.then((oauthResponse) => oauthResponse.json())
.catch((err) => res.status(500).send(err));
if (oauth) res.status(200).send(oauth);
};
-21
View File
@@ -1,21 +0,0 @@
const VoxeetSecret = require('../VoxeetSecret.json');
const fetch = require('node-fetch');
module.exports = async (req, res) => {
const { key, secret } = VoxeetSecret;
const credentials = Buffer.from(encodeURI(key) + ':' + encodeURI(secret)).toString('base64');
const headers = {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
Authorization: 'Basic ' + credentials,
};
const oauth = await fetch(VoxeetSecret.validateURL, {
method: 'POST',
headers,
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);
};
+29 -29
View File
@@ -2,18 +2,18 @@ const admin = require('firebase-admin');
module.exports = async (req, res) => {
const { player, roomId, uid, showChanged, userName } = JSON.parse(req.body);
// const statusString =
// player.status === 'PLAYING'
// ? 'started playing from'
// : player.status === 'PAUSED'
// ? 'paused at'
// : player.status === 'NOT_PLAYING'
// ? 'stopped at'
// : 'is at';
// const timeString =
// player.time > 3600
// ? new Date(player.time * 1000).toISOString().substr(11, 8)
// : new Date(player.time * 1000).toISOString().substr(14, 5);
const statusString =
player.status === 'PLAYING'
? 'started playing from'
: player.status === 'PAUSED'
? 'paused at'
: player.status === 'NOT_PLAYING'
? 'stopped at'
: 'is at';
const timeString =
player.time > 3600
? new Date(player.time * 1000).toISOString().substr(11, 8)
: new Date(player.time * 1000).toISOString().substr(14, 5);
const [roomExists, roomData] = await admin
.firestore()
.collection('rooms')
@@ -55,23 +55,23 @@ module.exports = async (req, res) => {
console.error(`Failed to update player : \n\t Room: ${roomId} \n\t ${uid}`, err);
res.status(500).send({ message: "Uh oh party could't be synced", err });
});
// await admin
// .firestore()
// .collection('rooms')
// .doc(roomId)
// .collection('messages')
// .add({
// uid: '<system>',
// name: '',
// text: `${userName} ${statusString} ${timeString}`,
// time: new Date(),
// })
// .catch((err) => {
// console.error(
// `Failed to add system update player message : \n\t Room: ${roomId} \n\t ${uid}`,
// err
// );
// });
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.collection('messages')
.add({
uid: '<system>',
name: '',
text: `${userName} ${statusString} ${timeString}`,
time: new Date(),
})
.catch((err) => {
console.error(
`Failed to add system update player message : \n\t Room: ${roomId} \n\t ${uid}`,
err
);
});
return res.status(200).send({ message: 'Synced party player' });
} else {
return res.status(208).send({ message: 'Player already up to date' });
-47
View File
@@ -1,47 +0,0 @@
const admin = require('firebase-admin');
module.exports = async (req, res) => {
const { conference, partyName, partyPin, userName } = JSON.parse(req.body);
// GET ROOM PIN
const roomPin = await admin
.firestore()
.collection('rooms')
.doc(partyName)
.get()
.then((roomSnap) => (roomSnap.exists ? roomSnap.data().pin : null));
// 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' });
}
} else {
// ROOM DOESN'T EXIST
return res.status(404).send({ message: 'Party Not Found' });
}
};