From 6bb38a61bfb86e19e0f3e31de314e66c9a581a98 Mon Sep 17 00:00:00 2001 From: Ayushya Amitabh Date: Sat, 8 Aug 2020 20:39:09 -0400 Subject: [PATCH] v1.6.0/release --- functions/modules/createRoom.js | 43 ++++++++++-------- functions/modules/joinRoom.js | 76 ++++++++++++++++---------------- functions/modules/sendMessage.js | 32 +++++--------- functions/modules2/createRoom.js | 14 ------ 4 files changed, 74 insertions(+), 91 deletions(-) diff --git a/functions/modules/createRoom.js b/functions/modules/createRoom.js index 714e50a..0ab8529 100644 --- a/functions/modules/createRoom.js +++ b/functions/modules/createRoom.js @@ -1,43 +1,48 @@ -const admin = require('firebase-admin'); +const admin = require("firebase-admin"); 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' }); + 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 .firestore() - .collection('rooms') - .doc(bodydata.partyName.replace('/', '\\')) + .collection("rooms") + .doc(bodydata.partyName.replace("/", "\\")) .get() .then((docSnap) => docSnap.exists); - if (roomNameTaken) return res.status(409).send({ message: 'That party name is already taken' }); + if (roomNameTaken) + return res + .status(409) + .send({ message: "That party name is already taken" }); await admin .firestore() - .collection('rooms') + .collection("rooms") .doc(bodydata.partyName) .set({ name: bodydata.partyName, pin: bodydata.partyPin, player: bodydata.player, createdAt: new Date(), - updatedAt: new Date(), adminUid: bodydata.uid, adminOnly: bodydata.adminOnly, }) .catch((err) => { - res.status(500).send({ message: 'Unable to start party.' }); + res.status(500).send({ message: "Unable to start party." }); console.error(err); }); await admin .firestore() - .collection('rooms') + .collection("rooms") .doc(bodydata.partyName) - .collection('messages') + .collection("messages") .add({ - uid: '', - name: '', + uid: "", + name: "", text: `${bodydata.userName} started the party.`, time: new Date(), }) @@ -49,9 +54,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: true, @@ -63,5 +68,7 @@ module.exports = async (req, res) => { err ); }); - res.status(201).send({ roomId: bodydata.partyName, message: 'Started a party!' }); + res + .status(201) + .send({ roomId: bodydata.partyName, message: "Started a party!" }); }; diff --git a/functions/modules/joinRoom.js b/functions/modules/joinRoom.js index 0f4192d..53add0f 100644 --- a/functions/modules/joinRoom.js +++ b/functions/modules/joinRoom.js @@ -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,13 +27,13 @@ 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" }); if (adminUid === bodydata.uid) { await admin .firestore() - .collection('rooms') + .collection("rooms") .doc(bodydata.partyName) - .collection('users') + .collection("users") .doc(bodydata.uid) .set({ admin: true, @@ -38,15 +43,15 @@ module.exports = async (req, res) => { // 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( @@ -60,13 +65,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 ( @@ -79,12 +86,12 @@ module.exports = async (req, res) => { ) { await admin .firestore() - .collection('rooms') + .collection("rooms") .doc(bodydata.partyName) - .collection('messages') + .collection("messages") .add({ - uid: '', - name: '', + uid: "", + name: "", text: `${bodydata.userName} joined the party.`, time: new Date(), }) @@ -96,9 +103,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: false, @@ -110,20 +117,9 @@ module.exports = async (req, res) => { err ); }); - await admin - .firestore() - .collection('rooms') - .doc(bodydata.partyName) - .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}`, - 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) { @@ -131,13 +127,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, }, @@ -146,9 +142,11 @@ 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" }); }; diff --git a/functions/modules/sendMessage.js b/functions/modules/sendMessage.js index c79f66d..103468c 100644 --- a/functions/modules/sendMessage.js +++ b/functions/modules/sendMessage.js @@ -1,47 +1,39 @@ -const admin = require('firebase-admin'); +const admin = require("firebase-admin"); module.exports = async (req, res) => { const { name, text, uid, roomId, type, replyTo } = JSON.parse(req.body); const roomExists = await admin .firestore() - .collection('rooms') + .collection("rooms") .doc(roomId) .get() .then((roomSnap) => roomSnap.exists); if (roomExists) { await admin .firestore() - .collection('rooms') + .collection("rooms") .doc(roomId) - .collection('messages') + .collection("messages") .add({ name, - text: text || '', + text: text || "", time: new Date(), uid, - type: type || '', + type: type || "", replyTo: replyTo || null, }) - .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", error: messageErr }); - }); - await admin - .firestore() - .collection('rooms') - .doc(roomId) - .update({ - updatedAt: new Date(), - }) .catch((err) => { console.error( - `Failed to set updated time when sending messasge to party : \n\t Room: ${roomId} \n\t User: ${uid}`, + `Failed to send message to party : \n\t Room: ${roomId} \n\t ${uid}`, err ); + res + .status(500) + .send({ message: "Couldn't send your message", error: messageErr }); }); - res.status(200).send({ message: 'Sent a message' }); + res.status(200).send({ message: "Sent a message" }); } else { - res.status(404).send({ message: 'Looks like this party is over' }); + res.status(404).send({ message: "Looks like this party is over" }); } }; diff --git a/functions/modules2/createRoom.js b/functions/modules2/createRoom.js index 325e6be..15e35b9 100644 --- a/functions/modules2/createRoom.js +++ b/functions/modules2/createRoom.js @@ -81,20 +81,6 @@ module.exports = async (req, res) => { time: new Date(), }); - if (player.source === "peacocktv.") { - admin - .firestore() - .collection("rooms") - .doc(bodydata.partyName) - .collection("messages") - .add({ - uid: "", - name: "", - text: `Sync may not work flawlessly with PeacockTV till Monday's update. Please stick with us as we address this issue.`, - time: new Date(), - }); - } - res .status(201) .send({ roomId: bodydata.partyName, message: "Started a party!" });