Merge pull request #456 from isymchych/rewrite-API-module

do not use xmpp module in API module
This commit is contained in:
hristoterezov 2016-01-25 12:55:30 -06:00
commit c2cfd4d6e2
3 changed files with 110 additions and 79 deletions

12
app.js
View File

@ -19,10 +19,10 @@ import RoomnameGenerator from './modules/util/RoomnameGenerator';
import UI from "./modules/UI/UI"; import UI from "./modules/UI/UI";
import statistics from "./modules/statistics/statistics"; import statistics from "./modules/statistics/statistics";
import settings from "./modules/settings/Settings"; import settings from "./modules/settings/Settings";
import UIEvents from './service/UI/UIEvents';
import conference from './conference'; import conference from './conference';
import API from './modules/API/API';
import UIEvents from './service/UI/UIEvents';
function buildRoomName () { function buildRoomName () {
@ -61,9 +61,9 @@ const APP = {
UI, UI,
statistics, statistics,
settings, settings,
conference,
API,
init () { init () {
this.conference = conference;
this.API = require("./modules/API/API");
this.connectionquality = this.connectionquality =
require("./modules/connectionquality/connectionquality"); require("./modules/connectionquality/connectionquality");
this.desktopsharing = this.desktopsharing =
@ -140,17 +140,13 @@ $(document).ready(function () {
APP.translation.init(settings.getLanguage()); APP.translation.init(settings.getLanguage());
if (APP.API.isEnabled()) {
APP.API.init(); APP.API.init();
}
obtainConfigAndInit(); obtainConfigAndInit();
}); });
$(window).bind('beforeunload', function () { $(window).bind('beforeunload', function () {
if (APP.API.isEnabled()) {
APP.API.dispose(); APP.API.dispose();
}
}); });
module.exports = APP; module.exports = APP;

View File

@ -346,6 +346,7 @@ export default {
room.on(ConferenceEvents.USER_JOINED, (id, user) => { room.on(ConferenceEvents.USER_JOINED, (id, user) => {
console.log('USER %s connnected', id, user); console.log('USER %s connnected', id, user);
APP.API.notifyUserJoined(id);
// FIXME email??? // FIXME email???
APP.UI.addUser(id, user.getDisplayName()); APP.UI.addUser(id, user.getDisplayName());
@ -354,6 +355,7 @@ export default {
}); });
room.on(ConferenceEvents.USER_LEFT, (id, user) => { room.on(ConferenceEvents.USER_LEFT, (id, user) => {
console.log('USER %s LEFT', id, user); console.log('USER %s LEFT', id, user);
APP.API.notifyUserLeft(id);
APP.UI.removeUser(id, user.getDisplayName()); APP.UI.removeUser(id, user.getDisplayName());
APP.UI.stopPrezi(id); APP.UI.stopPrezi(id);
}); });
@ -435,11 +437,14 @@ export default {
APP.UI.markVideoInterrupted(false); APP.UI.markVideoInterrupted(false);
}); });
room.on(ConferenceEvents.MESSAGE_RECEIVED, (id, text, ts) => { 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) => { room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, (id, displayName) => {
APP.API.notifyDisplayNameChanged(id, displayName);
APP.UI.changeDisplayName(id, displayName); APP.UI.changeDisplayName(id, displayName);
}); });
@ -482,6 +487,7 @@ export default {
if (!interfaceConfig.filmStripOnly) { if (!interfaceConfig.filmStripOnly) {
APP.UI.addListener(UIEvents.MESSAGE_CREATED, (message) => { APP.UI.addListener(UIEvents.MESSAGE_CREATED, (message) => {
APP.API.notifySendingChatMessage(message);
room.sendTextMessage(message); room.sendTextMessage(message);
}); });
} }

View File

@ -5,8 +5,6 @@
* applications that embed Jitsi Meet * applications that embed Jitsi Meet
*/ */
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
/** /**
* List of the available commands. * List of the available commands.
* @type {{ * @type {{
@ -43,7 +41,7 @@ function initCommands() {
* participantLeft: boolean * participantLeft: boolean
* }} * }}
*/ */
var events = { const events = {
incomingMessage: false, incomingMessage: false,
outgoingMessage:false, outgoingMessage:false,
displayNameChange: false, displayNameChange: false,
@ -51,8 +49,6 @@ var events = {
participantLeft: false participantLeft: false
}; };
var displayName = {};
/** /**
* Processes commands from external application. * Processes commands from external application.
* @param message the object with the command * @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});
});
}
var API = {
/**
* Check whether the API should be enabled or not. * Check whether the API should be enabled or not.
* @returns {boolean} * @returns {boolean}
*/ */
isEnabled: function () { function isEnabled () {
var hash = location.hash; let hash = location.hash;
if (hash && hash.indexOf("external") > -1 && window.postMessage) return 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 * Initializes the APIConnector. Setups message event listeners that will
* receive information from external applications that embed Jitsi Meet. * receive information from external applications that embed Jitsi Meet.
@ -173,50 +167,85 @@ var API = {
* is initialized. * is initialized.
*/ */
init: function () { init: function () {
if (!isEnabled()) {
return;
}
initCommands(); initCommands();
if (window.addEventListener) { if (window.addEventListener) {
window.addEventListener('message', window.addEventListener('message', processMessage, false);
processMessage, false); } else {
}
else {
window.attachEvent('onmessage', processMessage); window.attachEvent('onmessage', processMessage);
} }
sendMessage({type: "system", loaded: true}); 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 * Notify external application (if API is enabled) that message was sent.
* for that event. * @param {string} body message body
* @param name the name event
* @param object data associated with the event
*/ */
triggerEvent: function (name, object) { notifySendingChatMessage (body) {
if(this.isEnabled() && this.isEventEnabled(name)) triggerEvent("outgoingMessage", {"message": body});
sendMessage({ },
type: "event", action: "result", event: name, result: object});
/**
* 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. * Removes the listeners.
*/ */
dispose: function () { dispose: function () {
if(window.removeEventListener) { if (!isEnabled()) {
window.removeEventListener("message", return;
processMessage, false);
} }
else {
if (window.removeEventListener) {
window.removeEventListener("message", processMessage, false);
} else {
window.detachEvent('onmessage', processMessage); window.detachEvent('onmessage', processMessage);
} }
} }
}; };
module.exports = API;