Exchange peer status and verify it on the frontend

Also add a symbol on owner's preview
Should Fix #30 and #31
master
Daniel Berteaud 11 years ago
parent f77d2c296d
commit 1ec24bb93b
  1. 2
      docs/schema.mysql
  2. 4
      docs/upgrade.mysql
  3. 13
      public/css/vroom.css
  4. 44
      public/js/vroom.js
  5. 48
      public/vroom.pl

@ -20,6 +20,8 @@ DROP TABLE IF EXISTS `participants`;
CREATE TABLE `participants` ( CREATE TABLE `participants` (
`id` int(11) NOT NULL, `id` int(11) NOT NULL,
`participant` varchar(60) NOT NULL, `participant` varchar(60) NOT NULL,
`peer_id` varchar(40) DEFAULT NULL,
`role` varchar(20) DEFAULT 'participant',
PRIMARY KEY (`id`,`participant`) PRIMARY KEY (`id`,`participant`)
); );
#DROP TABLE IF EXISTS `turnusers_lt`; #DROP TABLE IF EXISTS `turnusers_lt`;

@ -1 +1,3 @@
ALTER TABLE rooms add COLUMN `owner_password` varchar(160) DEFAULT NULL after `join_password`; ALTER TABLE `rooms` ADD COLUMN `owner_password` varchar(160) DEFAULT NULL AFTER `join_password`;
ALTER TABLE `participants` ADD COLUMN `peer_id` varchar(40) DEFAULT NULL AFTER `participant`;
ALTER TABLE `participants` ADD COLUMN `role` varchar(20) DEFAULT 'participant' AFTER `peer_id`;

