v1.3.0-beta/UI redone

This commit is contained in:
2020-05-31 00:35:24 -04:00
parent fba32fd7d1
commit aa15c4eb30
21 changed files with 676 additions and 333 deletions
+1
View File
@@ -0,0 +1 @@
node_modules/
-4
View File
@@ -8,10 +8,6 @@ const FIREBASE_OPTIONS = {
credential: admin.credential.cert(secret),
databaseURL: 'https://watch-party-crx.firebaseio.com',
};
if (process.env.NODE_ENV === 'DEV') {
FIREBASE_OPTIONS.databaseURL = 'http://localhost:4000/firestore';
console.info('RUNNING IN DEV ENV');
}
admin.initializeApp(FIREBASE_OPTIONS);
+6 -4
View File
@@ -1,3 +1,5 @@
const admin = require('firebase-admin');
module.exports = (req, res, next) => {
res.set('Access-Control-Allow-Origin', ['*']);
if (req.method === 'OPTIONS') {
@@ -30,11 +32,11 @@ module.exports = (req, res, next) => {
})
.catch((err) => {
console.error('Failed Auth - ', req.headers.authorization, '\n', err);
return res.status(500).send({ message: 'Unable to verify user token', ...err });
return res.status(500).send({ message: 'Unable to verify user token' });
});
} catch (err) {
return res.status(401).send({ message: 'Got no user token', ...err });
console.log(err);
return res.status(401).send({ message: 'Something went wrong when authorizing' });
}
}
return res.status(401).send({ message: 'Got no user token' });
} else return res.status(401).send({ message: 'Got no user token' });
};
+11 -9
View File
@@ -4,20 +4,22 @@ 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.roomName) res.status(400).send({ message: 'Missing party 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.roomName)
.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.roomName)
.doc(bodydata.partyName)
.set({
name: bodydata.roomName,
name: bodydata.partyName,
pin: bodydata.partyPin,
player: bodydata.player,
createdAt: new Date(),
updatedAt: new Date(),
@@ -29,7 +31,7 @@ module.exports = async (req, res) => {
await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomName)
.doc(bodydata.partyName)
.collection('messages')
.add({
uid: '<system>',
@@ -39,14 +41,14 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(
`Failed to add system create party message : \n\t Room: ${bodydata.roomName}`,
`Failed to add system create party message : \n\t Room: ${bodydata.partyName}`,
err
);
});
await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomName)
.doc(bodydata.partyName)
.collection('users')
.doc(bodydata.uid)
.set({
@@ -55,9 +57,9 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(
`Failed to set admin when creating party : \n\t Room: ${bodydata.roomName} \n\t User: ${bodydata.uid}`,
`Failed to set admin when creating party : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
err
);
});
res.status(201).send({ roomId: bodydata.roomName, message: 'Started a party!' });
res.status(201).send({ roomId: bodydata.partyName, message: 'Started a party!' });
};
+52 -25
View File
@@ -2,46 +2,72 @@ 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.roomId);
const [exists, roomSource] = await admin
console.log('Requested Join - UID - ', bodydata.uid, '\n\tRoom - ', bodydata.partyName);
// GET ROOM EXISTS, ROOM SOURCE, & ROOM PIN
const [exists, roomSource, roomPin] = await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.doc(bodydata.partyName)
.get()
.then((roomSnap) => [roomSnap.exists, roomSnap.exists ? roomSnap.data().player.source : ''])
.then((roomSnap) => [
roomSnap.exists,
roomSnap.exists ? roomSnap.data().player.source : '',
roomSnap.exists ? roomSnap.data().pin : '',
])
.catch((err) => {
console.error(
`Failed to get room when joining room : \n\t Room: ${bodydata.roomId} \n\t User: ${bodydata.uid}`,
`Failed to get room when joining room : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
err
);
});
const [userExists, userAdmin] = await admin
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
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.doc(bodydata.partyName)
.collection('users')
.doc(bodydata.uid)
.get()
.then((userSnap) => [userSnap.exists, userSnap.exists ? userSnap.data().admin : false])
.then((userSnap) => [
userSnap.exists,
userSnap.exists ? userSnap.data().admin : false,
userSnap.exists ? userSnap.data().name : '',
])
.catch((err) => {
console.error(
`Failed to get user when joining room : \n\t Room: ${bodydata.roomId} \n\t User: ${bodydata.uid}`,
`Failed to get user when joining room : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
err
);
});
if (userExists && exists && roomSource === bodydata.source)
res.status(200).send({ roomId: bodydata.roomId, message: 'Joined a party' });
// 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
else if (
!userExists &&
exists &&
bodydata.roomId &&
bodydata.partyName &&
bodydata.userName &&
roomSource === bodydata.source
roomSource === bodydata.source &&
roomPin === bodydata.partyPin
) {
await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.doc(bodydata.partyName)
.collection('messages')
.add({
uid: '<system>',
@@ -51,14 +77,14 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(
`Failed to add system join party message : \n\t Room: ${roomId} \n\t ${bodydata.uid}`,
`Failed to add system join party message : \n\t Room: ${partyName} \n\t ${bodydata.uid}`,
err
);
});
await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.doc(bodydata.partyName)
.collection('users')
.doc(bodydata.uid)
.set({
@@ -67,36 +93,37 @@ module.exports = async (req, res) => {
})
.catch((err) => {
console.error(
`Failed to set admin when joining party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
`Failed to set admin when joining party : \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`,
err
);
});
await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.doc(bodydata.partyName)
.update({
updatedAt: new Date(),
})
.catch((err) => {
console.error(
`Failed to set updated time when joining party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
`Failed to set updated time when joining party : \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`,
err
);
});
res.status(200).send({ roomId: bodydata.roomId, message: 'Joined a party' });
} else if (!userExists && !bodydata.userName)
res.status(400).send({ message: 'Missing user name' });
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
}
// ONLY WRONG SOURCE
else if (exists && roomSource !== bodydata.source) {
// MOVE SOURCE IF ADMIN
if (userAdmin === true) {
await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.doc(bodydata.partyName)
.update({
player: {
showName: null,
source: roomSource,
source: null,
status: 'NOT_PLAYING',
time: 0,
updatedBy: bodydata.uid,
@@ -104,7 +131,7 @@ module.exports = async (req, res) => {
})
.catch(() => {
console.error(
`Failed to update player source when admin joining party \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`
`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' });
});
+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' });
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "watchparty",
"version": "1.0.0-beta",
"name": "watch-party-api",
"version": "1.2.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {