v1.7.6/auto leave and leave message

This commit is contained in:
2020-09-16 11:45:34 -04:00
parent e18a2271d8
commit ca73853408
4 changed files with 78 additions and 32 deletions
+43 -5
View File
@@ -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')