@ -53,7 +53,18 @@
font-family: 'Glyphicons Halflings'; font-family: 'Glyphicons Halflings';
color: red; color: red;
font-size: 2em; font-size: 2em;
content: "\e106";; content: "\e106";
}
.owner{
position: absolute;
left: 20px;
top: 0px;
}
.owner:before{
font-family: 'Glyphicons Halflings';
color: red;
font-size: 2em;
content: "\e124";;
} }
.displayName { .displayName {
text-align: center; text-align: center;

@ -117,13 +117,18 @@ function initVroom(room) {
function updateRole(){ function updateRole(){
$.ajax({ $.ajax({
data: { data: {
action: 'getRole', action: 'getRoomInfo',
room: roomName room: roomName,
id: peers.local.id
}, },
error: function(data){ error: function(data){
$.notify(locale.ERROR_OCCURED, 'error'); $.notify(locale.ERROR_OCCURED, 'error');
}, },
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; peers.local.role = data.role;
// Enable owner reserved menu // Enable owner reserved menu
if (data.role == 'owner'){ if (data.role == 'owner'){
@ -144,6 +149,29 @@ function initVroom(room) {
}); });
} }
// Get the role of a peer
function getPeerRole(id){
$.ajax({
data: {
action: 'getPeerRole',
room: roomName,
id: id
},
error: function(data){
$.notify(locale.ERROR_OCCURED, 'error');
},
success: function(data){
peers[id].role = data.role;
if (data.role == 'owner'){
$("#overlay_" + id).append('<div id="owner_' + id + '" class="owner"></div>');
}
else{
$('#owner_' + id).remove();
}
}
});
}
// Select a color (randomly) from this list, used for text chat // Select a color (randomly) from this list, used for text chat
function chooseColor(){ function chooseColor(){
// Shamelessly taken from http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ // Shamelessly taken from http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
@ -233,6 +261,8 @@ function initVroom(room) {
if(!peers.local.hasHistory && chatIndex == 0){ if(!peers.local.hasHistory && chatIndex == 0){
peer.sendDirectly('vroom', 'getHistory', ''); peer.sendDirectly('vroom', 'getHistory', '');
} }
// Get the role of this peer
getPeerRole(peer.id);
}, 3500); }, 3500);
} }
$(div).attr('id', 'peer_' + id); $(div).attr('id', 'peer_' + id);
@ -446,6 +476,12 @@ function initVroom(room) {
$("#overlay_" + data.id).append('<div id="' + div + '" class="' + cl + '"></div>'); $("#overlay_" + data.id).append('<div id="' + div + '" class="' + cl + '"></div>');
}); });
// This peer claims he changed its role (usually from participant to owner)
// Lets check this
webrtc.on('role_change', function(data){
getPeerRole(data.id);
});
// Handle unmute/resume // Handle unmute/resume
webrtc.on('unmute', function(data){ webrtc.on('unmute', function(data){
if (data.name === 'audio'){ if (data.name === 'audio'){
@ -494,6 +530,7 @@ function initVroom(room) {
// Handle the readyToCall event: join the room // Handle the readyToCall event: join the room
webrtc.once('readyToCall', function () { webrtc.once('readyToCall', function () {
peers.local.id = webrtc.connection.socket.sessionid; peers.local.id = webrtc.connection.socket.sessionid;
updateRole();
webrtc.joinRoom(room); webrtc.joinRoom(room);
}); });
@ -940,9 +977,6 @@ function initVroom(room) {
} }
}); });
// Check if we are the owner of the room
updateRole();
// Ping the room every minutes // Ping the room every minutes
// Used to detect inactive rooms // Used to detect inactive rooms
setInterval(function pingRoom(){ setInterval(function pingRoom(){

@ -193,6 +193,34 @@ helper get_participants => sub {
return @res; return @res;
}; };
# Set the role of a peer
helper set_peer_role => sub {
my $self = shift;
my ($room,$name,$id,$role) = @_;
# Check if this ID isn't the one from another peer first
my $sth = eval { $self->db->prepare("SELECT * FROM participants WHERE peer_id=? AND participant!=? AND id IN (SELECT id FROM rooms WHERE name=?)") } || return undef;
$sth->execute($id,$name,$room) || return undef;
return undef if ($sth->rows > 0);
$sth = eval { $self->db->prepare("UPDATE participants SET peer_id=?,role=? WHERE participant=? AND id IN (SELECT id FROM rooms WHERE name=?)") } || return undef;
$sth->execute($id,$role,$name,$room) || return undef;
return 1;
};
# Return the role of a peer, from it's signaling ID
helper get_peer_role => sub {
my $self = shift;
my ($room,$id) = @_;
my $sth = eval { $self->db->prepare("SELECT role from participants WHERE peer_id=? AND id IN (SELECT id FROM rooms WHERE name=?)") } || return undef;
$sth->execute($id,$room) || return undef;
if ($sth->rows == 1){
my ($role) = $sth->fetchrow_array();
return $role;
}
else{
return 'participant';
}
};
# Check if a participant has joined a room # Check if a participant has joined a room
# Takes two args: the session name, and the room name # Takes two args: the session name, and the room name
helper has_joined => sub { helper has_joined => sub {
@ -682,17 +710,33 @@ post '/action' => sub {
); );
} }
# Return your role and various info about the room # Return your role and various info about the room
elsif ($action eq 'getRole'){ elsif ($action eq 'getRoomInfo'){
my $id = $self->param('id');
my $res = 'error';
if ($self->session($room) && $self->session($room)->{role}){
$res = ($self->set_peer_role($room,$self->session('name'),$id, $self->session($room)->{role})) ? 'success':$res;
}
return $self->render( return $self->render(
json => { json => {
role => $self->session($room)->{role}, role => $self->session($room)->{role},
owner_auth => ($data->{owner_password}) ? 'yes' : 'no', owner_auth => ($data->{owner_password}) ? 'yes' : 'no',
join_auth => ($data->{join_password}) ? 'yes' : 'no', join_auth => ($data->{join_password}) ? 'yes' : 'no',
locked => ($data->{locked}) ? 'yes' : 'no' , locked => ($data->{locked}) ? 'yes' : 'no' ,
status => 'success' status => $res
}, },
); );
} }
# Return the role of a peer
elsif ($action eq 'getPeerRole'){
my $id = $self->param('id');
my $role = $self->get_peer_role($room,$id);
return $self->render(
json => {
role => $role,
status => 'success'
}
);
}
}; };
# use the templates defined in the config # use the templates defined in the config

Loading…
Cancel
Save