diff --git a/app.js b/app.js index 7ec3a8345..e6ac77886 100644 --- a/app.js +++ b/app.js @@ -19,10 +19,10 @@ import RoomnameGenerator from './modules/util/RoomnameGenerator'; import UI from "./modules/UI/UI"; import statistics from "./modules/statistics/statistics"; import settings from "./modules/settings/Settings"; -import UIEvents from './service/UI/UIEvents'; - import conference from './conference'; +import API from './modules/API/API'; +import UIEvents from './service/UI/UIEvents'; function buildRoomName () { @@ -61,9 +61,9 @@ const APP = { UI, statistics, settings, + conference, + API, init () { - this.conference = conference; - this.API = require("./modules/API/API"); this.connectionquality = require("./modules/connectionquality/connectionquality"); this.desktopsharing = @@ -140,17 +140,13 @@ $(document).ready(function () { APP.translation.init(settings.getLanguage()); - if (APP.API.isEnabled()) { - APP.API.init(); - } + APP.API.init(); obtainConfigAndInit(); }); $(window).bind('beforeunload', function () { - if (APP.API.isEnabled()) { - APP.API.dispose(); - } + APP.API.dispose(); }); module.exports = APP; diff --git a/conference.js b/conference.js index d75b11ec6..092bf1e55 100644 --- a/conference.js +++ b/conference.js @@ -346,6 +346,7 @@ export default { room.on(ConferenceEvents.USER_JOINED, (id, user) => { console.log('USER %s connnected', id, user); + APP.API.notifyUserJoined(id); // FIXME email??? APP.UI.addUser(id, user.getDisplayName()); @@ -354,6 +355,7 @@ export default { }); room.on(ConferenceEvents.USER_LEFT, (id, user) => { console.log('USER %s LEFT', id, user); + APP.API.notifyUserLeft(id); APP.UI.removeUser(id, user.getDisplayName()); APP.UI.stopPrezi(id); }); @@ -435,11 +437,14 @@ export default { APP.UI.markVideoInterrupted(false); }); room.on(ConferenceEvents.MESSAGE_RECEIVED, (id, text, ts) => { - APP.UI.addMessage(id, getDisplayName(id), text, ts); + let nick = getDisplayName(id); + APP.API.notifyReceivedChatMessage(id, nick, text, ts); + APP.UI.addMessage(id, nick, text, ts); }); } room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, (id, displayName) => { + APP.API.notifyDisplayNameChanged(id, displayName); APP.UI.changeDisplayName(id, displayName); }); @@ -482,6 +487,7 @@ export default { if (!interfaceConfig.filmStripOnly) { APP.UI.addListener(UIEvents.MESSAGE_CREATED, (message) => { + APP.API.notifySendingChatMessage(message); room.sendTextMessage(message); }); } diff --git a/modules/API/API.js b/modules/API/API.js index be02a69de..ea5cdb7e7 100644 --- a/modules/API/API.js +++ b/modules/API/API.js @@ -5,8 +5,6 @@ * applications that embed Jitsi Meet */ -var XMPPEvents = require("../../service/xmpp/XMPPEvents"); - /** * List of the available commands. * @type {{ @@ -43,7 +41,7 @@ function initCommands() { * participantLeft: boolean * }} */ -var events = { +const events = { incomingMessage: false, outgoingMessage:false, displayNameChange: false, @@ -51,8 +49,6 @@ var events = { participantLeft: false }; -var displayName = {}; - /** * Processes commands from external application. * @param message the object with the command @@ -128,44 +124,42 @@ function processMessage(event) { } } -function setupListeners() { - APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_JOINED, function (from) { - API.triggerEvent("participantJoined", {jid: from}); - }); - APP.xmpp.addListener(XMPPEvents.MESSAGE_RECEIVED, - function (from, nick, txt, myjid, stamp) { - if (from != myjid) - API.triggerEvent("incomingMessage", - {"from": from, "nick": nick, "message": txt, "stamp": stamp}); - }); - APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_LEFT, function (jid) { - API.triggerEvent("participantLeft", {jid: jid}); - }); - APP.xmpp.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, - function (jid, newDisplayName) { - var name = displayName[jid]; - if(!name || name != newDisplayName) { - API.triggerEvent("displayNameChange", - {jid: jid, displayname: newDisplayName}); - displayName[jid] = newDisplayName; - } - }); - APP.xmpp.addListener(XMPPEvents.SENDING_CHAT_MESSAGE, function (body) { - APP.API.triggerEvent("outgoingMessage", {"message": body}); - }); +/** + * Check whether the API should be enabled or not. + * @returns {boolean} + */ +function isEnabled () { + let hash = location.hash; + return hash && hash.indexOf("external") > -1 && window.postMessage; } -var API = { - /** - * Check whether the API should be enabled or not. - * @returns {boolean} - */ - isEnabled: function () { - var hash = location.hash; - if (hash && hash.indexOf("external") > -1 && window.postMessage) - return true; - return false; - }, +/** + * 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) { + if (this.isEnabled() && isEventEnabled(name)) { + sendMessage({ + type: "event", + action: "result", + event: name, + result: object + }); + } +} + +export default { /** * Initializes the APIConnector. Setups message event listeners that will * receive information from external applications that embed Jitsi Meet. @@ -173,50 +167,85 @@ var API = { * is initialized. */ init: function () { + if (!isEnabled()) { + return; + } initCommands(); if (window.addEventListener) { - window.addEventListener('message', - processMessage, false); - } - else { + window.addEventListener('message', processMessage, false); + } else { window.attachEvent('onmessage', processMessage); } sendMessage({type: "system", loaded: true}); - setupListeners(); - }, - /** - * Checks whether the event is enabled ot not. - * @param name the name of the event. - * @returns {*} - */ - isEventEnabled: function (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 + * Notify external application (if API is enabled) that message was sent. + * @param {string} body message body */ - triggerEvent: function (name, object) { - if(this.isEnabled() && this.isEventEnabled(name)) - sendMessage({ - type: "event", action: "result", event: name, result: object}); + notifySendingChatMessage (body) { + triggerEvent("outgoingMessage", {"message": body}); + }, + + /** + * 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 + */ + notifyReceivedChatMessage (id, nick, body, ts) { + if (APP.conference.isLocalId(id)) { + return; + } + + triggerEvent( + "incomingMessage", + {"from": id, "nick": nick, "message": body, "stamp": ts} + ); + }, + + /** + * Notify external application (if API is enabled) that + * user joined the conference. + * @param {string} id user id + */ + 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}); }, /** * Removes the listeners. */ dispose: function () { - if(window.removeEventListener) { - window.removeEventListener("message", - processMessage, false); + if (!isEnabled()) { + return; } - else { + + if (window.removeEventListener) { + window.removeEventListener("message", processMessage, false); + } else { window.detachEvent('onmessage', processMessage); } } }; - -module.exports = API; \ No newline at end of file