Handle API calls in a single function

master
Daniel Berteaud 9 years ago
parent 3717af0721
commit a0df933842
  1. 321
      public/js/vroom.js

@ -105,8 +105,9 @@ function localize(string){
return string; return string;
} }
// Parse and display an error when an API call failed function vroomApi(action, param, success, error){
function showApiError(data){ if (typeof error !== 'function'){
error = function(data){
data = data.responseJSON; data = data.responseJSON;
if (data.msg){ if (data.msg){
$.notify(data.msg, 'error'); $.notify(data.msg, 'error');
@ -114,26 +115,38 @@ function showApiError(data){
else{ else{
$.notify(localize('ERROR_OCCURRED'), 'error'); $.notify(localize('ERROR_OCCURRED'), 'error');
} }
} };
}
// Handle lang switch drop down menu if (typeof success !== 'function'){
$('#switch_lang').change(function(){ success = function(){};
}
$.ajax({ $.ajax({
data: { data: {
req: JSON.stringify({ req: JSON.stringify({
action: 'switch_lang', action: action,
param : { param: param
language: $('#switch_lang').val()
}
}) })
}, },
error: function(data){ error: function(data){
showApiError(data); error(data);
}, },
success: function(data){ success: function(data){
window.location.reload(); success(data);
} }
}); });
}
// Handle lang switch drop down menu
$('#switch_lang').change(function(){
vroomApi(
'switch_lang',
{
language: $('#switch_lang').val()
},
function(data){
window.location.reload();
}
);
}); });
// Escape entities to prevent XSS // Escape entities to prevent XSS
@ -372,11 +385,9 @@ $('#configureRoomForm').submit(function(e){
$('input[name="emails[]"]').each(function(){ $('input[name="emails[]"]').each(function(){
emails.push($(this).val()); emails.push($(this).val());
}); });
$.ajax({ vroomApi(
data: { 'update_room_conf',
req: JSON.stringify({ {
action: 'update_room_conf',
param: {
room: roomName, room: roomName,
locked: locked, locked: locked,
ask_for_name: askForName, ask_for_name: askForName,
@ -385,13 +396,8 @@ $('#configureRoomForm').submit(function(e){
persistent: persist, persistent: persist,
max_members: members, max_members: members,
emails: emails emails: emails
}
})
}, },
error: function(data){ function(data){
showApiError(data);
},
success: function(data){
// On success, reset the input fields, collapse the password inputs // On success, reset the input fields, collapse the password inputs
// and close the configuration modal // and close the configuration modal
$('#ownerPass,#ownerPassConfirm,#joinPass,#joinPassConfirm').val(''); $('#ownerPass,#ownerPassConfirm,#joinPass,#joinPassConfirm').val('');
@ -400,24 +406,17 @@ $('#configureRoomForm').submit(function(e){
$.notify(data.msg, 'info'); $.notify(data.msg, 'info');
$('#configureRoomForm').trigger('room_conf_updated'); $('#configureRoomForm').trigger('room_conf_updated');
} }
}); );
}); });
// Get our role and other room settings from the server // Get our role and other room settings from the server
function getRoomInfo(cb){ function getRoomInfo(cb){
$.ajax({ vroomApi(
data: { 'get_room_conf',
req: JSON.stringify({ {
action: 'get_room_conf',
param: {
room: roomName, room: roomName,
}
})
}, },
error: function(data){ function(data){
showApiError(data);
},
success: function(data){
roomInfo = data; roomInfo = data;
// Reset the list of email displayed, so first remove every input field but the last one // Reset the list of email displayed, so first remove every input field but the last one
// We keep it so we can clone it again // We keep it so we can clone it again
@ -444,7 +443,7 @@ function getRoomInfo(cb){
cb(); cb();
} }
} }
}); );
} }
// Used on the index page // Used on the index page
@ -461,20 +460,16 @@ function initIndex(){
}); });
} }
else{ else{
$.ajax({ vroomApi(
data: { 'create_room',
req: JSON.stringify({ {
action: 'create_room',
param: {
room: $('#roomName').val() room: $('#roomName').val()
}
})
}, },
success: function(data) { function(data) {
room = data.room; room = data.room;
window.location.assign(rootUrl + data.room); window.location.assign(rootUrl + data.room);
}, },
error: function(data){ function(data){
data = data.responseJSON; data = data.responseJSON;
if (data.err && data.err == 'ERROR_NAME_CONFLICT' ){ if (data.err && data.err == 'ERROR_NAME_CONFLICT' ){
room = data.room; room = data.room;
@ -490,7 +485,7 @@ function initIndex(){
$.notify(localize('ERROR_OCCURRED'), 'error'); $.notify(localize('ERROR_OCCURRED'), 'error');
} }
} }
}); );
} }
}); });
@ -630,39 +625,25 @@ function initAdminRooms(){
// Request the list of existing rooms to the server // Request the list of existing rooms to the server
function getRooms(){ function getRooms(){
$.ajax({ vroomApi(
data: { 'get_room_list',
req: JSON.stringify({ {},
action: 'get_room_list', function(data){
param: {}
})
},
error: function(data){
showApiError(data);
},
success: function(data){
roomList = data.rooms; roomList = data.rooms;
matches = Object.keys(roomList).length; matches = Object.keys(roomList).length;
updateRoomList($('#searchRoom').val(), 0, itemPerPage); updateRoomList($('#searchRoom').val(), 0, itemPerPage);
updatePagination(); updatePagination();
} }
}); );
} }
function getRoomConf(roomName){ function getRoomConf(roomName){
$.ajax({ vroomApi(
data: { 'get_room_conf',
req: JSON.stringify({ {
action: 'get_room_conf',
param: {
room: roomName, room: roomName,
}
})
}, },
error: function(data){ function(data){
showApiError(data);
},
success: function(data){
// Reset the list of email displayed, so first remove evry input field but the last one // Reset the list of email displayed, so first remove evry input field but the last one
// We keep it so we can clone it again // We keep it so we can clone it again
$('.email-list').find('.email-entry:not(:last)').remove(); $('.email-list').find('.email-entry:not(:last)').remove();
@ -689,7 +670,7 @@ function initAdminRooms(){
// And display the config modal dialog // And display the config modal dialog
$('#configureModal').modal('show'); $('#configureModal').modal('show');
} }
}); );
} }
// Handle submiting the configuration form // Handle submiting the configuration form
@ -710,24 +691,17 @@ function initAdminRooms(){
// Delete room form // Delete room form
$('#deleteRoomForm').submit(function(e){ $('#deleteRoomForm').submit(function(e){
e.preventDefault(); e.preventDefault();
$.ajax({ vroomApi(
data: { 'delete_room',
req: JSON.stringify({ {
action: 'delete_room',
param: {
room: roomName, room: roomName,
}
})
},
error: function(data){
showApiError(data);
}, },
success: function(data){ function(data){
$.notify(data.msg, 'success'); $.notify(data.msg, 'success');
getRooms(); getRooms();
$('#deleteRoomModal').modal('hide'); $('#deleteRoomModal').modal('hide');
} }
}); );
}); });
// Update room list when searching // Update room list when searching
@ -802,26 +776,19 @@ function initAdminAudit(){
} }
function reloadEvents(start,end){ function reloadEvents(start,end){
$.ajax({ vroomApi(
data: { 'get_event_list',
req: JSON.stringify({ {
action: 'get_event_list',
param: {
start: start, start: start,
end: end end: end
}
})
},
error: function(data) {
showApiError(data);
}, },
success: function(data){ function(data){
eventList = data.events; eventList = data.events;
matches = Object.keys(eventList).length; matches = Object.keys(eventList).length;
updateEventList($('#eventSearch').val(), 0, itemPerPage); updateEventList($('#eventSearch').val(), 0, itemPerPage);
updatePagination(); updatePagination();
} }
}); );
} }
// Intercept form submission // Intercept form submission
@ -942,17 +909,34 @@ function initJoin(room){
function try_auth(pass, showErrorMsg){ function try_auth(pass, showErrorMsg){
$.ajax({ vroomApi(
data: { 'authenticate',
req: JSON.stringify({ {
action: 'authenticate',
param: {
room: room, room: room,
password: pass password: pass
},
function(data){
$('.connecting-err-reason').hide();
// Once auth is passed, we get the room configuration
vroomApi(
'get_room_conf',
{
room: room,
},
function(data){
roomInfo = data;
// If our name is asked before joining the room, display the corresponding modal
// Else, just continue (with webrtc initialization
if (roomInfo.ask_for_name){
$('#display-name-pre').slideDown();
} }
}) else{
init_webrtc(roomName);
}
}
);
}, },
error: function(data){ function(data){
// 401 means password is needed // 401 means password is needed
if (data.status === 401){ if (data.status === 401){
data = data.responseJSON; data = data.responseJSON;
@ -968,38 +952,13 @@ function initJoin(room){
$('#room-is-locked').slideDown(); $('#room-is-locked').slideDown();
} }
if (showErrorMsg){ if (showErrorMsg){
showApiError(data); data = data.responseJSON;
} if (data.msg){
}, $.notify(data.msg, 'error');
success: function(data){
$('.connecting-err-reason').hide();
// Once auth is passed, we get the room configuration
$.ajax({
data: {
req: JSON.stringify({
action: 'get_room_conf',
param: {
room: room,
}
})
},
error: function(data){
showApiError(data);
},
success: function(data){
roomInfo = data;
// If our name is asked before joining the room, display the corresponding modal
// Else, just continue (with webrtc initialization
if (roomInfo.ask_for_name){
$('#display-name-pre').slideDown();
}
else{
init_webrtc(roomName);
} }
} }
});
} }
}); );
} }
// Always start by trying with an empty password. // Always start by trying with an empty password.
// Only if one is required the modal will appear // Only if one is required the modal will appear
@ -1009,19 +968,12 @@ function initJoin(room){
// This just create the webrtc object, and then continue // This just create the webrtc object, and then continue
function init_webrtc(room){ function init_webrtc(room){
// First get SimpleWebRTC config from the server // First get SimpleWebRTC config from the server
$.ajax({ vroomApi(
data: { 'get_rtc_conf',
req: JSON.stringify({ {
action: 'get_rtc_conf',
param: {
room: room, room: room,
}
})
},
error: function(data){
showApiError(data);
}, },
success: function(data){ function(data){
if (!video){ if (!video){
data.config.media.video = false; data.config.media.video = false;
} }
@ -1046,7 +998,7 @@ function init_webrtc(room){
initVroom(room); initVroom(room);
} }
} }
}); );
} }
// This is the main function called when you join a room (after auth and all the basic stuff ready) // This is the main function called when you join a room (after auth and all the basic stuff ready)
@ -1074,20 +1026,13 @@ function initVroom(room) {
// Get the role of a peer // Get the role of a peer
function getPeerRole(id){ function getPeerRole(id){
$.ajax({ vroomApi(
data: { 'get_peer_role',
req: JSON.stringify({ {
action: 'get_peer_role',
param: {
room: roomName, room: roomName,
peer_id: id peer_id: id
}
})
}, },
error: function(data){ function(data){
showApiError(data);
},
success: function(data){
// If its our own role, check if it chagned // If its our own role, check if it chagned
if (id === peers.local.id){ if (id === peers.local.id){
if (data.role != peers.local.role){ if (data.role != peers.local.role){
@ -1121,7 +1066,7 @@ function initVroom(room) {
} }
} }
} }
}); );
} }
// Put a video on the mainVideo div, called when you click on a video preview // Put a video on the mainVideo div, called when you click on a video preview
@ -1437,24 +1382,17 @@ function initVroom(room) {
// Promote a peer (he will be owner) // Promote a peer (he will be owner)
function promotePeer(id){ function promotePeer(id){
if (peers[id] && peers[id].role != 'owner'){ if (peers[id] && peers[id].role != 'owner'){
$.ajax({ vroomApi(
data: { 'promote_peer',
req: JSON.stringify({ {
action: 'promote_peer',
param: {
room: roomName, room: roomName,
peer_id: id peer_id: id
}
})
},
error: function(data){
showApiError(data);
}, },
success: function(data){ function(data){
webrtc.sendToAll('owner_promoted', {peer: id}); webrtc.sendToAll('owner_promoted', {peer: id});
$.notify(data.msg, 'success'); $.notify(data.msg, 'success');
} }
}); );
suspendButton($('#actionPromote_' + id)); suspendButton($('#actionPromote_' + id));
} }
else if (peers[id]){ else if (peers[id]){
@ -1843,21 +1781,14 @@ function initVroom(room) {
// If we were prompted for our display name before joining // If we were prompted for our display name before joining
// we send it. Not that I like sending this kind of data to the server // we send it. Not that I like sending this kind of data to the server
// but it's needed for email notifications // but it's needed for email notifications
$.ajax({ vroomApi(
data: { 'join',
req: JSON.stringify({ {
action: 'join',
param: {
room: roomName, room: roomName,
name: (peers.local.hasName) ? peers.local.displayName : '', name: (peers.local.hasName) ? peers.local.displayName : '',
peer_id: peers.local.id peer_id: peers.local.id
}
})
},
error: function(data){
showApiError(data);
}, },
success: function(data){ function(data){
if (data.msg){ if (data.msg){
$.notify(data.msg, 'success'); $.notify(data.msg, 'success');
} }
@ -1868,7 +1799,7 @@ function initVroom(room) {
$('#connecting').modal('hide'); $('#connecting').modal('hide');
}, 200); }, 200);
} }
}); );
checkMoh(); checkMoh();
}); });
@ -1966,21 +1897,14 @@ function initVroom(room) {
if (!validEmail){ if (!validEmail){
return false; return false;
} }
$.ajax({ vroomApi(
data: { 'invite_email',
req: JSON.stringify({ {
action: 'invite_email',
param: {
rcpts: rcpts, rcpts: rcpts,
message: message, message: message,
room: roomName room: roomName
}
})
}, },
error: function(data){ function(data){
showApiError(data);
},
success: function(data){
$('#recipient').val(''); $('#recipient').val('');
$('#inviteModal').modal('hide'); $('#inviteModal').modal('hide');
$('#email-list-invite').find('.email-entry:not(:last)').remove(); $('#email-list-invite').find('.email-entry:not(:last)').remove();
@ -1988,7 +1912,7 @@ function initVroom(room) {
$('#message').val(''); $('#message').val('');
$.notify(data.msg, 'success'); $.notify(data.msg, 'success');
} }
}); );
}); });
// Set your DisplayName // Set your DisplayName
@ -2129,20 +2053,13 @@ function initVroom(room) {
$('#ownerAuthForm').submit(function(event) { $('#ownerAuthForm').submit(function(event) {
event.preventDefault(); event.preventDefault();
var pass = $('#ownerAuthPass').val(); var pass = $('#ownerAuthPass').val();
$.ajax({ vroomApi(
data: { 'authenticate',
req: JSON.stringify({ {
action: 'authenticate',
param: {
password: pass, password: pass,
room: roomName room: roomName
}
})
},
error: function(data){
showApiError(data);
}, },
success: function(data){ function(data){
$('#authPass').val(''); $('#authPass').val('');
$('#ownerAuthModal').modal('hide'); $('#ownerAuthModal').modal('hide');
if (data.role === 'owner'){ if (data.role === 'owner'){
@ -2154,7 +2071,7 @@ function initVroom(room) {
$.notify(localize('WRONG_PASSWORD'), 'error'); $.notify(localize('WRONG_PASSWORD'), 'error');
} }
} }
}); );
}); });
// The configuration form has been submited successfuly // The configuration form has been submited successfuly

Loading…
Cancel
Save