diff --git a/appinfo/routes.php b/appinfo/routes.php index 0119906e..9374b626 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -15,6 +15,7 @@ array('name' => 'settings#setUser', 'url' => '/settings/user', 'verb' => 'POST'), array('name' => 'settings#getIceServers', 'url' => '/settings/iceServers', 'verb' => 'GET'), array('name' => 'settings#getUsers', 'url' => '/settings/users', 'verb' => 'GET'), + array('name' => 'settings#getServerType', 'url' => '/settings/servertype', 'verb' => 'GET'), array('name' => 'externalApi#index', 'url' => '/ajax/externalApi.php', 'verb' => 'POST'), // array('name' => 'externalApi#check_password', 'url' => '/api/v2/checkPassword', 'verb' => 'POST'), diff --git a/js/ojsxc.js b/js/ojsxc.js index dbc37c59..9439d2c9 100644 --- a/js/ojsxc.js +++ b/js/ojsxc.js @@ -202,10 +202,12 @@ password: password }, success: function(d) { - if (d.result === 'success' && d.data && d.data.serverType !== 'internal' && d.data.xmpp.url !== '' && d.data.xmpp.url !== null) { + if (d.result === 'success' && d.data && d.data.serverType !== 'internal' && d.data.xmpp.url !== '' && d.data.xmpp.url !== null) { jsxc.storage.setItem('serverType', d.data.serverType); cb(d.data); } else if (d.data && d.data.serverType === 'internal') { // fake successful connection + // jsxc.storage.setItem('serverType', 'internal'); + jsxc.gui.showLoginBox = function(){}; jsxc.bid = username.toLowerCase() + '@' + window.location.host; jsxc.storage.setItem('jid', jsxc.bid + '/internal'); @@ -323,6 +325,9 @@ $(document).on('connected.jsxc', function() { // reset default avatar cache jsxc.storage.removeUserItem('defaultAvatars'); + // when we are connected it doesn't matter anymore whether we logged in without chat since the user + // must have manually logged in + jsxc.storage.setItem('login_without_chat', false); }); $(document).on('status.contacts.count status.contact.updated', function() { @@ -370,6 +375,7 @@ if (jsxc.el_exists(jsxc.options.loginForm.form) && jsxc.el_exists(jsxc.options.loginForm.jid) && jsxc.el_exists(jsxc.options.loginForm.pass)) { var link = $('').text($.t('Log_in_without_chat')).attr('href', '#').click(function() { + jsxc.storage.setItem('login_without_chat', true); jsxc.submitLoginForm(); }); @@ -389,4 +395,62 @@ observeContactsMenu(); } }); + + $(document).on('ready.roster.jsxc', function(event, state) { // TODO this may be removed + $(document).on( "click", '#jsxc_roster p', function() { + if (jsxc.storage.getItem('serverType') === 'internal') { + startInternalBackend(); + } + }); + }); + + function startInternalBackend() { + jsxc.bid = OC.currentUser.toLowerCase() + '@' + window.location.host; + + jsxc.storage.setItem('jid', jsxc.bid + '/internal'); + jsxc.storage.setItem('sid', 'internal'); + jsxc.storage.setItem('rid', '123456'); + + jsxc.options.set('xmpp', { + url: OC.generateUrl('apps/ojsxc/http-bind') + }); + + jsxc.start(jsxc.bid + '/internal', 'internal', '123456'); + jsxc.gui.restore(); + jsxc.gui.roster.toggle(jsxc.CONST.SHOWN); + $(document).trigger('attached.jsxc'); + } + + if (jsxc.storage.getItem('serverType') === 'internal') { + // when the page is (re) loaded and we already know we are using the internal backend we must override + // the show loginBox method so that the form isn't shown when clicking the relogin link + jsxc.gui.showLoginBox = function () {}; + } + + $(document).on('stateChange.jsxc', function _handler(event, state) { + if (state === jsxc.CONST.STATE.SUSPEND) { + /** + * The first time we go into suspend mode we check if we are using the internal backend. + * If this is the case and the user dexplicitlylicity press the "login_without_chat" button when logging + * into Nextcloud we know we are using another authentication mechanism (like SAML/SSO) and thus have + * to manually start the connection. + */ + $(document).off('stateChange.jsxc', _handler); + if (jsxc.storage.getItem('serverType') === null) { + $.ajax({ + url: OC.generateUrl('apps/ojsxc/settings/servertype'), + success: function (data) { + jsxc.storage.setItem('serverType', data.serverType); + jsxc.gui.showLoginBox = function(){}; + if (data.serverType === 'internal' && jsxc.storage.getItem('login_without_chat') !== true) { + startInternalBackend(); + } + } + }); + } else if (jsxc.storage.getItem('serverType') === 'internal' && jsxc.storage.getItem('login_without_chat') !== true) { + jsxc.gui.showLoginBox = function(){}; + startInternalBackend(); + } + } + }); }(jQuery)); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index a189ec7a..094f6d6c 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -254,6 +254,14 @@ public function getUsers($search = '') return $response; } + /** + * @NoAdminRequired + */ + public function getServerType() + { + return ["serverType" => $this->getAppValue('serverType', 'internal')]; + } + private function getCurrentUser() { $currentUser = false;