diff --git a/functions/index.js b/functions/index.js index 93e4c7c..241532f 100644 --- a/functions/index.js +++ b/functions/index.js @@ -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); +}); diff --git a/public/background.js b/public/background.js index ec30f46..9aafcbd 100644 --- a/public/background.js +++ b/public/background.js @@ -81,7 +81,9 @@ chrome.runtime.onConnect.addListener((port) => { port.postMessage(snap.data()); } }, - (err) => {} + (err) => { + port.postMessage({ isError: true, ...err }); + } ); firebase .firestore() @@ -97,7 +99,9 @@ chrome.runtime.onConnect.addListener((port) => { port.postMessage({ admin: isAdmin }); } }, - (err) => {} + (err) => { + port.postMessage({ isError: true, ...err }); + } ); firebase .firestore() @@ -110,7 +114,9 @@ chrome.runtime.onConnect.addListener((port) => { if (snap) port.postMessage({ messages: snap.docs.map((v) => ({ ...v.data(), id: v.id })) }); }, - (err) => {} + (err) => { + port.postMessage({ isError: true, ...err }); + } ); } }); diff --git a/src/Chat.js b/src/Chat.js index 6801bc5..bf78061 100644 --- a/src/Chat.js +++ b/src/Chat.js @@ -64,6 +64,11 @@ export default class Chat extends Component { roomId: window.localStorage.getItem('watchPartyRoomId'), }); this.port.onMessage.addListener((response) => { + if (response.isError === true) { + this.props.notify('Uh oh. Got lost from the party.'); + goTo(Create, this.props); + return; + } this.setState(response); if (response.player && this.state.localPlayer.status !== response.player.status) { if (response.player.source !== getPlayerSource()) { @@ -104,6 +109,10 @@ export default class Chat extends Component { time, source, }, + }).catch((err) => { + this.props.updateLoading(false); + this.props.notify(err.message || err.statusText); + console.error('Sync Party API error', err); }); } else { this.mVideoListener.start(); @@ -115,12 +124,18 @@ export default class Chat extends Component { const data = { roomId: window.localStorage.getItem('watchPartyRoomId'), }; - DeleteRoomApi(data).then((res) => { - this.props.notify('Ended party'); - window.localStorage.removeItem('watchPartyRoomId'); - goTo(Create, this.props); - this.props.updateLoading(false); - }); + DeleteRoomApi(data) + .then((res) => { + this.props.notify('Ended party'); + window.localStorage.removeItem('watchPartyRoomId'); + goTo(Create, this.props); + this.props.updateLoading(false); + }) + .catch((err) => { + this.props.updateLoading(false); + this.props.notify(err.message || err.statusText); + console.error('End Party API error', err); + }); } leaveParty() { this.props.notify('Leaving party...'); @@ -129,12 +144,18 @@ export default class Chat extends Component { roomId: window.localStorage.getItem('watchPartyRoomId'), uid: window.localStorage.getItem('watchPartyUserUid'), }; - LeaveRoomApi(data).then((res) => { - this.props.notify('Left party'); - window.localStorage.removeItem('watchPartyRoomId'); - goTo(Create, this.props); - this.props.updateLoading(false); - }); + LeaveRoomApi(data) + .then((res) => { + this.props.notify('Left party'); + window.localStorage.removeItem('watchPartyRoomId'); + goTo(Create, this.props); + this.props.updateLoading(false); + }) + .catch((err) => { + this.props.updateLoading(false); + this.props.notify(err.message || err.statusText); + console.error('End Party API error', err); + }); } shareParty() { const el = document.createElement('textarea'); @@ -153,7 +174,10 @@ export default class Chat extends Component { text: message, time: new Date().toISOString(), }; - SendMessageApi(data); + SendMessageApi(data).catch((err) => { + this.props.notify(err.message || err.statusText); + console.error('Send Message API error', err); + }); } render() { const { admin, name, messages, newMessage } = this.state; diff --git a/src/Create.js b/src/Create.js index 64c2315..153a278 100644 --- a/src/Create.js +++ b/src/Create.js @@ -32,13 +32,14 @@ const joinRoom = async (data) => { const apidata = { roomId: data.joinName.trim(), userName: data.name.trim(), + source: getPlayerSource(), }; const confirmedRoomId = await JoinRoomApi(apidata) .then((res) => res.json()) .then((res) => res.roomId) .catch((err) => { data.props.updateLoading(false); - data.props.notify(err.statusText); + data.props.notify(err.message || err.statusText); console.error('Join Room API error', err); }); if (confirmedRoomId && confirmedRoomId === apidata.roomId) { @@ -86,12 +87,12 @@ const createRoom = async (data) => { goTo(Chat, data.props); } else { data.props.updateLoading(false); - data.props.notify(res.statusText); + data.props.notify(res.message || res.statusText); } }) .catch((err) => { data.props.updateLoading(false); - data.props.notify(err.statusText); + data.props.notify(err.message || err.statusText); console.error('Create Room API error', err); }); }; diff --git a/src/index.css b/src/index.css index ea868bf..6da61ef 100644 --- a/src/index.css +++ b/src/index.css @@ -31,6 +31,28 @@ body.amazon-prime #party-routes-wrapper { width: 35% !important; height: 100% !important; } +/* AMAZON STYLING FIX */ +body.amazon-prime input, +body.amazon-prime textarea { + background-color: inherit !important; + border: initial !important; + box-shadow: none !important; +} +body.amazon-prime input.MuiInputBase-input { + height: 1.1876em !important; + padding: 6px 0 7px !important; +} +body.amazon-prime input.MuiFilledInput-inputAdornedEnd { + padding-right: 0 !important; +} +body.amazon-prime input.MuiFilledInput-input { + padding: 27px 12px 10px !important; +} +body.amazon-prime input:focus, +body.amazon-prime textarea:focus { + border: none !important; + box-shadow: none !important; +} @keyframes backgroundpulse { 0% {