Cleanups in has_joined helper

master
Daniel Berteaud 10 years ago
parent 848fba10ae
commit 831cec4d0f
  1. 39
      vroom.pl

@ -482,24 +482,31 @@ helper promote_peer => sub {
}; };
# 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 a hashref room => room name and name => session name
helper has_joined => sub { helper has_joined => sub {
my $self = shift; my $self = shift;
my ($session,$name) = @_; my ($data) = @_;
my $ret = 0;
# TODO: replace with a JOIN, and a SELECT COUNT
# + check result with bind_columns + fetch
my $sth = eval { my $sth = eval {
$self->db->prepare('SELECT `id` $self->db->prepare('SELECT COUNT(`r`.`id`)
FROM `rooms` FROM `rooms` `r`
WHERE `name`=? LEFT JOIN `room_participants` `p` ON `r`.`id`=`p`.`room_id`
AND `id` IN (SELECT `room_id` WHERE `r`.`name`=?
FROM `room_participants` AND `p`.`participant`=?');
WHERE `participant`=?)'); };
} || return undef; if ($@){
$sth->execute($name,$session) || return undef; return {msg => $@};
$ret = 1 if ($sth->rows > 0); }
return $ret; $sth->execute($data->{room},$data->{name});
if ($sth->err){
return {msg => "DB Error: " . $sth->errstr . " (code " . $sth->err . ")"};
}
my $num;
$sth->bind_columns(\$num);
$sth->fetch;
return {
ok => 1,
data => ($num == 1) ? 1 : 0
}
}; };
# Purge disconnected participants from the DB # Purge disconnected participants from the DB
@ -1288,7 +1295,7 @@ post '/*action' => [action => [qw/action admin\/action/]] => sub {
} }
# Refuse any action from non members of the room # Refuse any action from non members of the room
if ($prefix ne 'admin' && (!$self->session('name') || if ($prefix ne 'admin' && (!$self->session('name') ||
!$self->has_joined($self->session('name'), $room) || !$self->has_joined({name => $self->session('name'), room => $room})->{data} ||
!$self->session($room) || !$self->session($room) ||
!$self->session($room)->{role})){ !$self->session($room)->{role})){
return $self->render( return $self->render(

Loading…
Cancel
Save