v1.3.0-beta/release
This commit is contained in:
@@ -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' });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user