2015-01-19 09:20:00 +00:00
|
|
|
/* global $, $iq, config, connection, UI, messageHandler,
|
2015-01-07 14:54:03 +00:00
|
|
|
roomName, sessionTerminated, Strophe, Util */
|
2015-01-28 14:35:22 +00:00
|
|
|
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
|
|
|
|
|
2014-11-28 15:17:53 +00:00
|
|
|
/**
|
|
|
|
* Contains logic responsible for enabling/disabling functionality available
|
|
|
|
* only to moderator users.
|
|
|
|
*/
|
2015-01-19 09:20:00 +00:00
|
|
|
var connection = null;
|
|
|
|
var focusUserJid;
|
2015-01-28 14:35:22 +00:00
|
|
|
|
2015-01-23 12:01:44 +00:00
|
|
|
function createExpBackoffTimer(step) {
|
|
|
|
var count = 1;
|
|
|
|
return function (reset) {
|
|
|
|
// Reset call
|
|
|
|
if (reset) {
|
|
|
|
count = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Calculate next timeout
|
|
|
|
var timeout = Math.pow(2, count - 1);
|
|
|
|
count += 1;
|
|
|
|
return timeout * step;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var getNextTimeout = createExpBackoffTimer(1000);
|
|
|
|
var getNextErrorTimeout = createExpBackoffTimer(1000);
|
2015-01-19 09:20:00 +00:00
|
|
|
// External authentication stuff
|
|
|
|
var externalAuthEnabled = false;
|
|
|
|
// Sip gateway can be enabled by configuring Jigasi host in config.js or
|
|
|
|
// it will be enabled automatically if focus detects the component through
|
|
|
|
// service discovery.
|
|
|
|
var sipGatewayEnabled = config.hosts.call_control !== undefined;
|
|
|
|
|
2015-01-27 09:56:22 +00:00
|
|
|
var eventEmitter = null;
|
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
var Moderator = {
|
|
|
|
isModerator: function () {
|
2015-01-07 14:54:03 +00:00
|
|
|
return connection && connection.emuc.isModerator();
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-11-28 15:17:53 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
isPeerModerator: function (peerJid) {
|
|
|
|
return connection &&
|
|
|
|
connection.emuc.getMemberRole(peerJid) === 'moderator';
|
|
|
|
},
|
2014-12-16 13:54:13 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
isExternalAuthEnabled: function () {
|
2014-12-16 13:54:13 +00:00
|
|
|
return externalAuthEnabled;
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-12-16 13:54:13 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
isSipGatewayEnabled: function () {
|
2015-01-05 15:31:31 +00:00
|
|
|
return sipGatewayEnabled;
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2015-01-05 15:31:31 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
setConnection: function (con) {
|
|
|
|
connection = con;
|
|
|
|
},
|
|
|
|
|
2015-01-27 09:56:22 +00:00
|
|
|
init: function (xmpp, emitter) {
|
2015-01-19 09:20:00 +00:00
|
|
|
this.xmppService = xmpp;
|
2015-01-27 09:56:22 +00:00
|
|
|
eventEmitter = emitter;
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-11-28 15:18:12 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
onMucLeft: function (jid) {
|
2015-01-07 14:54:03 +00:00
|
|
|
console.info("Someone left is it focus ? " + jid);
|
|
|
|
var resource = Strophe.getResourceFromJid(jid);
|
2015-01-19 09:20:00 +00:00
|
|
|
if (resource === 'focus' && !this.xmppService.sessionTerminated) {
|
2015-01-07 14:54:03 +00:00
|
|
|
console.info(
|
|
|
|
"Focus has left the room - leaving conference");
|
|
|
|
//hangUp();
|
|
|
|
// We'd rather reload to have everything re-initialized
|
|
|
|
// FIXME: show some message before reload
|
|
|
|
location.reload();
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setFocusUserJid: function (focusJid) {
|
2014-12-05 15:43:04 +00:00
|
|
|
if (!focusUserJid) {
|
|
|
|
focusUserJid = focusJid;
|
|
|
|
console.info("Focus jid set to: " + focusUserJid);
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-12-05 15:43:04 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
getFocusUserJid: function () {
|
2014-12-05 15:43:04 +00:00
|
|
|
return focusUserJid;
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-12-05 15:43:04 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
getFocusComponent: function () {
|
2014-12-05 15:43:04 +00:00
|
|
|
// Get focus component address
|
|
|
|
var focusComponent = config.hosts.focus;
|
|
|
|
// If not specified use default: 'focus.domain'
|
|
|
|
if (!focusComponent) {
|
|
|
|
focusComponent = 'focus.' + config.hosts.domain;
|
|
|
|
}
|
2014-12-05 15:58:11 +00:00
|
|
|
return focusComponent;
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-12-05 15:58:11 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
createConferenceIq: function (roomName) {
|
2014-12-05 15:43:04 +00:00
|
|
|
// Generate create conference IQ
|
2014-12-05 15:58:11 +00:00
|
|
|
var elem = $iq({to: Moderator.getFocusComponent(), type: 'set'});
|
2014-11-28 15:18:12 +00:00
|
|
|
elem.c('conference', {
|
|
|
|
xmlns: 'http://jitsi.org/protocol/focus',
|
|
|
|
room: roomName
|
|
|
|
});
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.hosts.bridge !== undefined) {
|
2014-12-08 08:24:23 +00:00
|
|
|
elem.c(
|
|
|
|
'property',
|
|
|
|
{ name: 'bridge', value: config.hosts.bridge})
|
|
|
|
.up();
|
|
|
|
}
|
2015-01-05 15:31:31 +00:00
|
|
|
// Tell the focus we have Jigasi configured
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.hosts.call_control !== undefined) {
|
2015-01-05 15:31:31 +00:00
|
|
|
elem.c(
|
|
|
|
'property',
|
|
|
|
{ name: 'call_control', value: config.hosts.call_control})
|
|
|
|
.up();
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.channelLastN !== undefined) {
|
2014-11-28 15:31:01 +00:00
|
|
|
elem.c(
|
|
|
|
'property',
|
|
|
|
{ name: 'channelLastN', value: config.channelLastN})
|
|
|
|
.up();
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.adaptiveLastN !== undefined) {
|
2014-11-28 15:31:01 +00:00
|
|
|
elem.c(
|
|
|
|
'property',
|
|
|
|
{ name: 'adaptiveLastN', value: config.adaptiveLastN})
|
|
|
|
.up();
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.adaptiveSimulcast !== undefined) {
|
2014-11-28 15:31:01 +00:00
|
|
|
elem.c(
|
|
|
|
'property',
|
|
|
|
{ name: 'adaptiveSimulcast', value: config.adaptiveSimulcast})
|
|
|
|
.up();
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.openSctp !== undefined) {
|
2014-12-05 15:29:59 +00:00
|
|
|
elem.c(
|
|
|
|
'property',
|
|
|
|
{ name: 'openSctp', value: config.openSctp})
|
|
|
|
.up();
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.enableFirefoxSupport !== undefined) {
|
2014-12-05 15:29:59 +00:00
|
|
|
elem.c(
|
|
|
|
'property',
|
2015-01-19 09:20:00 +00:00
|
|
|
{ name: 'enableFirefoxHacks',
|
|
|
|
value: config.enableFirefoxSupport})
|
2014-12-05 15:29:59 +00:00
|
|
|
.up();
|
|
|
|
}
|
2014-11-28 15:18:12 +00:00
|
|
|
elem.up();
|
2014-11-28 15:31:01 +00:00
|
|
|
return elem;
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-12-16 13:54:13 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
parseConfigOptions: function (resultIq) {
|
|
|
|
|
2014-12-16 13:54:13 +00:00
|
|
|
Moderator.setFocusUserJid(
|
|
|
|
$(resultIq).find('conference').attr('focusjid'));
|
2015-01-19 09:20:00 +00:00
|
|
|
|
2014-12-16 13:54:13 +00:00
|
|
|
var extAuthParam
|
|
|
|
= $(resultIq).find('>conference>property[name=\'externalAuth\']');
|
|
|
|
if (extAuthParam.length) {
|
|
|
|
externalAuthEnabled = extAuthParam.attr('value') === 'true';
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
|
2014-12-16 13:54:13 +00:00
|
|
|
console.info("External authentication enabled: " + externalAuthEnabled);
|
2015-01-19 09:20:00 +00:00
|
|
|
|
2015-01-05 15:31:31 +00:00
|
|
|
// Check if focus has auto-detected Jigasi component(this will be also
|
|
|
|
// included if we have passed our host from the config)
|
|
|
|
if ($(resultIq).find(
|
2015-01-19 09:20:00 +00:00
|
|
|
'>conference>property[name=\'sipGatewayEnabled\']').length) {
|
2015-01-05 15:31:31 +00:00
|
|
|
sipGatewayEnabled = true;
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
|
2015-01-05 15:31:31 +00:00
|
|
|
console.info("Sip gateway enabled: " + sipGatewayEnabled);
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-12-16 13:54:13 +00:00
|
|
|
|
2014-11-28 15:31:01 +00:00
|
|
|
// FIXME: we need to show the fact that we're waiting for the focus
|
|
|
|
// to the user(or that focus is not available)
|
2015-01-19 09:20:00 +00:00
|
|
|
allocateConferenceFocus: function (roomName, callback) {
|
2014-12-05 15:43:04 +00:00
|
|
|
// Try to use focus user JID from the config
|
|
|
|
Moderator.setFocusUserJid(config.focusUserJid);
|
|
|
|
// Send create conference IQ
|
2015-01-19 09:20:00 +00:00
|
|
|
var iq = Moderator.createConferenceIq(roomName);
|
2015-01-19 10:00:30 +00:00
|
|
|
var self = this;
|
2014-11-28 15:31:01 +00:00
|
|
|
connection.sendIQ(
|
|
|
|
iq,
|
2014-11-28 15:18:12 +00:00
|
|
|
function (result) {
|
|
|
|
if ('true' === $(result).find('conference').attr('ready')) {
|
2014-11-28 15:18:31 +00:00
|
|
|
// Reset both timers
|
2014-11-28 15:18:12 +00:00
|
|
|
getNextTimeout(true);
|
2014-11-28 15:18:31 +00:00
|
|
|
getNextErrorTimeout(true);
|
2014-12-16 13:54:13 +00:00
|
|
|
// Setup config options
|
|
|
|
Moderator.parseConfigOptions(result);
|
|
|
|
// Exec callback
|
2014-11-28 15:18:12 +00:00
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
var waitMs = getNextTimeout();
|
|
|
|
console.info("Waiting for the focus... " + waitMs);
|
2014-11-28 15:18:31 +00:00
|
|
|
// Reset error timeout
|
|
|
|
getNextErrorTimeout(true);
|
2014-11-28 15:18:12 +00:00
|
|
|
window.setTimeout(
|
|
|
|
function () {
|
2014-11-28 15:31:01 +00:00
|
|
|
Moderator.allocateConferenceFocus(
|
|
|
|
roomName, callback);
|
2014-11-28 15:18:12 +00:00
|
|
|
}, waitMs);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function (error) {
|
2014-12-16 13:54:13 +00:00
|
|
|
// Not authorized to create new room
|
|
|
|
if ($(error).find('>error>not-authorized').length) {
|
|
|
|
console.warn("Unauthorized to start the conference");
|
2015-01-14 16:58:13 +00:00
|
|
|
var toDomain
|
|
|
|
= Strophe.getDomainFromJid(error.getAttribute('to'));
|
|
|
|
if (toDomain === config.hosts.anonymousdomain) {
|
|
|
|
// we are connected with anonymous domain and
|
|
|
|
// only non anonymous users can create rooms
|
|
|
|
// we must authorize the user
|
2015-01-19 10:00:30 +00:00
|
|
|
|
|
|
|
self.xmppService.promptLogin();
|
2015-01-14 16:58:13 +00:00
|
|
|
} else {
|
2015-01-27 09:56:22 +00:00
|
|
|
|
|
|
|
eventEmitter.emit(XMPPEvents.AUTHENTICATION_REQUIRED, // External authentication mode
|
|
|
|
function () {
|
|
|
|
Moderator.allocateConferenceFocus(
|
|
|
|
roomName, callback);
|
|
|
|
});
|
|
|
|
|
2015-01-14 16:58:13 +00:00
|
|
|
}
|
2014-12-16 13:54:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-28 15:18:31 +00:00
|
|
|
var waitMs = getNextErrorTimeout();
|
2014-11-28 15:18:12 +00:00
|
|
|
console.error("Focus error, retry after " + waitMs, error);
|
2014-12-05 15:58:11 +00:00
|
|
|
// Show message
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.messageHandler.notify(
|
2014-12-05 15:58:11 +00:00
|
|
|
'Conference focus', 'disconnected',
|
2015-01-19 09:20:00 +00:00
|
|
|
Moderator.getFocusComponent() +
|
|
|
|
' not available - retry in ' +
|
|
|
|
(waitMs / 1000) + ' sec');
|
2014-11-28 15:18:31 +00:00
|
|
|
// Reset response timeout
|
|
|
|
getNextTimeout(true);
|
2014-11-28 15:18:12 +00:00
|
|
|
window.setTimeout(
|
|
|
|
function () {
|
|
|
|
Moderator.allocateConferenceFocus(roomName, callback);
|
|
|
|
}, waitMs);
|
|
|
|
}
|
|
|
|
);
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2014-11-28 15:17:53 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
getAuthUrl: function (roomName, urlCallback) {
|
2014-12-16 13:54:13 +00:00
|
|
|
var iq = $iq({to: Moderator.getFocusComponent(), type: 'get'});
|
|
|
|
iq.c('auth-url', {
|
|
|
|
xmlns: 'http://jitsi.org/protocol/focus',
|
|
|
|
room: roomName
|
|
|
|
});
|
|
|
|
connection.sendIQ(
|
|
|
|
iq,
|
|
|
|
function (result) {
|
|
|
|
var url = $(result).find('auth-url').attr('url');
|
|
|
|
if (url) {
|
|
|
|
console.info("Got auth url: " + url);
|
|
|
|
urlCallback(url);
|
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
"Failed to get auth url fro mthe focus", result);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function (error) {
|
|
|
|
console.error("Get auth url error", error);
|
|
|
|
}
|
|
|
|
);
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
};
|
2014-12-16 13:54:13 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
module.exports = Moderator;
|
2014-11-28 15:17:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|