v1.0.0-beta/amazon styling fix

This commit is contained in:
Ayushya Amitabh
2020-04-22 13:13:59 -04:00
parent 9604d8c448
commit d301a61349
5 changed files with 160 additions and 41 deletions
+88 -22
View File
@@ -42,7 +42,7 @@ const authenticator = (req, res, next) => {
const createRoom = async (req, res) => {
const bodydata = JSON.parse(req.body);
console.log('Requested Create - UID - ', bodydata.uid);
if (!bodydata.userName) res.status(400).send('Missing user name');
if (!bodydata.userName) res.status(400).send({ message: 'Missing user name' });
const roomId = await admin
.firestore()
.collection('rooms')
@@ -54,6 +54,7 @@ const createRoom = async (req, res) => {
})
.then((ref) => ref.id)
.catch((err) => {
res.status(500).send({ message: 'Unable to start party.' });
console.error(err);
});
await admin
@@ -66,6 +67,9 @@ const createRoom = async (req, res) => {
name: '',
text: `${bodydata.userName} started the party.`,
time: new Date().toISOString(),
})
.catch((err) => {
console.error(`Failed to add system create party message : \n\t Room: ${roomId}`, err);
});
await admin
.firestore()
@@ -76,6 +80,12 @@ const createRoom = async (req, res) => {
.set({
admin: true,
name: bodydata.userName,
})
.catch((err) => {
console.error(
`Failed to set admin when creating party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
err
);
});
res.status(201).send({ roomId });
};
@@ -83,12 +93,12 @@ const createRoom = async (req, res) => {
const joinRoom = async (req, res) => {
const bodydata = JSON.parse(req.body);
console.log('Requested Join - UID - ', bodydata.uid, '\n\tRoom - ', bodydata.roomId);
const exists = await admin
const [exists, roomSource] = await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.get()
.then((roomSnap) => roomSnap.exists);
.then((roomSnap) => [roomSnap.exists, roomSnap.exists ? roomSnap.data().player.source : '']);
const userExists = await admin
.firestore()
.collection('rooms')
@@ -97,8 +107,15 @@ const joinRoom = async (req, res) => {
.doc(bodydata.uid)
.get()
.then((userSnap) => userSnap.exists);
if (userExists) res.status(200).send({ roomId: bodydata.roomId });
else if (!userExists && exists && bodydata.roomId && bodydata.userName) {
if (userExists && exists && roomSource === bodydata.source)
res.status(200).send({ roomId: bodydata.roomId });
else if (
!userExists &&
exists &&
bodydata.roomId &&
bodydata.userName &&
roomSource === bodydata.source
) {
await admin
.firestore()
.collection('rooms')
@@ -109,6 +126,12 @@ const joinRoom = async (req, res) => {
name: '',
text: `${bodydata.userName} joined the party.`,
time: new Date().toISOString(),
})
.catch((err) => {
console.error(
`Failed to add system join party message : \n\t Room: ${roomId} \n\t ${bodydata.uid}`,
err
);
});
await admin
.firestore()
@@ -119,34 +142,63 @@ const joinRoom = async (req, res) => {
.set({
admin: false,
name: bodydata.userName,
})
.catch((err) => {
console.error(
`Failed to set admin when joining party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
err
);
});
await admin
.firestore()
.collection('rooms')
.doc(bodydata.roomId)
.update({
updatedAt: new Date().toISOString(),
})
.catch((err) => {
console.error(
`Failed to set updated time when joining party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
err
);
});
await admin.firestore().collection('rooms').doc(bodydata.roomId).update({
updatedAt: new Date().toISOString(),
});
res.status(200).send({ roomId: bodydata.roomId });
} else if (!userExists && !bodydata.userName)
res.status(400).send({ message: 'Missing user name' });
else if (exists && roomSource !== bodydata.source)
res.status(417).send({ message: `This party is on ${roomSource}` });
else res.status(404).send({ message: 'Party Not Found' });
};
const sendMessage = async (req, res) => {
const { name, text, time, uid, roomId } = JSON.parse(req.body);
await admin
const roomExists = await admin
.firestore()
.collection('rooms')
.doc(roomId)
.collection('messages')
.add({
name,
text,
time,
uid,
})
.catch((messageErr) => res.status(500).send(messageErr));
await admin.firestore().collection('rooms').doc(roomId).update({
updatedAt: new Date().toISOString(),
});
res.status(200).send({ message: 'Sent a message' });
.get()
.then((roomSnap) => roomSnap.exists);
if (roomExists) {
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.collection('messages')
.add({
name,
text,
time,
uid,
})
.catch((messageErr) => res.status(500).send(messageErr));
await admin.firestore().collection('rooms').doc(roomId).update({
updatedAt: new Date().toISOString(),
});
res.status(200).send({ message: 'Sent a message' });
} else {
res.status(404).send({ message: 'Looks like this party is over' });
}
};
const leaveRoom = async (req, res) => {
@@ -158,7 +210,12 @@ const leaveRoom = async (req, res) => {
.collection('users')
.doc(req.params.uid)
.delete()
.catch((err) => res.status(500).send(err));
.catch((err) =>
console.error(
`Failed to remove user from party : \n\t Room: ${req.params.rid} \n\t User: ${req.params.uid}`,
err
)
);
res.status(200).send({ message: 'Left the party' });
};
@@ -297,3 +354,12 @@ app.delete('/delete/:rid', deleteRoom);
app.post('/updatePlayer', updatePlayer);
exports.widgets = functions.https.onRequest(app);
exports.scheduledCleanUp = functions.pubsub.schedule('every 1 hour').onRun(async () => {
const roomsList = await admin
.firestore()
.collection('rooms')
.listDocuments()
.then((rL) => rL.map(async (r) => await r.get()));
console.log(roomsList);
});