229 lines
7.3 KiB
JavaScript
229 lines
7.3 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 INTELECTUAL PROPERTY OF AYUSHYA AMITABH, AND MAY NOT BE
|
|
COPIED, EDITED, OR REPRODUCED WITHOUT PERMISSION.
|
|
************************************************************************/
|
|
import VoxeetSDK from "@voxeet/voxeet-web-sdk";
|
|
import {
|
|
GetVoxeetTokenApi,
|
|
GetRefreshedVoxeetTokenApi,
|
|
LocalData,
|
|
UpdateVoxeetApi,
|
|
updateLocalData,
|
|
} from "./Api";
|
|
|
|
export default class CallManager {
|
|
constructor(listener, notify) {
|
|
this.listener = listener;
|
|
this.notify = notify;
|
|
this.__refreshToken = LocalData().vaT;
|
|
this.__accessToken = LocalData().vrT;
|
|
this.__sessionInitialized = false;
|
|
this.__admin = false;
|
|
this.__adminOnly = true;
|
|
}
|
|
toggleCall(onCall, conference, admin, adminOnly) {
|
|
try {
|
|
this.listener({ callActionPending: true });
|
|
this.__admin = admin;
|
|
this.__adminOnly = adminOnly;
|
|
if (onCall === true) this.leaveCall();
|
|
else this.boot(conference);
|
|
} catch (e) {
|
|
this.notify("Uh oh something went wrong, try rejoining the call.");
|
|
}
|
|
}
|
|
toggleMute(muted) {
|
|
this.listener({ callActionPending: true });
|
|
VoxeetSDK.conference.mute(VoxeetSDK.session.participant, !muted);
|
|
this.listener({ muted: !muted, callActionPending: false });
|
|
}
|
|
async boot(conference) {
|
|
await this.initialize();
|
|
const opened = await this.openSession();
|
|
if (opened && conference) {
|
|
await VoxeetSDK.conference
|
|
.fetch(conference._id)
|
|
.then(this.joinCall)
|
|
.catch((err) => {
|
|
console.error("Failed to fetch conference", err);
|
|
this.startCall();
|
|
});
|
|
} else if (opened && !conference) {
|
|
this.startCall();
|
|
} else {
|
|
this.listener({ onCall: false, muted: false, callActionPending: false });
|
|
console.error("Unable to open Voxeet Session");
|
|
}
|
|
}
|
|
async startCall() {
|
|
if (!this.__adminOnly || (this.__adminOnly && this.__admin)) {
|
|
VoxeetSDK.conference
|
|
.create({
|
|
alias: `AUTODELETE_${LocalData().partyName}`,
|
|
pin: LocalData().partyPin,
|
|
})
|
|
.then(async (conference) => {
|
|
const updatedVoxeet = await UpdateVoxeetApi({
|
|
conference,
|
|
partyName: LocalData().partyName,
|
|
partyPin: LocalData().partyPin,
|
|
userName: LocalData().userName,
|
|
})
|
|
.then((res) => res.json())
|
|
.then((res) => res.status <= 299)
|
|
.catch((err) => {
|
|
if (err.conference) {
|
|
this.joinCall(err.conference);
|
|
return true;
|
|
} else {
|
|
this.notify(err.message);
|
|
this.listener({
|
|
onCall: false,
|
|
muted: false,
|
|
callActionPending: false,
|
|
});
|
|
console.error("Update Voxeet API Error", err);
|
|
return false;
|
|
}
|
|
});
|
|
if (updatedVoxeet === true) {
|
|
this.joinCall(conference);
|
|
} else {
|
|
this.listener({
|
|
onCall: false,
|
|
muted: false,
|
|
callActionPending: false,
|
|
});
|
|
console.error("Failed to update party Voxeet");
|
|
}
|
|
});
|
|
} else {
|
|
this.notify("Ask your party admin to start the call.");
|
|
this.leaveCall();
|
|
}
|
|
}
|
|
async joinCall(conference) {
|
|
const joined = await VoxeetSDK.conference
|
|
.join(conference, { constraints: { audio: true, video: false } })
|
|
.then(() => true)
|
|
.catch((e) => {
|
|
this.listener({
|
|
onCall: false,
|
|
muted: false,
|
|
callActionPending: false,
|
|
});
|
|
this.notify("Unable to join call. Try again in a moment.");
|
|
console.error("Error joining call", e);
|
|
return true;
|
|
});
|
|
// Leave changes as comment for later
|
|
// if (!joined) {
|
|
// this.startCall();
|
|
// }
|
|
}
|
|
async leaveCall() {
|
|
VoxeetSDK.conference.leave().then(() => {
|
|
VoxeetSDK.session.close();
|
|
this.listener({ onCall: false, muted: false, callActionPending: false });
|
|
});
|
|
}
|
|
async openSession() {
|
|
if (!this.__sessionInitialized) {
|
|
try {
|
|
await VoxeetSDK.session.open({
|
|
externalId: LocalData().userUid,
|
|
name: LocalData().userName,
|
|
});
|
|
return true;
|
|
} catch (error) {
|
|
console.error("Error Opening Voxeet Session", error);
|
|
}
|
|
console.error("Session not initialized");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
async usersListHandle() {
|
|
var usersOnCall = {};
|
|
VoxeetSDK.conference.participants.forEach((v, k, m) => {
|
|
usersOnCall[k] = v;
|
|
});
|
|
this.listener({ usersOnCall });
|
|
}
|
|
async initialize() {
|
|
VoxeetSDK.conference.on("participantAdded", () => this.usersListHandle());
|
|
VoxeetSDK.conference.on("participantUpdated", () => this.usersListHandle());
|
|
|
|
VoxeetSDK.conference.on("joined", () => {
|
|
this.listener({ onCall: true, callActionPending: false });
|
|
});
|
|
|
|
VoxeetSDK.conference.on("left", () => {
|
|
this.listener({
|
|
onCall: false,
|
|
callActionPending: false,
|
|
usersOnCall: {},
|
|
});
|
|
});
|
|
|
|
VoxeetSDK.conference.on("streamAdded", (participant, stream) => {
|
|
this.listener({ callActionPending: false });
|
|
});
|
|
|
|
VoxeetSDK.conference.on("streamRemoved", (participant, stream) => {
|
|
this.listener({ callActionPending: false });
|
|
});
|
|
|
|
if (
|
|
this.__accessToken !== undefined ||
|
|
this.__refreshToken !== undefined ||
|
|
this.__accessToken !== null ||
|
|
this.__refreshToken !== null
|
|
) {
|
|
await GetVoxeetTokenApi({ partyName: LocalData().partyName })
|
|
.then((res) => res.json())
|
|
.then((tokenResult) => {
|
|
if (tokenResult.status <= 299) {
|
|
const { access_token, refresh_token } = tokenResult;
|
|
this.updateTokens(access_token, refresh_token);
|
|
VoxeetSDK.initializeToken(access_token, refresh_token);
|
|
} else {
|
|
console.error("Get Voxeet Token API Result Error", tokenResult);
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.error("Get Voxeet Token API error", err);
|
|
});
|
|
} else {
|
|
VoxeetSDK.initializeToken(this.__accessToken, this.__refreshToken);
|
|
}
|
|
}
|
|
async refreshToken() {
|
|
return new Promise((resolve, reject) => {
|
|
GetRefreshedVoxeetTokenApi({ token: this.__refreshToken })
|
|
.then((res) => res.json())
|
|
.then((tokenResult) => {
|
|
const { access_token, refresh_token } = tokenResult;
|
|
this.updateTokens(access_token, refresh_token);
|
|
resolve(access_token);
|
|
})
|
|
.catch((err) => {
|
|
console.error("Get Voxeet Token API error", err);
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
updateTokens(aT, rT) {
|
|
this.__accessToken = aT;
|
|
this.__refreshToken = rT;
|
|
updateLocalData({ vaT: aT, vrT: rT });
|
|
}
|
|
}
|