Cleanup create_room helper

master
Daniel Berteaud 10 years ago
parent bdb86d4973
commit 28cffe0481
  1. 86
      vroom.pl

@ -158,26 +158,57 @@ helper logout => sub {
helper create_room => sub { helper create_room => sub {
my $self = shift; my $self = shift;
my ($name,$owner) = @_; my ($name,$owner) = @_;
my $res = {status => undef, msg => undef};
# Convert room names to lowercase # Convert room names to lowercase
if ($name ne lc $name){ if ($name ne lc $name){
$name = lc $name; $name = lc $name;
} }
# Exit if the name isn't valid or already taken # Check if the name is valid
return undef if ($self->get_room_by_name($name) || !$self->valid_room_name($name)->{status}); $res = $self->valid_room_name($name);
if (!$res->{status}){
return $res;
}
elsif ($self->get_room_by_name($name)){
$res->{msg} = 'ERROR_NAME_CONFLICT';
return $res;
}
my $sth = eval { my $sth = eval {
$self->db->prepare('INSERT INTO `rooms` $self->db->prepare('INSERT INTO `rooms`
(`name`,`create_date`,`last_activity`,`owner`,`token`,`realm`) (`name`,
VALUES (?,CONVERT_TZ(NOW(), @@session.time_zone, \'+00:00\'),CONVERT_TZ(NOW(), @@session.time_zone, \'+00:00\'),?,?,?)'); `create_date`,
} || return undef; `last_activity`,
# Gen a random token. Will be used as a turnPassword `owner`,
my $tp = $self->get_random(256); `token`,
$sth->execute($name,$owner,$tp,$config->{'turn.realm'}) || return undef; `realm`)
VALUES (?,
CONVERT_TZ(NOW(), @@session.time_zone, \'+00:00\'),
CONVERT_TZ(NOW(), @@session.time_zone, \'+00:00\'),
?,
?,
?)');
};
if ($@){
$res->{msg} = $@;
return $res;
}
$sth->execute(
$name,
$owner,
$self->get_random(256),
$config->{'turn.realm'}
);
if ($sth->err){
$res->{msg} = "DB Error: " . $sth->errstr . " (code " . $sth->err . ")";
return $res;
}
$self->app->log->info("Room $name created by " . $self->session('name')); $self->app->log->info("Room $name created by " . $self->session('name'));
# Etherpad integration ? If so, create the corresponding pad # Etherpad integration ? If so, create the corresponding pad
if ($ec){ if ($ec){
$self->create_pad($name); $self->create_pad($name);
} }
return 1; $res->{status} = 1;
$res->{msg} = 'ROOM_CREATED';
return $res;
}; };
# Read room param in the DB and return a perl hashref # Read room param in the DB and return a perl hashref
@ -993,30 +1024,35 @@ post '/create' => sub {
my $name = $self->param('roomName') || $self->get_random_name(); my $name = $self->param('roomName') || $self->get_random_name();
# Create a session for this user, but don't set a role for now # Create a session for this user, but don't set a role for now
$self->login; $self->login;
my $status = 'error'; my $json = {
my $err = ''; status => 'error',
my $msg = $self->l('ERROR_OCCURRED'); err => 'ERROR_OCCURRED',
msg => $self->l('ERROR_OCCURRED'),
room => $name
};
# Cleanup unused rooms before trying to create it # Cleanup unused rooms before trying to create it
$self->delete_rooms; $self->delete_rooms;
my $res = $self->valid_room_name($name); my $res = $self->valid_room_name($name);
if (!$res->{status}){ if (!$res->{status}){
$err = $res->{msg}; $json->{err} = $res->{msg};
$msg = $self->l($res->{msg}); $json->{msg} = $self->l($res->{msg});
return $self->render(json => $json);
} }
elsif ($self->get_room_by_name($name)){ elsif ($self->get_room_by_name($name)){
$err = 'ERROR_NAME_CONFLICT'; $json->{err} = 'ERROR_NAME_CONFLICT';
$msg = $self->l('ERROR_NAME_CONFLICT'); $json->{msg} = $self->l('ERROR_NAME_CONFLICT');
return $self->render(json => $json);
} }
elsif ($self->create_room($name,$self->session('name'))){ $res = $self->create_room($name,$self->session('name'));
$status = 'success'; if (!$res->{status}){
$self->session($name => {role => 'owner'}); $json->{err} = $res->{msg};
$json->{msg} = $self->l($res->{msg});
return $self->render(json => $json);
} }
$self->render(json => { $json->{status} = 'success';
status => $status, $json->{err} = $res->{msg};
err => $err, $self->session($name => {role => 'owner'});
msg => $msg, return $self->render(json => $json);
room => $name
});
}; };
# Translation for JS resources # Translation for JS resources

Loading…
Cancel
Save