mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
[fix] Connect to WebSocket route before every API call
This commit is contained in:
parent
9215420093
commit
3fc7599a99
1 changed files with 142 additions and 153 deletions
283
js/app.js
283
js/app.js
|
@ -93,160 +93,153 @@ app = Sammy('#main', function (sam) {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// Websocket connection
|
// API call
|
||||||
websocket: function(callback) {
|
api: function(uri, callback, method, data) {
|
||||||
c = this;
|
c = this;
|
||||||
if (store.get('connected')) {
|
|
||||||
c.ws = new WebSocket('wss://'+ store.get('url') +'/messages');
|
|
||||||
c.ws.onmessage = function(evt) {
|
|
||||||
console.log(evt.data);
|
|
||||||
$.each($.parseJSON(evt.data), function(k, v) {
|
|
||||||
c.flash(k, v);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
c.ws.onopen = function() {
|
|
||||||
if (typeof callback === 'function') {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.ws.onclose = function() {
|
|
||||||
delete c.ws;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// API connection helper
|
// Open a WebSocket connection to retrieve live messages from the moulinette
|
||||||
api: function (uri, callback, method, data) {
|
ws = new WebSocket('wss://'+ store.get('url') +'/messages');
|
||||||
c = this;
|
ws.onmessage = function(evt) {
|
||||||
method = typeof method !== 'undefined' ? method : 'GET';
|
console.log(evt.data);
|
||||||
data = typeof data !== 'undefined' ? data : {};
|
$.each($.parseJSON(evt.data), function(k, v) {
|
||||||
if (window.navigator && window.navigator.language && (typeof data.locale === 'undefined')) {
|
c.flash(k, v);
|
||||||
data.locale = window.navigator.language;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var args = data;
|
// If not connected, WebSocket connection will raise an error, but we do not want to interrupt API request
|
||||||
// auth = "Basic "+ btoa('admin' +':'+ atob('yolo'));
|
ws.onerror = ws.onopen;
|
||||||
if (uri === '/postinstall') {
|
|
||||||
var installing = false;
|
|
||||||
|
|
||||||
setInterval(function () {
|
ws.onopen = function(evt) {
|
||||||
installing = true;
|
method = typeof method !== 'undefined' ? method : 'GET';
|
||||||
}, 1500);
|
data = typeof data !== 'undefined' ? data : {};
|
||||||
|
if (window.navigator && window.navigator.language && (typeof data.locale === 'undefined')) {
|
||||||
|
data.locale = window.navigator.language;
|
||||||
|
}
|
||||||
|
|
||||||
$('#popup-title').text(y18n.t('installing'));
|
var args = data;
|
||||||
$('#popup-body').html('<p>'+y18n.t('installation_complete_wait', [data.domain])+'</p>');
|
// auth = "Basic "+ btoa('admin' +':'+ atob('yolo'));
|
||||||
$('#popup-body').append('<div class="loader loader-popup"></div>');
|
if (uri === '/postinstall') {
|
||||||
$('#popup').modal('show');
|
var installing = false;
|
||||||
} else {
|
|
||||||
loaded = false;
|
|
||||||
if ($('div.loader').length == 0) {
|
|
||||||
setInterval(function () {
|
|
||||||
if (!loaded && $('div.loader').length == 0) {
|
|
||||||
$('#main').append('<div class="loader loader-content"></div>');
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
jQuery.ajax({
|
|
||||||
url: 'https://'+ store.get('url') + uri,
|
|
||||||
type: method,
|
|
||||||
crossdomain: true,
|
|
||||||
data: data,
|
|
||||||
traditional: true,
|
|
||||||
dataType: 'json',
|
|
||||||
// beforeSend: function(req) {
|
|
||||||
// req.setRequestHeader('Authorization', auth);
|
|
||||||
// }
|
|
||||||
})
|
|
||||||
/*
|
|
||||||
.always(function(data) {
|
|
||||||
if (data.status !== 'undefined' && uri === '/login') {
|
|
||||||
if (data.status === 401) {
|
|
||||||
$('#popup').modal('hide');
|
|
||||||
c.flash('fail', y18n.t('wrong_password'));
|
|
||||||
}
|
|
||||||
// 200 & empty response TODO: better comment
|
|
||||||
// /login
|
|
||||||
else if (data.status === 200) {
|
|
||||||
// data = typeof data !== 'undefined' ? data : {};
|
|
||||||
if (typeof data.win !== 'undefined') {
|
|
||||||
$.each(data.win, function(k, v) {
|
|
||||||
c.flash('success', v);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
callback(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
loaded = true;
|
|
||||||
$('div.loader').remove();
|
|
||||||
|
|
||||||
})
|
setInterval(function () {
|
||||||
*/
|
installing = true;
|
||||||
.always(function(xhr, ts, error) {
|
}, 1500);
|
||||||
// console.log("always");
|
|
||||||
// console.log(xhr);
|
$('#popup-title').text(y18n.t('installing'));
|
||||||
// console.log(ts);
|
$('#popup-body').html('<p>'+y18n.t('installation_complete_wait', [data.domain])+'</p>');
|
||||||
// console.log(error);
|
$('#popup-body').append('<div class="loader loader-popup"></div>');
|
||||||
})
|
$('#popup').modal('show');
|
||||||
.done(function(data) {
|
|
||||||
// console.log('success');console.log(data);
|
|
||||||
// data = typeof data !== 'undefined' ? data : {};
|
|
||||||
data = data || {};
|
|
||||||
if (typeof data.win !== 'undefined') {
|
|
||||||
$.each(data.win, function(k, v) {
|
|
||||||
c.flash('success', v);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
callback(data);
|
|
||||||
})
|
|
||||||
.fail(function(xhr) {
|
|
||||||
// console.log('fail');console.log(xhr);
|
|
||||||
if (xhr.status == 401) {
|
|
||||||
$('#popup').modal('hide');
|
|
||||||
if (uri !== '/login') {
|
|
||||||
c.flash('fail', y18n.t('unauthorized'));
|
|
||||||
c.redirect('#/login');
|
|
||||||
}
|
|
||||||
} else if (typeof xhr.responseJSON !== 'undefined') {
|
|
||||||
$('#popup').modal('hide');
|
|
||||||
c.flash('fail', xhr.responseJSON.error);
|
|
||||||
} else if (typeof xhr.responseText !== 'undefined' && uri !== '/postinstall') {
|
|
||||||
$('#popup').modal('hide');
|
|
||||||
c.flash('fail', xhr.responseText);
|
|
||||||
} else {
|
} else {
|
||||||
if (uri == '/postinstall') {
|
loaded = false;
|
||||||
if (installing) {
|
if ($('div.loader').length == 0) {
|
||||||
if (args.domain.match(/\.nohost\.me$/) || args.domain.match(/\.noho\.st$/)) {
|
setInterval(function () {
|
||||||
$('#popup-title').text(y18n.t('installed'));
|
if (!loaded && $('div.loader').length == 0) {
|
||||||
$('#popup-body p').text(y18n.t('installation_complete_dns'));
|
$('#main').append('<div class="loader loader-content"></div>');
|
||||||
interval = 180000;
|
|
||||||
} else {
|
|
||||||
interval = 5000;
|
|
||||||
}
|
|
||||||
setInterval(function () {
|
|
||||||
if (window.location.hostname === args.domain) {
|
|
||||||
$('#popup-title').text(y18n.t('installation_complete'));
|
|
||||||
$('#popup-body').html(
|
|
||||||
'<p>'+ y18n.t('installation_complete_desc', ['https://'+ args.domain +'/yunohost/admin', args.domain +'/yunohost/admin']) +'</p>'
|
|
||||||
+ '<br>'
|
|
||||||
+ '<p><small>'+ y18n.t('installation_complete_help_dns') +'</small></p>');
|
|
||||||
} else {
|
|
||||||
$('#popup').modal('hide');
|
|
||||||
c.flash('success', y18n.t('installation_complete'));
|
|
||||||
c.redirect('#/login');
|
|
||||||
}
|
|
||||||
}, interval);
|
|
||||||
} else {
|
|
||||||
$('#popup').modal('hide');
|
|
||||||
c.flash('fail', y18n.t('error_occured'));
|
|
||||||
}
|
}
|
||||||
} else {
|
}, 500);
|
||||||
c.flash('fail', y18n.t('error_server'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
store.clear('slide');
|
jQuery.ajax({
|
||||||
c.redirect(store.get('path-1'));
|
url: 'https://'+ store.get('url') + uri,
|
||||||
});
|
type: method,
|
||||||
|
crossdomain: true,
|
||||||
|
data: data,
|
||||||
|
traditional: true,
|
||||||
|
dataType: 'json',
|
||||||
|
// beforeSend: function(req) {
|
||||||
|
// req.setRequestHeader('Authorization', auth);
|
||||||
|
// }
|
||||||
|
})
|
||||||
|
/*
|
||||||
|
.always(function(data) {
|
||||||
|
if (data.status !== 'undefined' && uri === '/login') {
|
||||||
|
if (data.status === 401) {
|
||||||
|
$('#popup').modal('hide');
|
||||||
|
c.flash('fail', y18n.t('wrong_password'));
|
||||||
|
}
|
||||||
|
// 200 & empty response TODO: better comment
|
||||||
|
// /login
|
||||||
|
else if (data.status === 200) {
|
||||||
|
// data = typeof data !== 'undefined' ? data : {};
|
||||||
|
if (typeof data.win !== 'undefined') {
|
||||||
|
$.each(data.win, function(k, v) {
|
||||||
|
c.flash('success', v);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
callback(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loaded = true;
|
||||||
|
$('div.loader').remove();
|
||||||
|
|
||||||
|
})
|
||||||
|
*/
|
||||||
|
.always(function(xhr, ts, error) {
|
||||||
|
// console.log("always");
|
||||||
|
// console.log(xhr);
|
||||||
|
// console.log(ts);
|
||||||
|
// console.log(error);
|
||||||
|
})
|
||||||
|
.done(function(data) {
|
||||||
|
// console.log('success');console.log(data);
|
||||||
|
// data = typeof data !== 'undefined' ? data : {};
|
||||||
|
data = data || {};
|
||||||
|
if (typeof data.win !== 'undefined') {
|
||||||
|
$.each(data.win, function(k, v) {
|
||||||
|
c.flash('success', v);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
callback(data);
|
||||||
|
})
|
||||||
|
.fail(function(xhr) {
|
||||||
|
// console.log('fail');console.log(xhr);
|
||||||
|
if (xhr.status == 401) {
|
||||||
|
$('#popup').modal('hide');
|
||||||
|
if (uri !== '/login') {
|
||||||
|
c.flash('fail', y18n.t('unauthorized'));
|
||||||
|
c.redirect('#/login');
|
||||||
|
}
|
||||||
|
} else if (typeof xhr.responseJSON !== 'undefined') {
|
||||||
|
$('#popup').modal('hide');
|
||||||
|
c.flash('fail', xhr.responseJSON.error);
|
||||||
|
} else if (typeof xhr.responseText !== 'undefined' && uri !== '/postinstall') {
|
||||||
|
$('#popup').modal('hide');
|
||||||
|
c.flash('fail', xhr.responseText);
|
||||||
|
} else {
|
||||||
|
if (uri == '/postinstall') {
|
||||||
|
if (installing) {
|
||||||
|
if (args.domain.match(/\.nohost\.me$/) || args.domain.match(/\.noho\.st$/)) {
|
||||||
|
$('#popup-title').text(y18n.t('installed'));
|
||||||
|
$('#popup-body p').text(y18n.t('installation_complete_dns'));
|
||||||
|
interval = 180000;
|
||||||
|
} else {
|
||||||
|
interval = 5000;
|
||||||
|
}
|
||||||
|
setInterval(function () {
|
||||||
|
if (window.location.hostname === args.domain) {
|
||||||
|
$('#popup-title').text(y18n.t('installation_complete'));
|
||||||
|
$('#popup-body').html(
|
||||||
|
'<p>'+ y18n.t('installation_complete_desc', ['https://'+ args.domain +'/yunohost/admin', args.domain +'/yunohost/admin']) +'</p>'
|
||||||
|
+ '<br>'
|
||||||
|
+ '<p><small>'+ y18n.t('installation_complete_help_dns') +'</small></p>');
|
||||||
|
} else {
|
||||||
|
$('#popup').modal('hide');
|
||||||
|
c.flash('success', y18n.t('installation_complete'));
|
||||||
|
c.redirect('#/login');
|
||||||
|
}
|
||||||
|
}, interval);
|
||||||
|
} else {
|
||||||
|
$('#popup').modal('hide');
|
||||||
|
c.flash('fail', y18n.t('error_occured'));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.flash('fail', y18n.t('error_server'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
store.clear('slide');
|
||||||
|
c.redirect(store.get('path-1'));
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Render view (cross-browser)
|
// Render view (cross-browser)
|
||||||
|
@ -396,7 +389,6 @@ app = Sammy('#main', function (sam) {
|
||||||
}
|
}
|
||||||
c.api('/login', function(data) {
|
c.api('/login', function(data) {
|
||||||
store.set('connected', true);
|
store.set('connected', true);
|
||||||
c.websocket();
|
|
||||||
|
|
||||||
$('.logout-button').fadeIn();
|
$('.logout-button').fadeIn();
|
||||||
c.flash('success', y18n.t('logged_in'));
|
c.flash('success', y18n.t('logged_in'));
|
||||||
|
@ -1079,9 +1071,6 @@ app = Sammy('#main', function (sam) {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
sam.websocket();
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue