2020-04-10 09:24:46 +00:00
|
|
|
/**
|
|
|
|
* Calls controller
|
|
|
|
*
|
|
|
|
* @author Pierre Hubert
|
|
|
|
*/
|
|
|
|
|
2020-04-10 11:18:26 +00:00
|
|
|
/**
|
|
|
|
* @type {Map<number, CallWindow>}
|
|
|
|
*/
|
2020-04-12 15:10:44 +00:00
|
|
|
let OpenCalls = new Map();
|
2020-04-10 11:18:26 +00:00
|
|
|
|
2020-04-10 09:24:46 +00:00
|
|
|
class CallsController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a call for a conversation
|
|
|
|
*
|
|
|
|
* @param {Conversation} conv Information about the target conversation
|
|
|
|
*/
|
|
|
|
static Open(conv) {
|
2020-04-12 16:38:41 +00:00
|
|
|
if(OpenCalls.has(conv.ID) && OpenCalls.get(conv.ID).isOpen)
|
2020-04-10 11:18:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
console.info("Open call for conversation " + conv.ID);
|
|
|
|
|
|
|
|
// Create a new window for the conversation
|
|
|
|
const window = new CallWindow(conv);
|
2020-04-12 15:10:44 +00:00
|
|
|
OpenCalls.set(conv.ID, window)
|
2020-04-10 11:51:36 +00:00
|
|
|
this.AddToLocalStorage(conv.ID);
|
|
|
|
|
|
|
|
window.on("close", () => {
|
2020-04-12 15:10:44 +00:00
|
|
|
OpenCalls.delete(conv.ID)
|
2020-04-10 11:51:36 +00:00
|
|
|
this.RemoveFromLocalStorage(conv.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the conversation to local storage
|
|
|
|
*
|
|
|
|
* @param {number} convID Target conversation ID
|
|
|
|
*/
|
|
|
|
static AddToLocalStorage(convID) {
|
|
|
|
const list = this.GetListLocalStorage();
|
|
|
|
if(!list.includes(convID))
|
|
|
|
list.push(convID)
|
|
|
|
this.SetListLocalStorage(list)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} convID Target conversation ID
|
|
|
|
*/
|
|
|
|
static RemoveFromLocalStorage(convID) {
|
|
|
|
this.SetListLocalStorage(
|
|
|
|
this.GetListLocalStorage().filter(e => e != convID)
|
|
|
|
)
|
2020-04-10 09:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 11:51:36 +00:00
|
|
|
/**
|
|
|
|
* @return {number[]} The ID of the opened conversations
|
|
|
|
*/
|
|
|
|
static GetListLocalStorage() {
|
|
|
|
const content = localStorage.getItem("calls")
|
|
|
|
if(content == null)
|
|
|
|
return []
|
|
|
|
else
|
|
|
|
return JSON.parse(content).filter(e => e != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the list of open calls
|
|
|
|
*
|
|
|
|
* @param {number[]} list New list
|
|
|
|
*/
|
|
|
|
static SetListLocalStorage(list) {
|
|
|
|
localStorage.setItem("calls", JSON.stringify(list))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:09:40 +00:00
|
|
|
document.addEventListener("userJoinedCall", (e) => {
|
|
|
|
const detail = e.detail;
|
|
|
|
|
2020-04-12 15:10:44 +00:00
|
|
|
if(OpenCalls.has(detail.callID))
|
|
|
|
OpenCalls.get(detail.callID).AddMember(detail.userID)
|
2020-04-10 15:09:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
document.addEventListener("userLeftCall", (e) => {
|
|
|
|
const detail = e.detail;
|
|
|
|
|
2020-04-12 15:10:44 +00:00
|
|
|
if(OpenCalls.has(detail.callID))
|
|
|
|
OpenCalls.get(detail.callID).RemoveMember(detail.userID)
|
2020-04-10 15:09:40 +00:00
|
|
|
})
|
|
|
|
|
2020-04-11 12:05:29 +00:00
|
|
|
document.addEventListener("newCallSignal", (e) => {
|
|
|
|
const detail = e.detail
|
2020-04-12 13:23:46 +00:00
|
|
|
let signal = detail.data
|
|
|
|
|
|
|
|
// Fix candidate format
|
|
|
|
if(signal.hasOwnProperty("candidate"))
|
|
|
|
signal = {
|
|
|
|
candidate: signal
|
|
|
|
}
|
2020-04-11 12:05:29 +00:00
|
|
|
|
2020-04-12 15:10:44 +00:00
|
|
|
if(OpenCalls.has(detail.callID))
|
|
|
|
OpenCalls.get(detail.callID).NewSignal(detail.peerID, signal)
|
2020-04-11 12:05:29 +00:00
|
|
|
});
|
|
|
|
|
2020-04-11 12:50:37 +00:00
|
|
|
document.addEventListener("callPeerReady", (e) => {
|
|
|
|
const detail = e.detail;
|
|
|
|
|
2020-04-12 15:10:44 +00:00
|
|
|
if(OpenCalls.has(detail.callID))
|
|
|
|
OpenCalls.get(detail.callID).PeerReady(detail.peerID)
|
2020-04-11 12:50:37 +00:00
|
|
|
})
|
|
|
|
|
2020-04-13 06:46:39 +00:00
|
|
|
document.addEventListener("callClosed", e => {
|
|
|
|
const callID = e.detail;
|
|
|
|
|
|
|
|
if(OpenCalls.has(callID))
|
|
|
|
OpenCalls.get(callID).Close(false);
|
|
|
|
});
|
2020-04-10 15:09:40 +00:00
|
|
|
|
2020-04-10 11:51:36 +00:00
|
|
|
document.addEventListener("wsClosed", () => {
|
|
|
|
// Close all the current conversations
|
2020-04-12 15:10:44 +00:00
|
|
|
OpenCalls.forEach((v) => v.Close(false))
|
2020-04-10 11:51:36 +00:00
|
|
|
|
2020-04-12 15:10:44 +00:00
|
|
|
OpenCalls.clear();
|
2020-04-10 11:51:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-04-11 06:17:51 +00:00
|
|
|
document.addEventListener("openPage", () => {
|
2020-04-10 11:51:36 +00:00
|
|
|
CallsController.GetListLocalStorage().forEach(async c => {
|
2020-04-12 15:10:44 +00:00
|
|
|
if(!OpenCalls.has(c))
|
2020-04-11 06:17:51 +00:00
|
|
|
CallsController.Open(await getSingleConversation(c))
|
2020-04-10 11:51:36 +00:00
|
|
|
})
|
2020-04-13 09:18:04 +00:00
|
|
|
|
|
|
|
// Check if a conversation can be integrated inside the page
|
|
|
|
for(const call of OpenCalls.values()) {
|
|
|
|
call.CheckNewTargetForWindow()
|
|
|
|
}
|
2020-04-10 11:51:36 +00:00
|
|
|
})
|