jiti-meet/modules/API/API.js

252 lines
6.4 KiB
JavaScript
Raw Normal View History

/* global APP */
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
*/
/**
* List of the available commands.
* @type {{
* displayName: inputDisplayNameHandler,
* toggleAudio: toggleAudio,
* toggleVideo: toggleVideo,
* toggleFilmStrip: toggleFilmStrip,
* toggleChat: toggleChat,
* toggleContactList: toggleContactList
2015-01-09 13:39:32 +00:00
* }}
*/
var commands = {};
function initCommands() {
commands = {
displayName: APP.UI.inputDisplayNameHandler,
2015-11-30 11:54:54 +00:00
toggleAudio: APP.conference.toggleAudioMuted,
toggleVideo: APP.conference.toggleVideoMuted,
toggleFilmStrip: APP.UI.toggleFilmStrip,
toggleChat: APP.UI.toggleChat,
toggleContactList: APP.UI.toggleContactList
};
}
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 {{
* incomingMessage: boolean,
* outgoingMessage: boolean,
* displayNameChange: boolean,
* participantJoined: boolean,
* participantLeft: boolean
* }}
*/
2016-01-14 15:05:54 +00:00
const events = {
2015-01-09 13:39:32 +00:00
incomingMessage: false,
outgoingMessage:false,
displayNameChange: false,
participantJoined: false,
participantLeft: false
};
/**
* Processes commands from external application.
2015-01-09 13:39:32 +00:00
* @param message the object with the command
*/
function processCommand(message) {
if (message.action != "execute") {
2015-01-09 13:39:32 +00:00
console.error("Unknown action of the message");
return;
}
for (var key in message) {
2015-01-09 13:39:32 +00:00
if(commands[key])
commands[key].apply(null, message[key]);
}
}
/**
* Processes events objects from external applications
* @param event the event
*/
function processEvent(event) {
if (!event.action) {
2015-01-09 13:39:32 +00:00
console.error("Event with no action is received.");
return;
}
var i = 0;
switch(event.action) {
2015-01-09 13:39:32 +00:00
case "add":
for (; i < event.events.length; i++) {
2015-01-09 13:39:32 +00:00
events[event.events[i]] = true;
}
break;
case "remove":
for (; i < event.events.length; i++) {
2015-01-09 13:39:32 +00:00
events[event.events[i]] = false;
}
break;
default:
console.error("Unknown action for event.");
}
}
/**
* Sends message to the external application.
* @param object
*/
function sendMessage(object) {
window.parent.postMessage(JSON.stringify(object), "*");
}
/**
* Processes a message event from the external application
* @param event the message event
*/
function processMessage(event) {
2015-01-09 13:39:32 +00:00
var message;
try {
message = JSON.parse(event.data);
} catch (e) {}
if(!message.type)
return;
switch (message.type) {
2015-01-09 13:39:32 +00:00
case "command":
processCommand(message);
break;
case "event":
processEvent(message);
break;
default:
console.error("Unknown type of the message");
return;
}
}
2016-01-14 15:05:54 +00:00
/**
* Check whether the API should be enabled or not.
* @returns {boolean}
*/
function isEnabled () {
let hash = location.hash;
return hash && hash.indexOf("external=true") > -1 && window.postMessage;
}
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-01-25 20:08:50 +00:00
if (isEnabled() && isEventEnabled(name)) {
2016-01-14 15:05:54 +00:00
sendMessage({
type: "event",
action: "result",
event: name,
result: object
});
}
}
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.
*/
init: function () {
2016-01-14 15:05:54 +00:00
if (!isEnabled()) {
return;
}
initCommands();
if (window.addEventListener) {
2016-01-14 15:05:54 +00:00
window.addEventListener('message', processMessage, false);
} else {
2015-01-09 13:39:32 +00:00
window.attachEvent('onmessage', processMessage);
}
sendMessage({type: "system", loaded: true});
},
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) {
triggerEvent("outgoingMessage", {"message": body});
},
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(
"incomingMessage",
{"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) {
triggerEvent("participantJoined", {id});
},
/**
* Notify external application (if API is enabled) that
* user left the conference.
* @param {string} id user id
*/
notifyUserLeft (id) {
triggerEvent("participantLeft", {id});
},
/**
* 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) {
triggerEvent("displayNameChange", {id, displayname: displayName});
2015-01-09 13:39:32 +00:00
},
/**
* Removes the listeners.
*/
dispose: function () {
2016-01-14 15:05:54 +00:00
if (!isEnabled()) {
return;
2015-01-09 13:39:32 +00:00
}
2016-01-14 15:05:54 +00:00
if (window.removeEventListener) {
window.removeEventListener("message", processMessage, false);
} else {
2015-01-09 13:39:32 +00:00
window.detachEvent('onmessage', processMessage);
}
}
};