From b65ad2c63e59e08b048052d2726646d18e6f9471 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Thu, 22 Jan 2015 13:28:26 +0100 Subject: [PATCH] Handle email notifications in the new config menu --- public/js/vroom.js | 7 ++++++- vroom.pl | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/public/js/vroom.js b/public/js/vroom.js index 6520919..7dc07c8 100644 --- a/public/js/vroom.js +++ b/public/js/vroom.js @@ -546,12 +546,17 @@ function initVroom(room) { if (countPeers() > 1){ $('.threePeersEl').show(500); } + // Reset the list of email displayed, so first remove evry input field but the first one + // We keep it so we can clone it again $('.email-list').find('.email-entry:not(:first)').remove(); $.each(data.notif, function(index, obj){ addNotifiedEmail(obj.email); addEmailInputField(obj.email); }); - $('.email-list').find('.email-entry:first').remove(); + // Now, remove the first one if the list is not empty + if (data.notif.length > 0){ + $('.email-list').find('.email-entry:first').remove(); + } } // We're are not owner of the room else{ diff --git a/vroom.pl b/vroom.pl index 03f54a0..c482dcb 100755 --- a/vroom.pl +++ b/vroom.pl @@ -620,7 +620,7 @@ helper add_notification => sub { my $self = shift; my ($room,$email) = @_; my $data = $self->get_room_by_name($room); - if (!$data){ + if (!$data || !$self->valid_email($email)){ return 0; } my $sth = eval { @@ -632,8 +632,34 @@ helper add_notification => sub { $data->{id}, $email ); - $self->app->log->debug($self->session('name') . - " has added $email to the list of email which will be notified when someone joins room $room"); + return 1; +}; + +# Update the list of notified email for a room in one go +# Take the room and an array ref of emails +helper update_email_notifications => sub { + my $self = shift; + my ($room,$emails) = @_; + my $data = $self->get_room_by_name($room); + if (!$data){ + return 0; + } + # First, drop all existing notifications + my $sth = eval { + $self->db->prepare('DELETE FROM `email_notifications` + WHERE `room_id`=?'); + }; + $sth->execute( + $data->{id}, + ); + # Now, insert new emails + foreach my $email (@$emails){ + # Skip empty inputs + if ($email eq ''){ + next; + } + $self->add_notification($room,$email) || return 0; + } return 1; }; @@ -1348,7 +1374,7 @@ any '/api' => sub { $room->{$pass} = Crypt::SaltedHash->new(algorithm => 'SHA-256')->add($req->{param}->{$pass})->generate; } } - if ($self->modify_room($room)){ + if ($self->modify_room($room) && $self->update_email_notifications($room->{name},$req->{param}->{emails})){ return $self->render( json => { status => 'success',