325 lines
9.6 KiB
JavaScript
325 lines
9.6 KiB
JavaScript
/************************************************************************
|
|
Copyright (C) Ayushya Amitabh - All Rights Reserved
|
|
Unauthorized copying of this file, via any medium is strictly prohibited
|
|
Proprietary and confidential
|
|
Written by Ayushya Amitabh <admin@chromewatchparty.com>, 2020
|
|
https://www.chromewatchparty.com/copyright
|
|
=========================================================================
|
|
ALL CONTENT ON WATCH PARTY'S WEBSITE OR IN WATCH PARTY'S CHROME EXTENSION APP, UNLESS OTHERWISE
|
|
CITED ARE THE SOLE INTELLECTUAL PROPERTY OF AYUSHYA AMITABH, AND MAY NOT BE
|
|
COPIED, EDITED, OR REPRODUCED WITHOUT PERMISSION.
|
|
************************************************************************/
|
|
/* global chrome */
|
|
/* global browser */
|
|
/* global firebase */
|
|
|
|
let CHROME_OR_BROWSER = chrome || browser;
|
|
let currentUser;
|
|
let BASE_URL = "chromewatchparty";
|
|
|
|
try {
|
|
if (browser) {
|
|
BASE_URL = "firefoxwatchparty";
|
|
console.info("FIREFOX ENV");
|
|
}
|
|
} catch (err) {
|
|
console.info("CHROME ENV");
|
|
}
|
|
|
|
const APP_VERSION = "1.7.11";
|
|
|
|
const firebaseConfig = {
|
|
apiKey: "AIzaSyCJLuYpzXLDRmu2ttzFuSGmYcpFxqToWFU",
|
|
authDomain: "watch-party-crx.firebaseapp.com",
|
|
databaseURL: "https://watch-party-crx.firebaseio.com",
|
|
projectId: "watch-party-crx",
|
|
storageBucket: "watch-party-crx.appspot.com",
|
|
messagingSenderId: "712242707190",
|
|
appId: "1:712242707190:web:65fa7f772e30da364b0ad7",
|
|
measurementId: "G-EZS4PEVG4T",
|
|
};
|
|
|
|
firebase.initializeApp(firebaseConfig);
|
|
|
|
const isAuthed = () => true && firebase.auth().currentUser;
|
|
const doAuth = () =>
|
|
firebase
|
|
.auth()
|
|
.setPersistence(firebase.auth.Auth.Persistence.LOCAL)
|
|
.then(() => firebase.auth().signInAnonymously());
|
|
|
|
if (!isAuthed()) doAuth();
|
|
else currentUser = firebase.auth().currentUser;
|
|
|
|
firebase.auth().onAuthStateChanged((user) => {
|
|
if (user) {
|
|
currentUser = user;
|
|
} else {
|
|
currentUser = {};
|
|
}
|
|
});
|
|
|
|
const fetcher = async (request, sendResponse) => {
|
|
if (!isAuthed()) await doAuth();
|
|
const { url, method } = request;
|
|
delete request.type;
|
|
delete request.url;
|
|
delete request.method;
|
|
const idToken = await firebase.auth().currentUser.getIdToken();
|
|
let resStatus = {};
|
|
fetch(url, {
|
|
method,
|
|
headers: {
|
|
Authorization: `Token ${idToken}:${firebase.auth().currentUser.uid}`,
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Keep-Alive": true,
|
|
},
|
|
...request,
|
|
})
|
|
.then((res) => {
|
|
resStatus = {
|
|
status: res.status,
|
|
statusText: res.statusText,
|
|
};
|
|
return res.json();
|
|
})
|
|
.then((res) => sendResponse({ ...res, ...resStatus }))
|
|
.catch((err) =>
|
|
sendResponse({
|
|
...err,
|
|
status: 503,
|
|
statusText: "Couldn't talk to server",
|
|
})
|
|
);
|
|
};
|
|
|
|
const getActiveParties = () =>
|
|
window.localStorage.getItem("activeParties")
|
|
? JSON.parse(window.localStorage.getItem("activeParties"))
|
|
: {};
|
|
const updateActiveParties = (partyName, doDelete = false) => {
|
|
if (partyName !== "WATCH_PARTY_STATUS") {
|
|
var activeParties = getActiveParties();
|
|
if (activeParties[partyName] && doDelete === true) {
|
|
if (
|
|
Math.round(performance.now() - 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: performance.now(),
|
|
uid: currentUser.uid,
|
|
};
|
|
}
|
|
}
|
|
window.localStorage.setItem("activeParties", JSON.stringify(activeParties));
|
|
}
|
|
};
|
|
|
|
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") {
|
|
CHROME_OR_BROWSER.tabs.create({
|
|
url: `http://www.${BASE_URL}.com/changes/v${APP_VERSION}`,
|
|
active: true,
|
|
});
|
|
}
|
|
window.localStorage.setItem("watchPartyVersion", APP_VERSION);
|
|
}
|
|
} else {
|
|
CHROME_OR_BROWSER.tabs.create({
|
|
url: `https://www.${BASE_URL}.com/welcome`,
|
|
active: true,
|
|
});
|
|
window.localStorage.setItem("watchPartyVersion", APP_VERSION);
|
|
}
|
|
} else if (details.reason === "update") {
|
|
if (window.localStorage.getItem("watchPartyVersion")) {
|
|
if (window.localStorage.getItem("watchPartyVersion") !== APP_VERSION) {
|
|
if (window.localStorage.getItem("showChangelog") === "true") {
|
|
CHROME_OR_BROWSER.tabs.create({
|
|
url: `http://www.${BASE_URL}.com/changes/v${APP_VERSION}`,
|
|
active: true,
|
|
});
|
|
}
|
|
window.localStorage.setItem("watchPartyVersion", APP_VERSION);
|
|
}
|
|
} else {
|
|
if (window.localStorage.getItem("showChangelog") === "true") {
|
|
CHROME_OR_BROWSER.tabs.create({
|
|
url: `http://www.${BASE_URL}.com/changes/v${APP_VERSION}`,
|
|
active: true,
|
|
});
|
|
}
|
|
window.localStorage.setItem("watchPartyVersion", APP_VERSION);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
CHROME_OR_BROWSER.runtime.onMessage.addListener(
|
|
(request, sender, sendResponse) => {
|
|
const { type } = request;
|
|
switch (type) {
|
|
case "API_CALL":
|
|
fetcher(request, sendResponse);
|
|
break;
|
|
case "GET_AUTH":
|
|
sendResponse({ uid: currentUser.uid });
|
|
break;
|
|
case "GET_PARTIES":
|
|
sendResponse(
|
|
window.localStorage.getItem("parties")
|
|
? JSON.parse(window.localStorage.getItem("parties"))
|
|
: []
|
|
);
|
|
break;
|
|
case "EMPTY_PARTIES":
|
|
window.localStorage.removeItem("parties");
|
|
break;
|
|
case "IS_HIDDEN":
|
|
sendResponse(
|
|
window.localStorage.getItem("showWPButton")
|
|
? JSON.parse(window.localStorage.getItem("showWPButton"))
|
|
: true
|
|
);
|
|
break;
|
|
case "GET_TUTORIAL":
|
|
sendResponse(
|
|
window.localStorage.getItem("tutorialComplete")
|
|
? JSON.parse(window.localStorage.getItem("tutorialComplete"))
|
|
: false
|
|
);
|
|
break;
|
|
case "SET_TUTORIAL":
|
|
window.localStorage.setItem("tutorialComplete", request.status);
|
|
break;
|
|
default:
|
|
console.warn("Unknown message type: <", type, ">");
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
);
|
|
|
|
CHROME_OR_BROWSER.runtime.onConnect.addListener((port) => {
|
|
port.onDisconnect.addListener(() => {
|
|
updateActiveParties(port.name);
|
|
port.roomListener && port.roomListener();
|
|
port.userListener && port.userListener();
|
|
port.statusListener && port.statusListener();
|
|
port.messageListener && port.messageListener();
|
|
setTimeout(() => updateActiveParties(port.name, true), 60000);
|
|
});
|
|
updateActiveParties(port.name);
|
|
port.onMessage.addListener((request) => {
|
|
const { type, roomId, roomPin } = request;
|
|
if (type === "INIT") {
|
|
port.partyName = roomId;
|
|
updateActiveParties(roomId);
|
|
port.roomListener = firebase
|
|
.firestore()
|
|
.collection("rooms")
|
|
.doc(roomId)
|
|
.onSnapshot(
|
|
(snap) => {
|
|
if (snap) {
|
|
port.postMessage(snap.data());
|
|
}
|
|
},
|
|
(err) => {
|
|
port.postMessage({ isError: true, ...err });
|
|
}
|
|
);
|
|
port.userListener = firebase
|
|
.firestore()
|
|
.collection("rooms")
|
|
.doc(roomId)
|
|
.collection("users")
|
|
.onSnapshot(
|
|
(snap) => {
|
|
if (snap) {
|
|
const isAdmin = snap.docs
|
|
.filter(
|
|
(user) => user.id === firebase.auth().currentUser.uid
|
|
)[0]
|
|
.data().admin;
|
|
const users = snap.docs.map((userSnap) => userSnap.data().name);
|
|
port.postMessage({ admin: isAdmin, users });
|
|
}
|
|
},
|
|
(err) => {
|
|
port.postMessage({ isError: true, ...err });
|
|
}
|
|
);
|
|
port.messageListener = firebase
|
|
.firestore()
|
|
.collection("rooms")
|
|
.doc(roomId)
|
|
.collection("messages")
|
|
.orderBy("time")
|
|
.onSnapshot(
|
|
(snap) => {
|
|
if (snap) {
|
|
let messages = snap.docs.map((v) => ({ ...v.data(), id: v.id }));
|
|
port.postMessage({
|
|
messages,
|
|
});
|
|
}
|
|
},
|
|
(err) => {
|
|
port.postMessage({ isError: true, ...err });
|
|
}
|
|
);
|
|
if (window.localStorage.getItem("parties")) {
|
|
var existing = JSON.parse(window.localStorage.getItem("parties"));
|
|
if (!(existing.filter((i) => i.name === roomId).length > 0)) {
|
|
existing.push({
|
|
name: roomId,
|
|
pin: roomPin,
|
|
});
|
|
window.localStorage.setItem("parties", JSON.stringify(existing));
|
|
}
|
|
} else {
|
|
window.localStorage.setItem(
|
|
"parties",
|
|
JSON.stringify([
|
|
{
|
|
name: roomId,
|
|
pin: roomPin,
|
|
},
|
|
])
|
|
);
|
|
}
|
|
} else if (type === "STATUS") {
|
|
port.statusListener = firebase
|
|
.firestore()
|
|
.collection("server")
|
|
.doc("status")
|
|
.onSnapshot(
|
|
(statusSnap) => {
|
|
port.postMessage({
|
|
status: statusSnap.data(),
|
|
});
|
|
},
|
|
(err) => {
|
|
port.postMessage({ isError: true, ...err });
|
|
}
|
|
);
|
|
}
|
|
});
|
|
});
|