2016-06-17 20:35:40 +00:00
|
|
|
/* global APP, getConfigParamsFromUrl */
|
2015-01-09 13:39:32 +00:00
|
|
|
/**
|
|
|
|
* Implements API class that communicates with external api class
|
|
|
|
* and provides interface to access Jitsi Meet features by external
|
|
|
|
* applications that embed Jitsi Meet
|
|
|
|
*/
|
|
|
|
|
2016-06-17 20:35:40 +00:00
|
|
|
import postisInit from 'postis';
|
2016-06-13 21:11:44 +00:00
|
|
|
|
2015-01-09 13:39:32 +00:00
|
|
|
/**
|
|
|
|
* List of the available commands.
|
|
|
|
* @type {{
|
|
|
|
* displayName: inputDisplayNameHandler,
|
2015-07-28 18:43:05 +00:00
|
|
|
* toggleAudio: toggleAudio,
|
|
|
|
* toggleVideo: toggleVideo,
|
2015-07-28 18:22:11 +00:00
|
|
|
* toggleFilmStrip: toggleFilmStrip,
|
|
|
|
* toggleChat: toggleChat,
|
|
|
|
* toggleContactList: toggleContactList
|
2015-01-09 13:39:32 +00:00
|
|
|
* }}
|
|
|
|
*/
|
2016-06-13 21:11:44 +00:00
|
|
|
let commands = {};
|
|
|
|
|
2016-06-17 20:35:40 +00:00
|
|
|
let hashParams = getConfigParamsFromUrl();
|
|
|
|
|
2016-06-13 21:11:44 +00:00
|
|
|
/**
|
2016-06-17 20:35:40 +00:00
|
|
|
* JitsiMeetExternalAPI id - unique for a webpage.
|
2016-06-13 21:11:44 +00:00
|
|
|
*/
|
2016-06-17 20:35:40 +00:00
|
|
|
let jitsi_meet_external_api_id = hashParams.jitsi_meet_external_api_id;
|
2016-06-13 21:11:44 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-17 20:35:40 +00:00
|
|
|
* Object that will execute sendMessage
|
2016-06-13 21:11:44 +00:00
|
|
|
*/
|
2016-06-17 20:35:40 +00:00
|
|
|
let target = window.opener ? window.opener : window.parent;
|
2016-06-13 21:11:44 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-17 20:35:40 +00:00
|
|
|
* Postis instance. Used to communicate with the external application.
|
2016-06-13 21:11:44 +00:00
|
|
|
*/
|
2016-06-17 20:35:40 +00:00
|
|
|
let postis;
|
2016-06-13 21:11:44 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-17 20:35:40 +00:00
|
|
|
* Current status (enabled/disabled) of API.
|
2016-06-13 21:11:44 +00:00
|
|
|
*/
|
2016-06-17 20:35:40 +00:00
|
|
|
let enabled = false;
|
2015-07-02 21:33:06 +00:00
|
|
|
|
|
|
|
function initCommands() {
|
2015-07-28 18:22:11 +00:00
|
|
|
commands = {
|
2016-06-17 20:35:40 +00:00
|
|
|
"display-name": APP.UI.inputDisplayNameHandler,
|
2016-09-23 11:04:19 +00:00
|
|
|
"toggle-audio": APP.conference.toggleAudioMuted.bind(APP.conference),
|
|
|
|
"toggle-video": APP.conference.toggleVideoMuted.bind(APP.conference),
|
2016-06-17 20:35:40 +00:00
|
|
|
"toggle-film-strip": APP.UI.toggleFilmStrip,
|
|
|
|
"toggle-chat": APP.UI.toggleChat,
|
|
|
|
"toggle-contact-list": APP.UI.toggleContactList,
|
2016-10-18 19:37:55 +00:00
|
|
|
"toggle-share-screen":
|
|
|
|
APP.conference.toggleScreenSharing.bind(APP.conference),
|
2016-10-05 21:33:09 +00:00
|
|
|
"video-hangup": () => APP.conference.hangup()
|
2015-07-02 21:33:06 +00:00
|
|
|
};
|
2016-06-17 20:35:40 +00:00
|
|
|
Object.keys(commands).forEach(function (key) {
|
|
|
|
postis.listen(key, commands[key]);
|
|
|
|
});
|
2015-07-02 21:33:06 +00:00
|
|
|
}
|
2015-01-09 13:39:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps the supported events and their status
|
|
|
|
* (true it the event is enabled and false if it is disabled)
|
|
|
|
* @type {{
|
2016-06-17 20:35:40 +00:00
|
|
|
* incoming-message: boolean,
|
|
|
|
* outgoing-message: boolean,
|
|
|
|
* display-name-change: boolean,
|
|
|
|
* participant-left: boolean,
|
|
|
|
* participant-joined: boolean,
|
|
|
|
* video-conference-left: boolean,
|
|
|
|
* video-conference-joined: boolean
|
2015-01-09 13:39:32 +00:00
|
|
|
* }}
|
|
|
|
*/
|
2016-01-14 15:05:54 +00:00
|
|
|
const events = {
|
2016-06-17 20:35:40 +00:00
|
|
|
"incoming-message": false,
|
|
|
|
"outgoing-message":false,
|
|
|
|
"display-name-change": false,
|
|
|
|
"participant-joined": false,
|
|
|
|
"participant-left": false,
|
|
|
|
"video-conference-joined": false,
|
2016-10-05 21:33:09 +00:00
|
|
|
"video-conference-left": false,
|
|
|
|
"video-ready-to-close": false
|
2015-01-09 13:39:32 +00:00
|
|
|
};
|
|
|
|
|
2016-06-13 21:11:44 +00:00
|
|
|
/**
|
|
|
|
* Sends message to the external application.
|
2016-06-17 20:35:40 +00:00
|
|
|
* @param message {object}
|
|
|
|
* @param method {string}
|
|
|
|
* @param params {object} the object that will be sent as JSON string
|
2016-06-13 21:11:44 +00:00
|
|
|
*/
|
2016-06-17 20:35:40 +00:00
|
|
|
function sendMessage(message) {
|
|
|
|
if(enabled)
|
|
|
|
postis.send(message);
|
2016-06-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 15:05:54 +00:00
|
|
|
/**
|
|
|
|
* Check whether the API should be enabled or not.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isEnabled () {
|
2016-06-17 20:35:40 +00:00
|
|
|
return (typeof jitsi_meet_external_api_id === "number");
|
2015-01-23 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 15:05:54 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the event is enabled ot not.
|
|
|
|
* @param name the name of the event.
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function isEventEnabled (name) {
|
|
|
|
return events[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends event object to the external application that has been subscribed
|
|
|
|
* for that event.
|
|
|
|
* @param name the name event
|
|
|
|
* @param object data associated with the event
|
|
|
|
*/
|
|
|
|
function triggerEvent (name, object) {
|
2016-06-17 20:35:40 +00:00
|
|
|
if(isEventEnabled(name))
|
|
|
|
sendMessage({method: name, params: object});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles system messages. (for example: enable/disable events)
|
|
|
|
* @param message {object} the message
|
|
|
|
*/
|
|
|
|
function onSystemMessage(message) {
|
|
|
|
switch (message.type) {
|
|
|
|
case "eventStatus":
|
|
|
|
if(!message.name || !message.value) {
|
|
|
|
console.warn("Unknown system message format", message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
events[message.name] = message.value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn("Unknown system message type", message);
|
2016-01-14 15:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2015-01-09 13:39:32 +00:00
|
|
|
/**
|
|
|
|
* Initializes the APIConnector. Setups message event listeners that will
|
|
|
|
* receive information from external applications that embed Jitsi Meet.
|
|
|
|
* It also sends a message to the external application that APIConnector
|
|
|
|
* is initialized.
|
2016-06-13 21:11:44 +00:00
|
|
|
* @param options {object}
|
2016-06-17 20:35:40 +00:00
|
|
|
* @param forceEnable {boolean} if true the module will be enabled.
|
|
|
|
* @param enabledEvents {array} array of events that should be enabled.
|
2015-01-09 13:39:32 +00:00
|
|
|
*/
|
2016-06-17 20:35:40 +00:00
|
|
|
init (options = {}) {
|
|
|
|
if(!isEnabled() && !options.forceEnable)
|
2016-01-14 15:05:54 +00:00
|
|
|
return;
|
2016-06-13 21:11:44 +00:00
|
|
|
|
2016-06-17 20:35:40 +00:00
|
|
|
enabled = true;
|
|
|
|
if(options.enabledEvents)
|
|
|
|
options.enabledEvents.forEach(function (eventName) {
|
|
|
|
events[eventName] = true;
|
|
|
|
});
|
|
|
|
let postisOptions = {
|
|
|
|
window: target
|
|
|
|
};
|
|
|
|
if(typeof jitsi_meet_external_api_id === "number")
|
|
|
|
postisOptions.scope
|
|
|
|
= "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
|
|
|
|
postis = postisInit(postisOptions);
|
|
|
|
postis.listen("jitsiSystemMessage", onSystemMessage);
|
|
|
|
initCommands();
|
2015-01-09 13:39:32 +00:00
|
|
|
},
|
2016-01-14 15:05:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify external application (if API is enabled) that message was sent.
|
|
|
|
* @param {string} body message body
|
|
|
|
*/
|
|
|
|
notifySendingChatMessage (body) {
|
2016-06-17 20:35:40 +00:00
|
|
|
triggerEvent("outgoing-message", {"message": body});
|
2016-06-13 21:11:44 +00:00
|
|
|
},
|
|
|
|
|
2015-01-09 13:39:32 +00:00
|
|
|
/**
|
2016-01-14 15:05:54 +00:00
|
|
|
* Notify external application (if API is enabled) that
|
|
|
|
* message was received.
|
|
|
|
* @param {string} id user id
|
|
|
|
* @param {string} nick user nickname
|
|
|
|
* @param {string} body message body
|
|
|
|
* @param {number} ts message creation timestamp
|
2015-01-09 13:39:32 +00:00
|
|
|
*/
|
2016-01-14 15:05:54 +00:00
|
|
|
notifyReceivedChatMessage (id, nick, body, ts) {
|
|
|
|
if (APP.conference.isLocalId(id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerEvent(
|
2016-06-17 20:35:40 +00:00
|
|
|
"incoming-message",
|
2016-01-14 15:05:54 +00:00
|
|
|
{"from": id, "nick": nick, "message": body, "stamp": ts}
|
|
|
|
);
|
2015-01-09 13:39:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-01-14 15:05:54 +00:00
|
|
|
* Notify external application (if API is enabled) that
|
|
|
|
* user joined the conference.
|
|
|
|
* @param {string} id user id
|
2015-01-09 13:39:32 +00:00
|
|
|
*/
|
2016-01-14 15:05:54 +00:00
|
|
|
notifyUserJoined (id) {
|
2016-06-17 20:35:40 +00:00
|
|
|
triggerEvent("participant-joined", {id});
|
2016-01-14 15:05:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify external application (if API is enabled) that
|
|
|
|
* user left the conference.
|
|
|
|
* @param {string} id user id
|
|
|
|
*/
|
|
|
|
notifyUserLeft (id) {
|
2016-06-17 20:35:40 +00:00
|
|
|
triggerEvent("participant-left", {id});
|
2016-01-14 15:05:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify external application (if API is enabled) that
|
|
|
|
* user changed their nickname.
|
|
|
|
* @param {string} id user id
|
|
|
|
* @param {string} displayName user nickname
|
|
|
|
*/
|
|
|
|
notifyDisplayNameChanged (id, displayName) {
|
2016-06-17 20:35:40 +00:00
|
|
|
triggerEvent("display-name-change", {id, displayname: displayName});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify external application (if API is enabled) that
|
|
|
|
* user changed their nickname.
|
|
|
|
* @param {string} id user id
|
|
|
|
* @param {string} displayName user nickname
|
|
|
|
*/
|
|
|
|
notifyConferenceJoined (room) {
|
|
|
|
triggerEvent("video-conference-joined", {roomName: room});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify external application (if API is enabled) that
|
|
|
|
* user changed their nickname.
|
|
|
|
* @param {string} id user id
|
|
|
|
* @param {string} displayName user nickname
|
|
|
|
*/
|
|
|
|
notifyConferenceLeft (room) {
|
|
|
|
triggerEvent("video-conference-left", {roomName: room});
|
2015-01-09 13:39:32 +00:00
|
|
|
},
|
|
|
|
|
2016-10-05 21:33:09 +00:00
|
|
|
/**
|
|
|
|
* Notify external application (if API is enabled) that
|
|
|
|
* we are ready to be closed.
|
|
|
|
*/
|
|
|
|
notifyReadyToClose () {
|
|
|
|
triggerEvent("video-ready-to-close", {});
|
|
|
|
},
|
|
|
|
|
2015-01-09 13:39:32 +00:00
|
|
|
/**
|
|
|
|
* Removes the listeners.
|
|
|
|
*/
|
|
|
|
dispose: function () {
|
2016-06-17 20:35:40 +00:00
|
|
|
if(enabled)
|
|
|
|
postis.destroy();
|
2015-01-09 13:39:32 +00:00
|
|
|
}
|
|
|
|
};
|