Some changes in the way peer role is handled

master
Daniel Berteaud 10 years ago
parent a2c65ad26c
commit 1edd844272
  1. 120
      public/js/vroom.js
  2. 118
      vroom.pl

@ -724,10 +724,9 @@ function initVroom(room) {
$.ajax({ $.ajax({
data: { data: {
req: JSON.stringify({ req: JSON.stringify({
action: 'get_room_info', action: 'get_room_conf',
param: { param: {
room: roomName, room: roomName,
peer_id: peers.local.id
} }
}) })
}, },
@ -735,42 +734,21 @@ function initVroom(room) {
showApiError(data); showApiError(data);
}, },
success: function(data){ success: function(data){
// Notify others if our role changed
if (data.role != peers.local.role){
webrtc.sendToAll('role_change', {});
}
peers.local.role = data.role;
roomInfo = data; roomInfo = data;
// Enable owner reserved menu // Reset the list of email displayed, so first remove evry input field but the last one
if (data.role == 'owner'){ // We keep it so we can clone it again
$('.unauthEl').hide(500); $('.email-list').find('.email-entry:not(:last)').remove();
$('.ownerEl').show(500); $.each(data.notif, function(index, obj){
// Reset the list of email displayed, so first remove evry input field but the last one addEmailInputField('email-list-notification', obj.email);
// We keep it so we can clone it again });
$('.email-list').find('.email-entry:not(:last)').remove(); // Now, remove the first one if the list is not empty
$.each(data.notif, function(index, obj){ if (Object.keys(data.notif).length > 0){
addEmailInputField('email-list-notification', obj.email); $('.email-list').find('.email-entry:first').remove();
});
// Now, remove the first one if the list is not empty
if (Object.keys(data.notif).length > 0){
$('.email-list').find('.email-entry:first').remove();
}
else{
$('.email-list').find('.email-entry:first').find('input:first').val('');
}
adjustAddRemoveEmailButtons();
} }
// We're are not owner of the room
else{ else{
// Hide owner reserved elements $('.email-list').find('.email-entry:first').find('input:first').val('');
$('.ownerEl').hide(500);
if (data.owner_auth){
$('.unauthEl').show(500);
}
else{
$('.unauthEl').hide(500);
}
} }
adjustAddRemoveEmailButtons();
// Update config switches // Update config switches
$('#lockedSet').bootstrapSwitch('state', data.locked); $('#lockedSet').bootstrapSwitch('state', data.locked);
$('#askForNameSet').bootstrapSwitch('state', data.ask_for_name); $('#askForNameSet').bootstrapSwitch('state', data.ask_for_name);
@ -804,7 +782,23 @@ function initVroom(room) {
showApiError(data); showApiError(data);
}, },
success: function(data){ success: function(data){
if (peers[id]){ if (id === peers.local.id){
if (data.role != peers.local.role){
webrtc.sendToAll('role_change', {});
}
peers.local.role = data.role;
if (data.role == 'owner'){
$('.unauthEl').hide(500);
$('.ownerEl').show(500);
}
else {
if (roomInfo.owner_auth){
$('.unauthEl').show(500);
}
$('.ownerEl').hide(500);
}
}
else if (peers[id]){
peers[id].role = data.role; peers[id].role = data.role;
if (data.role == 'owner'){ if (data.role == 'owner'){
// If this peer is a owner, we add the mark on its preview // If this peer is a owner, we add the mark on its preview
@ -1320,7 +1314,7 @@ function initVroom(room) {
} }
if (data.payload.peer && data.payload.peer == peers.local.id && peers.local.role != 'owner'){ if (data.payload.peer && data.payload.peer == peers.local.id && peers.local.role != 'owner'){
var who = (peers[data.id].hasName) ? peers[data.id].displayName : localize('A_ROOM_ADMIN'); var who = (peers[data.id].hasName) ? peers[data.id].displayName : localize('A_ROOM_ADMIN');
getRoomInfo(); getPeerRole(peers.local.id);
if (peers.local.role == 'owner'){ if (peers.local.role == 'owner'){
$.notify(sprintf(localize('s_IS_PROMOTING_YOU'), who), 'success'); $.notify(sprintf(localize('s_IS_PROMOTING_YOU'), who), 'success');
} }
@ -1523,32 +1517,32 @@ function initVroom(room) {
}); });
} }
}, 10000); }, 10000);
// Ping the room every minutes }
// Used to detect inactive rooms // Ping the room every minutes
setInterval(function(){ // Used to detect inactive rooms
$.ajax({ setInterval(function(){
data: { $.ajax({
req: JSON.stringify({ data: {
action: 'ping', req: JSON.stringify({
param: { action: 'ping',
room: roomName param: {
} room: roomName
})
},
error: function(data) {
showApiError(data);
},
success: function(data) {
if (data.msg && data.msg != ''){
$.notify(data.msg, {
className: 'info',
autoHide: false
});
} }
})
},
error: function(data) {
showApiError(data);
},
success: function(data) {
if (data.msg && data.msg != ''){
$.notify(data.msg, {
className: 'info',
autoHide: false
});
} }
}); }
}, 60000); });
} }, 60000);
// Notify the server a new participant has joined (ourself) // Notify the server a new participant has joined (ourself)
// 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
@ -1559,7 +1553,8 @@ function initVroom(room) {
action: 'join', action: 'join',
param: { param: {
room: roomName, room: roomName,
name: (peers.local.hasName) ? peers.local.displayName : '' name: (peers.local.hasName) ? peers.local.displayName : '',
peer_id: peers.local.id
} }
}) })
}, },
@ -1572,6 +1567,7 @@ function initVroom(room) {
} }
$('#videoLocalContainer').show(200); $('#videoLocalContainer').show(200);
$('#timeCounterXs,#timeCounter').tinyTimer({ from: new Date }); $('#timeCounterXs,#timeCounter').tinyTimer({ from: new Date });
getPeerRole(peers.local.id);
setTimeout(function(){ setTimeout(function(){
$('#connecting').modal('hide'); $('#connecting').modal('hide');
}, 200); }, 200);
@ -1884,7 +1880,7 @@ function initVroom(room) {
success: function(data){ success: function(data){
$('#authPass').val(''); $('#authPass').val('');
$('#ownerAuthModal').modal('hide'); $('#ownerAuthModal').modal('hide');
getRoomInfo(); getPeerRole(peers.local.id);
$('#joinPassFields,#ownerPassFields').hide(); $('#joinPassFields,#ownerPassFields').hide();
$.notify(data.msg, 'success'); $.notify(data.msg, 'success');
} }

@ -838,6 +838,35 @@ helper make_key_admin => sub {
return 1; return 1;
}; };
# Get the role of an API key for a room
helper get_key_role => sub {
my $self = shift;
my ($token,$room) = @_;
my $key = $self->get_key_by_token($token);
if (!$key){
$self->app->log->debug("Invalid API key");
return 0;
}
# An admin key is considered owner of any room
if ($key->{admin}){
return 'owner';
}
# Now, lookup the DB the role of this key for this room
my $sth = eval {
$self->db->prepare('SELECT `role`
FROM `room_keys`
LEFT JOIN `rooms` ON `room_keys`.`room_id`=`rooms`.`id`
WHERE `room_keys`.`key_id`=?
AND `rooms`.`name`=?
LIMIT 1');
};
$sth->execute($key->{id},$room);
$sth->bind_columns(\$key->{role});
$sth->fetch;
$self->app->log->debug("Key $token has role:" . $key->{role} . " in room $room");
return $key->{role};
};
# Check if a key can perform an action against a room # Check if a key can perform an action against a room
helper key_can_do_this => sub { helper key_can_do_this => sub {
my $self = shift; my $self = shift;
@ -866,19 +895,8 @@ helper key_can_do_this => sub {
$self->app->log->debug("Invalid room ID"); $self->app->log->debug("Invalid room ID");
return 0; return 0;
} }
$key->{role} = $self->get_key_role($data->{token}, $data->{param}->{room});
# Now, lookup the DB the role of this key for this room
my $sth = eval {
$self->db->prepare('SELECT `role`
FROM `room_keys`
LEFT JOIN `rooms` ON `room_keys`.`room_id`=`rooms`.`id`
WHERE `room_keys`.`key_id`=?
AND `rooms`.`name`=?
LIMIT 1');
};
$sth->execute($key->{id},$data->{param}->{room});
$sth->bind_columns(\$key->{role});
$sth->fetch;
$self->app->log->debug("Key role: " . $key->{role} . " and action: " . $data->{action}); $self->app->log->debug("Key role: " . $key->{role} . " and action: " . $data->{action});
# If this key has owner privileges on this room, allow both owner and partitipant actions # If this key has owner privileges on this room, allow both owner and partitipant actions
if ($key->{role} eq 'owner' && ($actions->{owner}->{$data->{action}} || $actions->{participant}->{$data->{action}})){ if ($key->{role} eq 'owner' && ($actions->{owner}->{$data->{action}} || $actions->{participant}->{$data->{action}})){
@ -1823,10 +1841,19 @@ any '/api' => sub {
json => $self->get_room_conf($room) json => $self->get_room_conf($room)
); );
} }
# Return your role and various info about the room # Return the role of a peer
elsif ($req->{action} eq 'get_room_info'){ elsif ($req->{action} eq 'get_peer_role'){
my $peer_id = $req->{param}->{peer_id}; my $peer_id = $req->{param}->{peer_id};
if ($self->session($room->{name}) && $self->session($room->{name})->{role}){ if (!$peer_id){
return $self->render(
json => {
msg => $self->l('ERROR_PEER_ID_MISSING'),
err => 'ERROR_PEER_ID_MISSING'
},
status => 400
);
}
if ($self->session('peer_id') && $self->session('peer_id') eq $peer_id){
# If we just have been promoted to owner # If we just have been promoted to owner
if ($self->session($room->{name})->{role} ne 'owner' && if ($self->session($room->{name})->{role} ne 'owner' &&
$self->get_peer_role({room => $room->{name}, peer_id => $peer_id}) && $self->get_peer_role({room => $room->{name}, peer_id => $peer_id}) &&
@ -1837,40 +1864,22 @@ any '/api' => sub {
key => $self->session('key'), key => $self->session('key'),
role => 'owner' role => 'owner'
); );
} my $res = $self->set_peer_role({
my $res = $self->set_peer_role({ room => $room->{name},
room => $room->{name}, peer_id => $peer_id,
peer_id => $peer_id, role => $self->session($room->{name})->{role}
role => $self->session($room->{name})->{role} });
}); if (!$res){
if (!$res){ return $self->render(
return $self->render( json => {
json => { msg => $self->l('ERROR_OCCURRED'),
msg => $self->l('ERROR_OCCURRED'), err => 'ERROR_OCCURRED'
err => 'ERROR_OCCURRED' },
}, status => 503
status => 503 );
); }
} }
} }
my $conf = $self->get_room_conf($room);
$conf->{role} = $self->session($room->{name})->{role};
return $self->render(
json => $conf
);
}
# Return the role of a peer
elsif ($req->{action} eq 'get_peer_role'){
my $peer_id = $req->{param}->{peer_id};
if (!$peer_id){
return $self->render(
json => {
msg => $self->l('ERROR_PEER_ID_MISSING'),
err => 'ERROR_PEER_ID_MISSING'
},
status => 400
);
}
my $role = $self->get_peer_role({room => $room->{name}, peer_id => $peer_id}); my $role = $self->get_peer_role({room => $room->{name}, peer_id => $peer_id});
if (!$role){ if (!$role){
return $self->render( return $self->render(
@ -1890,6 +1899,21 @@ any '/api' => sub {
# Notify the backend when we join a room # Notify the backend when we join a room
elsif ($req->{action} eq 'join'){ elsif ($req->{action} eq 'join'){
my $name = $req->{param}->{name} || ''; my $name = $req->{param}->{name} || '';
my $peer_id = $req->{param}->{peer_id};
my $res = $self->set_peer_role({
room => $room->{name},
peer_id => $peer_id,
role => $self->session($room->{name})->{role}
});
if (!$res){
return $self->render(
json => {
msg => $self->l('ERROR_OCCURRED'),
err => 'ERROR_OCCURRED'
},
status => 503
);
}
my $subj = sprintf($self->l('s_JOINED_ROOM_s'), ($name eq '') ? $self->l('SOMEONE') : $name, $room->{name}); my $subj = sprintf($self->l('s_JOINED_ROOM_s'), ($name eq '') ? $self->l('SOMEONE') : $name, $room->{name});
# Send notifications # Send notifications
my $recipients = $self->get_email_notifications($room->{name}); my $recipients = $self->get_email_notifications($room->{name});

Loading…
Cancel
Save