Append notifications REST API (#1851)

This commit is contained in:
Christophe Maudoux 2019-07-22 15:39:59 +02:00
parent 05cb1e1c91
commit fb7a222c9d
2 changed files with 28 additions and 24 deletions

View File

@ -227,7 +227,7 @@ sub toForm {
} }
sub notificationServer { sub notificationServer {
my ( $self, $req ) = @_; my ( $self, $req, @args ) = @_;
$self->p->logger->debug("REST request for notifications"); $self->p->logger->debug("REST request for notifications");
return $self->p->sendError( $req, 'Only JSON requests here', 400 ) return $self->p->sendError( $req, 'Only JSON requests here', 400 )
unless ( $req->wantJSON ); unless ( $req->wantJSON );
@ -239,16 +239,10 @@ sub notificationServer {
return $self->p->sendError( $req, $@, 500 ) if ($@); return $self->p->sendError( $req, $@, 500 ) if ($@);
} }
elsif ( $req->method =~ /^GET$/i ) { elsif ( $req->method =~ /^GET$/i ) {
my $content = $req->content || '{}'; my ( $uid, $ref ) = @args;
my $json;
eval { $json = from_json( $content, { allow_nonref => 1 } ) };
return $self->p->sendError( $req, "Unable to decode JSON file: $@",
400 )
if ($@);
my $notifs; my $notifs;
my $user = $json->{uid} || '';
( $notifs, $err ) = ( $notifs, $err ) =
eval { $self->notifObject->getNotifications( $user ) }; eval { $self->notifObject->getNotifications($uid) };
return $self->p->sendError( $req, $@, 500 ) if ($@); return $self->p->sendError( $req, $@, 500 ) if ($@);
$res = []; $res = [];
foreach ( keys %$notifs ) { foreach ( keys %$notifs ) {
@ -258,20 +252,21 @@ sub notificationServer {
return $self->p->sendError( $req, "Unable to decode JSON file: $@", return $self->p->sendError( $req, "Unable to decode JSON file: $@",
400 ) 400 )
if ($@); if ($@);
push( @$res, push @$res,
{ "uid" => $json->{uid}, "reference" => $json->{reference} } ); { "uid" => $json->{uid}, "reference" => $json->{reference} };
if ($ref) {
@$res = grep { $_->{reference} =~ /^$ref$/ } @$res;
}
} }
} }
elsif ( $req->method =~ /^DELETE$/i ) { elsif ( $req->method =~ /^DELETE$/i ) {
my $json; my $uid = $req->params('uid');
eval { $json = from_json( $req->content, { allow_nonref => 1 } ) }; my $ref = $req->params('reference');
return $self->p->sendError( $req, "Unable to decode JSON file: $@", return $self->p->sendError( $req,
400 ) "Missing parameters -> uid: $uid / ref: $ref", 400 )
if ($@); unless ( $uid and $ref );
( $res, $err ) = eval { ( $res, $err ) =
$self->notifObject->deleteNotification( $json->{uid}, eval { $self->notifObject->deleteNotification( $uid, $ref ); };
$json->{reference} );
};
return $self->p->sendError( $req, $@, 500 ) if ($@); return $self->p->sendError( $req, $@, 500 ) if ($@);
} }
else { else {

View File

@ -48,8 +48,17 @@ sub init {
if ( $self->conf->{notificationServer} ) { if ( $self->conf->{notificationServer} ) {
$self->logger->debug('Notification server enable'); $self->logger->debug('Notification server enable');
$self->addUnauthRoute( $self->addUnauthRoute(
'notifications' => 'notificationServer', notifications => 'notificationServer',
[ 'GET', 'POST', 'DELETE' ] ['POST']
);
$self->addUnauthRoute(
notifications => { '*' => 'notificationServer' },
['GET']
);
$self->addUnauthRoute(
notifications =>
{ ':uid' => { ':reference' => 'notificationServer' } },
['DELETE']
); );
} }
@ -142,8 +151,8 @@ sub getNotifBack {
} }
sub notificationServer { sub notificationServer {
my ( $self, $req ) = @_; my ( $self, $req, @args ) = @_;
return $self->module->notificationServer($req); return $self->module->notificationServer($req, @args);
} }
1; 1;