diff --git a/lib/Vroom/Constants.pm b/lib/Vroom/Constants.pm index d002ad4..60cd824 100644 --- a/lib/Vroom/Constants.pm +++ b/lib/Vroom/Constants.pm @@ -164,7 +164,8 @@ use constant API_ACTIONS => { unlock_room => 1, set_join_password => 1, set_owner_password => 1, - set_ask_for_name => 1 + set_ask_for_name => 1, + email_notification => 1 }, participant => { ping => 1, diff --git a/public/js/vroom.js b/public/js/vroom.js index 3a058d6..ee67261 100644 --- a/public/js/vroom.js +++ b/public/js/vroom.js @@ -98,11 +98,16 @@ function addNotifiedEmail(email){ function removeNotifiedEmail(email){ var id = escapeJqSelector(email.replace(/['"]/, '_').replace('\\\'', '\'')); $.ajax({ + url: rootUrl + 'api', data: { - action: 'emailNotification', - type: 'remove', - email: email, - room: roomName + req: JSON.stringify({ + action: 'email_notification', + param: { + set: 'off', + email: email, + room: roomName + } + }) }, error: function() { $.notify(locale.ERROR_OCCURRED, 'error'); @@ -2091,11 +2096,16 @@ function initVroom(room) { $('#newEmailNotificationForm').submit(function(event){ event.preventDefault(); $.ajax({ + url: rootUrl + 'api', data: { - action: 'emailNotification', - type: 'add', - email: $('#newEmailNotification').val(), - room: roomName + req: JSON.stringify({ + action: 'email_notification', + param: { + set: 'on', + email: $('#newEmailNotification').val(), + room: roomName + } + }) }, error: function() { $.notify(locale.ERROR_OCCURRED, 'error'); diff --git a/vroom.pl b/vroom.pl index b8b32eb..92fd257 100755 --- a/vroom.pl +++ b/vroom.pl @@ -115,6 +115,17 @@ helper valid_id => sub { return 1; }; +# Check email address +# TODO: replace with Email::Valid +helper valid_email => sub { + my $self = shift; + my ($email) = @_; + if ($email !~ m/^\S+@\S+\.\S+$/){ + return 0; + } + return 1; +}; + ########################## # Various helpers # ########################## @@ -1409,6 +1420,41 @@ any '/api' => sub { } ); } + # Add or remove an email address to the list of email notifications + elsif ($req->{action} eq 'email_notification'){ + my $email = $req->{param}->{email}; + my $set = $req->{param}->{set}; + if (!$self->valid_email($email)){ + return $self->render( + json => { + msg => $self->l('ERROR_MAIL_INVALID'), + status => 'error' + } + ); + } + elsif ($set eq 'on' && $self->add_notification($room->{name},$email)){ + return $self->render( + json => { + status => 'success', + msg => sprintf($self->l('s_WILL_BE_NOTIFIED'), $email) + } + ); + } + elsif ($set eq 'off' && $self->remove_notification($room->{name},$email)){ + return $self->render( + json => { + status => 'success', + msg => sprintf($self->l('s_WONT_BE_NOTIFIED_ANYMORE'), $email) + } + ); + } + return $self->render( + json => { + msg => $self->l('ERROR_OCCURRED'), + status => 'error' + } + ); + } elsif ($req->{action} eq 'authenticate'){ my $pass = $req->{param}->{'password'}; # Auth succeed ? lets promote him to owner of the room @@ -1635,33 +1681,6 @@ post '/*jsapi' => { jsapi => [qw(jsapi admin/jsapi)] } => sub { }, ); } - # Add a new email for notifications when someone joins - elsif ($action eq 'emailNotification'){ - my $email = $self->param('email'); - my $type = $self->param('type'); - my $status = 'error'; - my $msg = $self->l('ERROR_OCCURRED'); - if ($prefix ne 'admin' && $self->session($room)->{role} ne 'owner'){ - $msg = $self->l('NOT_ALLOWED'); - } - elsif ($email !~ m/^\S+@\S+\.\S+$/){ - $msg = $self->l('ERROR_MAIL_INVALID'); - } - elsif ($type eq 'add' && $self->add_notification($room,$email)){ - $status = 'success'; - $msg = sprintf($self->l('s_WILL_BE_NOTIFIED'), $email); - } - elsif ($type eq 'remove' && $self->remove_notification($room,$email)){ - $status = 'success'; - $msg = sprintf($self->l('s_WONT_BE_NOTIFIED_ANYMORE'), $email); - } - return $self->render( - json => { - msg => $msg, - status => $status - } - ); - } # New participant joined the room elsif ($action eq 'join'){ my $name = $self->param('name') || '';