v.1.7.9/webhook room cleanup update

This commit is contained in:
2021-02-26 15:46:33 -05:00
parent d3c1895abb
commit 470e335d91
6 changed files with 3801 additions and 307 deletions
+36 -24
View File
@@ -1,19 +1,24 @@
const admin = require('firebase-admin');
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);
console.log(
"Requested Join - UID - ",
bodydata.uid,
"\n\tRoom - ",
bodydata.partyName
);
// GET ROOM EXISTS, ROOM SOURCE, & ROOM PIN
const [exists, roomSource, roomPin, adminUid] = await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.get()
.then((roomSnap) => [
roomSnap.exists,
roomSnap.exists ? roomSnap.data().player.source : '',
roomSnap.exists ? roomSnap.data().pin : '',
roomSnap.exists ? roomSnap.data().adminUid : '',
roomSnap.exists ? roomSnap.data().player.source : "",
roomSnap.exists ? roomSnap.data().pin : "",
roomSnap.exists ? roomSnap.data().adminUid : "",
])
.catch((err) => {
console.error(
@@ -22,19 +27,19 @@ module.exports = async (req, res) => {
);
});
if (exists && roomPin !== bodydata.partyPin)
return res.status(401).send({ message: 'Incorrect party pin' });
return res.status(401).send({ message: "Incorrect party pin" });
// GET IF USER EXISTS AND IS ADMIN
const [userExists, userAdmin, userName] = await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('users')
.collection("users")
.doc(bodydata.uid)
.get()
.then((userSnap) => [
userSnap.exists,
userSnap.exists ? userSnap.data().admin : false,
userSnap.exists ? userSnap.data().name : '',
userSnap.exists ? userSnap.data().name : "",
])
.catch((err) => {
console.error(
@@ -48,13 +53,15 @@ module.exports = async (req, res) => {
if (userName !== bodydata.userName) {
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('users')
.collection("users")
.doc(bodydata.uid)
.update({ name: bodydata.userName });
}
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
res
.status(200)
.send({ partyName: bodydata.partyName, message: "Joined a party" });
}
// USER DOES NOT PRE-EXIST BUT SAME SOURCE AND RIGHT PIN
else if (
@@ -67,12 +74,12 @@ module.exports = async (req, res) => {
) {
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('messages')
.collection("messages")
.add({
uid: '<system>',
name: '',
uid: "<system>",
name: "",
text: `${bodydata.userName} joined the party.`,
time: new Date(),
})
@@ -84,9 +91,9 @@ module.exports = async (req, res) => {
});
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('users')
.collection("users")
.doc(bodydata.uid)
.set({
admin: adminUid === bodydata.uid,
@@ -98,7 +105,9 @@ module.exports = async (req, res) => {
err
);
});
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
res
.status(200)
.send({ partyName: bodydata.partyName, message: "Joined a party" });
}
// ONLY WRONG SOURCE
else if (exists && roomSource !== bodydata.source) {
@@ -106,13 +115,13 @@ module.exports = async (req, res) => {
if (userAdmin === true) {
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.update({
player: {
showName: null,
source: bodydata.source,
status: 'NOT_PLAYING',
status: "NOT_PLAYING",
time: 0,
updatedBy: bodydata.uid,
},
@@ -121,9 +130,12 @@ module.exports = async (req, res) => {
console.error(
`Failed to update player source when admin joining party \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`
);
res.status(500).send({ message: 'Unable to move party, please try again' });
res
.status(500)
.send({ message: "Unable to move party, please try again" });
});
res.status(200).send({ message: `Moved party to ${roomSource}` });
} else res.status(417).send({ message: `This party is on ${roomSource}` });
} else res.status(404).send({ message: 'Party Not Found' });
} else res.status(404).send({ message: "Party Not Found" });
return;
};