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>}
|
|
|
|
*/
|
|
|
|
let OpenConversations = new Map();
|
|
|
|
|
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-10 11:18:26 +00:00
|
|
|
if(OpenConversations.has(conv.ID))
|
|
|
|
return;
|
|
|
|
|
|
|
|
console.info("Open call for conversation " + conv.ID);
|
|
|
|
|
|
|
|
// Create a new window for the conversation
|
|
|
|
const window = new CallWindow(conv);
|
|
|
|
OpenConversations.set(conv.ID, window)
|
2020-04-10 11:51:36 +00:00
|
|
|
this.AddToLocalStorage(conv.ID);
|
|
|
|
|
|
|
|
window.on("close", () => {
|
|
|
|
OpenConversations.delete(conv.ID)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("wsClosed", () => {
|
|
|
|
// Close all the current conversations
|
|
|
|
OpenConversations.forEach((v) => v.Close(false))
|
|
|
|
|
2020-04-10 14:15:52 +00:00
|
|
|
OpenConversations.clear();
|
2020-04-10 11:51:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
document.addEventListener("wsOpen", () => {
|
|
|
|
CallsController.GetListLocalStorage().forEach(async c => {
|
|
|
|
CallsController.Open(await getSingleConversation(c))
|
|
|
|
})
|
|
|
|
})
|