From 831cec4d0f5daa113c9f30f0c58f0e95d7170068 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Wed, 15 Oct 2014 22:44:48 +0200 Subject: [PATCH] Cleanups in has_joined helper --- vroom.pl | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/vroom.pl b/vroom.pl index 1f606b8..43151c2 100755 --- a/vroom.pl +++ b/vroom.pl @@ -482,24 +482,31 @@ helper promote_peer => sub { }; # 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 { my $self = shift; - my ($session,$name) = @_; - my $ret = 0; - # TODO: replace with a JOIN, and a SELECT COUNT - # + check result with bind_columns + fetch + my ($data) = @_; my $sth = eval { - $self->db->prepare('SELECT `id` - FROM `rooms` - WHERE `name`=? - AND `id` IN (SELECT `room_id` - FROM `room_participants` - WHERE `participant`=?)'); - } || return undef; - $sth->execute($name,$session) || return undef; - $ret = 1 if ($sth->rows > 0); - return $ret; + $self->db->prepare('SELECT COUNT(`r`.`id`) + FROM `rooms` `r` + LEFT JOIN `room_participants` `p` ON `r`.`id`=`p`.`room_id` + WHERE `r`.`name`=? + AND `p`.`participant`=?'); + }; + if ($@){ + return {msg => $@}; + } + $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 @@ -1288,7 +1295,7 @@ post '/*action' => [action => [qw/action admin\/action/]] => sub { } # Refuse any action from non members of the room 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)->{role})){ return $self->render(