v1.0.0-beta

This commit is contained in:
Ayushya Amitabh
2020-04-22 11:17:06 -04:00
parent 4225ad7d3e
commit 9604d8c448
13 changed files with 439 additions and 213 deletions
+69 -21
View File
@@ -5,11 +5,11 @@ const express = require('express');
const cors = require('cors');
const authenticator = (req, res, next) => {
res.set('Access-Control-Allow-Origin', ['*']);
if (req.method === 'OPTIONS') {
res.status(200).send();
return next();
}
const token = req.headers.authorization.split(' ')[1];
const reqToken = token.split(':')[0];
const reqUid = token.split(':')[1];
@@ -97,8 +97,8 @@ const joinRoom = async (req, res) => {
.doc(bodydata.uid)
.get()
.then((userSnap) => userSnap.exists);
if (userExists) return res.status(200).send({ roomId: bodydata.roomId });
if (exists && bodydata.roomId && bodydata.userName) {
if (userExists) res.status(200).send({ roomId: bodydata.roomId });
else if (!userExists && exists && bodydata.roomId && bodydata.userName) {
await admin
.firestore()
.collection('rooms')
@@ -124,9 +124,9 @@ const joinRoom = async (req, res) => {
updatedAt: new Date().toISOString(),
});
res.status(200).send({ roomId: bodydata.roomId });
}
if (!bodydata.userName) res.status(400).send('Missing user name');
else res.status(404).send('Party Not Found');
} else if (!userExists && !bodydata.userName)
res.status(400).send({ message: 'Missing user name' });
else res.status(404).send({ message: 'Party Not Found' });
};
const sendMessage = async (req, res) => {
@@ -143,9 +143,10 @@ const sendMessage = async (req, res) => {
uid,
})
.catch((messageErr) => res.status(500).send(messageErr));
await admin.firestore().collection('rooms').doc(bodydata.roomId).update({
await admin.firestore().collection('rooms').doc(roomId).update({
updatedAt: new Date().toISOString(),
});
res.status(200).send({ message: 'Sent a message' });
};
const leaveRoom = async (req, res) => {
@@ -158,7 +159,7 @@ const leaveRoom = async (req, res) => {
.doc(req.params.uid)
.delete()
.catch((err) => res.status(500).send(err));
res.status(200).send();
res.status(200).send({ message: 'Left the party' });
};
const deleteRoom = async (req, res) => {
@@ -179,7 +180,7 @@ const deleteRoom = async (req, res) => {
});
if (!isAdmin) {
console.log('[UNAUTHORIZED] Requested Delete - Room - ', req.params.rid);
res.status(401).send();
res.status(401).send({ message: 'Not authorized to end this party.' });
return;
}
await admin
@@ -204,11 +205,30 @@ const deleteRoom = async (req, res) => {
.doc(req.params.rid)
.delete()
.catch((err) => res.status(500).send(err));
res.status(200).send();
res.status(200).send({ message: 'Ended the party' });
};
const updatePlayer = async (req, res) => {
const { player, roomId, uid } = JSON.parse(req.body);
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 [roomExists, roomData] = await admin
.firestore()
.collection('rooms')
.doc(roomId)
.get()
.then((docSnap) => [docSnap.exists, docSnap.data()]);
if (!roomExists) return res.status(404).send({ message: 'Party not found.' });
const userExists = await admin
.firestore()
.collection('rooms')
@@ -218,17 +238,45 @@ const updatePlayer = async (req, res) => {
.get()
.then((userSnap) => userSnap.exists);
if (userExists) {
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.update({
player,
})
.catch((err) => res.status(500).send(err));
res.status(200).send();
if (showChanged === true) {
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.collection('messages')
.add({
uid: '<system>',
name: '',
text: `${userName} changed show to \n${player.showName}`,
time: new Date().toISOString(),
});
}
if (roomData.status !== player.status) {
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.update({
player,
})
.catch((err) => res.status(500).send(err));
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.collection('messages')
.add({
uid: '<system>',
name: '',
text: `${userName} ${statusString} ${timeString}`,
time: new Date().toISOString(),
});
return res.status(200).send({ message: 'Synced party player' });
} else {
return res.status(208).send({ message: 'Player already up to date' });
}
} else {
res.status(401).send('Not in room to sync');
return res.status(401).send('Not in room to sync');
}
};