From d597d4a7907a347e848a75a16e1d9e781f6117bf Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Tue, 27 May 2014 19:09:30 +0200 Subject: [PATCH] Add notifications in the table before sending the email And manage invitations cleanups when rooms expire --- public/vroom.pl | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/public/vroom.pl b/public/vroom.pl index 5d6398c..8a60395 100755 --- a/public/vroom.pl +++ b/public/vroom.pl @@ -280,6 +280,7 @@ helper delete_rooms => sub { my $timeout = time()-$config->{inactivityTimeout}; $self->db->do("DELETE FROM participants WHERE id IN (SELECT id FROM rooms WHERE activity_timestamp < $timeout AND persistent='0');"); $self->db->do("DELETE FROM notifications WHERE id IN (SELECT id FROM rooms WHERE activity_timestamp < $timeout AND persistent='0');"); + $self->db->do("DELETE FROM invitations WHERE id IN (SELECT id FROM rooms WHERE activity_timestamp < $timeout AND persistent='0');"); $self->db->do("DELETE FROM rooms WHERE activity_timestamp < $timeout AND persistent='0';"); } || return undef; if ($config->{persistentInactivityTimeout} && $config->{persistentInactivityTimeout} > 0){ @@ -287,6 +288,7 @@ helper delete_rooms => sub { my $timeout = time()-$config->{persistentInactivityTimeout}; $self->db->do("DELETE FROM participants WHERE id IN (SELECT id FROM rooms WHERE activity_timestamp < $timeout AND persistent='1');"); $self->db->do("DELETE FROM notifications WHERE id IN (SELECT id FROM rooms WHERE activity_timestamp < $timeout AND persistent='1');"); + $self->db->do("DELETE FROM invitations WHERE id IN (SELECT id FROM rooms WHERE activity_timestamp < $timeout AND persistent='1');"); $self->db->do("DELETE FROM rooms WHERE activity_timestamp < $timeout AND persistent='1';"); } || return undef; } @@ -318,10 +320,17 @@ helper valid_room_name => sub { return $ret; }; +# Generate a random token +helper get_random => sub { + my $self = shift; + my ($size) = @_; + return join '' => map{('a'..'z','0'..'9')[rand 36]} 0..$size; +}; + # Generate a random name helper get_random_name => sub { my $self = shift; - my $name = join '' => map{('a'..'z','0'..'9')[rand 36]} 0..9; + my $name = $self->get_random(9); # Get another one if already taken while ($self->get_room($name)){ $name = $self->get_random_name(); @@ -442,6 +451,18 @@ helper choose_moh => sub { return basename($files[rand @files]); }; +# Add a invitation +helper add_invitation => sub { + my $self = shift; + my ($room,$email) = @_; + my $from = $self->session('name') || return undef; + my $data = $self->get_room($room); + return undef unless ($data); + my $sth = eval { $self->db->prepare("INSERT INTO invitations (`id`,`from`,`token`,`email`) VALUES (?,?,?,?)") } || return undef; + $sth->execute($data->{id},$from,$self->get_random(50),$email) || return undef; + return 1; +}; + # Route / to the index page any '/' => 'index'; @@ -710,7 +731,7 @@ post '/action' => sub { elsif ($rcpt !~ m/\S+@\S+\.\S+$/){ $msg = $self->l('ERROR_MAIL_INVALID'); } - elsif ($self->email( + elsif ($self->add_invitation($room,$rcpt) && $self->email( header => [ Subject => encode("MIME-Header", $self->l("EMAIL_INVITATION")), To => $rcpt