v.1.4.3/api update
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "watch-party-crx",
|
"name": "watch-party-crx",
|
||||||
"version": "1.4.2",
|
"version": "1.4.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@giphy/js-fetch-api": "^1.5.8",
|
"@giphy/js-fetch-api": "^1.5.8",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "Watch Party",
|
"name": "Watch Party",
|
||||||
"short_name": "WP",
|
"short_name": "WP",
|
||||||
"version": "1.4.2",
|
"version": "1.4.3",
|
||||||
"description": "Watch streamed content together.",
|
"description": "Watch streamed content together.",
|
||||||
"background": {
|
"background": {
|
||||||
"page": "background.html"
|
"page": "background.html"
|
||||||
|
|||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
/*global chrome*/
|
/*global chrome*/
|
||||||
|
|
||||||
const API_HOST = window.location.href.startsWith('http://localhost')
|
const API_HOST = window.location.href.startsWith('http://localhost')
|
||||||
? 'http://localhost:5000/watch-party-crx/us-central1/widgets2'
|
? 'http://localhost:5000/watch-party-crx/us-central1/widgets'
|
||||||
: 'https://us-central1-watch-party-crx.cloudfunctions.net/widgets2';
|
: 'https://us-central1-watch-party-crx.cloudfunctions.net/widgets';
|
||||||
|
|
||||||
export const PLAYER_STATUS = {
|
export const PLAYER_STATUS = {
|
||||||
PLAYING: 'PLAYING',
|
PLAYING: 'PLAYING',
|
||||||
|
|||||||
+5
-16
@@ -1,33 +1,22 @@
|
|||||||
const FunctionModules = require('./modules/index');
|
const FunctionModules = require('./modules/index');
|
||||||
const FunctionModules2 = require('./modules2/index');
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
|
||||||
// VERSION 1
|
// VERSION 1
|
||||||
|
// VERSION 2
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors({ origin: true }));
|
app.use(cors({ origin: true }));
|
||||||
app.use(FunctionModules.authenticator);
|
app.use(FunctionModules.authenticator);
|
||||||
|
app.get('/validator', FunctionModules.tokenValidator);
|
||||||
|
app.get('/refresher/:token', FunctionModules.tokenRefresher);
|
||||||
app.post('/create', FunctionModules.createRoom);
|
app.post('/create', FunctionModules.createRoom);
|
||||||
app.post('/join', FunctionModules.joinRoom);
|
app.post('/join', FunctionModules.joinRoom);
|
||||||
app.post('/message', FunctionModules.sendMessage);
|
app.post('/message', FunctionModules.sendMessage);
|
||||||
|
app.post('/updatePlayer', FunctionModules.updatePlayer);
|
||||||
|
app.post('/updateVoxeet', FunctionModules.updateVoxeet);
|
||||||
app.delete('/leave/:rid/users/:uid', FunctionModules.leaveRoom);
|
app.delete('/leave/:rid/users/:uid', FunctionModules.leaveRoom);
|
||||||
app.delete('/delete/:rid', FunctionModules.deleteRoom);
|
app.delete('/delete/:rid', FunctionModules.deleteRoom);
|
||||||
app.post('/updatePlayer', FunctionModules.updatePlayer);
|
|
||||||
// VERSION 2
|
|
||||||
const app2 = express();
|
|
||||||
app2.use(cors({ origin: true }));
|
|
||||||
app2.use(FunctionModules2.authenticator);
|
|
||||||
app2.get('/validator', FunctionModules2.tokenValidator);
|
|
||||||
app2.get('/refresher/:token', FunctionModules2.tokenRefresher);
|
|
||||||
app2.post('/create', FunctionModules2.createRoom);
|
|
||||||
app2.post('/join', FunctionModules2.joinRoom);
|
|
||||||
app2.post('/message', FunctionModules2.sendMessage);
|
|
||||||
app2.post('/updatePlayer', FunctionModules2.updatePlayer);
|
|
||||||
app2.post('/updateVoxeet', FunctionModules2.updateVoxeet);
|
|
||||||
app2.delete('/leave/:rid/users/:uid', FunctionModules2.leaveRoom);
|
|
||||||
app2.delete('/delete/:rid', FunctionModules2.deleteRoom);
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
v1: app,
|
v1: app,
|
||||||
v2: app2,
|
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-3
@@ -1,4 +1,4 @@
|
|||||||
const FunctionModules2 = require('./modules2/index');
|
const FunctionModules = require('./modules/index');
|
||||||
const functions = require('firebase-functions');
|
const functions = require('firebase-functions');
|
||||||
const secret = require('./FirebaseSecret.json');
|
const secret = require('./FirebaseSecret.json');
|
||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
@@ -14,8 +14,8 @@ admin.initializeApp(FIREBASE_OPTIONS);
|
|||||||
// VERSION 1
|
// VERSION 1
|
||||||
exports.widgets = functions.https.onRequest(REST.v1);
|
exports.widgets = functions.https.onRequest(REST.v1);
|
||||||
// VERSION 2
|
// VERSION 2
|
||||||
exports.widgets2 = functions.https.onRequest(REST.v2);
|
exports.widgets2 = functions.https.onRequest(REST.v1);
|
||||||
|
|
||||||
exports.scheduledCleanUp = functions.pubsub
|
exports.scheduledCleanUp = functions.pubsub
|
||||||
.schedule('every 60 minutes')
|
.schedule('every 60 minutes')
|
||||||
.onRun(FunctionModules2.scheduledCleanUp);
|
.onRun(FunctionModules.scheduledCleanUp);
|
||||||
|
|||||||
@@ -6,31 +6,37 @@ module.exports = (req, res, next) => {
|
|||||||
res.status(200).send();
|
res.status(200).send();
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
const token = req.headers.authorization.split(' ')[1];
|
if (req.headers.authorization) {
|
||||||
const reqToken = token.split(':')[0];
|
try {
|
||||||
const reqUid = token.split(':')[1];
|
const token = req.headers.authorization.split(' ')[1];
|
||||||
admin
|
const reqToken = token.split(':')[0];
|
||||||
.auth()
|
const reqUid = token.split(':')[1];
|
||||||
.verifyIdToken(reqToken)
|
admin
|
||||||
.then((user) => {
|
.auth()
|
||||||
if (user.uid === reqUid) {
|
.verifyIdToken(reqToken)
|
||||||
console.info('Verified Token - UID - ', reqUid);
|
.then((user) => {
|
||||||
if (req.method === 'POST') {
|
if (user.uid === reqUid) {
|
||||||
const bodydata = JSON.parse(req.body);
|
console.info('Verified Token - UID - ', reqUid);
|
||||||
bodydata.uid = user.uid;
|
if (req.method === 'POST') {
|
||||||
req.body = JSON.stringify(bodydata);
|
const bodydata = JSON.parse(req.body);
|
||||||
}
|
bodydata.uid = user.uid;
|
||||||
if (req.method === 'GET' || req.method === 'DELETE') {
|
req.body = JSON.stringify(bodydata);
|
||||||
req.body = JSON.stringify({ uid: user.uid });
|
}
|
||||||
}
|
if (req.method === 'GET' || req.method === 'DELETE') {
|
||||||
return next();
|
req.body = JSON.stringify({ uid: user.uid });
|
||||||
} else {
|
}
|
||||||
return res.status(401).send({ message: 'Unauthorized' });
|
return next();
|
||||||
}
|
} else {
|
||||||
})
|
return res.status(401).send({ message: 'Unauthorized' });
|
||||||
.catch((err) => {
|
}
|
||||||
console.error('Failed Auth - ', req.headers.authorization, '\n', err);
|
})
|
||||||
return res.status(500).send({ message: 'Unable to verify user token', error: err });
|
.catch((err) => {
|
||||||
});
|
console.error('Failed Auth - ', req.headers.authorization, '\n', err);
|
||||||
return null;
|
return res.status(500).send({ message: 'Unable to verify user token' });
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
return res.status(401).send({ message: 'Something went wrong when authorizing' });
|
||||||
|
}
|
||||||
|
} else return res.status(401).send({ message: 'Got no user token' });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
|
|
||||||
module.exports = async (roomId) => {
|
module.exports = async (roomId) => {
|
||||||
await admin
|
admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(roomId)
|
.doc(roomId)
|
||||||
@@ -11,7 +11,7 @@ module.exports = async (roomId) => {
|
|||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(`Failed to run auto clean up (delete room messages) @ ${roomId}`, err);
|
console.error(`Failed to run auto clean up (delete room messages) @ ${roomId}`, err);
|
||||||
});
|
});
|
||||||
await admin
|
admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(roomId)
|
.doc(roomId)
|
||||||
@@ -21,7 +21,7 @@ module.exports = async (roomId) => {
|
|||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(`Failed to run auto clean up (delete room users) @ ${roomId}`, err);
|
console.error(`Failed to run auto clean up (delete room users) @ ${roomId}`, err);
|
||||||
});
|
});
|
||||||
await admin
|
admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(roomId)
|
.doc(roomId)
|
||||||
|
|||||||
@@ -4,16 +4,26 @@ module.exports = async (req, res) => {
|
|||||||
const bodydata = JSON.parse(req.body);
|
const bodydata = JSON.parse(req.body);
|
||||||
console.log('Requested Create - UID - ', bodydata.uid);
|
console.log('Requested Create - UID - ', bodydata.uid);
|
||||||
if (!bodydata.userName) res.status(400).send({ message: 'Missing user name' });
|
if (!bodydata.userName) res.status(400).send({ message: 'Missing user name' });
|
||||||
const roomId = await admin
|
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()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.add({
|
.doc(bodydata.partyName.replace('/', '\\'))
|
||||||
name: bodydata.roomName,
|
.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.partyName)
|
||||||
|
.set({
|
||||||
|
name: bodydata.partyName,
|
||||||
|
pin: bodydata.partyPin,
|
||||||
player: bodydata.player,
|
player: bodydata.player,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
})
|
})
|
||||||
.then((ref) => ref.id)
|
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
res.status(500).send({ message: 'Unable to start party.' });
|
res.status(500).send({ message: 'Unable to start party.' });
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@@ -21,7 +31,7 @@ module.exports = async (req, res) => {
|
|||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(roomId)
|
.doc(bodydata.partyName)
|
||||||
.collection('messages')
|
.collection('messages')
|
||||||
.add({
|
.add({
|
||||||
uid: '<system>',
|
uid: '<system>',
|
||||||
@@ -30,12 +40,15 @@ module.exports = async (req, res) => {
|
|||||||
time: new Date(),
|
time: new Date(),
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(`Failed to add system create party message : \n\t Room: ${roomId}`, err);
|
console.error(
|
||||||
|
`Failed to add system create party message : \n\t Room: ${bodydata.partyName}`,
|
||||||
|
err
|
||||||
|
);
|
||||||
});
|
});
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(roomId)
|
.doc(bodydata.partyName)
|
||||||
.collection('users')
|
.collection('users')
|
||||||
.doc(bodydata.uid)
|
.doc(bodydata.uid)
|
||||||
.set({
|
.set({
|
||||||
@@ -44,9 +57,9 @@ module.exports = async (req, res) => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(
|
console.error(
|
||||||
`Failed to set admin when creating party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
|
`Failed to set admin when creating party : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
|
||||||
err
|
err
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
res.status(201).send({ roomId, message: 'Started a party!' });
|
res.status(201).send({ roomId: bodydata.partyName, message: 'Started a party!' });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ const leaveRoom = require('./leaveRoom');
|
|||||||
const deleteRoom = require('./deleteRoom');
|
const deleteRoom = require('./deleteRoom');
|
||||||
const timeDiff = require('./timeDiff');
|
const timeDiff = require('./timeDiff');
|
||||||
const updatePlayer = require('./updatePlayer');
|
const updatePlayer = require('./updatePlayer');
|
||||||
|
const tokenValidator = require('./tokenValidator');
|
||||||
|
const tokenRefresher = require('./tokenRefresher');
|
||||||
|
const updateVoxeet = require('./updateVoxeet');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
authenticator,
|
authenticator,
|
||||||
@@ -20,4 +23,7 @@ module.exports = {
|
|||||||
scheduledCleanUp,
|
scheduledCleanUp,
|
||||||
cleanUpRoom,
|
cleanUpRoom,
|
||||||
timeDiff,
|
timeDiff,
|
||||||
|
tokenValidator,
|
||||||
|
tokenRefresher,
|
||||||
|
updateVoxeet,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,46 +2,72 @@ const admin = require('firebase-admin');
|
|||||||
|
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const bodydata = JSON.parse(req.body);
|
const bodydata = JSON.parse(req.body);
|
||||||
console.log('Requested Join - UID - ', bodydata.uid, '\n\tRoom - ', bodydata.roomId);
|
console.log('Requested Join - UID - ', bodydata.uid, '\n\tRoom - ', bodydata.partyName);
|
||||||
const [exists, roomSource] = await admin
|
// GET ROOM EXISTS, ROOM SOURCE, & ROOM PIN
|
||||||
|
const [exists, roomSource, roomPin] = await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(bodydata.roomId)
|
.doc(bodydata.partyName)
|
||||||
.get()
|
.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) => {
|
.catch((err) => {
|
||||||
console.error(
|
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
|
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()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(bodydata.roomId)
|
.doc(bodydata.partyName)
|
||||||
.collection('users')
|
.collection('users')
|
||||||
.doc(bodydata.uid)
|
.doc(bodydata.uid)
|
||||||
.get()
|
.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) => {
|
.catch((err) => {
|
||||||
console.error(
|
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
|
err
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
if (userExists && exists && roomSource === bodydata.source)
|
// IF USER PRE-EXISTS AND SAME SOURCE - JOIN
|
||||||
res.status(200).send({ roomId: bodydata.roomId, message: 'Joined a party' });
|
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(bodydata.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 (
|
else if (
|
||||||
!userExists &&
|
!userExists &&
|
||||||
exists &&
|
exists &&
|
||||||
bodydata.roomId &&
|
bodydata.partyName &&
|
||||||
bodydata.userName &&
|
bodydata.userName &&
|
||||||
roomSource === bodydata.source
|
roomSource === bodydata.source &&
|
||||||
|
roomPin === bodydata.partyPin
|
||||||
) {
|
) {
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(bodydata.roomId)
|
.doc(bodydata.partyName)
|
||||||
.collection('messages')
|
.collection('messages')
|
||||||
.add({
|
.add({
|
||||||
uid: '<system>',
|
uid: '<system>',
|
||||||
@@ -51,14 +77,14 @@ module.exports = async (req, res) => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(
|
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
|
err
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(bodydata.roomId)
|
.doc(bodydata.partyName)
|
||||||
.collection('users')
|
.collection('users')
|
||||||
.doc(bodydata.uid)
|
.doc(bodydata.uid)
|
||||||
.set({
|
.set({
|
||||||
@@ -67,36 +93,37 @@ module.exports = async (req, res) => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(
|
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
|
err
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(bodydata.roomId)
|
.doc(bodydata.partyName)
|
||||||
.update({
|
.update({
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(
|
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
|
err
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
res.status(200).send({ roomId: bodydata.roomId, message: 'Joined a party' });
|
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
|
||||||
} else if (!userExists && !bodydata.userName)
|
}
|
||||||
res.status(400).send({ message: 'Missing user name' });
|
// ONLY WRONG SOURCE
|
||||||
else if (exists && roomSource !== bodydata.source) {
|
else if (exists && roomSource !== bodydata.source) {
|
||||||
|
// MOVE SOURCE IF ADMIN
|
||||||
if (userAdmin === true) {
|
if (userAdmin === true) {
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection('rooms')
|
||||||
.doc(bodydata.roomId)
|
.doc(bodydata.partyName)
|
||||||
.update({
|
.update({
|
||||||
player: {
|
player: {
|
||||||
showName: null,
|
showName: null,
|
||||||
source: roomSource,
|
source: null,
|
||||||
status: 'NOT_PLAYING',
|
status: 'NOT_PLAYING',
|
||||||
time: 0,
|
time: 0,
|
||||||
updatedBy: bodydata.uid,
|
updatedBy: bodydata.uid,
|
||||||
@@ -104,7 +131,7 @@ module.exports = async (req, res) => {
|
|||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.error(
|
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' });
|
res.status(500).send({ message: 'Unable to move party, please try again' });
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
const FunctionModules = require('./index');
|
const cleanUpRoom = require('./cleanUpRoom');
|
||||||
|
|
||||||
|
const timeDiff = (ds1, ds2) => {
|
||||||
|
const d1 = new Date(ds1);
|
||||||
|
const d2 = new Date(ds2);
|
||||||
|
var diff = (d2.getTime() - d1.getTime()) / 1000;
|
||||||
|
diff /= 60 * 60;
|
||||||
|
return Math.abs(Math.round(diff));
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = async () => {
|
module.exports = async () => {
|
||||||
const startedTime = new Date();
|
const startedTime = new Date();
|
||||||
@@ -19,11 +27,12 @@ module.exports = async () => {
|
|||||||
.get()
|
.get()
|
||||||
.then(async (roomSnap) => {
|
.then(async (roomSnap) => {
|
||||||
if (
|
if (
|
||||||
FunctionModules.timeDiff(roomSnap.createTime.toDate(), startedTime) >= 24 ||
|
timeDiff(roomSnap.createTime.toDate(), startedTime) >= 24 ||
|
||||||
FunctionModules.timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 12
|
timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 12
|
||||||
)
|
) {
|
||||||
await FunctionModules.cleanUpRoom(roomSnap.id);
|
await cleanUpRoom(roomSnap.id);
|
||||||
console.info(`Deleted room ${roomSnap.id} from auto cleanup`);
|
console.info(`Deleted room ${roomSnap.id} from auto cleanup`);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module.exports = (ds1, ds2) => {
|
module.exports = function (ds1, ds2) {
|
||||||
const d1 = new Date(ds1);
|
const d1 = new Date(ds1);
|
||||||
const d2 = new Date(ds2);
|
const d2 = new Date(ds2);
|
||||||
var diff = (d2.getTime() - d1.getTime()) / 1000;
|
var diff = (d2.getTime() - d1.getTime()) / 1000;
|
||||||
|
|||||||
@@ -2,18 +2,18 @@ const admin = require('firebase-admin');
|
|||||||
|
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const { player, roomId, uid, showChanged, userName } = JSON.parse(req.body);
|
const { player, roomId, uid, showChanged, userName } = JSON.parse(req.body);
|
||||||
const statusString =
|
// const statusString =
|
||||||
player.status === 'PLAYING'
|
// player.status === 'PLAYING'
|
||||||
? 'started playing from'
|
// ? 'started playing from'
|
||||||
: player.status === 'PAUSED'
|
// : player.status === 'PAUSED'
|
||||||
? 'paused at'
|
// ? 'paused at'
|
||||||
: player.status === 'NOT_PLAYING'
|
// : player.status === 'NOT_PLAYING'
|
||||||
? 'stopped at'
|
// ? 'stopped at'
|
||||||
: 'is at';
|
// : 'is at';
|
||||||
const timeString =
|
// const timeString =
|
||||||
player.time > 3600
|
// player.time > 3600
|
||||||
? new Date(player.time * 1000).toISOString().substr(11, 8)
|
// ? new Date(player.time * 1000).toISOString().substr(11, 8)
|
||||||
: new Date(player.time * 1000).toISOString().substr(14, 5);
|
// : new Date(player.time * 1000).toISOString().substr(14, 5);
|
||||||
const [roomExists, roomData] = await admin
|
const [roomExists, roomData] = await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.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);
|
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 });
|
res.status(500).send({ message: "Uh oh party could't be synced", err });
|
||||||
});
|
});
|
||||||
await admin
|
// await admin
|
||||||
.firestore()
|
// .firestore()
|
||||||
.collection('rooms')
|
// .collection('rooms')
|
||||||
.doc(roomId)
|
// .doc(roomId)
|
||||||
.collection('messages')
|
// .collection('messages')
|
||||||
.add({
|
// .add({
|
||||||
uid: '<system>',
|
// uid: '<system>',
|
||||||
name: '',
|
// name: '',
|
||||||
text: `${userName} ${statusString} ${timeString}`,
|
// text: `${userName} ${statusString} ${timeString}`,
|
||||||
time: new Date(),
|
// time: new Date(),
|
||||||
})
|
// })
|
||||||
.catch((err) => {
|
// .catch((err) => {
|
||||||
console.error(
|
// console.error(
|
||||||
`Failed to add system update player message : \n\t Room: ${roomId} \n\t ${uid}`,
|
// `Failed to add system update player message : \n\t Room: ${roomId} \n\t ${uid}`,
|
||||||
err
|
// err
|
||||||
);
|
// );
|
||||||
});
|
// });
|
||||||
return res.status(200).send({ message: 'Synced party player' });
|
return res.status(200).send({ message: 'Synced party player' });
|
||||||
} else {
|
} else {
|
||||||
return res.status(208).send({ message: 'Player already up to date' });
|
return res.status(208).send({ message: 'Player already up to date' });
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
const admin = require('firebase-admin');
|
|
||||||
|
|
||||||
module.exports = (req, res, next) => {
|
|
||||||
res.set('Access-Control-Allow-Origin', ['*']);
|
|
||||||
if (req.method === 'OPTIONS') {
|
|
||||||
res.status(200).send();
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
if (req.headers.authorization) {
|
|
||||||
try {
|
|
||||||
const token = req.headers.authorization.split(' ')[1];
|
|
||||||
const reqToken = token.split(':')[0];
|
|
||||||
const reqUid = token.split(':')[1];
|
|
||||||
admin
|
|
||||||
.auth()
|
|
||||||
.verifyIdToken(reqToken)
|
|
||||||
.then((user) => {
|
|
||||||
if (user.uid === reqUid) {
|
|
||||||
console.info('Verified Token - UID - ', reqUid);
|
|
||||||
if (req.method === 'POST') {
|
|
||||||
const bodydata = JSON.parse(req.body);
|
|
||||||
bodydata.uid = user.uid;
|
|
||||||
req.body = JSON.stringify(bodydata);
|
|
||||||
}
|
|
||||||
if (req.method === 'GET' || req.method === 'DELETE') {
|
|
||||||
req.body = JSON.stringify({ uid: user.uid });
|
|
||||||
}
|
|
||||||
return next();
|
|
||||||
} else {
|
|
||||||
return res.status(401).send({ message: 'Unauthorized' });
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error('Failed Auth - ', req.headers.authorization, '\n', err);
|
|
||||||
return res.status(500).send({ message: 'Unable to verify user token' });
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
return res.status(401).send({ message: 'Something went wrong when authorizing' });
|
|
||||||
}
|
|
||||||
} else return res.status(401).send({ message: 'Got no user token' });
|
|
||||||
};
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
const admin = require('firebase-admin');
|
|
||||||
|
|
||||||
module.exports = async (roomId) => {
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.collection('messages')
|
|
||||||
.listDocuments()
|
|
||||||
.then((messages) => messages.forEach((message) => message.delete()))
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`Failed to run auto clean up (delete room messages) @ ${roomId}`, err);
|
|
||||||
});
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.collection('users')
|
|
||||||
.listDocuments()
|
|
||||||
.then((users) => users.forEach((user) => user.delete()))
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`Failed to run auto clean up (delete room users) @ ${roomId}`, err);
|
|
||||||
});
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.delete()
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`Failed to run auto clean up (delete room) @ ${roomId}`, err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
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' });
|
|
||||||
const roomNameTaken = await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.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.partyName)
|
|
||||||
.set({
|
|
||||||
name: bodydata.partyName,
|
|
||||||
pin: bodydata.partyPin,
|
|
||||||
player: bodydata.player,
|
|
||||||
createdAt: new Date(),
|
|
||||||
updatedAt: new Date(),
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
res.status(500).send({ message: 'Unable to start party.' });
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(bodydata.partyName)
|
|
||||||
.collection('messages')
|
|
||||||
.add({
|
|
||||||
uid: '<system>',
|
|
||||||
name: '',
|
|
||||||
text: `${bodydata.userName} started the party.`,
|
|
||||||
time: new Date(),
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to add system create party message : \n\t Room: ${bodydata.partyName}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(bodydata.partyName)
|
|
||||||
.collection('users')
|
|
||||||
.doc(bodydata.uid)
|
|
||||||
.set({
|
|
||||||
admin: true,
|
|
||||||
name: bodydata.userName,
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to set admin when creating party : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
res.status(201).send({ roomId: bodydata.partyName, message: 'Started a party!' });
|
|
||||||
};
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
const admin = require('firebase-admin');
|
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
|
||||||
console.log('Requested Delete - Room - ', req.params.rid);
|
|
||||||
console.log(req.body);
|
|
||||||
const isAdmin = await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(req.params.rid)
|
|
||||||
.collection('users')
|
|
||||||
.doc(JSON.parse(req.body).uid)
|
|
||||||
.get()
|
|
||||||
.then((v) => {
|
|
||||||
if (v.exists) {
|
|
||||||
return v.data().admin;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
if (!isAdmin) {
|
|
||||||
console.log('[UNAUTHORIZED] Requested Delete - Room - ', req.params.rid);
|
|
||||||
res.status(401).send({ message: 'Not authorized to end this party.' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(req.params.rid)
|
|
||||||
.collection('messages')
|
|
||||||
.listDocuments()
|
|
||||||
.then((messages) => messages.forEach((message) => message.delete()))
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to delete room messages : \n\t Room: ${req.params.rid} \n\t User: ${req.params.uid}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(req.params.rid)
|
|
||||||
.collection('users')
|
|
||||||
.listDocuments()
|
|
||||||
.then((users) => users.forEach((user) => user.delete()))
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to delete room users : \n\t Room: ${req.params.rid} \n\t User: ${req.params.uid}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(req.params.rid)
|
|
||||||
.delete()
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to delete room : \n\t Room: ${req.params.rid} \n\t User: ${req.params.uid}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
res.status(200).send({ message: 'Ended the party' });
|
|
||||||
};
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
const authenticator = require('./authenticator');
|
|
||||||
const cleanUpRoom = require('./cleanUpRoom');
|
|
||||||
const scheduledCleanUp = require('./scheduledCleanUp');
|
|
||||||
const createRoom = require('./createRoom');
|
|
||||||
const joinRoom = require('./joinRoom');
|
|
||||||
const sendMessage = require('./sendMessage');
|
|
||||||
const leaveRoom = require('./leaveRoom');
|
|
||||||
const deleteRoom = require('./deleteRoom');
|
|
||||||
const timeDiff = require('./timeDiff');
|
|
||||||
const updatePlayer = require('./updatePlayer');
|
|
||||||
const tokenValidator = require('./tokenValidator');
|
|
||||||
const tokenRefresher = require('./tokenRefresher');
|
|
||||||
const updateVoxeet = require('./updateVoxeet');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
authenticator,
|
|
||||||
createRoom,
|
|
||||||
joinRoom,
|
|
||||||
sendMessage,
|
|
||||||
updatePlayer,
|
|
||||||
leaveRoom,
|
|
||||||
deleteRoom,
|
|
||||||
scheduledCleanUp,
|
|
||||||
cleanUpRoom,
|
|
||||||
timeDiff,
|
|
||||||
tokenValidator,
|
|
||||||
tokenRefresher,
|
|
||||||
updateVoxeet,
|
|
||||||
};
|
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
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
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(bodydata.partyName)
|
|
||||||
.get()
|
|
||||||
.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.partyName} \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
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(bodydata.partyName)
|
|
||||||
.collection('users')
|
|
||||||
.doc(bodydata.uid)
|
|
||||||
.get()
|
|
||||||
.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.partyName} \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
|
|
||||||
else if (
|
|
||||||
!userExists &&
|
|
||||||
exists &&
|
|
||||||
bodydata.partyName &&
|
|
||||||
bodydata.userName &&
|
|
||||||
roomSource === bodydata.source &&
|
|
||||||
roomPin === bodydata.partyPin
|
|
||||||
) {
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(bodydata.partyName)
|
|
||||||
.collection('messages')
|
|
||||||
.add({
|
|
||||||
uid: '<system>',
|
|
||||||
name: '',
|
|
||||||
text: `${bodydata.userName} joined the party.`,
|
|
||||||
time: new Date(),
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to add system join party message : \n\t Room: ${partyName} \n\t ${bodydata.uid}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(bodydata.partyName)
|
|
||||||
.collection('users')
|
|
||||||
.doc(bodydata.uid)
|
|
||||||
.set({
|
|
||||||
admin: false,
|
|
||||||
name: bodydata.userName,
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to set admin when joining party : \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`,
|
|
||||||
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' });
|
|
||||||
}
|
|
||||||
// ONLY WRONG SOURCE
|
|
||||||
else if (exists && roomSource !== bodydata.source) {
|
|
||||||
// MOVE SOURCE IF ADMIN
|
|
||||||
if (userAdmin === true) {
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(bodydata.partyName)
|
|
||||||
.update({
|
|
||||||
player: {
|
|
||||||
showName: null,
|
|
||||||
source: null,
|
|
||||||
status: 'NOT_PLAYING',
|
|
||||||
time: 0,
|
|
||||||
updatedBy: bodydata.uid,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
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(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' });
|
|
||||||
};
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
const admin = require('firebase-admin');
|
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
|
||||||
console.log('Requested Leave - UID - ', req.params.uid, '\n\tRoom - ', req.params.rid);
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(req.params.rid)
|
|
||||||
.collection('users')
|
|
||||||
.doc(req.params.uid)
|
|
||||||
.delete()
|
|
||||||
.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' });
|
|
||||||
};
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
const admin = require('firebase-admin');
|
|
||||||
const cleanUpRoom = require('./cleanUpRoom');
|
|
||||||
|
|
||||||
const timeDiff = (ds1, ds2) => {
|
|
||||||
const d1 = new Date(ds1);
|
|
||||||
const d2 = new Date(ds2);
|
|
||||||
var diff = (d2.getTime() - d1.getTime()) / 1000;
|
|
||||||
diff /= 60 * 60;
|
|
||||||
return Math.abs(Math.round(diff));
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = async () => {
|
|
||||||
const startedTime = new Date();
|
|
||||||
const roomList = await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.listDocuments()
|
|
||||||
.then((rL) => rL)
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to run auto clean up (get room list) @ ${new Date().toLocaleTimeString()}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
roomList.forEach((roomRef) => {
|
|
||||||
roomRef
|
|
||||||
.get()
|
|
||||||
.then(async (roomSnap) => {
|
|
||||||
if (
|
|
||||||
timeDiff(roomSnap.createTime.toDate(), startedTime) >= 24 ||
|
|
||||||
timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 12
|
|
||||||
)
|
|
||||||
await cleanUpRoom(roomSnap.id);
|
|
||||||
console.info(`Deleted room ${roomSnap.id} from auto cleanup`);
|
|
||||||
return;
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to run auto clean up (get room snap) @ ${new Date().toLocaleTimeString()}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
const admin = require('firebase-admin');
|
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
|
||||||
const { name, text, uid, roomId, type } = JSON.parse(req.body);
|
|
||||||
|
|
||||||
const roomExists = await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.get()
|
|
||||||
.then((roomSnap) => roomSnap.exists);
|
|
||||||
if (roomExists) {
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.collection('messages')
|
|
||||||
.add({
|
|
||||||
name,
|
|
||||||
text: text || '',
|
|
||||||
time: new Date(),
|
|
||||||
uid,
|
|
||||||
type: type || '',
|
|
||||||
})
|
|
||||||
.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}`,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
res.status(200).send({ message: 'Sent a message' });
|
|
||||||
} else {
|
|
||||||
res.status(404).send({ message: 'Looks like this party is over' });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
module.exports = function (ds1, ds2) {
|
|
||||||
const d1 = new Date(ds1);
|
|
||||||
const d2 = new Date(ds2);
|
|
||||||
var diff = (d2.getTime() - d1.getTime()) / 1000;
|
|
||||||
diff /= 60 * 60;
|
|
||||||
return Math.abs(Math.round(diff));
|
|
||||||
};
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
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 [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')
|
|
||||||
.doc(roomId)
|
|
||||||
.collection('users')
|
|
||||||
.doc(uid)
|
|
||||||
.get()
|
|
||||||
.then((userSnap) => userSnap.exists);
|
|
||||||
if (userExists) {
|
|
||||||
if (showChanged === true) {
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.collection('messages')
|
|
||||||
.add({
|
|
||||||
uid: '<system>',
|
|
||||||
name: '',
|
|
||||||
text: `${userName} changed \n${player.showName ? 'to ' + player.showName : ''}`,
|
|
||||||
time: new Date(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (roomData.status !== player.status) {
|
|
||||||
await admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.update({
|
|
||||||
player,
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
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
|
|
||||||
// );
|
|
||||||
// });
|
|
||||||
return res.status(200).send({ message: 'Synced party player' });
|
|
||||||
} else {
|
|
||||||
return res.status(208).send({ message: 'Player already up to date' });
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return res.status(401).send({ message: 'Not in room to sync' });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "watch-party-api",
|
"name": "watch-party-api",
|
||||||
"version": "1.4.2",
|
"version": "1.4.3",
|
||||||
"description": "Cloud Functions for Watch Party",
|
"description": "Cloud Functions for Watch Party",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
"logs": "firebase functions:log"
|
"logs": "firebase functions:log"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "8"
|
"node": "10"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "watch-party",
|
"name": "watch-party",
|
||||||
"version": "1.4.2",
|
"version": "1.4.3",
|
||||||
"description": "Watch Party",
|
"description": "Watch Party",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "firebase emulators:start",
|
"start": "firebase emulators:start",
|
||||||
|
|||||||
Reference in New Issue
Block a user