diff --git a/app/public/background.js b/app/public/background.js index abd3f59..340ad82 100644 --- a/app/public/background.js +++ b/app/public/background.js @@ -41,10 +41,13 @@ const firebaseConfig = { }; firebase.initializeApp(firebaseConfig); -firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL); const isAuthed = () => true && firebase.auth().currentUser; -const doAuth = () => firebase.auth().signInAnonymously(); +const doAuth = () => + firebase + .auth() + .setPersistence(firebase.auth.Auth.Persistence.LOCAL) + .then(() => firebase.auth().signInAnonymously()); if (!isAuthed()) doAuth(); else currentUser = firebase.auth().currentUser; @@ -91,12 +94,44 @@ const fetcher = async (request, sendResponse) => { ); }; +const getActiveParties = () => + window.localStorage.getItem('activeParties') + ? JSON.parse(window.localStorage.getItem('activeParties')) + : {}; +const updateActiveParties = (partyName, doDelete = false) => { + var activeParties = getActiveParties(); + if (activeParties[partyName] && doDelete === true) { + if (new Date().getTime() - activeParties[partyName].activeAt > 60000) { + fetcher( + { + url: `https://api.chromewatchparty.com/leave/${partyName}/users/${activeParties[partyName].uid}`, + method: 'DELETE', + }, + (v) => console.info('[AUTO-LEAVE]', v) + ); + delete activeParties[partyName]; + } + } else { + if (partyName) { + activeParties[partyName] = { + activeAt: new Date().getTime(), + uid: currentUser.uid, + }; + } + } + window.localStorage.setItem('activeParties', JSON.stringify(activeParties)); +}; + +const waitAndLeaveParty = (partyName) => { + setTimeout(() => updateActiveParties(partyName, true), 90000); +}; + CHROME_OR_BROWSER.runtime.onInstalled.addListener((details) => { if (details.reason) { if (details.reason === 'install') { if (window.localStorage.getItem('watchPartyVersion')) { if (window.localStorage.getItem('watchPartyVersion') !== APP_VERSION) { - if (window.localStorage.getItem('showChangelog') === true) { + if (window.localStorage.getItem('showChangelog') === 'true') { CHROME_OR_BROWSER.tabs.create({ url: `http://www.${BASE_URL}.com/changes/v${APP_VERSION}`, active: true, @@ -114,7 +149,7 @@ CHROME_OR_BROWSER.runtime.onInstalled.addListener((details) => { } else if (details.reason === 'update') { if (window.localStorage.getItem('watchPartyVersion')) { if (window.localStorage.getItem('watchPartyVersion') !== APP_VERSION) { - if (window.localStorage.getItem('showChangelog') === true) { + if (window.localStorage.getItem('showChangelog') === 'true') { CHROME_OR_BROWSER.tabs.create({ url: `http://www.${BASE_URL}.com/changes/v${APP_VERSION}`, active: true, @@ -123,7 +158,7 @@ CHROME_OR_BROWSER.runtime.onInstalled.addListener((details) => { window.localStorage.setItem('watchPartyVersion', APP_VERSION); } } else { - if (window.localStorage.getItem('showChangelog') === true) { + if (window.localStorage.getItem('showChangelog') === 'true') { CHROME_OR_BROWSER.tabs.create({ url: `http://www.${BASE_URL}.com/changes/v${APP_VERSION}`, active: true, @@ -184,10 +219,13 @@ CHROME_OR_BROWSER.runtime.onConnect.addListener((port) => { port.userListener && port.userListener(); port.statusListener && port.statusListener(); port.messageListener && port.messageListener(); + waitAndLeaveParty(port.partyName, port.uid); }); port.onMessage.addListener((request) => { const { type, roomId, roomPin } = request; if (type === 'INIT') { + port.partyName = roomId; + updateActiveParties(roomId); port.roomListener = firebase .firestore() .collection('rooms') diff --git a/functions/modules/leaveRoom.js b/functions/modules/leaveRoom.js index fe0ba5b..214ead5 100644 --- a/functions/modules/leaveRoom.js +++ b/functions/modules/leaveRoom.js @@ -2,6 +2,14 @@ const admin = require('firebase-admin'); module.exports = async (req, res) => { console.log('Requested Leave - UID - ', req.params.uid, '\n\tRoom - ', req.params.rid); + const userName = await admin + .firestore() + .collection('rooms') + .doc(req.params.rid) + .collection('users') + .doc(req.params.uid) + .get() + .then((userSnap) => userSnap.data().name); await admin .firestore() .collection('rooms') @@ -15,5 +23,16 @@ module.exports = async (req, res) => { err ) ); + admin + .firestore() + .collection('rooms') + .doc(req.params.rid) + .collection('messages') + .add({ + uid: '', + time: new Date(), + name: '', + text: `${userName} left the party.`, + }); res.status(200).send({ message: 'Left the party' }); }; diff --git a/website/404.html b/website/404.html index c8eb2fd..aa9a926 100644 --- a/website/404.html +++ b/website/404.html @@ -120,17 +120,6 @@ COPIED, EDITED, OR REPRODUCED WITHOUT PERMISSION.
-
-
🌐
-
-

FIREFOX

-

- If you're here for the Firefox Add-On, it will be available soon. It's pending approval - from Mozilla. -

- GO HOME -
-
404
diff --git a/website/changes/index.html b/website/changes/index.html index bb9c21c..6b5551a 100644 --- a/website/changes/index.html +++ b/website/changes/index.html @@ -179,22 +179,22 @@ COPIED, EDITED, OR REPRODUCED WITHOUT PERMISSION.